diff options
Diffstat (limited to 'tests/frontend/workspace.py')
-rw-r--r-- | tests/frontend/workspace.py | 156 |
1 files changed, 105 insertions, 51 deletions
diff --git a/tests/frontend/workspace.py b/tests/frontend/workspace.py index 51b7d6088..f620d4c13 100644 --- a/tests/frontend/workspace.py +++ b/tests/frontend/workspace.py @@ -21,6 +21,7 @@ # Phillip Smyth <phillip.smyth@codethink.co.uk> # Jonathan Maw <jonathan.maw@codethink.co.uk> # Richard Maw <richard.maw@codethink.co.uk> +# William Salmon <will.salmon@codethink.co.uk> # import os @@ -43,65 +44,118 @@ DATA_DIR = os.path.join( ) -def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir=None, - project_path=None, element_attrs=None): - if not workspace_dir: - workspace_dir = os.path.join(str(tmpdir), 'workspace{}'.format(suffix)) - if not project_path: - project_path = os.path.join(datafiles.dirname, datafiles.basename) - else: - shutil.copytree(os.path.join(datafiles.dirname, datafiles.basename), project_path) - bin_files_path = os.path.join(project_path, 'files', 'bin-files') - element_path = os.path.join(project_path, 'elements') - element_name = 'workspace-test-{}{}.bst'.format(kind, suffix) +class WorkspaceCreater(): + def __init__(self, cli, tmpdir, datafiles, project_path=None): + self.cli = cli + self.tmpdir = tmpdir + self.datafiles = datafiles + + if not project_path: + project_path = os.path.join(datafiles.dirname, datafiles.basename) + else: + shutil.copytree(os.path.join(datafiles.dirname, datafiles.basename), 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, track, 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.rstrip('.bst')) + + # 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) + if track: + ref = None + + # Write out our test target + element = { + 'kind': 'import', + 'sources': [ + repo.source_config(ref=ref) + ] + } + if element_attrs: + element = {**element, **element_attrs} + _yaml.dump(element, + os.path.join(element_path, + element_name)) + return element_name, element_path, workspace_dir - # 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)) - ref = repo.create(bin_files_path) - if track: - ref = None + def create_workspace_elements(self, kinds, track, suffixs=None, workspace_dir_usr=None, + element_attrs=None): - # Write out our test target - element = { - 'kind': 'import', - 'sources': [ - repo.source_config(ref=ref) - ] - } - if element_attrs: - element = {**element, **element_attrs} - _yaml.dump(element, - os.path.join(element_path, - element_name)) + element_tuples = [] - # Assert that there is no reference, a track & fetch is needed - state = cli.get_element_state(project_path, element_name) - if track: - assert state == 'no reference' - else: - assert state == 'fetch needed' + if suffixs is None: + suffixs = ['', ] * len(kinds) + else: + if len(suffixs) != len(kinds): + raise "terable error" - # Now open the workspace, this should have the effect of automatically - # tracking & fetching the source from the repo. - args = ['workspace', 'open'] - if track: - args.append('--track') - args.extend([element_name, workspace_dir]) - result = cli.run(project=project_path, args=args) + for suffix, kind in zip(suffixs, kinds): + element_name, element_path, workspace_dir = \ + self.create_workspace_element(kind, track, suffix, workspace_dir_usr, + element_attrs) - result.assert_success() + # Assert that there is no reference, a track & fetch is needed + state = self.cli.get_element_state(self.project_path, element_name) + if track: + assert state == 'no reference' + else: + assert state == 'fetch needed' + element_tuples.append((element_name, workspace_dir)) - # Assert that we are now buildable because the source is - # now cached. - assert cli.get_element_state(project_path, element_name) == 'buildable' + return element_tuples - # Check that the executable hello file is found in the workspace - filename = os.path.join(workspace_dir, 'usr', 'bin', 'hello') - assert os.path.exists(filename) + def open_workspaces(self, kinds, track, suffixs=None, workspace_dir=None, + element_attrs=None): + + element_tuples = self.create_workspace_elements(kinds, track, suffixs, workspace_dir, + element_attrs) + os.makedirs(self.workspace_cmd, exist_ok=True) + + # Now open the workspace, this should have the effect of automatically + # tracking & fetching the source from the repo. + args = ['workspace', 'open'] + if track: + args.append('--track') + if workspace_dir is not None: + assert len(element_tuples) == 1, "test logic error" + _, workspace_dir = element_tuples[0] + args.extend(['--directory', workspace_dir]) + + 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) - return (element_name, project_path, workspace_dir) + result.assert_success() + + for element_name, workspace_dir in element_tuples: + # Assert that we are now buildable because the source is + # now cached. + assert self.cli.get_element_state(self.project_path, element_name) == 'buildable' + + # Check that the executable hello file is found in the workspace + filename = os.path.join(workspace_dir, 'usr', 'bin', 'hello') + assert os.path.exists(filename) + + return element_tuples + + +def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir=None, + project_path=None, element_attrs=None): + workspace_object = WorkspaceCreater(cli, tmpdir, datafiles, project_path) + workspaces = workspace_object.open_workspaces((kind, ), track, (suffix, ), workspace_dir, + element_attrs) + assert len(workspaces) == 1 + element_name, workspace = workspaces[0] + return element_name, workspace_object.project_path, workspace @pytest.mark.datafiles(DATA_DIR) |