Debian Codegraph @

tests/sources/bzr.py

raw
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 subprocess
20
21 import pytest
22
23 from buildstream import _yaml
24 from buildstream._testing import cli # pylint: disable=unused-import
25 from buildstream._testing import create_repo
26 from buildstream._testing import generate_element
27
28 from tests.testutils.site import HAVE_BZR
29
30 pytestmark = pytest.mark.skipif(HAVE_BZR is False, reason="bzr is not available")
31 DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "bzr")
32
33
34 @pytest.mark.datafiles(os.path.join(DATA_DIR))
35 def test_fetch_checkout(cli, tmpdir, datafiles):
36 project = str(datafiles)
37 checkoutdir = os.path.join(str(tmpdir), "checkout")
38
39 repo = create_repo("bzr", str(tmpdir))
40 ref = repo.create(os.path.join(project, "basic"))
41
42 # Write out our test target
43 element = {"kind": "import", "sources": [repo.source_config(ref=ref)]}
44 generate_element(project, "target.bst", element)
45
46 # Fetch, build, checkout
47 result = cli.run(project=project, args=["source", "fetch", "target.bst"])
48 assert result.exit_code == 0
49 result = cli.run(project=project, args=["build", "target.bst"])
50 assert result.exit_code == 0
51 result = cli.run(project=project, args=["artifact", "checkout", "target.bst", "--directory", checkoutdir])
52 assert result.exit_code == 0
53
54 # Assert we checked out the file as it was commited
55 with open(os.path.join(checkoutdir, "test"), encoding="utf-8") as f:
56 text = f.read()
57
58 assert text == "test\n"
59
60
61 @pytest.mark.datafiles(DATA_DIR)
62 def test_open_bzr_customize(cli, tmpdir, datafiles):
63 project = str(datafiles)
64 repo = create_repo("bzr", str(tmpdir))
65 ref = repo.create(os.path.join(project, "basic"))
66
67 element = {"kind": "import", "sources": [repo.source_config(ref=ref)]}
68 generate_element(project, "target.bst", element)
69
70 workspace = os.path.join(datafiles, "bzr-workspace")
71 result = cli.run(cwd=project, project=project, args=["workspace", "open", "--directory", workspace, "target.bst"])
72 result.assert_success()
73
74 # Check that the .bzr dir exists
75 assert os.path.isdir(os.path.join(workspace, ".bzr"))
76
77 # Check that the correct origin branch is set
78 element_config = _yaml.load(os.path.join(project, "target.bst"), shortname=None)
79 source_config = element_config.get_sequence("sources").mapping_at(0)
80 output = subprocess.check_output(["bzr", "info"], cwd=workspace)
81 stripped_url = source_config.get_str("url").lstrip("file:///")
82 expected_output_str = "checkout of branch: /{}/{}".format(stripped_url, source_config.get_str("track"))
83 assert expected_output_str in str(output)
84
85
86 @pytest.mark.datafiles(os.path.join(DATA_DIR))
87 def test_show_source_info(cli, tmpdir, datafiles):
88 project = str(datafiles)
89 repo = create_repo("bzr", str(tmpdir))
90 ref = repo.create(os.path.join(project, "basic"))
91
92 # Write out our test target
93 source_config = repo.source_config(ref=ref)
94 source_config["version"] = "1.2.3"
95 element = {"kind": "import", "sources": [source_config]}
96 generate_element(project, "target.bst", element)
97
98 # Get the source info
99 result = cli.run(project=project, args=["show", "--format", "%{name}:\n%{source-info}", "target.bst"])
100 result.assert_success()
101
102 # Check the results
103 loaded = _yaml.load_data(result.output)
104 sources = loaded.get_sequence("target.bst")
105 source_info = sources.mapping_at(0)
106
107 assert source_info.get_str("kind") == "bzr"
108 assert source_info.get_str("url") == "file://" + repo.repo
109 assert source_info.get_str("medium") == "bzr"
110 assert source_info.get_str("version-type") == "commit"
111 assert source_info.get_str("version") == "1"
112 assert source_info.get_str("version-guess") == "1.2.3"