diff options
Diffstat (limited to 'src/buildstream/testing/_sourcetests/workspace.py')
-rw-r--r-- | src/buildstream/testing/_sourcetests/workspace.py | 130 |
1 files changed, 25 insertions, 105 deletions
diff --git a/src/buildstream/testing/_sourcetests/workspace.py b/src/buildstream/testing/_sourcetests/workspace.py index 3520a8cc4..9edd9d994 100644 --- a/src/buildstream/testing/_sourcetests/workspace.py +++ b/src/buildstream/testing/_sourcetests/workspace.py @@ -20,7 +20,6 @@ # pylint: disable=redefined-outer-name import os -import shutil import pytest from buildstream import _yaml @@ -33,116 +32,37 @@ TOP_DIR = os.path.dirname(os.path.realpath(__file__)) DATA_DIR = os.path.join(TOP_DIR, "project") -class WorkspaceCreator: - def __init__(self, cli, tmpdir, datafiles, project_path=None): - self.cli = cli - self.tmpdir = tmpdir - self.datafiles = datafiles - - if not project_path: - project_path = str(datafiles) - else: - shutil.copytree(str(datafiles), project_path) - - self.project_path = project_path - self.bin_files_path = os.path.join(project_path, "files", "bin-files") - - self.workspace_cmd = os.path.join(self.project_path, "workspace_cmd") - - def create_workspace_element(self, kind, suffix="", workspace_dir=None, element_attrs=None): - element_name = "workspace-test-{}{}.bst".format(kind, suffix) - element_path = os.path.join(self.project_path, "elements") - if not workspace_dir: - workspace_dir = os.path.join(self.workspace_cmd, element_name) - if workspace_dir[-4:] == ".bst": - workspace_dir = workspace_dir[:-4] - - # Create our repo object of the given source type with - # the bin files, and then collect the initial ref. - repo = create_repo(kind, str(self.tmpdir)) - ref = repo.create(self.bin_files_path) - - # Write out our test target - element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} - if element_attrs: - element = {**element, **element_attrs} - _yaml.roundtrip_dump(element, os.path.join(element_path, element_name)) - return element_name, element_path, workspace_dir - - def create_workspace_elements(self, kinds, suffixs=None, workspace_dir_usr=None, element_attrs=None): - - element_tuples = [] - - if suffixs is None: - suffixs = ["",] * len(kinds) - else: - if len(suffixs) != len(kinds): - raise "terable error" - - for suffix, kind in zip(suffixs, kinds): - element_name, _, workspace_dir = self.create_workspace_element( - kind, suffix, workspace_dir_usr, element_attrs - ) - element_tuples.append((element_name, workspace_dir)) - - # Assert that there is no reference, a fetch is needed - states = self.cli.get_element_states(self.project_path, [e for e, _ in element_tuples]) - assert not any(states[e] != "fetch needed" for e, _ in element_tuples) - - return element_tuples - - def open_workspaces(self, kinds, suffixs=None, workspace_dir=None, element_attrs=None, no_checkout=False): - - element_tuples = self.create_workspace_elements(kinds, suffixs, workspace_dir, element_attrs) - os.makedirs(self.workspace_cmd, exist_ok=True) - - # Now open the workspace, this should have the effect of automatically - # fetching the source from the repo. - args = ["workspace", "open"] - - if no_checkout: - args.append("--no-checkout") - if workspace_dir is not None: - assert len(element_tuples) == 1, "test logic error" - _, workspace_dir = element_tuples[0] - args.extend(["--directory", workspace_dir]) +@pytest.mark.datafiles(DATA_DIR) +def test_open(cli, tmpdir_factory, datafiles, kind): + project_path = str(datafiles) + bin_files_path = os.path.join(project_path, "files", "bin-files") - args.extend([element_name for element_name, workspace_dir_suffix in element_tuples]) - result = self.cli.run(cwd=self.workspace_cmd, project=self.project_path, args=args) + element_name = "workspace-test-{}.bst".format(kind) + element_path = os.path.join(project_path, "elements") - result.assert_success() + # Create our repo object of the given source type with + # the bin files, and then collect the initial ref. + repo = create_repo(kind, str(tmpdir_factory.mktemp("repo-{}".format(kind)))) + ref = repo.create(bin_files_path) - if not no_checkout: - # Assert that we are now buildable because the source is now cached. - states = self.cli.get_element_states(self.project_path, [e for e, _ in element_tuples]) - assert not any(states[e] != "buildable" for e, _ in element_tuples) + # Write out our test target + element = {"kind": "import", "sources": [repo.source_config(ref=ref)]} + _yaml.roundtrip_dump(element, os.path.join(element_path, element_name)) - # Check that the executable hello file is found in each workspace - for _, workspace in element_tuples: - filename = os.path.join(workspace, "usr", "bin", "hello") - assert os.path.exists(filename) + # Assert that there is no reference, a fetch is needed + assert cli.get_element_state(project_path, element_name) == "fetch needed" - return element_tuples + workspace_dir = os.path.join(tmpdir_factory.mktemp("opened_workspace")) + # Now open the workspace, this should have the effect of automatically + # fetching the source from the repo. + result = cli.run(project=project_path, args=["workspace", "open", "--directory", workspace_dir, element_name]) -def open_workspace( - cli, - tmpdir, - datafiles, - kind, - suffix="", - workspace_dir=None, - project_path=None, - element_attrs=None, - no_checkout=False, -): - workspace_object = WorkspaceCreator(cli, tmpdir, datafiles, project_path) - workspaces = workspace_object.open_workspaces((kind,), (suffix,), workspace_dir, element_attrs, no_checkout) - assert len(workspaces) == 1 - element_name, workspace = workspaces[0] - return element_name, workspace_object.project_path, workspace + result.assert_success() + # Assert that we are now buildable because the source is now cached. + assert cli.get_element_state(project_path, element_name) == "buildable" -@pytest.mark.datafiles(DATA_DIR) -def test_open(cli, tmpdir, datafiles, kind): - open_workspace(cli, tmpdir, datafiles, kind) + # Check that the executable hello file is found in each workspace + filename = os.path.join(workspace_dir, "usr", "bin", "hello") + assert os.path.exists(filename) |