Commit acbbcbdf authored by William Salmon's avatar William Salmon
Browse files

tests/frontend/workspace.py: Refactor support function

The Refactor now makes it possible to test for the forth coming multi
element workspace open commands and to test for failure.
parent 327b19dd
Loading
Loading
Loading
Loading
+105 −51
Original line number Diff line number Diff line
@@ -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,23 +44,33 @@ 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))
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)
    bin_files_path = os.path.join(project_path, 'files', 'bin-files')
    element_path = os.path.join(project_path, 'elements')

        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(tmpdir))
    ref = repo.create(bin_files_path)
        repo = create_repo(kind, str(self.tmpdir))
        ref = repo.create(self.bin_files_path)
        if track:
            ref = None

@@ -75,33 +86,76 @@ def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir
        _yaml.dump(element,
                   os.path.join(element_path,
                                element_name))
        return element_name, element_path, workspace_dir

    def create_workspace_elements(self, kinds, track, 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, element_path, workspace_dir = \
                self.create_workspace_element(kind, track, suffix, workspace_dir_usr,
                                              element_attrs)

            # Assert that there is no reference, a track & fetch is needed
    state = cli.get_element_state(project_path, element_name)
            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))

        return element_tuples

    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')
    args.extend([element_name, workspace_dir])
    result = cli.run(project=project_path, args=args)
        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)

        result.assert_success()

        for element_name, workspace_dir in element_tuples:
            # Assert that we are now buildable because the source is
            # now cached.
    assert cli.get_element_state(project_path, element_name) == 'buildable'
            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_name, project_path, workspace_dir)
        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)