From 3ae24057522c458d78dcb006d02b90bb205444da Mon Sep 17 00:00:00 2001 From: Thomas Coldrick Date: Mon, 20 Jan 2020 09:28:28 +0000 Subject: testing: Add functions to generate yaml files Adds functions to the `buildstream.testing` package to allow plugins to dump elements and projects on the fly. Before this plugins were just accessing the private yaml API for tests and loading/dumping directly. I also allow access to just `_yaml.load()` from testing. --- src/buildstream/testing/__init__.py | 2 + src/buildstream/testing/_yaml.py | 19 +++++++ tests/sources/bzr.py | 5 +- tests/sources/git.py | 97 +++++++++++++++------------------ tests/sources/no_fetch_cached.py | 5 +- tests/sources/pip.py | 8 +-- tests/sources/previous_source_access.py | 6 +- tests/sources/remote.py | 34 +++++------- tests/sources/tar.py | 48 +++++++--------- tests/sources/zip.py | 28 +++------- 10 files changed, 116 insertions(+), 136 deletions(-) create mode 100644 src/buildstream/testing/_yaml.py diff --git a/src/buildstream/testing/__init__.py b/src/buildstream/testing/__init__.py index 46cadbbfa..dafc3a9fe 100644 --- a/src/buildstream/testing/__init__.py +++ b/src/buildstream/testing/__init__.py @@ -22,6 +22,8 @@ This package contains various utilities which make it easier to test plugins. import os from collections import OrderedDict from buildstream.exceptions import ErrorDomain, LoadErrorReason +from buildstream._yaml import load as load_yaml # type: ignore +from ._yaml import generate_project, generate_element from .repo import Repo from .runcli import cli, cli_integration, cli_remote_execution from .integration import integration_cache diff --git a/src/buildstream/testing/_yaml.py b/src/buildstream/testing/_yaml.py new file mode 100644 index 000000000..ccf65a1ae --- /dev/null +++ b/src/buildstream/testing/_yaml.py @@ -0,0 +1,19 @@ +import os + +from buildstream._yaml import roundtrip_dump # type: ignore + + +def generate_project(project_dir, config=None): + if config is None: + config = {} + project_file = os.path.join(project_dir, "project.conf") + if "name" not in config: + config["name"] = os.path.basename(project_dir) + roundtrip_dump(config, project_file) + + +def generate_element(element_dir, element_name, config=None): + if config is None: + config = {} + element_path = os.path.join(element_dir, element_name) + roundtrip_dump(config, element_path) diff --git a/tests/sources/bzr.py b/tests/sources/bzr.py index ca727c793..2dcacfef5 100644 --- a/tests/sources/bzr.py +++ b/tests/sources/bzr.py @@ -4,10 +4,9 @@ import os import pytest -from buildstream import _yaml - from buildstream.testing import cli # pylint: disable=unused-import from buildstream.testing import create_repo +from buildstream.testing import generate_element from buildstream.testing._utils.site import HAVE_BZR DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "bzr") @@ -24,7 +23,7 @@ def test_fetch_checkout(cli, tmpdir, datafiles): # Write out our test target element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch, build, checkout result = cli.run(project=project, args=["source", "fetch", "target.bst"]) diff --git a/tests/sources/git.py b/tests/sources/git.py index 1176c8ffd..25ec49347 100644 --- a/tests/sources/git.py +++ b/tests/sources/git.py @@ -29,10 +29,11 @@ import shutil import pytest -from buildstream import _yaml, Node +from buildstream import Node from buildstream.exceptions import ErrorDomain from buildstream.plugin import CoreWarnings from buildstream.testing import cli # pylint: disable=unused-import +from buildstream.testing import generate_project, generate_element, load_yaml from buildstream.testing import create_repo from buildstream.testing._utils.site import HAVE_GIT, HAVE_OLD_GIT @@ -50,7 +51,7 @@ def test_fetch_bad_ref(cli, tmpdir, datafiles): # Write out our test target with a bad ref element = {"kind": "import", "sources": [repo.source_config(ref="5")]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Assert that fetch raises an error here result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -77,7 +78,7 @@ def test_submodule_fetch_checkout(cli, tmpdir, datafiles): # Write out our test target element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch, build, checkout result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -116,7 +117,7 @@ def test_recursive_submodule_fetch_checkout(cli, tmpdir, datafiles): # Write out our test target element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch, build, checkout result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -151,7 +152,7 @@ def test_submodule_fetch_source_enable_explicit(cli, tmpdir, datafiles): # Write out our test target element = {"kind": "import", "sources": [repo.source_config_extra(ref=ref, checkout_submodules=True)]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch, build, checkout result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -185,7 +186,7 @@ def test_submodule_fetch_source_disable(cli, tmpdir, datafiles): # Write out our test target element = {"kind": "import", "sources": [repo.source_config_extra(ref=ref, checkout_submodules=False)]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch, build, checkout result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -219,7 +220,7 @@ def test_submodule_fetch_submodule_does_override(cli, tmpdir, datafiles): # Write out our test target element = {"kind": "import", "sources": [repo.source_config_extra(ref=ref, checkout_submodules=False)]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch, build, checkout result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -258,7 +259,7 @@ def test_submodule_fetch_submodule_individual_checkout(cli, tmpdir, datafiles): # Write out our test target element = {"kind": "import", "sources": [repo.source_config_extra(ref=ref, checkout_submodules=True)]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch, build, checkout result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -298,7 +299,7 @@ def test_submodule_fetch_submodule_individual_checkout_explicit(cli, tmpdir, dat # Write out our test target element = {"kind": "import", "sources": [repo.source_config_extra(ref=ref, checkout_submodules=True)]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch, build, checkout result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -333,7 +334,7 @@ def test_submodule_fetch_project_override(cli, tmpdir, datafiles): # Write out our test target element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch, build, checkout result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -359,7 +360,7 @@ def test_submodule_track_ignore_inconsistent(cli, tmpdir, datafiles): # Write out our test target element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Now add a .gitmodules file with an inconsistent submodule, # we are calling this inconsistent because the file was created @@ -393,8 +394,7 @@ def test_submodule_track_no_ref_or_track(cli, tmpdir, datafiles): gitsource = repo.source_config(ref=None) gitsource.pop("track") element = {"kind": "import", "sources": [gitsource]} - - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Track will encounter an inconsistent submodule without any ref result = cli.run(project=project, args=["show", "target.bst"]) @@ -410,8 +410,7 @@ def test_ref_not_in_track(cli, tmpdir, datafiles, fail): # Make the warning an error if we're testing errors if fail == "error": - project_template = {"name": "foo", "fatal-warnings": [CoreWarnings.REF_NOT_IN_TRACK]} - _yaml.roundtrip_dump(project_template, os.path.join(project, "project.conf")) + generate_project(project, config={"fatal-warnings": [CoreWarnings.REF_NOT_IN_TRACK]}) # Create the repo from 'repofiles', create a branch without latest commit repo = create_repo("git", str(tmpdir)) @@ -424,7 +423,7 @@ def test_ref_not_in_track(cli, tmpdir, datafiles, fail): # Write out our test target element = {"kind": "import", "sources": [gitsource]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) result = cli.run(project=project, args=["build", "target.bst"]) @@ -445,8 +444,7 @@ def test_unlisted_submodule(cli, tmpdir, datafiles, fail): # Make the warning an error if we're testing errors if fail == "error": - project_template = {"name": "foo", "fatal-warnings": ["git:unlisted-submodule"]} - _yaml.roundtrip_dump(project_template, os.path.join(project, "project.conf")) + generate_project(project, config={"fatal-warnings": ["git:unlisted-submodule"]}) # Create the submodule first from the 'subrepofiles' subdir subrepo = create_repo("git", str(tmpdir), "subrepo") @@ -470,7 +468,7 @@ def test_unlisted_submodule(cli, tmpdir, datafiles, fail): # Write out our test target element = {"kind": "import", "sources": [gitsource]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # We will not see the warning or error before the first fetch, because # we don't have the repository yet and so we have no knowledge of @@ -512,8 +510,7 @@ def test_track_unlisted_submodule(cli, tmpdir, datafiles, fail): # Make the warning an error if we're testing errors if fail == "error": - project_template = {"name": "foo", "fatal-warnings": ["git:unlisted-submodule"]} - _yaml.roundtrip_dump(project_template, os.path.join(project, "project.conf")) + generate_project(project, config={"fatal-warnings": ["git:unlisted-submodule"]}) # Create the submodule first from the 'subrepofiles' subdir subrepo = create_repo("git", str(tmpdir), "subrepo") @@ -534,7 +531,7 @@ def test_track_unlisted_submodule(cli, tmpdir, datafiles, fail): # Write out our test target element = {"kind": "import", "sources": [gitsource]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch the repo, we will not see the warning because we # are still pointing to a ref which predates the submodules @@ -567,8 +564,7 @@ def test_invalid_submodule(cli, tmpdir, datafiles, fail): # Make the warning an error if we're testing errors if fail == "error": - project_template = {"name": "foo", "fatal-warnings": ["git:invalid-submodule"]} - _yaml.roundtrip_dump(project_template, os.path.join(project, "project.conf")) + generate_project(project, config={"fatal-warnings": ["git:invalid-submodule"]}) # Create the repo from 'repofiles' subdir repo = create_repo("git", str(tmpdir)) @@ -586,7 +582,7 @@ def test_invalid_submodule(cli, tmpdir, datafiles, fail): # Write out our test target element = {"kind": "import", "sources": [gitsource]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # We will not see the warning or error before the first fetch, because # we don't have the repository yet and so we have no knowledge of @@ -629,8 +625,7 @@ def test_track_invalid_submodule(cli, tmpdir, datafiles, fail): # Make the warning an error if we're testing errors if fail == "error": - project_template = {"name": "foo", "fatal-warnings": ["git:invalid-submodule"]} - _yaml.roundtrip_dump(project_template, os.path.join(project, "project.conf")) + generate_project(project, config={"fatal-warnings": ["git:invalid-submodule"]}) # Create the submodule first from the 'subrepofiles' subdir subrepo = create_repo("git", str(tmpdir), "subrepo") @@ -652,7 +647,7 @@ def test_track_invalid_submodule(cli, tmpdir, datafiles, fail): # Write out our test target element = {"kind": "import", "sources": [gitsource]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Fetch the repo, we will not see the warning because we # are still pointing to a ref which predates the submodules @@ -692,14 +687,14 @@ def test_track_fetch(cli, tmpdir, datafiles, ref_format, tag, extra_commit): # Write out our test target element = {"kind": "import", "sources": [repo.source_config()]} element["sources"][0]["ref-format"] = ref_format + generate_element(project, "target.bst", element) element_path = os.path.join(project, "target.bst") - _yaml.roundtrip_dump(element, element_path) # Track it result = cli.run(project=project, args=["source", "track", "target.bst"]) result.assert_success() - element = _yaml.load(element_path) + element = load_yaml(element_path) new_ref = element.get_sequence("sources").mapping_at(0).get_str("ref") if ref_format == "git-describe" and tag: @@ -724,9 +719,9 @@ def test_track_fetch(cli, tmpdir, datafiles, ref_format, tag, extra_commit): def test_git_describe(cli, tmpdir, datafiles, ref_storage, tag_type): project = str(datafiles) - project_config = _yaml.load(os.path.join(project, "project.conf")) + project_config = load_yaml(os.path.join(project, "project.conf")) project_config["ref-storage"] = ref_storage - _yaml.roundtrip_dump(project_config, os.path.join(project, "project.conf")) + generate_project(project, config=project_config) repofiles = os.path.join(str(tmpdir), "repofiles") os.makedirs(repofiles, exist_ok=True) @@ -775,8 +770,8 @@ def test_git_describe(cli, tmpdir, datafiles, ref_storage, tag_type): "kind": "import", "sources": [config], } + generate_element(project, "target.bst", element) element_path = os.path.join(project, "target.bst") - _yaml.roundtrip_dump(element, element_path) if ref_storage == "inline": result = cli.run(project=project, args=["source", "track", "target.bst"]) @@ -786,7 +781,7 @@ def test_git_describe(cli, tmpdir, datafiles, ref_storage, tag_type): result.assert_success() if ref_storage == "inline": - element = _yaml.load(element_path) + element = load_yaml(element_path) tags = element.get_sequence("sources").mapping_at(0).get_sequence("tags") assert len(tags) == 2 for tag in tags: @@ -834,9 +829,9 @@ def test_git_describe(cli, tmpdir, datafiles, ref_storage, tag_type): def test_git_describe_head_is_tagged(cli, tmpdir, datafiles, ref_storage, tag_type): project = str(datafiles) - project_config = _yaml.load(os.path.join(project, "project.conf")) + project_config = load_yaml(os.path.join(project, "project.conf")) project_config["ref-storage"] = ref_storage - _yaml.roundtrip_dump(project_config, os.path.join(project, "project.conf")) + generate_project(project, config=project_config) repofiles = os.path.join(str(tmpdir), "repofiles") os.makedirs(repofiles, exist_ok=True) @@ -884,8 +879,8 @@ def test_git_describe_head_is_tagged(cli, tmpdir, datafiles, ref_storage, tag_ty "kind": "import", "sources": [config], } + generate_element(project, "target.bst", element) element_path = os.path.join(project, "target.bst") - _yaml.roundtrip_dump(element, element_path) if ref_storage == "inline": result = cli.run(project=project, args=["source", "track", "target.bst"]) @@ -895,7 +890,7 @@ def test_git_describe_head_is_tagged(cli, tmpdir, datafiles, ref_storage, tag_ty result.assert_success() if ref_storage == "inline": - element = _yaml.load(element_path) + element = load_yaml(element_path) source = element.get_sequence("sources").mapping_at(0) tags = source.get_sequence("tags") assert len(tags) == 1 @@ -941,9 +936,9 @@ def test_git_describe_head_is_tagged(cli, tmpdir, datafiles, ref_storage, tag_ty def test_git_describe_relevant_history(cli, tmpdir, datafiles): project = str(datafiles) - project_config = _yaml.load(os.path.join(project, "project.conf")) + project_config = load_yaml(os.path.join(project, "project.conf")) project_config["ref-storage"] = "project.refs" - _yaml.roundtrip_dump(project_config, os.path.join(project, "project.conf")) + generate_project(project, config=project_config) repofiles = os.path.join(str(tmpdir), "repofiles") os.makedirs(repofiles, exist_ok=True) @@ -989,8 +984,7 @@ def test_git_describe_relevant_history(cli, tmpdir, datafiles): "kind": "import", "sources": [config], } - element_path = os.path.join(project, "target.bst") - _yaml.roundtrip_dump(element, element_path) + generate_element(project, "target.bst", element) result = cli.run(project=project, args=["source", "track", "target.bst", "--deps", "all"]) result.assert_success() @@ -1015,9 +1009,9 @@ def test_git_describe_relevant_history(cli, tmpdir, datafiles): def test_default_do_not_track_tags(cli, tmpdir, datafiles): project = str(datafiles) - project_config = _yaml.load(os.path.join(project, "project.conf")) + project_config = load_yaml(os.path.join(project, "project.conf")) project_config["ref-storage"] = "inline" - _yaml.roundtrip_dump(project_config, os.path.join(project, "project.conf")) + generate_project(project, config=project_config) repofiles = os.path.join(str(tmpdir), "repofiles") os.makedirs(repofiles, exist_ok=True) @@ -1038,13 +1032,13 @@ def test_default_do_not_track_tags(cli, tmpdir, datafiles): "kind": "import", "sources": [config], } + generate_element(project, "target.bst", element) element_path = os.path.join(project, "target.bst") - _yaml.roundtrip_dump(element, element_path) result = cli.run(project=project, args=["source", "track", "target.bst"]) result.assert_success() - element = _yaml.load(element_path) + element = load_yaml(element_path) source = element.get_sequence("sources").mapping_at(0) assert "tags" not in source @@ -1070,10 +1064,10 @@ def test_overwrite_rogue_tag_multiple_remotes(cli, tmpdir, datafiles): top_commit = repo.create(repofiles) repodir, reponame = os.path.split(repo.repo) - project_config = _yaml.load(os.path.join(project, "project.conf")) + project_config = load_yaml(os.path.join(project, "project.conf")) project_config["aliases"] = Node.from_dict({"repo": "http://example.com/"}) project_config["mirrors"] = [{"name": "middle-earth", "aliases": {"repo": ["file://{}/".format(repodir)]}}] - _yaml.roundtrip_dump(project_config, os.path.join(project, "project.conf")) + generate_project(project, config=project_config) repo.add_annotated_tag("tag", "tag") @@ -1092,8 +1086,7 @@ def test_overwrite_rogue_tag_multiple_remotes(cli, tmpdir, datafiles): "kind": "import", "sources": [config], } - element_path = os.path.join(project, "target.bst") - _yaml.roundtrip_dump(element, element_path) + generate_element(project, "target.bst", element) result = cli.run(project=project, args=["build", "target.bst"]) result.assert_success() @@ -1116,7 +1109,7 @@ def test_overwrite_rogue_tag_multiple_remotes(cli, tmpdir, datafiles): repodir, reponame = os.path.split(repo.repo) - _yaml.roundtrip_dump(project_config, os.path.join(project, "project.conf")) + generate_project(project, config=project_config) config = repo.source_config(ref=new_ref) del config["track"] @@ -1126,7 +1119,7 @@ def test_overwrite_rogue_tag_multiple_remotes(cli, tmpdir, datafiles): "kind": "import", "sources": [config], } - _yaml.roundtrip_dump(element, element_path) + generate_element(project, "target.bst", element) result = cli.run(project=project, args=["build", "target.bst"]) result.assert_success() diff --git a/tests/sources/no_fetch_cached.py b/tests/sources/no_fetch_cached.py index 1ee3dd7bd..4984f5326 100644 --- a/tests/sources/no_fetch_cached.py +++ b/tests/sources/no_fetch_cached.py @@ -4,10 +4,9 @@ import os import pytest -from buildstream import _yaml - from buildstream.testing import cli # pylint: disable=unused-import from buildstream.testing import create_repo +from buildstream.testing import generate_element from buildstream.testing._utils.site import HAVE_GIT DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "no-fetch-cached") @@ -28,7 +27,7 @@ def test_no_fetch_cached(cli, tmpdir, datafiles): # Write out test target with a cached and a non-cached source element = {"kind": "import", "sources": [repo.source_config(ref=ref), {"kind": "always_cached"}]} - _yaml.roundtrip_dump(element, os.path.join(project, "target.bst")) + generate_element(project, "target.bst", element) # Test fetch of target with a cached and a non-cached source result = cli.run(project=project, args=["source", "fetch", "target.bst"]) diff --git a/tests/sources/pip.py b/tests/sources/pip.py index 7020f19c2..cfe20438a 100644 --- a/tests/sources/pip.py +++ b/tests/sources/pip.py @@ -4,19 +4,13 @@ import os import pytest -from buildstream import _yaml from buildstream.exceptions import ErrorDomain from buildstream.plugins.sources.pip import _match_package_name -from buildstream.testing import cli # pylint: disable=unused-import +from buildstream.testing import cli, ErrorDomain, generate_project # pylint: disable=unused-import DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pip",) -def generate_project(project_dir): - project_file = os.path.join(project_dir, "project.conf") - _yaml.roundtrip_dump({"name": "foo"}, project_file) - - # Test that without ref, consistency is set appropriately. @pytest.mark.datafiles(os.path.join(DATA_DIR, "no-ref")) def test_no_ref(cli, datafiles): diff --git a/tests/sources/previous_source_access.py b/tests/sources/previous_source_access.py index fadf6710c..3c95d77e2 100644 --- a/tests/sources/previous_source_access.py +++ b/tests/sources/previous_source_access.py @@ -4,7 +4,7 @@ import os import pytest -from buildstream import _yaml +from buildstream.testing import generate_project, load_yaml from buildstream.testing import cli # pylint: disable=unused-import DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "previous_source_access") @@ -20,10 +20,10 @@ def test_custom_transform_source(cli, datafiles): # Set the project_dir alias in project.conf to the path to the tested project project_config_path = os.path.join(project, "project.conf") - project_config = _yaml.load(project_config_path) + project_config = load_yaml(project_config_path) aliases = project_config.get_mapping("aliases") aliases["project_dir"] = "file://{}".format(project) - _yaml.roundtrip_dump(project_config, project_config_path) + generate_project(project, project_config) # Ensure we can track result = cli.run(project=project, args=["source", "track", "target.bst"]) diff --git a/tests/sources/remote.py b/tests/sources/remote.py index 00a7687b3..d546c6131 100644 --- a/tests/sources/remote.py +++ b/tests/sources/remote.py @@ -6,28 +6,18 @@ import stat import pytest from buildstream.testing import ErrorDomain -from buildstream import _yaml +from buildstream.testing import generate_project from buildstream.testing import cli # pylint: disable=unused-import from tests.testutils.file_server import create_file_server DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "remote",) -def generate_project(project_dir, tmpdir): - project_file = os.path.join(project_dir, "project.conf") - _yaml.roundtrip_dump({"name": "foo", "aliases": {"tmpdir": "file:///" + str(tmpdir)}}, project_file) - - -def generate_project_file_server(server, project_dir): - project_file = os.path.join(project_dir, "project.conf") - _yaml.roundtrip_dump({"name": "foo", "aliases": {"tmpdir": server.base_url()}}, project_file) - - # Test that without ref, consistency is set appropriately. @pytest.mark.datafiles(os.path.join(DATA_DIR, "no-ref")) def test_no_ref(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, {"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) assert cli.get_element_state(project, "target.bst") == "no reference" @@ -36,7 +26,7 @@ def test_no_ref(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "missing-file")) def test_missing_file(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, {"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) # Try to fetch it result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -48,7 +38,7 @@ def test_missing_file(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "path-in-filename")) def test_path_in_filename(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, {"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) # Try to fetch it result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -60,7 +50,8 @@ def test_path_in_filename(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "single-file")) def test_simple_file_build(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, {"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) + checkoutdir = os.path.join(str(tmpdir), "checkout") # Try to fetch it @@ -87,7 +78,8 @@ def test_simple_file_build(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "single-file-custom-name")) def test_simple_file_custom_name_build(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, {"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) + checkoutdir = os.path.join(str(tmpdir), "checkout") # Try to fetch it @@ -109,7 +101,8 @@ def test_unique_key(cli, tmpdir, datafiles): to generating a cache key for the source. """ project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, {"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) + states = cli.get_element_states(project, ["target.bst", "target-custom.bst", "target-custom-executable.bst"]) assert states["target.bst"] == "fetch needed" assert states["target-custom.bst"] == "fetch needed" @@ -118,7 +111,7 @@ def test_unique_key(cli, tmpdir, datafiles): # Try to fetch it cli.run(project=project, args=["source", "fetch", "target.bst"]) - # We should download the file only once + # We should download_yaml the file only once states = cli.get_element_states(project, ["target.bst", "target-custom.bst", "target-custom-executable.bst"]) assert states["target.bst"] == "buildable" assert states["target-custom.bst"] == "buildable" @@ -137,7 +130,8 @@ def test_executable(cli, tmpdir, datafiles): """This test confirms that the 'ecxecutable' parameter is honoured. """ project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, {"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) + checkoutdir = os.path.join(str(tmpdir), "checkout") assert cli.get_element_state(project, "target-custom-executable.bst") == "fetch needed" # Try to fetch it @@ -167,7 +161,7 @@ def test_use_netrc(cli, datafiles, server_type, tmpdir): with create_file_server(server_type) as server: server.add_user("testuser", "12345", project) - generate_project_file_server(server, project) + generate_project(project, {"aliases": {"tmpdir": server.base_url()}}) server.start() diff --git a/tests/sources/tar.py b/tests/sources/tar.py index d35cc2964..8ac627492 100644 --- a/tests/sources/tar.py +++ b/tests/sources/tar.py @@ -11,8 +11,8 @@ import urllib.parse import pytest from buildstream import utils -from buildstream import _yaml from buildstream.exceptions import ErrorDomain +from buildstream.testing import generate_project, generate_element from buildstream.testing import cli # pylint: disable=unused-import from buildstream.testing._utils.site import HAVE_LZIP from tests.testutils.file_server import create_file_server @@ -41,21 +41,11 @@ def _assemble_tar_lz(workingdir, srcdir, dstfile): os.chdir(old_dir) -def generate_project(project_dir, tmpdir): - project_file = os.path.join(project_dir, "project.conf") - _yaml.roundtrip_dump({"name": "foo", "aliases": {"tmpdir": "file:///" + str(tmpdir)}}, project_file) - - -def generate_project_file_server(base_url, project_dir): - project_file = os.path.join(project_dir, "project.conf") - _yaml.roundtrip_dump({"name": "foo", "aliases": {"tmpdir": base_url}}, project_file) - - # Test that without ref, consistency is set appropriately. @pytest.mark.datafiles(os.path.join(DATA_DIR, "no-ref")) def test_no_ref(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) assert cli.get_element_state(project, "target.bst") == "no reference" @@ -63,7 +53,7 @@ def test_no_ref(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "fetch")) def test_fetch_bad_url(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) # Try to fetch it result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -76,7 +66,7 @@ def test_fetch_bad_url(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "fetch")) def test_fetch_bad_ref(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) # Create a local tar src_tar = os.path.join(str(tmpdir), "a.tar.gz") @@ -92,7 +82,7 @@ def test_fetch_bad_ref(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "fetch")) def test_track_warning(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) # Create a local tar src_tar = os.path.join(str(tmpdir), "a.tar.gz") @@ -109,7 +99,7 @@ def test_track_warning(cli, tmpdir, datafiles): @pytest.mark.parametrize("srcdir", ["a", "./a"]) def test_stage_default_basedir(cli, tmpdir, datafiles, srcdir): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) checkoutdir = os.path.join(str(tmpdir), "checkout") # Create a local tar @@ -138,7 +128,7 @@ def test_stage_default_basedir(cli, tmpdir, datafiles, srcdir): @pytest.mark.parametrize("srcdir", ["a", "./a"]) def test_stage_no_basedir(cli, tmpdir, datafiles, srcdir): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) checkoutdir = os.path.join(str(tmpdir), "checkout") # Create a local tar @@ -167,7 +157,7 @@ def test_stage_no_basedir(cli, tmpdir, datafiles, srcdir): @pytest.mark.parametrize("srcdir", ["a", "./a"]) def test_stage_explicit_basedir(cli, tmpdir, datafiles, srcdir): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) checkoutdir = os.path.join(str(tmpdir), "checkout") # Create a local tar @@ -196,7 +186,7 @@ def test_stage_explicit_basedir(cli, tmpdir, datafiles, srcdir): @pytest.mark.datafiles(os.path.join(DATA_DIR, "contains-links")) def test_stage_contains_links(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) checkoutdir = os.path.join(str(tmpdir), "checkout") # Create a local tar @@ -232,7 +222,7 @@ def test_stage_contains_links(cli, tmpdir, datafiles): @pytest.mark.parametrize("srcdir", ["a", "./a"]) def test_stage_default_basedir_lzip(cli, tmpdir, datafiles, srcdir): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) checkoutdir = os.path.join(str(tmpdir), "checkout") # Create a local tar @@ -265,17 +255,17 @@ def test_stage_default_basedir_lzip(cli, tmpdir, datafiles, srcdir): def test_read_only_dir(cli, tmpdir, datafiles, tar_name, base_dir): try: project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) - bst_path = os.path.join(project, "target.bst") tar_file = "{}.tar.gz".format(tar_name) - _yaml.roundtrip_dump( + generate_element( + project, + "target.bst", { "kind": "import", "sources": [{"kind": "tar", "url": "tmpdir:/{}".format(tar_file), "ref": "foo", "base-dir": base_dir}], }, - bst_path, ) # Get the tarball in tests/sources/tar/read-only/content @@ -326,7 +316,7 @@ def test_use_netrc(cli, datafiles, server_type, tmpdir): with create_file_server(server_type) as server: server.add_user("testuser", "12345", file_server_files) - generate_project_file_server(server.base_url(), project) + generate_project(project, config={"aliases": {"tmpdir": server.base_url()}}) src_tar = os.path.join(file_server_files, "a.tar.gz") _assemble_tar(os.path.join(str(datafiles), "content"), "a", src_tar) @@ -368,7 +358,7 @@ def test_netrc_already_specified_user(cli, datafiles, server_type, tmpdir): server.add_user("otheruser", "12345", file_server_files) parts = urllib.parse.urlsplit(server.base_url()) base_url = urllib.parse.urlunsplit([parts[0], "otheruser@{}".format(parts[1]), *parts[2:]]) - generate_project_file_server(base_url, project) + generate_project(project, config={"aliases": {"tmpdir": base_url}}) src_tar = os.path.join(file_server_files, "a.tar.gz") _assemble_tar(os.path.join(str(datafiles), "content"), "a", src_tar) @@ -385,7 +375,7 @@ def test_netrc_already_specified_user(cli, datafiles, server_type, tmpdir): @pytest.mark.datafiles(os.path.join(DATA_DIR, "fetch")) def test_homeless_environment(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) # Create a local tar src_tar = os.path.join(str(tmpdir), "a.tar.gz") @@ -407,7 +397,7 @@ def test_out_of_basedir_hardlinks(cli, tmpdir, datafiles): return member project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) checkoutdir = os.path.join(str(tmpdir), "checkout") # Create a tarball with an odd hardlink @@ -445,7 +435,7 @@ def test_out_of_basedir_hardlinks(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "out-of-basedir-hardlinks")) def test_malicious_out_of_basedir_hardlinks(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) # Create a maliciously-hardlinked tarball def ensure_link(member): diff --git a/tests/sources/zip.py b/tests/sources/zip.py index 7c47058b5..201271fe0 100644 --- a/tests/sources/zip.py +++ b/tests/sources/zip.py @@ -6,8 +6,8 @@ import zipfile import pytest -from buildstream import _yaml from buildstream.exceptions import ErrorDomain +from buildstream.testing import generate_project from buildstream.testing import cli # pylint: disable=unused-import from tests.testutils.file_server import create_file_server from . import list_dir_contents @@ -27,21 +27,11 @@ def _assemble_zip(workingdir, dstfile): os.chdir(old_dir) -def generate_project(project_dir, tmpdir): - project_file = os.path.join(project_dir, "project.conf") - _yaml.roundtrip_dump({"name": "foo", "aliases": {"tmpdir": "file:///" + str(tmpdir)}}, project_file) - - -def generate_project_file_server(server, project_dir): - project_file = os.path.join(project_dir, "project.conf") - _yaml.roundtrip_dump({"name": "foo", "aliases": {"tmpdir": server.base_url()}}, project_file) - - # Test that without ref, consistency is set appropriately. @pytest.mark.datafiles(os.path.join(DATA_DIR, "no-ref")) def test_no_ref(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) assert cli.get_element_state(project, "target.bst") == "no reference" @@ -49,7 +39,7 @@ def test_no_ref(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "fetch")) def test_fetch_bad_url(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) # Try to fetch it result = cli.run(project=project, args=["source", "fetch", "target.bst"]) @@ -62,7 +52,7 @@ def test_fetch_bad_url(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "fetch")) def test_fetch_bad_ref(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) # Create a local tar src_zip = os.path.join(str(tmpdir), "a.zip") @@ -78,7 +68,7 @@ def test_fetch_bad_ref(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "fetch")) def test_track_warning(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) # Create a local tar src_zip = os.path.join(str(tmpdir), "a.zip") @@ -94,7 +84,7 @@ def test_track_warning(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "fetch")) def test_stage_default_basedir(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) checkoutdir = os.path.join(str(tmpdir), "checkout") # Create a local tar @@ -122,7 +112,7 @@ def test_stage_default_basedir(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "no-basedir")) def test_stage_no_basedir(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) checkoutdir = os.path.join(str(tmpdir), "checkout") # Create a local tar @@ -150,7 +140,7 @@ def test_stage_no_basedir(cli, tmpdir, datafiles): @pytest.mark.datafiles(os.path.join(DATA_DIR, "explicit-basedir")) def test_stage_explicit_basedir(cli, tmpdir, datafiles): project = str(datafiles) - generate_project(project, tmpdir) + generate_project(project, config={"aliases": {"tmpdir": "file:///" + str(tmpdir)}}) checkoutdir = os.path.join(str(tmpdir), "checkout") # Create a local tar @@ -193,7 +183,7 @@ def test_use_netrc(cli, datafiles, server_type, tmpdir): with create_file_server(server_type) as server: server.add_user("testuser", "12345", file_server_files) - generate_project_file_server(server, project) + generate_project(project, config={"aliases": {"tmpdir": server.base_url()}}) src_zip = os.path.join(file_server_files, "a.zip") _assemble_zip(os.path.join(str(datafiles), "content"), src_zip) -- cgit v1.2.1