Skip to content

Commit a91adf9

Browse files
Add source provenance attribute tests
1 parent b3a7488 commit a91adf9

5 files changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
#
14+
15+
# Pylint doesn't play well with fixtures and dependency injection from pytest
16+
# pylint: disable=redefined-outer-name
17+
18+
import os
19+
import pytest
20+
21+
from buildstream._testing import generate_project, load_yaml
22+
from buildstream._testing import cli # pylint: disable=unused-import
23+
from buildstream.exceptions import ErrorDomain, LoadErrorReason
24+
25+
26+
DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "source_provenance_attributes")
27+
28+
29+
##################################################################
30+
# Tests #
31+
##################################################################
32+
# Test that no defined source provenance attributes blocks all source provenance data
33+
@pytest.mark.datafiles(DATA_DIR)
34+
def test_source_provenance_no_defined_attributes(cli, datafiles):
35+
project = str(datafiles)
36+
37+
# Set the project_dir alias in project.conf to the path to the tested project
38+
project_config_path = os.path.join(project, "project.conf")
39+
project_config = load_yaml(project_config_path)
40+
aliases = project_config.get_mapping("aliases")
41+
aliases["project_dir"] = "file://{}".format(project)
42+
43+
generate_project(project, project_config)
44+
45+
# Make sure a non-default attribute fails
46+
result = cli.run(
47+
project=project,
48+
args=["show", "--format", "%{source-info}", "target_a.bst"],
49+
)
50+
result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.UNDEFINED_SOURCE_PROVENANCE_ATTRIBUTE)
51+
52+
# Make sure a default attribute fails
53+
result = cli.run(
54+
project=project,
55+
args=["show", "--format", "%{source-info}", "target_b.bst"],
56+
)
57+
result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.UNDEFINED_SOURCE_PROVENANCE_ATTRIBUTE)
58+
59+
60+
# Test that no defined source provenance attributes blocks all source provenance data
61+
@pytest.mark.datafiles(DATA_DIR)
62+
def test_source_provenance_default_attributes(cli, datafiles):
63+
project = str(datafiles)
64+
65+
# Set the project_dir alias in project.conf to the path to the tested project
66+
project_config_path = os.path.join(project, "project.conf")
67+
project_config = load_yaml(project_config_path)
68+
aliases = project_config.get_mapping("aliases")
69+
aliases["project_dir"] = "file://{}".format(project)
70+
71+
# Edit config to fallback to default source provenance attributes
72+
project_config.safe_del("source-provenance-fields")
73+
74+
generate_project(project, project_config)
75+
76+
# Make sure defined attributes are available
77+
result = cli.run(
78+
project=project,
79+
args=["show", "--format", "%{source-info}", "target_b.bst"],
80+
)
81+
result.assert_success()
82+
83+
provenance_result = ""
84+
for line in result.output.split("\n"):
85+
if "provenance:" in line or " " in line:
86+
provenance_result += line
87+
88+
assert provenance_result == " provenance: homepage: foo"
89+
90+
# Make sure undefined attributes fail
91+
result = cli.run(
92+
project=project,
93+
args=["show", "--format", "%{source-info}", "target_a.bst"],
94+
)
95+
result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.UNDEFINED_SOURCE_PROVENANCE_ATTRIBUTE)
96+
97+
98+
# Test that no defined source provenance attributes blocks all source provenance data
99+
@pytest.mark.datafiles(DATA_DIR)
100+
def test_source_provenance_project_defined_attributes(cli, datafiles):
101+
project = str(datafiles)
102+
103+
# Set the project_dir alias in project.conf to the path to the tested project
104+
project_config_path = os.path.join(project, "project.conf")
105+
project_config = load_yaml(project_config_path)
106+
aliases = project_config.get_mapping("aliases")
107+
aliases["project_dir"] = "file://{}".format(project)
108+
109+
# Edit config to only use project specified source provenance attributes
110+
111+
source_provenance_attrs = project_config.get_mapping("source-provenance-fields")
112+
source_provenance_attrs["originator"] = "Testing"
113+
114+
generate_project(project, project_config)
115+
116+
# Make sure defined attributes are available
117+
result = cli.run(
118+
project=project,
119+
args=["show", "--format", "%{source-info}", "target_a.bst"],
120+
)
121+
result.assert_success()
122+
123+
provenance_result = ""
124+
for line in result.output.split("\n"):
125+
if "provenance:" in line or " " in line:
126+
provenance_result += line
127+
128+
assert provenance_result == " provenance: originator: bar"
129+
130+
# Make sure undefined attributes fail
131+
result = cli.run(
132+
project=project,
133+
args=["show", "--format", "%{source-info}", "target_b.bst"],
134+
)
135+
result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.UNDEFINED_SOURCE_PROVENANCE_ATTRIBUTE)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
kind: import
2+
3+
sources:
4+
- kind: remote
5+
url: project_dir:/files/file
6+
provenance:
7+
originator: bar
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
kind: import
2+
3+
sources:
4+
- kind: remote
5+
url: project_dir:/files/file
6+
provenance:
7+
homepage: foo
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World!
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Project with source provenance attributes
2+
name: foo
3+
min-version: 2.0
4+
5+
element-path: elements
6+
7+
# Don't allow any provenance
8+
source-provenance-fields: {}
9+
10+
aliases:
11+
project_dir: file://{project_dir}

0 commit comments

Comments
 (0)