[Notes] [Git][BuildStream/buildstream][jonathan/workspace-fragment-create] 18 commits: tests/plugin/pipeline.py: Avoid using host user conf



Title: GitLab

Jonathan Maw pushed to branch jonathan/workspace-fragment-create at BuildStream / buildstream

Commits:

19 changed files:

Changes:

  • NEWS
    ... ... @@ -63,6 +63,13 @@ buildstream 1.3.1
    63 63
     
    
    64 64
       o Added new `bst source-checkout` command to checkout sources of an element.
    
    65 65
     
    
    66
    +  o `bst workspace open` now supports the creation of multiple elements and
    
    67
    +    allows the user to set a default location for their creation. This has meant
    
    68
    +    that the new CLI is no longer backwards compatible with buildstream 1.2.
    
    69
    +
    
    70
    +  o Opening a workspace now creates a .bstproject.yaml file that allows buildstream
    
    71
    +    commands to be run from a workspace that is not inside a project.
    
    72
    +
    
    66 73
     
    
    67 74
     =================
    
    68 75
     buildstream 1.1.5
    

  • buildstream/_context.py
    ... ... @@ -31,7 +31,7 @@ from ._exceptions import LoadError, LoadErrorReason, BstError
    31 31
     from ._message import Message, MessageType
    
    32 32
     from ._profile import Topics, profile_start, profile_end
    
    33 33
     from ._artifactcache import ArtifactCache
    
    34
    -from ._workspaces import Workspaces
    
    34
    +from ._workspaces import Workspaces, WorkspaceLocals
    
    35 35
     from .plugin import _plugin_lookup
    
    36 36
     
    
    37 37
     
    
    ... ... @@ -59,6 +59,9 @@ class Context():
    59 59
             # The directory where build sandboxes will be created
    
    60 60
             self.builddir = None
    
    61 61
     
    
    62
    +        # Default root location for workspaces
    
    63
    +        self.workspacedir = None
    
    64
    +
    
    62 65
             # The local binary artifact cache directory
    
    63 66
             self.artifactdir = None
    
    64 67
     
    
    ... ... @@ -118,6 +121,10 @@ class Context():
    118 121
             # remove a workspace directory.
    
    119 122
             self.prompt_workspace_close_remove_dir = None
    
    120 123
     
    
    124
    +        # Boolean, whether we double-check with the user that they meant to
    
    125
    +        # close the workspace when they're using it to access the project.
    
    126
    +        self.prompt_workspace_close_project_inaccessible = None
    
    127
    +
    
    121 128
             # Boolean, whether we double-check with the user that they meant to do
    
    122 129
             # a hard reset of a workspace, potentially losing changes.
    
    123 130
             self.prompt_workspace_reset_hard = None
    
    ... ... @@ -136,6 +143,7 @@ class Context():
    136 143
             self._projects = []
    
    137 144
             self._project_overrides = {}
    
    138 145
             self._workspaces = None
    
    146
    +        self._workspace_locals = WorkspaceLocals()
    
    139 147
             self._log_handle = None
    
    140 148
             self._log_filename = None
    
    141 149
     
    
    ... ... @@ -177,10 +185,10 @@ class Context():
    177 185
             _yaml.node_validate(defaults, [
    
    178 186
                 'sourcedir', 'builddir', 'artifactdir', 'logdir',
    
    179 187
                 'scheduler', 'artifacts', 'logging', 'projects',
    
    180
    -            'cache', 'prompt'
    
    188
    +            'cache', 'prompt', 'workspacedir',
    
    181 189
             ])
    
    182 190
     
    
    183
    -        for directory in ['sourcedir', 'builddir', 'artifactdir', 'logdir']:
    
    191
    +        for directory in ['sourcedir', 'builddir', 'artifactdir', 'logdir', 'workspacedir']:
    
    184 192
                 # Allow the ~ tilde expansion and any environment variables in
    
    185 193
                 # path specification in the config files.
    
    186 194
                 #
    
    ... ... @@ -245,12 +253,15 @@ class Context():
    245 253
                 defaults, Mapping, 'prompt')
    
    246 254
             _yaml.node_validate(prompt, [
    
    247 255
                 'auto-init', 'really-workspace-close-remove-dir',
    
    256
    +            'really-workspace-close-project-inaccessible',
    
    248 257
                 'really-workspace-reset-hard',
    
    249 258
             ])
    
    250 259
             self.prompt_auto_init = _node_get_option_str(
    
    251 260
                 prompt, 'auto-init', ['ask', 'no']) == 'ask'
    
    252 261
             self.prompt_workspace_close_remove_dir = _node_get_option_str(
    
    253 262
                 prompt, 'really-workspace-close-remove-dir', ['ask', 'yes']) == 'ask'
    
    263
    +        self.prompt_workspace_close_project_inaccessible = _node_get_option_str(
    
    264
    +            prompt, 'really-workspace-close-project-inaccessible', ['ask', 'yes']) == 'ask'
    
    254 265
             self.prompt_workspace_reset_hard = _node_get_option_str(
    
    255 266
                 prompt, 'really-workspace-reset-hard', ['ask', 'yes']) == 'ask'
    
    256 267
     
    
    ... ... @@ -307,6 +318,16 @@ class Context():
    307 318
         def get_workspaces(self):
    
    308 319
             return self._workspaces
    
    309 320
     
    
    321
    +    # get_workspace_locals():
    
    322
    +    #
    
    323
    +    # Return the WorkspaceLocals object used for this BuildStream invocation
    
    324
    +    #
    
    325
    +    # Returns:
    
    326
    +    #    (WorkspaceLocals): The WorkspaceLocals object
    
    327
    +    #
    
    328
    +    def get_workspace_locals(self):
    
    329
    +        return self._workspace_locals
    
    330
    +
    
    310 331
         # get_overrides():
    
    311 332
         #
    
    312 333
         # Fetch the override dictionary for the active project. This returns
    

  • buildstream/_frontend/cli.py
    ... ... @@ -59,18 +59,9 @@ def complete_target(args, incomplete):
    59 59
         :return: all the possible user-specified completions for the param
    
    60 60
         """
    
    61 61
     
    
    62
    +    from .. import utils
    
    62 63
         project_conf = 'project.conf'
    
    63 64
     
    
    64
    -    def ensure_project_dir(directory):
    
    65
    -        directory = os.path.abspath(directory)
    
    66
    -        while not os.path.isfile(os.path.join(directory, project_conf)):
    
    67
    -            parent_dir = os.path.dirname(directory)
    
    68
    -            if directory == parent_dir:
    
    69
    -                break
    
    70
    -            directory = parent_dir
    
    71
    -
    
    72
    -        return directory
    
    73
    -
    
    74 65
         # First resolve the directory, in case there is an
    
    75 66
         # active --directory/-C option
    
    76 67
         #
    
    ... ... @@ -89,7 +80,7 @@ def complete_target(args, incomplete):
    89 80
         else:
    
    90 81
             # Check if this directory or any of its parent directories
    
    91 82
             # contain a project config file
    
    92
    -        base_directory = ensure_project_dir(base_directory)
    
    83
    +        base_directory = utils._search_upward_for_file(base_directory, project_conf)
    
    93 84
     
    
    94 85
         # Now parse the project.conf just to find the element path,
    
    95 86
         # this is unfortunately a bit heavy.
    
    ... ... @@ -707,31 +698,23 @@ def workspace():
    707 698
     @click.option('--no-checkout', default=False, is_flag=True,
    
    708 699
                   help="Do not checkout the source, only link to the given directory")
    
    709 700
     @click.option('--force', '-f', default=False, is_flag=True,
    
    710
    -              help="Overwrite files existing in checkout directory")
    
    701
    +              help="The workspace will be created even if the directory in which it will be created is not empty " +
    
    702
    +              "or if a workspace for that element already exists")
    
    711 703
     @click.option('--track', 'track_', default=False, is_flag=True,
    
    712 704
                   help="Track and fetch new source references before checking out the workspace")
    
    713
    -@click.argument('element',
    
    714
    -                type=click.Path(readable=False))
    
    715
    -@click.argument('directory', type=click.Path(file_okay=False))
    
    705
    +@click.option('--directory', type=click.Path(file_okay=False), default=None,
    
    706
    +              help="Only for use when a single Element is given: Set the directory to use to create the workspace")
    
    707
    +@click.argument('elements', nargs=-1, type=click.Path(readable=False), required=True)
    
    716 708
     @click.pass_obj
    
    717
    -def workspace_open(app, no_checkout, force, track_, element, directory):
    
    709
    +def workspace_open(app, no_checkout, force, track_, directory, elements):
    
    718 710
         """Open a workspace for manual source modification"""
    
    719 711
     
    
    720
    -    if os.path.exists(directory):
    
    721
    -
    
    722
    -        if not os.path.isdir(directory):
    
    723
    -            click.echo("Checkout directory is not a directory: {}".format(directory), err=True)
    
    724
    -            sys.exit(-1)
    
    725
    -
    
    726
    -        if not (no_checkout or force) and os.listdir(directory):
    
    727
    -            click.echo("Checkout directory is not empty: {}".format(directory), err=True)
    
    728
    -            sys.exit(-1)
    
    729
    -
    
    730 712
         with app.initialized():
    
    731
    -        app.stream.workspace_open(element, directory,
    
    713
    +        app.stream.workspace_open(elements,
    
    732 714
                                       no_checkout=no_checkout,
    
    733 715
                                       track_first=track_,
    
    734
    -                                  force=force)
    
    716
    +                                  force=force,
    
    717
    +                                  custom_dir=directory)
    
    735 718
     
    
    736 719
     
    
    737 720
     ##################################################################
    
    ... ... @@ -764,11 +747,18 @@ def workspace_close(app, remove_dir, all_, elements):
    764 747
     
    
    765 748
             elements = app.stream.redirect_element_names(elements)
    
    766 749
     
    
    767
    -        # Check that the workspaces in question exist
    
    750
    +        # Check that the workspaces in question exist, and that it's safe to
    
    751
    +        # remove them.
    
    768 752
             nonexisting = []
    
    769 753
             for element_name in elements:
    
    770 754
                 if not app.stream.workspace_exists(element_name):
    
    771 755
                     nonexisting.append(element_name)
    
    756
    +            if (app.stream.workspace_is_required(element_name) and app.interactive and
    
    757
    +                    app.context.prompt_workspace_close_project_inaccessible):
    
    758
    +                click.echo("Removing '{}' will prevent you from running buildstream commands".format(element_name))
    
    759
    +                if not click.confirm('Are you sure you want to close this workspace?'):
    
    760
    +                    click.echo('Aborting', err=True)
    
    761
    +                    sys.exit(-1)
    
    772 762
             if nonexisting:
    
    773 763
                 raise AppError("Workspace does not exist", detail="\n".join(nonexisting))
    
    774 764
     
    

  • buildstream/_project.py
    ... ... @@ -94,8 +94,10 @@ class Project():
    94 94
             # The project name
    
    95 95
             self.name = None
    
    96 96
     
    
    97
    -        # The project directory
    
    98
    -        self.directory = self._ensure_project_dir(directory)
    
    97
    +        self._context = context  # The invocation Context, a private member
    
    98
    +
    
    99
    +        # The project directory, and whether the project was found from an external workspace
    
    100
    +        self.directory, self._required_workspace_element = self._find_project_dir(directory)
    
    99 101
     
    
    100 102
             # Absolute path to where elements are loaded from within the project
    
    101 103
             self.element_path = None
    
    ... ... @@ -116,7 +118,6 @@ class Project():
    116 118
             #
    
    117 119
             # Private Members
    
    118 120
             #
    
    119
    -        self._context = context  # The invocation Context
    
    120 121
     
    
    121 122
             self._default_mirror = default_mirror    # The name of the preferred mirror.
    
    122 123
     
    
    ... ... @@ -370,6 +371,14 @@ class Project():
    370 371
     
    
    371 372
             self._load_second_pass()
    
    372 373
     
    
    374
    +    # required_workspace_element()
    
    375
    +    #
    
    376
    +    # Returns the element whose workspace is required to load this project,
    
    377
    +    # if any.
    
    378
    +    #
    
    379
    +    def required_workspace_element(self):
    
    380
    +        return self._required_workspace_element
    
    381
    +
    
    373 382
         # cleanup()
    
    374 383
         #
    
    375 384
         # Cleans up resources used loading elements
    
    ... ... @@ -651,7 +660,7 @@ class Project():
    651 660
             # Source url aliases
    
    652 661
             output._aliases = _yaml.node_get(config, Mapping, 'aliases', default_value={})
    
    653 662
     
    
    654
    -    # _ensure_project_dir()
    
    663
    +    # _find_project_dir()
    
    655 664
         #
    
    656 665
         # Returns path of the project directory, if a configuration file is found
    
    657 666
         # in given directory or any of its parent directories.
    
    ... ... @@ -662,18 +671,26 @@ class Project():
    662 671
         # Raises:
    
    663 672
         #    LoadError if project.conf is not found
    
    664 673
         #
    
    665
    -    def _ensure_project_dir(self, directory):
    
    666
    -        directory = os.path.abspath(directory)
    
    667
    -        while not os.path.isfile(os.path.join(directory, _PROJECT_CONF_FILE)):
    
    668
    -            parent_dir = os.path.dirname(directory)
    
    669
    -            if directory == parent_dir:
    
    674
    +    # Returns:
    
    675
    +    #    (str) - the directory that contains the project, and
    
    676
    +    #    (str) - the name of the element required to find the project, or an empty string
    
    677
    +    #
    
    678
    +    def _find_project_dir(self, directory):
    
    679
    +        workspace_element = ""
    
    680
    +        project_directory = utils._search_upward_for_file(directory, _PROJECT_CONF_FILE)
    
    681
    +        if not project_directory:
    
    682
    +            workspace_locals = self._context.get_workspace_locals()
    
    683
    +            workspace_local = workspace_locals.get(directory)
    
    684
    +            if workspace_local.has_projects():
    
    685
    +                project_directory = workspace_local.get_default_path()
    
    686
    +                workspace_element = workspace_local.get_default_element()
    
    687
    +            else:
    
    670 688
                     raise LoadError(
    
    671 689
                         LoadErrorReason.MISSING_PROJECT_CONF,
    
    672 690
                         '{} not found in current directory or any of its parent directories'
    
    673 691
                         .format(_PROJECT_CONF_FILE))
    
    674
    -            directory = parent_dir
    
    675 692
     
    
    676
    -        return directory
    
    693
    +        return project_directory, workspace_element
    
    677 694
     
    
    678 695
         def _load_plugin_factories(self, config, output):
    
    679 696
             plugin_source_origins = []   # Origins of custom sources
    

  • buildstream/_stream.py
    ... ... @@ -464,44 +464,30 @@ class Stream():
    464 464
         # Open a project workspace
    
    465 465
         #
    
    466 466
         # Args:
    
    467
    -    #    target (str): The target element to open the workspace for
    
    468
    -    #    directory (str): The directory to stage the source in
    
    467
    +    #    targets (list): List of target elements to open workspaces for
    
    469 468
         #    no_checkout (bool): Whether to skip checking out the source
    
    470 469
         #    track_first (bool): Whether to track and fetch first
    
    471 470
         #    force (bool): Whether to ignore contents in an existing directory
    
    471
    +    #    custom_dir (str): Custom location to create a workspace or false to use default location.
    
    472 472
         #
    
    473
    -    def workspace_open(self, target, directory, *,
    
    473
    +    def workspace_open(self, targets, *,
    
    474 474
                            no_checkout,
    
    475 475
                            track_first,
    
    476
    -                       force):
    
    476
    +                       force,
    
    477
    +                       custom_dir):
    
    478
    +        # This function is a little funny but it is trying to be as atomic as possible.
    
    477 479
     
    
    478 480
             if track_first:
    
    479
    -            track_targets = (target,)
    
    481
    +            track_targets = targets
    
    480 482
             else:
    
    481 483
                 track_targets = ()
    
    482 484
     
    
    483
    -        elements, track_elements = self._load((target,), track_targets,
    
    485
    +        elements, track_elements = self._load(targets, track_targets,
    
    484 486
                                                   selection=PipelineSelection.REDIRECT,
    
    485 487
                                                   track_selection=PipelineSelection.REDIRECT)
    
    486
    -        target = elements[0]
    
    487
    -        directory = os.path.abspath(directory)
    
    488
    -
    
    489
    -        if not list(target.sources()):
    
    490
    -            build_depends = [x.name for x in target.dependencies(Scope.BUILD, recurse=False)]
    
    491
    -            if not build_depends:
    
    492
    -                raise StreamError("The given element has no sources")
    
    493
    -            detail = "Try opening a workspace on one of its dependencies instead:\n"
    
    494
    -            detail += "  \n".join(build_depends)
    
    495
    -            raise StreamError("The given element has no sources", detail=detail)
    
    496 488
     
    
    497 489
             workspaces = self._context.get_workspaces()
    
    498 490
     
    
    499
    -        # Check for workspace config
    
    500
    -        workspace = workspaces.get_workspace(target._get_full_name())
    
    501
    -        if workspace and not force:
    
    502
    -            raise StreamError("Workspace '{}' is already defined at: {}"
    
    503
    -                              .format(target.name, workspace.get_absolute_path()))
    
    504
    -
    
    505 491
             # If we're going to checkout, we need at least a fetch,
    
    506 492
             # if we were asked to track first, we're going to fetch anyway.
    
    507 493
             #
    
    ... ... @@ -511,29 +497,93 @@ class Stream():
    511 497
                     track_elements = elements
    
    512 498
                 self._fetch(elements, track_elements=track_elements)
    
    513 499
     
    
    514
    -        if not no_checkout and target._get_consistency() != Consistency.CACHED:
    
    515
    -            raise StreamError("Could not stage uncached source. " +
    
    516
    -                              "Use `--track` to track and " +
    
    517
    -                              "fetch the latest version of the " +
    
    518
    -                              "source.")
    
    500
    +        expanded_directories = []
    
    501
    +        #  To try to be more atomic, loop through the elements and raise any errors we can early
    
    502
    +        for target in elements:
    
    503
    +
    
    504
    +            if not list(target.sources()):
    
    505
    +                build_depends = [x.name for x in target.dependencies(Scope.BUILD, recurse=False)]
    
    506
    +                if not build_depends:
    
    507
    +                    raise StreamError("The element {}  has no sources".format(target.name))
    
    508
    +                detail = "Try opening a workspace on one of its dependencies instead:\n"
    
    509
    +                detail += "  \n".join(build_depends)
    
    510
    +                raise StreamError("The element {} has no sources".format(target.name), detail=detail)
    
    511
    +
    
    512
    +            # Check for workspace config
    
    513
    +            workspace = workspaces.get_workspace(target._get_full_name())
    
    514
    +            if workspace and not force:
    
    515
    +                raise StreamError("Element '{}' already has workspace defined at: {}"
    
    516
    +                                  .format(target.name, workspace.get_absolute_path()))
    
    517
    +
    
    518
    +            if not no_checkout and target._get_consistency() != Consistency.CACHED:
    
    519
    +                raise StreamError("Could not stage uncached source. For {} ".format(target.name) +
    
    520
    +                                  "Use `--track` to track and " +
    
    521
    +                                  "fetch the latest version of the " +
    
    522
    +                                  "source.")
    
    523
    +
    
    524
    +            if not custom_dir:
    
    525
    +                directory = os.path.abspath(os.path.join(self._context.workspacedir, target.name))
    
    526
    +                if directory[-4:] == '.bst':
    
    527
    +                    directory = directory[:-4]
    
    528
    +                expanded_directories.append(directory)
    
    529
    +
    
    530
    +        if custom_dir:
    
    531
    +            if len(elements) != 1:
    
    532
    +                raise StreamError("Exactly one element can be given if --directory is used",
    
    533
    +                                  reason='directory-with-multiple-elements')
    
    534
    +            expanded_directories = [custom_dir, ]
    
    535
    +        else:
    
    536
    +            # If this fails it is a bug in what ever calls this, usually cli.py and so can not be tested for via the
    
    537
    +            # run bst test mechanism.
    
    538
    +            assert len(elements) == len(expanded_directories)
    
    539
    +
    
    540
    +        for target, directory in zip(elements, expanded_directories):
    
    541
    +            if os.path.exists(directory):
    
    542
    +                if not os.path.isdir(directory):
    
    543
    +                    raise StreamError("For element '{}', Directory path is not a directory: {}"
    
    544
    +                                      .format(target.name, directory), reason='bad-directory')
    
    545
    +
    
    546
    +                if not (no_checkout or force) and os.listdir(directory):
    
    547
    +                    raise StreamError("For element '{}', Directory path is not empty: {}"
    
    548
    +                                      .format(target.name, directory), reason='bad-directory')
    
    549
    +
    
    550
    +        # So far this function has tried to catch as many issues as possible with out making any changes
    
    551
    +        # Now it dose the bits that can not be made atomic.
    
    552
    +        targetGenerator = zip(elements, expanded_directories)
    
    553
    +        workspace_locals = self._context.get_workspace_locals()
    
    554
    +        project = self._context.get_toplevel_project()
    
    555
    +        for target, directory in targetGenerator:
    
    556
    +            self._message(MessageType.INFO, "Creating workspace for element {}"
    
    557
    +                          .format(target.name))
    
    558
    +
    
    559
    +            workspace = workspaces.get_workspace(target._get_full_name())
    
    560
    +            if workspace:
    
    561
    +                workspaces.delete_workspace(target._get_full_name())
    
    562
    +                workspaces.save_config()
    
    563
    +                shutil.rmtree(directory)
    
    564
    +            try:
    
    565
    +                os.makedirs(directory, exist_ok=True)
    
    566
    +            except OSError as e:
    
    567
    +                todo_elements = " ".join([str(target.name) for target, directory_dict in targetGenerator])
    
    568
    +                if todo_elements:
    
    569
    +                    # This output should make creating the remaining workspaces as easy as possible.
    
    570
    +                    todo_elements = "\nDid not try to create workspaces for " + todo_elements
    
    571
    +                raise StreamError("Failed to create workspace directory: {}".format(e) + todo_elements) from e
    
    519 572
     
    
    520
    -        if workspace:
    
    521
    -            workspaces.delete_workspace(target._get_full_name())
    
    522
    -            workspaces.save_config()
    
    523
    -            shutil.rmtree(directory)
    
    524
    -        try:
    
    525
    -            os.makedirs(directory, exist_ok=True)
    
    526
    -        except OSError as e:
    
    527
    -            raise StreamError("Failed to create workspace directory: {}".format(e)) from e
    
    573
    +            workspaces.create_workspace(target._get_full_name(), directory)
    
    528 574
     
    
    529
    -        workspaces.create_workspace(target._get_full_name(), directory)
    
    575
    +            if not no_checkout:
    
    576
    +                with target.timed_activity("Staging sources to {}".format(directory)):
    
    577
    +                    target._open_workspace()
    
    530 578
     
    
    531
    -        if not no_checkout:
    
    532
    -            with target.timed_activity("Staging sources to {}".format(directory)):
    
    533
    -                target._open_workspace()
    
    579
    +            workspace_local = workspace_locals.add(directory, project.directory, target._get_full_name())
    
    580
    +            workspace_local.write()
    
    534 581
     
    
    535
    -        workspaces.save_config()
    
    536
    -        self._message(MessageType.INFO, "Saved workspace configuration")
    
    582
    +            # Saving the workspace once it is set up means that if the next workspace fails to be created before
    
    583
    +            # the configuration gets saved. The successfully created workspace still gets saved.
    
    584
    +            workspaces.save_config()
    
    585
    +            self._message(MessageType.INFO, "Created a workspace for element: {}"
    
    586
    +                          .format(target._get_full_name()))
    
    537 587
     
    
    538 588
         # workspace_close
    
    539 589
         #
    
    ... ... @@ -556,6 +606,9 @@ class Stream():
    556 606
                     except OSError as e:
    
    557 607
                         raise StreamError("Could not remove  '{}': {}"
    
    558 608
                                           .format(workspace.get_absolute_path(), e)) from e
    
    609
    +        else:
    
    610
    +            workspace_locals = self._context.get_workspace_locals()
    
    611
    +            workspace_locals.remove(workspace.get_absolute_path())
    
    559 612
     
    
    560 613
             # Delete the workspace and save the configuration
    
    561 614
             workspaces.delete_workspace(element_name)
    
    ... ... @@ -599,6 +652,8 @@ class Stream():
    599 652
             for element in elements:
    
    600 653
                 workspace = workspaces.get_workspace(element._get_full_name())
    
    601 654
                 workspace_path = workspace.get_absolute_path()
    
    655
    +            workspace_locals = self._context.get_workspace_locals()
    
    656
    +            workspace_local = workspace_locals.get(workspace_path)
    
    602 657
                 if soft:
    
    603 658
                     workspace.prepared = False
    
    604 659
                     self._message(MessageType.INFO, "Reset workspace state for {} at: {}"
    
    ... ... @@ -619,6 +674,8 @@ class Stream():
    619 674
                 with element.timed_activity("Staging sources to {}".format(workspace_path)):
    
    620 675
                     element._open_workspace()
    
    621 676
     
    
    677
    +            workspace_local.write()
    
    678
    +
    
    622 679
                 self._message(MessageType.INFO,
    
    623 680
                               "Reset workspace for {} at: {}".format(element.name,
    
    624 681
                                                                      workspace_path))
    
    ... ... @@ -649,6 +706,20 @@ class Stream():
    649 706
     
    
    650 707
             return False
    
    651 708
     
    
    709
    +    # workspace_is_required()
    
    710
    +    #
    
    711
    +    # Checks whether the workspace belonging to element_name is required to
    
    712
    +    # load the project
    
    713
    +    #
    
    714
    +    # Args:
    
    715
    +    #    element_name (str): The element whose workspace may be required
    
    716
    +    #
    
    717
    +    # Returns:
    
    718
    +    #    (bool): True if the workspace is required
    
    719
    +    def workspace_is_required(self, element_name):
    
    720
    +        required_elm = self._project.required_workspace_element()
    
    721
    +        return required_elm == element_name
    
    722
    +
    
    652 723
         # workspace_list
    
    653 724
         #
    
    654 725
         # Serializes the workspaces and dumps them in YAML to stdout.
    

  • buildstream/_workspaces.py
    ... ... @@ -25,6 +25,209 @@ from ._exceptions import LoadError, LoadErrorReason
    25 25
     
    
    26 26
     
    
    27 27
     BST_WORKSPACE_FORMAT_VERSION = 3
    
    28
    +BST_WORKSPACE_LOCAL_FORMAT_VERSION = 1
    
    29
    +WORKSPACE_LOCAL_FILE = ".bstproject.yaml"
    
    30
    +
    
    31
    +
    
    32
    +# WorkspaceLocal()
    
    33
    +#
    
    34
    +# An object to contain various helper functions and data required for
    
    35
    +# referring from a workspace back to buildstream.
    
    36
    +#
    
    37
    +# Args:
    
    38
    +#    directory (str): The directory that the workspace exists in
    
    39
    +#    project_path (str): The project path used to refer back
    
    40
    +#                        to buildstream projects.
    
    41
    +#    element_name (str): The name of the element used to create this workspace.
    
    42
    +class WorkspaceLocal():
    
    43
    +    def __init__(self, directory, project_path="", element_name=""):
    
    44
    +        self._projects = []
    
    45
    +        self._directory = directory
    
    46
    +
    
    47
    +        assert (project_path and element_name) or (not project_path and not element_name)
    
    48
    +        if project_path:
    
    49
    +            self._add_project(project_path, element_name)
    
    50
    +
    
    51
    +    # has_projects()
    
    52
    +    #
    
    53
    +    # Whether this object contains any projects
    
    54
    +    #
    
    55
    +    # Returns:
    
    56
    +    #    (bool): True if any projects are stored, else False
    
    57
    +    def has_projects(self):
    
    58
    +        return True if self._projects else False
    
    59
    +
    
    60
    +    # get_default_path()
    
    61
    +    #
    
    62
    +    # Retrieves the default path to a project.
    
    63
    +    #
    
    64
    +    # Returns:
    
    65
    +    #    (str): The path to a project
    
    66
    +    def get_default_path(self):
    
    67
    +        return self._projects[0]['project-path']
    
    68
    +
    
    69
    +    # get_default_element()
    
    70
    +    #
    
    71
    +    # Retrieves the name of the element that owns this workspace.
    
    72
    +    #
    
    73
    +    # Returns:
    
    74
    +    #    (str): The name of an element
    
    75
    +    def get_default_element(self):
    
    76
    +        return self._projects[0]['element-name']
    
    77
    +
    
    78
    +    # to_dict()
    
    79
    +    #
    
    80
    +    # Turn the members data into a dict for serialization purposes
    
    81
    +    #
    
    82
    +    # Returns:
    
    83
    +    #    (dict): A dict representation of the WorkspaceLocal
    
    84
    +    #
    
    85
    +    def to_dict(self):
    
    86
    +        ret = {
    
    87
    +            'projects': self._projects,
    
    88
    +            'format-version': BST_WORKSPACE_LOCAL_FORMAT_VERSION,
    
    89
    +        }
    
    90
    +        return ret
    
    91
    +
    
    92
    +    # from_dict()
    
    93
    +    #
    
    94
    +    # Loads a new WorkspaceLocal from a simple dictionary
    
    95
    +    #
    
    96
    +    # Args:
    
    97
    +    #    directory (str): The directory that the workspace exists in
    
    98
    +    #    dictionary (dict): The dict to generate a WorkspaceLocal from
    
    99
    +    #
    
    100
    +    # Returns:
    
    101
    +    #   (WorkspaceLocal): A newly instantiated WorkspaceLocal
    
    102
    +    @classmethod
    
    103
    +    def from_dict(cls, directory, dictionary):
    
    104
    +        # Only know how to handle one format-version at the moment.
    
    105
    +        format_version = int(dictionary['format-version'])
    
    106
    +        assert format_version == BST_WORKSPACE_LOCAL_FORMAT_VERSION, \
    
    107
    +            "Format version {} not found in {}".format(BST_WORKSPACE_LOCAL_FORMAT_VERSION, dictionary)
    
    108
    +
    
    109
    +        workspace_local = cls(directory)
    
    110
    +        for item in dictionary['projects']:
    
    111
    +            workspace_local._add_project(item['project-path'], item['element-name'])
    
    112
    +
    
    113
    +        return workspace_local
    
    114
    +
    
    115
    +    # load()
    
    116
    +    #
    
    117
    +    # Loads the WorkspaceLocal for a given directory. This directory may be a
    
    118
    +    # subdirectory of the workspace's directory.
    
    119
    +    #
    
    120
    +    # Args:
    
    121
    +    #    directory (str): The directory
    
    122
    +    # Returns:
    
    123
    +    #    (WorkspaceLocal): The created WorkspaceLocal, if in a workspace, or
    
    124
    +    #    (NoneType): None, if the directory is not inside a workspace.
    
    125
    +    @classmethod
    
    126
    +    def load(cls, directory):
    
    127
    +        local_dir = cls.search_for_dir(directory)
    
    128
    +        if local_dir:
    
    129
    +            workspace_file = os.path.join(local_dir, WORKSPACE_LOCAL_FILE)
    
    130
    +            data_dict = _yaml.load(workspace_file)
    
    131
    +            return cls.from_dict(local_dir, data_dict)
    
    132
    +        else:
    
    133
    +            return None
    
    134
    +
    
    135
    +    # write()
    
    136
    +    #
    
    137
    +    # Writes the WorkspaceLocal to disk
    
    138
    +    def write(self):
    
    139
    +        os.makedirs(self._directory, exist_ok=True)
    
    140
    +        _yaml.dump(self.to_dict(), self._get_filename())
    
    141
    +
    
    142
    +    # search_for_dir()
    
    143
    +    #
    
    144
    +    # Returns the directory that contains the workspace local file,
    
    145
    +    # searching upwards from search_dir.
    
    146
    +    @staticmethod
    
    147
    +    def search_for_dir(search_dir):
    
    148
    +        return utils._search_upward_for_file(search_dir, WORKSPACE_LOCAL_FILE)
    
    149
    +
    
    150
    +    def _get_filename(self):
    
    151
    +        return os.path.join(self._directory, WORKSPACE_LOCAL_FILE)
    
    152
    +
    
    153
    +    def _add_project(self, project_path, element_name):
    
    154
    +        assert (project_path and element_name)
    
    155
    +        self._projects.append({'project-path': project_path, 'element-name': element_name})
    
    156
    +
    
    157
    +
    
    158
    +# WorkspaceLocals()
    
    159
    +#
    
    160
    +# A class to manage workspace local data for multiple workspaces.
    
    161
    +#
    
    162
    +class WorkspaceLocals():
    
    163
    +    def __init__(self):
    
    164
    +        self._locals = {}  # Mapping of a workspace directory to its WorkspaceLocal
    
    165
    +
    
    166
    +    # get()
    
    167
    +    #
    
    168
    +    # Returns a WorkspaceLocal for a given directory, loading one from disk if present
    
    169
    +    # and caching it.
    
    170
    +    #
    
    171
    +    # Args:
    
    172
    +    #    directory (str): The directory to search for a WorkspaceLocal.
    
    173
    +    #
    
    174
    +    # Returns:
    
    175
    +    #    (WorkspaceLocal): The WorkspaceLocal that was found for that directory.
    
    176
    +    #
    
    177
    +    def get(self, directory):
    
    178
    +        # NOTE: Later, this will load any WorkspaceLocal found from directory
    
    179
    +        try:
    
    180
    +            local = self._locals[directory]
    
    181
    +        except KeyError:
    
    182
    +            local = WorkspaceLocal.load(directory)
    
    183
    +            if not local:
    
    184
    +                local = WorkspaceLocal(directory)
    
    185
    +            self._locals[directory] = local
    
    186
    +
    
    187
    +        return local
    
    188
    +
    
    189
    +    # add()
    
    190
    +    #
    
    191
    +    # Adds the project path and element name to the WorkspaceLocal that exists
    
    192
    +    # for that directory
    
    193
    +    #
    
    194
    +    # Args:
    
    195
    +    #    directory (str): The directory to search for a WorkspaceLocal.
    
    196
    +    #    project_path (str): The path to the project that refers to this workspace
    
    197
    +    #    element_name (str): The element in the project that was refers to this workspace
    
    198
    +    #
    
    199
    +    # Returns:
    
    200
    +    #    (WorkspaceLocal): The WorkspaceLocal that was found for that directory.
    
    201
    +    #
    
    202
    +    def add(self, directory, project_path='', element_name=''):
    
    203
    +        local = self.get(directory)
    
    204
    +        if project_path:
    
    205
    +            local._add_project(project_path, element_name)
    
    206
    +        return local
    
    207
    +
    
    208
    +    # remove()
    
    209
    +    #
    
    210
    +    # Removes the project path and element name from the WorkspaceLocal that exists
    
    211
    +    # for that directory.
    
    212
    +    #
    
    213
    +    # NOTE: This currently just deletes the file, but with support for multiple
    
    214
    +    # projects opening the same workspace, this will involve decreasing the count
    
    215
    +    # and deleting the file if there are no more projects.
    
    216
    +    #
    
    217
    +    # Args:
    
    218
    +    #    directory (str): The directory to search for a WorkspaceLocal.
    
    219
    +    #    project_path (str): **UNUSED** The path to the project that refers to this workspace
    
    220
    +    #    element_name (str): **UNUSED** The element in the project that was refers to this workspace
    
    221
    +    #
    
    222
    +    def remove(self, directory, project_path='', element_name=''):
    
    223
    +        # NOTE: project_path and element_name will only be used when I implement
    
    224
    +        #       multiple owners of a workspace
    
    225
    +        local = self.get(directory)
    
    226
    +        path = local._get_filename()
    
    227
    +        try:
    
    228
    +            os.unlink(path)
    
    229
    +        except FileNotFoundError:
    
    230
    +            pass
    
    28 231
     
    
    29 232
     
    
    30 233
     # Workspace()
    
    ... ... @@ -174,10 +377,15 @@ class Workspace():
    174 377
             if recalculate or self._key is None:
    
    175 378
                 fullpath = self.get_absolute_path()
    
    176 379
     
    
    380
    +            excluded_files = [WORKSPACE_LOCAL_FILE]
    
    381
    +
    
    177 382
                 # Get a list of tuples of the the project relative paths and fullpaths
    
    178 383
                 if os.path.isdir(fullpath):
    
    179 384
                     filelist = utils.list_relative_paths(fullpath)
    
    180
    -                filelist = [(relpath, os.path.join(fullpath, relpath)) for relpath in filelist]
    
    385
    +                filelist = [
    
    386
    +                    (relpath, os.path.join(fullpath, relpath)) for relpath in filelist
    
    387
    +                    if relpath not in excluded_files
    
    388
    +                ]
    
    181 389
                 else:
    
    182 390
                     filelist = [(self.get_absolute_path(), fullpath)]
    
    183 391
     
    

  • buildstream/data/userconfig.yaml
    ... ... @@ -22,6 +22,9 @@ artifactdir: ${XDG_CACHE_HOME}/buildstream/artifacts
    22 22
     # Location to store build logs
    
    23 23
     logdir: ${XDG_CACHE_HOME}/buildstream/logs
    
    24 24
     
    
    25
    +# Default root location for workspaces, blank for no default set.
    
    26
    +workspacedir: .
    
    27
    +
    
    25 28
     #
    
    26 29
     #    Cache
    
    27 30
     #
    
    ... ... @@ -125,6 +128,14 @@ prompt:
    125 128
       #
    
    126 129
       really-workspace-close-remove-dir: ask
    
    127 130
     
    
    131
    +  # Whether to really proceed with 'bst workspace close' when doing so would
    
    132
    +  # stop them from running bst commands in this workspace.
    
    133
    +  #
    
    134
    +  #  ask - Ask the user if they are sure.
    
    135
    +  #  yes - Always close, without asking.
    
    136
    +  #
    
    137
    +  really-workspace-close-project-inaccessible: ask
    
    138
    +
    
    128 139
       # Whether to really proceed with 'bst workspace reset' doing a hard reset of
    
    129 140
       # a workspace, potentially losing changes.
    
    130 141
       #
    

  • buildstream/utils.py
    ... ... @@ -1242,3 +1242,17 @@ def _deduplicate(iterable, key=None):
    1242 1242
     def _get_link_mtime(path):
    
    1243 1243
         path_stat = os.lstat(path)
    
    1244 1244
         return path_stat.st_mtime
    
    1245
    +
    
    1246
    +
    
    1247
    +# Returns the first directory to contain filename, or an empty string if
    
    1248
    +# none found
    
    1249
    +#
    
    1250
    +def _search_upward_for_file(directory, filename):
    
    1251
    +    directory = os.path.abspath(directory)
    
    1252
    +    while not os.path.isfile(os.path.join(directory, filename)):
    
    1253
    +        parent_dir = os.path.dirname(directory)
    
    1254
    +        if directory == parent_dir:
    
    1255
    +            return ""
    
    1256
    +        directory = parent_dir
    
    1257
    +
    
    1258
    +    return directory

  • doc/sessions/developing.run
    ... ... @@ -7,7 +7,7 @@ commands:
    7 7
     # Capture workspace open output 
    
    8 8
     - directory: ../examples/developing/
    
    9 9
       output: ../source/sessions/developing-workspace-open.html
    
    10
    -  command: workspace open hello.bst workspace_hello
    
    10
    +  command: workspace open --directory workspace_hello hello.bst
    
    11 11
     
    
    12 12
     # Catpure output from workspace list
    
    13 13
     - directory: ../examples/developing/
    
    ... ... @@ -37,7 +37,7 @@ commands:
    37 37
     # Reopen workspace
    
    38 38
     - directory: ../examples/developing/
    
    39 39
       output: ../source/sessions/developing-reopen-workspace.html
    
    40
    -  command: workspace open --no-checkout hello.bst workspace_hello
    
    40
    +  command: workspace open --no-checkout --directory workspace_hello hello.bst
    
    41 41
     
    
    42 42
     # Reset workspace
    
    43 43
     - directory: ../examples/developing/
    

  • doc/sessions/junctions.run
    ... ... @@ -13,7 +13,7 @@ commands:
    13 13
     # Open a crossJunction workspace:
    
    14 14
     - directory: ../examples/junctions
    
    15 15
       output: ../source/sessions/junctions-workspace-open.html
    
    16
    -  command: workspace open hello-junction.bst:hello.bst workspace_hello
    
    16
    +  command: workspace open --directory workspace_hello hello-junction.bst:hello.bst
    
    17 17
     
    
    18 18
     # Remove the workspace 
    
    19 19
     - directory: ../examples/junctions
    

  • tests/examples/developing.py
    ... ... @@ -59,7 +59,7 @@ def test_open_workspace(cli, tmpdir, datafiles):
    59 59
         project = os.path.join(datafiles.dirname, datafiles.basename)
    
    60 60
         workspace_dir = os.path.join(str(tmpdir), "workspace_hello")
    
    61 61
     
    
    62
    -    result = cli.run(project=project, args=['workspace', 'open', '-f', 'hello.bst', workspace_dir])
    
    62
    +    result = cli.run(project=project, args=['workspace', 'open', '-f', '--directory', workspace_dir, 'hello.bst', ])
    
    63 63
         result.assert_success()
    
    64 64
     
    
    65 65
         result = cli.run(project=project, args=['workspace', 'list'])
    
    ... ... @@ -78,7 +78,7 @@ def test_make_change_in_workspace(cli, tmpdir, datafiles):
    78 78
         project = os.path.join(datafiles.dirname, datafiles.basename)
    
    79 79
         workspace_dir = os.path.join(str(tmpdir), "workspace_hello")
    
    80 80
     
    
    81
    -    result = cli.run(project=project, args=['workspace', 'open', '-f', 'hello.bst', workspace_dir])
    
    81
    +    result = cli.run(project=project, args=['workspace', 'open', '-f', '--directory', workspace_dir, 'hello.bst'])
    
    82 82
         result.assert_success()
    
    83 83
     
    
    84 84
         result = cli.run(project=project, args=['workspace', 'list'])
    

  • tests/examples/junctions.py
    ... ... @@ -48,7 +48,7 @@ def test_open_cross_junction_workspace(cli, tmpdir, datafiles):
    48 48
         workspace_dir = os.path.join(str(tmpdir), "workspace_hello_junction")
    
    49 49
     
    
    50 50
         result = cli.run(project=project,
    
    51
    -                     args=['workspace', 'open', 'hello-junction.bst:hello.bst', workspace_dir])
    
    51
    +                     args=['workspace', 'open', '--directory', workspace_dir, 'hello-junction.bst:hello.bst'])
    
    52 52
         result.assert_success()
    
    53 53
     
    
    54 54
         result = cli.run(project=project,
    

  • tests/frontend/buildcheckout.py
    ... ... @@ -509,7 +509,7 @@ def test_build_checkout_workspaced_junction(cli, tmpdir, datafiles):
    509 509
     
    
    510 510
         # Now open a workspace on the junction
    
    511 511
         #
    
    512
    -    result = cli.run(project=project, args=['workspace', 'open', 'junction.bst', workspace])
    
    512
    +    result = cli.run(project=project, args=['workspace', 'open', '--directory', workspace, 'junction.bst'])
    
    513 513
         result.assert_success()
    
    514 514
         filename = os.path.join(workspace, 'files', 'etc-files', 'etc', 'animal.conf')
    
    515 515
     
    

  • tests/frontend/cross_junction_workspace.py
    ... ... @@ -47,7 +47,7 @@ def open_cross_junction(cli, tmpdir):
    47 47
         workspace = tmpdir.join("workspace")
    
    48 48
     
    
    49 49
         element = 'sub.bst:data.bst'
    
    50
    -    args = ['workspace', 'open', element, str(workspace)]
    
    50
    +    args = ['workspace', 'open', '--directory', str(workspace), element]
    
    51 51
         result = cli.run(project=project, args=args)
    
    52 52
         result.assert_success()
    
    53 53
     
    

  • tests/frontend/workspace.py
    ... ... @@ -21,14 +21,17 @@
    21 21
     #           Phillip Smyth <phillip smyth codethink co uk>
    
    22 22
     #           Jonathan Maw <jonathan maw codethink co uk>
    
    23 23
     #           Richard Maw <richard maw codethink co uk>
    
    24
    +#           William Salmon <will salmon codethink co uk>
    
    24 25
     #
    
    25 26
     
    
    26 27
     import os
    
    28
    +import stat
    
    27 29
     import pytest
    
    28 30
     import shutil
    
    29 31
     import subprocess
    
    30 32
     from ruamel.yaml.comments import CommentedSet
    
    31 33
     from tests.testutils import cli, create_repo, ALL_REPO_KINDS, wait_for_cache_granularity
    
    34
    +from tests.testutils import create_artifact_share
    
    32 35
     
    
    33 36
     from buildstream import _yaml
    
    34 37
     from buildstream._exceptions import ErrorDomain, LoadError, LoadErrorReason
    
    ... ... @@ -43,65 +46,120 @@ DATA_DIR = os.path.join(
    43 46
     )
    
    44 47
     
    
    45 48
     
    
    46
    -def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir=None,
    
    47
    -                   project_path=None, element_attrs=None):
    
    48
    -    if not workspace_dir:
    
    49
    -        workspace_dir = os.path.join(str(tmpdir), 'workspace{}'.format(suffix))
    
    50
    -    if not project_path:
    
    51
    -        project_path = os.path.join(datafiles.dirname, datafiles.basename)
    
    52
    -    else:
    
    53
    -        shutil.copytree(os.path.join(datafiles.dirname, datafiles.basename), project_path)
    
    54
    -    bin_files_path = os.path.join(project_path, 'files', 'bin-files')
    
    55
    -    element_path = os.path.join(project_path, 'elements')
    
    56
    -    element_name = 'workspace-test-{}{}.bst'.format(kind, suffix)
    
    49
    +class WorkspaceCreater():
    
    50
    +    def __init__(self, cli, tmpdir, datafiles, project_path=None):
    
    51
    +        self.cli = cli
    
    52
    +        self.tmpdir = tmpdir
    
    53
    +        self.datafiles = datafiles
    
    54
    +
    
    55
    +        if not project_path:
    
    56
    +            project_path = os.path.join(datafiles.dirname, datafiles.basename)
    
    57
    +        else:
    
    58
    +            shutil.copytree(os.path.join(datafiles.dirname, datafiles.basename), project_path)
    
    59
    +
    
    60
    +        self.project_path = project_path
    
    61
    +        self.bin_files_path = os.path.join(project_path, 'files', 'bin-files')
    
    62
    +
    
    63
    +        self.workspace_cmd = os.path.join(self.project_path, 'workspace_cmd')
    
    64
    +
    
    65
    +    def create_workspace_element(self, kind, track, suffix='', workspace_dir=None,
    
    66
    +                                 element_attrs=None):
    
    67
    +        element_name = 'workspace-test-{}{}.bst'.format(kind, suffix)
    
    68
    +        element_path = os.path.join(self.project_path, 'elements')
    
    69
    +        if not workspace_dir:
    
    70
    +            workspace_dir = os.path.join(self.workspace_cmd, element_name)
    
    71
    +            if workspace_dir[-4:] == '.bst':
    
    72
    +                workspace_dir = workspace_dir[:-4]
    
    73
    +
    
    74
    +        # Create our repo object of the given source type with
    
    75
    +        # the bin files, and then collect the initial ref.
    
    76
    +        repo = create_repo(kind, str(self.tmpdir))
    
    77
    +        ref = repo.create(self.bin_files_path)
    
    78
    +        if track:
    
    79
    +            ref = None
    
    80
    +
    
    81
    +        # Write out our test target
    
    82
    +        element = {
    
    83
    +            'kind': 'import',
    
    84
    +            'sources': [
    
    85
    +                repo.source_config(ref=ref)
    
    86
    +            ]
    
    87
    +        }
    
    88
    +        if element_attrs:
    
    89
    +            element = {**element, **element_attrs}
    
    90
    +        _yaml.dump(element,
    
    91
    +                   os.path.join(element_path,
    
    92
    +                                element_name))
    
    93
    +        return element_name, element_path, workspace_dir
    
    57 94
     
    
    58
    -    # Create our repo object of the given source type with
    
    59
    -    # the bin files, and then collect the initial ref.
    
    60
    -    #
    
    61
    -    repo = create_repo(kind, str(tmpdir))
    
    62
    -    ref = repo.create(bin_files_path)
    
    63
    -    if track:
    
    64
    -        ref = None
    
    95
    +    def create_workspace_elements(self, kinds, track, suffixs=None, workspace_dir_usr=None,
    
    96
    +                                  element_attrs=None):
    
    65 97
     
    
    66
    -    # Write out our test target
    
    67
    -    element = {
    
    68
    -        'kind': 'import',
    
    69
    -        'sources': [
    
    70
    -            repo.source_config(ref=ref)
    
    71
    -        ]
    
    72
    -    }
    
    73
    -    if element_attrs:
    
    74
    -        element = {**element, **element_attrs}
    
    75
    -    _yaml.dump(element,
    
    76
    -               os.path.join(element_path,
    
    77
    -                            element_name))
    
    98
    +        element_tuples = []
    
    78 99
     
    
    79
    -    # Assert that there is no reference, a track & fetch is needed
    
    80
    -    state = cli.get_element_state(project_path, element_name)
    
    81
    -    if track:
    
    82
    -        assert state == 'no reference'
    
    83
    -    else:
    
    84
    -        assert state == 'fetch needed'
    
    100
    +        if suffixs is None:
    
    101
    +            suffixs = ['', ] * len(kinds)
    
    102
    +        else:
    
    103
    +            if len(suffixs) != len(kinds):
    
    104
    +                raise "terable error"
    
    85 105
     
    
    86
    -    # Now open the workspace, this should have the effect of automatically
    
    87
    -    # tracking & fetching the source from the repo.
    
    88
    -    args = ['workspace', 'open']
    
    89
    -    if track:
    
    90
    -        args.append('--track')
    
    91
    -    args.extend([element_name, workspace_dir])
    
    92
    -    result = cli.run(project=project_path, args=args)
    
    106
    +        for suffix, kind in zip(suffixs, kinds):
    
    107
    +            element_name, element_path, workspace_dir = \
    
    108
    +                self.create_workspace_element(kind, track, suffix, workspace_dir_usr,
    
    109
    +                                              element_attrs)
    
    93 110
     
    
    94
    -    result.assert_success()
    
    111
    +            # Assert that there is no reference, a track & fetch is needed
    
    112
    +            state = self.cli.get_element_state(self.project_path, element_name)
    
    113
    +            if track:
    
    114
    +                assert state == 'no reference'
    
    115
    +            else:
    
    116
    +                assert state == 'fetch needed'
    
    117
    +            element_tuples.append((element_name, workspace_dir))
    
    95 118
     
    
    96
    -    # Assert that we are now buildable because the source is
    
    97
    -    # now cached.
    
    98
    -    assert cli.get_element_state(project_path, element_name) == 'buildable'
    
    119
    +        return element_tuples
    
    99 120
     
    
    100
    -    # Check that the executable hello file is found in the workspace
    
    101
    -    filename = os.path.join(workspace_dir, 'usr', 'bin', 'hello')
    
    102
    -    assert os.path.exists(filename)
    
    121
    +    def open_workspaces(self, kinds, track, suffixs=None, workspace_dir=None,
    
    122
    +                        element_attrs=None):
    
    103 123
     
    
    104
    -    return (element_name, project_path, workspace_dir)
    
    124
    +        element_tuples = self.create_workspace_elements(kinds, track, suffixs, workspace_dir,
    
    125
    +                                                        element_attrs)
    
    126
    +        os.makedirs(self.workspace_cmd, exist_ok=True)
    
    127
    +
    
    128
    +        # Now open the workspace, this should have the effect of automatically
    
    129
    +        # tracking & fetching the source from the repo.
    
    130
    +        args = ['workspace', 'open']
    
    131
    +        if track:
    
    132
    +            args.append('--track')
    
    133
    +        if workspace_dir is not None:
    
    134
    +            assert len(element_tuples) == 1, "test logic error"
    
    135
    +            _, workspace_dir = element_tuples[0]
    
    136
    +            args.extend(['--directory', workspace_dir])
    
    137
    +
    
    138
    +        args.extend([element_name for element_name, workspace_dir_suffix in element_tuples])
    
    139
    +        result = self.cli.run(cwd=self.workspace_cmd, project=self.project_path, args=args)
    
    140
    +
    
    141
    +        result.assert_success()
    
    142
    +
    
    143
    +        for element_name, workspace_dir in element_tuples:
    
    144
    +            # Assert that we are now buildable because the source is
    
    145
    +            # now cached.
    
    146
    +            assert self.cli.get_element_state(self.project_path, element_name) == 'buildable'
    
    147
    +
    
    148
    +            # Check that the executable hello file is found in the workspace
    
    149
    +            filename = os.path.join(workspace_dir, 'usr', 'bin', 'hello')
    
    150
    +            assert os.path.exists(filename)
    
    151
    +
    
    152
    +        return element_tuples
    
    153
    +
    
    154
    +
    
    155
    +def open_workspace(cli, tmpdir, datafiles, kind, track, suffix='', workspace_dir=None,
    
    156
    +                   project_path=None, element_attrs=None):
    
    157
    +    workspace_object = WorkspaceCreater(cli, tmpdir, datafiles, project_path)
    
    158
    +    workspaces = workspace_object.open_workspaces((kind, ), track, (suffix, ), workspace_dir,
    
    159
    +                                                  element_attrs)
    
    160
    +    assert len(workspaces) == 1
    
    161
    +    element_name, workspace = workspaces[0]
    
    162
    +    return element_name, workspace_object.project_path, workspace
    
    105 163
     
    
    106 164
     
    
    107 165
     @pytest.mark.datafiles(DATA_DIR)
    
    ... ... @@ -128,6 +186,128 @@ def test_open_bzr_customize(cli, tmpdir, datafiles):
    128 186
         assert(expected_output_str in str(output))
    
    129 187
     
    
    130 188
     
    
    189
    +@pytest.mark.datafiles(DATA_DIR)
    
    190
    +def test_open_multi(cli, tmpdir, datafiles):
    
    191
    +
    
    192
    +    workspace_object = WorkspaceCreater(cli, tmpdir, datafiles)
    
    193
    +    workspaces = workspace_object.open_workspaces(repo_kinds, False)
    
    194
    +
    
    195
    +    for (elname, workspace), kind in zip(workspaces, repo_kinds):
    
    196
    +        assert kind in elname
    
    197
    +        workspace_lsdir = os.listdir(workspace)
    
    198
    +        if kind == 'git':
    
    199
    +            assert('.git' in workspace_lsdir)
    
    200
    +        elif kind == 'bzr':
    
    201
    +            assert('.bzr' in workspace_lsdir)
    
    202
    +        else:
    
    203
    +            assert not ('.git' in workspace_lsdir)
    
    204
    +            assert not ('.bzr' in workspace_lsdir)
    
    205
    +
    
    206
    +
    
    207
    +@pytest.mark.datafiles(DATA_DIR)
    
    208
    +def test_open_multi_unwritable(cli, tmpdir, datafiles):
    
    209
    +    workspace_object = WorkspaceCreater(cli, tmpdir, datafiles)
    
    210
    +
    
    211
    +    element_tuples = workspace_object.create_workspace_elements(repo_kinds, False, repo_kinds)
    
    212
    +    os.makedirs(workspace_object.workspace_cmd, exist_ok=True)
    
    213
    +
    
    214
    +    # Now open the workspace, this should have the effect of automatically
    
    215
    +    # tracking & fetching the source from the repo.
    
    216
    +    args = ['workspace', 'open']
    
    217
    +    args.extend([element_name for element_name, workspace_dir_suffix in element_tuples])
    
    218
    +    cli.configure({'workspacedir': workspace_object.workspace_cmd})
    
    219
    +
    
    220
    +    cwdstat = os.stat(workspace_object.workspace_cmd)
    
    221
    +    try:
    
    222
    +        os.chmod(workspace_object.workspace_cmd, cwdstat.st_mode - stat.S_IWRITE)
    
    223
    +        result = workspace_object.cli.run(project=workspace_object.project_path, args=args)
    
    224
    +    finally:
    
    225
    +        # Using this finally to make sure we always put thing back how they should be.
    
    226
    +        os.chmod(workspace_object.workspace_cmd, cwdstat.st_mode)
    
    227
    +
    
    228
    +    result.assert_main_error(ErrorDomain.STREAM, None)
    
    229
    +    # Normally we avoid checking stderr in favour of using the mechine readable result.assert_main_error
    
    230
    +    # But Tristan was very keen that the names of the elements left needing workspaces were present in the out put
    
    231
    +    assert (" ".join([element_name for element_name, workspace_dir_suffix in element_tuples[1:]]) in result.stderr)
    
    232
    +
    
    233
    +
    
    234
    +@pytest.mark.datafiles(DATA_DIR)
    
    235
    +def test_open_multi_with_directory(cli, tmpdir, datafiles):
    
    236
    +    workspace_object = WorkspaceCreater(cli, tmpdir, datafiles)
    
    237
    +
    
    238
    +    element_tuples = workspace_object.create_workspace_elements(repo_kinds, False, repo_kinds)
    
    239
    +    os.makedirs(workspace_object.workspace_cmd, exist_ok=True)
    
    240
    +
    
    241
    +    # Now open the workspace, this should have the effect of automatically
    
    242
    +    # tracking & fetching the source from the repo.
    
    243
    +    args = ['workspace', 'open']
    
    244
    +    args.extend(['--directory', 'any/dir/should/fail'])
    
    245
    +
    
    246
    +    args.extend([element_name for element_name, workspace_dir_suffix in element_tuples])
    
    247
    +    result = workspace_object.cli.run(cwd=workspace_object.workspace_cmd, project=workspace_object.project_path,
    
    248
    +                                      args=args)
    
    249
    +
    
    250
    +    result.assert_main_error(ErrorDomain.STREAM, 'directory-with-multiple-elements')
    
    251
    +
    
    252
    +
    
    253
    +@pytest.mark.datafiles(DATA_DIR)
    
    254
    +def test_open_defaultlocation(cli, tmpdir, datafiles):
    
    255
    +    workspace_object = WorkspaceCreater(cli, tmpdir, datafiles)
    
    256
    +
    
    257
    +    ((element_name, workspace_dir), ) = workspace_object.create_workspace_elements(['git'], False, ['git'])
    
    258
    +    os.makedirs(workspace_object.workspace_cmd, exist_ok=True)
    
    259
    +
    
    260
    +    # Now open the workspace, this should have the effect of automatically
    
    261
    +    # tracking & fetching the source from the repo.
    
    262
    +    args = ['workspace', 'open']
    
    263
    +    args.append(element_name)
    
    264
    +
    
    265
    +    # In the other tests we set the cmd to workspace_object.workspace_cmd with the optional
    
    266
    +    # argument, cwd for the workspace_object.cli.run function. But hear we set the default
    
    267
    +    # workspace location to workspace_object.workspace_cmd and run the cli.run function with
    
    268
    +    # no cwd option so that it runs in the project directory.
    
    269
    +    cli.configure({'workspacedir': workspace_object.workspace_cmd})
    
    270
    +    result = workspace_object.cli.run(project=workspace_object.project_path,
    
    271
    +                                      args=args)
    
    272
    +
    
    273
    +    result.assert_success()
    
    274
    +
    
    275
    +    assert cli.get_element_state(workspace_object.project_path, element_name) == 'buildable'
    
    276
    +
    
    277
    +    # Check that the executable hello file is found in the workspace
    
    278
    +    # even though the cli.run function was not run with cwd = workspace_object.workspace_cmd
    
    279
    +    # the workspace should be created in there as we used the 'workspacedir' configuration
    
    280
    +    # option.
    
    281
    +    filename = os.path.join(workspace_dir, 'usr', 'bin', 'hello')
    
    282
    +    assert os.path.exists(filename)
    
    283
    +
    
    284
    +
    
    285
    +@pytest.mark.datafiles(DATA_DIR)
    
    286
    +def test_open_defaultlocation_exists(cli, tmpdir, datafiles):
    
    287
    +    workspace_object = WorkspaceCreater(cli, tmpdir, datafiles)
    
    288
    +
    
    289
    +    ((element_name, workspace_dir), ) = workspace_object.create_workspace_elements(['git'], False, ['git'])
    
    290
    +    os.makedirs(workspace_object.workspace_cmd, exist_ok=True)
    
    291
    +
    
    292
    +    with open(workspace_dir, 'w') as fl:
    
    293
    +        fl.write('foo')
    
    294
    +
    
    295
    +    # Now open the workspace, this should have the effect of automatically
    
    296
    +    # tracking & fetching the source from the repo.
    
    297
    +    args = ['workspace', 'open']
    
    298
    +    args.append(element_name)
    
    299
    +
    
    300
    +    # In the other tests we set the cmd to workspace_object.workspace_cmd with the optional
    
    301
    +    # argument, cwd for the workspace_object.cli.run function. But hear we set the default
    
    302
    +    # workspace location to workspace_object.workspace_cmd and run the cli.run function with
    
    303
    +    # no cwd option so that it runs in the project directory.
    
    304
    +    cli.configure({'workspacedir': workspace_object.workspace_cmd})
    
    305
    +    result = workspace_object.cli.run(project=workspace_object.project_path,
    
    306
    +                                      args=args)
    
    307
    +
    
    308
    +    result.assert_main_error(ErrorDomain.STREAM, 'bad-directory')
    
    309
    +
    
    310
    +
    
    131 311
     @pytest.mark.datafiles(DATA_DIR)
    
    132 312
     @pytest.mark.parametrize("kind", repo_kinds)
    
    133 313
     def test_open_track(cli, tmpdir, datafiles, kind):
    
    ... ... @@ -150,7 +330,7 @@ def test_open_force(cli, tmpdir, datafiles, kind):
    150 330
     
    
    151 331
         # Now open the workspace again with --force, this should happily succeed
    
    152 332
         result = cli.run(project=project, args=[
    
    153
    -        'workspace', 'open', '--force', element_name, workspace
    
    333
    +        'workspace', 'open', '--force', '--directory', workspace, element_name
    
    154 334
         ])
    
    155 335
         result.assert_success()
    
    156 336
     
    
    ... ... @@ -165,7 +345,7 @@ def test_open_force_open(cli, tmpdir, datafiles, kind):
    165 345
     
    
    166 346
         # Now open the workspace again with --force, this should happily succeed
    
    167 347
         result = cli.run(project=project, args=[
    
    168
    -        'workspace', 'open', '--force', element_name, workspace
    
    348
    +        'workspace', 'open', '--force', '--directory', workspace, element_name
    
    169 349
         ])
    
    170 350
         result.assert_success()
    
    171 351
     
    
    ... ... @@ -196,7 +376,7 @@ def test_open_force_different_workspace(cli, tmpdir, datafiles, kind):
    196 376
     
    
    197 377
         # Now open the workspace again with --force, this should happily succeed
    
    198 378
         result = cli.run(project=project, args=[
    
    199
    -        'workspace', 'open', '--force', element_name2, workspace
    
    379
    +        'workspace', 'open', '--force', '--directory', workspace, element_name2
    
    200 380
         ])
    
    201 381
     
    
    202 382
         # Assert that the file in workspace 1 has been replaced
    
    ... ... @@ -436,9 +616,12 @@ def test_list(cli, tmpdir, datafiles):
    436 616
     @pytest.mark.datafiles(DATA_DIR)
    
    437 617
     @pytest.mark.parametrize("kind", repo_kinds)
    
    438 618
     @pytest.mark.parametrize("strict", [("strict"), ("non-strict")])
    
    439
    -def test_build(cli, tmpdir, datafiles, kind, strict):
    
    619
    +@pytest.mark.parametrize("call_from", [("project"), ("workspace")])
    
    620
    +def test_build(cli, tmpdir_factory, datafiles, kind, strict, call_from):
    
    621
    +    tmpdir = tmpdir_factory.mktemp('')
    
    440 622
         element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, kind, False)
    
    441 623
         checkout = os.path.join(str(tmpdir), 'checkout')
    
    624
    +    args_pre = ['-C', workspace] if call_from == "project" else []
    
    442 625
     
    
    443 626
         # Modify workspace
    
    444 627
         shutil.rmtree(os.path.join(workspace, 'usr', 'bin'))
    
    ... ... @@ -461,15 +644,14 @@ def test_build(cli, tmpdir, datafiles, kind, strict):
    461 644
         # Build modified workspace
    
    462 645
         assert cli.get_element_state(project, element_name) == 'buildable'
    
    463 646
         assert cli.get_element_key(project, element_name) == "{:?<64}".format('')
    
    464
    -    result = cli.run(project=project, args=['build', element_name])
    
    647
    +    result = cli.run(project=project, args=args_pre + ['build', element_name])
    
    465 648
         result.assert_success()
    
    466 649
         assert cli.get_element_state(project, element_name) == 'cached'
    
    467 650
         assert cli.get_element_key(project, element_name) != "{:?<64}".format('')
    
    468 651
     
    
    469 652
         # Checkout the result
    
    470
    -    result = cli.run(project=project, args=[
    
    471
    -        'checkout', element_name, checkout
    
    472
    -    ])
    
    653
    +    result = cli.run(project=project,
    
    654
    +                     args=args_pre + ['checkout', element_name, checkout])
    
    473 655
         result.assert_success()
    
    474 656
     
    
    475 657
         # Check that the pony.conf from the modified workspace exists
    
    ... ... @@ -504,7 +686,7 @@ def test_buildable_no_ref(cli, tmpdir, datafiles):
    504 686
         # Now open the workspace. We don't need to checkout the source though.
    
    505 687
         workspace = os.path.join(str(tmpdir), 'workspace-no-ref')
    
    506 688
         os.makedirs(workspace)
    
    507
    -    args = ['workspace', 'open', '--no-checkout', element_name, workspace]
    
    689
    +    args = ['workspace', 'open', '--no-checkout', '--directory', workspace, element_name]
    
    508 690
         result = cli.run(project=project, args=args)
    
    509 691
         result.assert_success()
    
    510 692
     
    
    ... ... @@ -766,7 +948,7 @@ def test_list_supported_workspace(cli, tmpdir, datafiles, workspace_cfg, expecte
    766 948
                                 element_name))
    
    767 949
     
    
    768 950
         # Make a change to the workspaces file
    
    769
    -    result = cli.run(project=project, args=['workspace', 'open', element_name, workspace])
    
    951
    +    result = cli.run(project=project, args=['workspace', 'open', '--directory', workspace, element_name])
    
    770 952
         result.assert_success()
    
    771 953
         result = cli.run(project=project, args=['workspace', 'close', '--remove-dir', element_name])
    
    772 954
         result.assert_success()
    
    ... ... @@ -876,3 +1058,131 @@ def test_multiple_failed_builds(cli, tmpdir, datafiles):
    876 1058
             result = cli.run(project=project, args=["build", element_name])
    
    877 1059
             assert "BUG" not in result.stderr
    
    878 1060
             assert cli.get_element_state(project, element_name) != "cached"
    
    1061
    +
    
    1062
    +
    
    1063
    +@pytest.mark.datafiles(DATA_DIR)
    
    1064
    +def test_external_fetch(cli, datafiles, tmpdir_factory):
    
    1065
    +    # Fetching from a workspace outside a project doesn't fail horribly
    
    1066
    +    tmpdir = tmpdir_factory.mktemp('')
    
    1067
    +    element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, "git", False)
    
    1068
    +
    
    1069
    +    result = cli.run(project=project, args=['-C', workspace, 'fetch', element_name])
    
    1070
    +    result.assert_success()
    
    1071
    +
    
    1072
    +    # We already fetched it by opening the workspace, but we're also checking
    
    1073
    +    # `bst show` works here
    
    1074
    +    assert cli.get_element_state(project, element_name) == 'buildable'
    
    1075
    +
    
    1076
    +
    
    1077
    +@pytest.mark.datafiles(DATA_DIR)
    
    1078
    +def test_external_push_pull(cli, datafiles, tmpdir_factory):
    
    1079
    +    # Pushing and pulling to/from an artifact cache works from an external workspace
    
    1080
    +    tmpdir = tmpdir_factory.mktemp('')
    
    1081
    +    element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, "git", False)
    
    1082
    +
    
    1083
    +    with create_artifact_share(os.path.join(str(tmpdir), 'artifactshare')) as share:
    
    1084
    +        result = cli.run(project=project, args=['-C', workspace, 'build', element_name])
    
    1085
    +        result.assert_success()
    
    1086
    +
    
    1087
    +        cli.configure({
    
    1088
    +            'artifacts': {'url': share.repo, 'push': True}
    
    1089
    +        })
    
    1090
    +
    
    1091
    +        result = cli.run(project=project, args=['-C', workspace, 'push', element_name])
    
    1092
    +        result.assert_success()
    
    1093
    +
    
    1094
    +        result = cli.run(project=project, args=['-C', workspace, 'pull', '--deps', 'all', 'target.bst'])
    
    1095
    +        result.assert_success()
    
    1096
    +
    
    1097
    +
    
    1098
    +@pytest.mark.datafiles(DATA_DIR)
    
    1099
    +def test_external_track(cli, datafiles, tmpdir_factory):
    
    1100
    +    # Tracking does not get horribly confused
    
    1101
    +    tmpdir = tmpdir_factory.mktemp('')
    
    1102
    +    element_name, project, workspace = open_workspace(cli, tmpdir, datafiles, "git", True)
    
    1103
    +
    
    1104
    +    # The workspace is necessarily already tracked, so we only care that
    
    1105
    +    # there's no weird errors.
    
    1106
    +    result = cli.run(project=project, args=['-C', workspace, 'track', element_name])
    
    1107
    +    result.assert_success()
    
    1108
    +
    
    1109
    +
    
    1110
    +@pytest.mark.datafiles(DATA_DIR)
    
    1111
    +def test_external_open_other(cli, datafiles, tmpdir_factory):
    
    1112
    +    # >From inside an external workspace, open another workspace
    
    1113
    +    tmpdir1 = tmpdir_factory.mktemp('')
    
    1114
    +    tmpdir2 = tmpdir_factory.mktemp('')
    
    1115
    +    # Making use of the assumption that it's the same project in both invocations of open_workspace
    
    1116
    +    alpha_element, project, alpha_workspace = open_workspace(cli, tmpdir1, datafiles, "git", False, suffix="-alpha")
    
    1117
    +    beta_element, _, beta_workspace = open_workspace(cli, tmpdir2, datafiles, "git", False, suffix="-beta")
    
    1118
    +
    
    1119
    +    # Closing the other element first, because I'm too lazy to create an
    
    1120
    +    # element without opening it
    
    1121
    +    result = cli.run(project=project, args=['workspace', 'close', beta_element])
    
    1122
    +    result.assert_success()
    
    1123
    +
    
    1124
    +    result = cli.run(project=project, args=[
    
    1125
    +        '-C', alpha_workspace, 'workspace', 'open', '--force', beta_element, beta_workspace
    
    1126
    +    ])
    
    1127
    +    result.assert_success()
    
    1128
    +
    
    1129
    +
    
    1130
    +@pytest.mark.datafiles(DATA_DIR)
    
    1131
    +def test_external_close_other(cli, datafiles, tmpdir_factory):
    
    1132
    +    # >From inside an external workspace, close the other workspace
    
    1133
    +    tmpdir1 = tmpdir_factory.mktemp('')
    
    1134
    +    tmpdir2 = tmpdir_factory.mktemp('')
    
    1135
    +    # Making use of the assumption that it's the same project in both invocations of open_workspace
    
    1136
    +    alpha_element, project, alpha_workspace = open_workspace(cli, tmpdir1, datafiles, "git", False, suffix="-alpha")
    
    1137
    +    beta_element, _, beta_workspace = open_workspace(cli, tmpdir2, datafiles, "git", False, suffix="-beta")
    
    1138
    +
    
    1139
    +    result = cli.run(project=project, args=['-C', alpha_workspace, 'workspace', 'close', beta_element])
    
    1140
    +    result.assert_success()
    
    1141
    +
    
    1142
    +
    
    1143
    +@pytest.mark.datafiles(DATA_DIR)
    
    1144
    +def test_external_close_self(cli, datafiles, tmpdir_factory):
    
    1145
    +    # >From inside an external workspace, close it
    
    1146
    +    tmpdir1 = tmpdir_factory.mktemp('')
    
    1147
    +    tmpdir2 = tmpdir_factory.mktemp('')
    
    1148
    +    # Making use of the assumption that it's the same project in both invocations of open_workspace
    
    1149
    +    alpha_element, project, alpha_workspace = open_workspace(cli, tmpdir1, datafiles, "git", False, suffix="-alpha")
    
    1150
    +    beta_element, _, beta_workspace = open_workspace(cli, tmpdir2, datafiles, "git", False, suffix="-beta")
    
    1151
    +
    
    1152
    +    result = cli.run(project=project, args=['-C', alpha_workspace, 'workspace', 'close', alpha_element])
    
    1153
    +    result.assert_success()
    
    1154
    +
    
    1155
    +
    
    1156
    +@pytest.mark.datafiles(DATA_DIR)
    
    1157
    +def test_external_reset_other(cli, datafiles, tmpdir_factory):
    
    1158
    +    tmpdir1 = tmpdir_factory.mktemp('')
    
    1159
    +    tmpdir2 = tmpdir_factory.mktemp('')
    
    1160
    +    # Making use of the assumption that it's the same project in both invocations of open_workspace
    
    1161
    +    alpha_element, project, alpha_workspace = open_workspace(cli, tmpdir1, datafiles, "git", False, suffix="-alpha")
    
    1162
    +    beta_element, _, beta_workspace = open_workspace(cli, tmpdir2, datafiles, "git", False, suffix="-beta")
    
    1163
    +
    
    1164
    +    result = cli.run(project=project, args=['-C', alpha_workspace, 'workspace', 'reset', beta_element])
    
    1165
    +    result.assert_success()
    
    1166
    +
    
    1167
    +
    
    1168
    +@pytest.mark.datafiles(DATA_DIR)
    
    1169
    +def test_external_reset_self(cli, datafiles, tmpdir):
    
    1170
    +    element, project, workspace = open_workspace(cli, tmpdir, datafiles, "git", False)
    
    1171
    +
    
    1172
    +    # Command succeeds
    
    1173
    +    result = cli.run(project=project, args=['-C', workspace, 'workspace', 'reset', element])
    
    1174
    +    result.assert_success()
    
    1175
    +
    
    1176
    +    # Successive commands still work (i.e. .bstproject.yaml hasn't been deleted)
    
    1177
    +    result = cli.run(project=project, args=['-C', workspace, 'workspace', 'list'])
    
    1178
    +    result.assert_success()
    
    1179
    +
    
    1180
    +
    
    1181
    +@pytest.mark.datafiles(DATA_DIR)
    
    1182
    +def test_external_list(cli, datafiles, tmpdir_factory):
    
    1183
    +    tmpdir = tmpdir_factory.mktemp('')
    
    1184
    +    # Making use of the assumption that it's the same project in both invocations of open_workspace
    
    1185
    +    element, project, workspace = open_workspace(cli, tmpdir, datafiles, "git", False)
    
    1186
    +
    
    1187
    +    result = cli.run(project=project, args=['-C', workspace, 'workspace', 'list'])
    
    1188
    +    result.assert_success()

  • tests/integration/shell.py
    ... ... @@ -290,7 +290,7 @@ def test_workspace_visible(cli, tmpdir, datafiles):
    290 290
     
    
    291 291
         # Open a workspace on our build failing element
    
    292 292
         #
    
    293
    -    res = cli.run(project=project, args=['workspace', 'open', element_name, workspace])
    
    293
    +    res = cli.run(project=project, args=['workspace', 'open', '--directory', workspace, element_name])
    
    294 294
         assert res.exit_code == 0
    
    295 295
     
    
    296 296
         # Ensure the dependencies of our build failing element are built
    
    ... ... @@ -353,3 +353,29 @@ def test_integration_devices(cli, tmpdir, datafiles):
    353 353
     
    
    354 354
         result = execute_shell(cli, project, ["true"], element=element_name)
    
    355 355
         assert result.exit_code == 0
    
    356
    +
    
    357
    +
    
    358
    +# Test that a shell can be opened from an external workspace
    
    359
    +@pytest.mark.datafiles(DATA_DIR)
    
    360
    +@pytest.mark.parametrize("build_shell", [("build"), ("nobuild")])
    
    361
    +@pytest.mark.skipif(IS_LINUX and not HAVE_BWRAP, reason='Only available with bubblewrap on Linux')
    
    362
    +def test_integration_external_workspace(cli, tmpdir_factory, datafiles, build_shell):
    
    363
    +    tmpdir = tmpdir_factory.mktemp("")
    
    364
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    365
    +    element_name = 'autotools/amhello.bst'
    
    366
    +    workspace_dir = os.path.join(str(tmpdir), 'workspace')
    
    367
    +
    
    368
    +    result = cli.run(project=project, args=[
    
    369
    +        'workspace', 'open', element_name, workspace_dir
    
    370
    +    ])
    
    371
    +    result.assert_success()
    
    372
    +
    
    373
    +    result = cli.run(project=project, args=['-C', workspace_dir, 'build', element_name])
    
    374
    +    result.assert_success()
    
    375
    +
    
    376
    +    command = ['shell']
    
    377
    +    if build_shell == 'build':
    
    378
    +        command.append('--build')
    
    379
    +    command.extend([element_name, '--', 'true'])
    
    380
    +    result = cli.run(project=project, cwd=workspace_dir, args=command)
    
    381
    +    result.assert_success()

  • tests/integration/workspace.py
    ... ... @@ -24,7 +24,7 @@ def test_workspace_mount(cli, tmpdir, datafiles):
    24 24
         workspace = os.path.join(cli.directory, 'workspace')
    
    25 25
         element_name = 'workspace/workspace-mount.bst'
    
    26 26
     
    
    27
    -    res = cli.run(project=project, args=['workspace', 'open', element_name, workspace])
    
    27
    +    res = cli.run(project=project, args=['workspace', 'open', '--directory', workspace, element_name])
    
    28 28
         assert res.exit_code == 0
    
    29 29
     
    
    30 30
         res = cli.run(project=project, args=['build', element_name])
    
    ... ... @@ -41,7 +41,7 @@ def test_workspace_commanddir(cli, tmpdir, datafiles):
    41 41
         workspace = os.path.join(cli.directory, 'workspace')
    
    42 42
         element_name = 'workspace/workspace-commanddir.bst'
    
    43 43
     
    
    44
    -    res = cli.run(project=project, args=['workspace', 'open', element_name, workspace])
    
    44
    +    res = cli.run(project=project, args=['workspace', 'open', '--directory', workspace, element_name])
    
    45 45
         assert res.exit_code == 0
    
    46 46
     
    
    47 47
         res = cli.run(project=project, args=['build', element_name])
    
    ... ... @@ -78,7 +78,7 @@ def test_workspace_updated_dependency(cli, tmpdir, datafiles):
    78 78
         _yaml.dump(dependency, os.path.join(element_path, dep_name))
    
    79 79
     
    
    80 80
         # First open the workspace
    
    81
    -    res = cli.run(project=project, args=['workspace', 'open', element_name, workspace])
    
    81
    +    res = cli.run(project=project, args=['workspace', 'open', '--directory', workspace, element_name])
    
    82 82
         assert res.exit_code == 0
    
    83 83
     
    
    84 84
         # We build the workspaced element, so that we have an artifact
    
    ... ... @@ -134,7 +134,7 @@ def test_workspace_update_dependency_failed(cli, tmpdir, datafiles):
    134 134
         _yaml.dump(dependency, os.path.join(element_path, dep_name))
    
    135 135
     
    
    136 136
         # First open the workspace
    
    137
    -    res = cli.run(project=project, args=['workspace', 'open', element_name, workspace])
    
    137
    +    res = cli.run(project=project, args=['workspace', 'open', '--directory', workspace, element_name])
    
    138 138
         assert res.exit_code == 0
    
    139 139
     
    
    140 140
         # We build the workspaced element, so that we have an artifact
    
    ... ... @@ -210,7 +210,7 @@ def test_updated_dependency_nested(cli, tmpdir, datafiles):
    210 210
         _yaml.dump(dependency, os.path.join(element_path, dep_name))
    
    211 211
     
    
    212 212
         # First open the workspace
    
    213
    -    res = cli.run(project=project, args=['workspace', 'open', element_name, workspace])
    
    213
    +    res = cli.run(project=project, args=['workspace', 'open', '--directory', workspace, element_name])
    
    214 214
         assert res.exit_code == 0
    
    215 215
     
    
    216 216
         # We build the workspaced element, so that we have an artifact
    
    ... ... @@ -264,7 +264,7 @@ def test_incremental_configure_commands_run_only_once(cli, tmpdir, datafiles):
    264 264
         _yaml.dump(element, os.path.join(element_path, element_name))
    
    265 265
     
    
    266 266
         # We open a workspace on the above element
    
    267
    -    res = cli.run(project=project, args=['workspace', 'open', element_name, workspace])
    
    267
    +    res = cli.run(project=project, args=['workspace', 'open', '--directory', workspace, element_name])
    
    268 268
         res.assert_success()
    
    269 269
     
    
    270 270
         # Then we build, and check whether the configure step succeeded
    

  • tests/plugins/filter.py
    ... ... @@ -108,19 +108,28 @@ def test_filter_forbid_also_rdep(datafiles, cli):
    108 108
     def test_filter_workspace_open(datafiles, cli, tmpdir):
    
    109 109
         project = os.path.join(datafiles.dirname, datafiles.basename)
    
    110 110
         workspace_dir = os.path.join(tmpdir.dirname, tmpdir.basename, "workspace")
    
    111
    -    result = cli.run(project=project, args=['workspace', 'open', 'deps-permitted.bst', workspace_dir])
    
    111
    +    result = cli.run(project=project, args=['workspace', 'open', '--directory', workspace_dir, 'deps-permitted.bst'])
    
    112 112
         result.assert_success()
    
    113 113
         assert os.path.exists(os.path.join(workspace_dir, "foo"))
    
    114 114
         assert os.path.exists(os.path.join(workspace_dir, "bar"))
    
    115 115
         assert os.path.exists(os.path.join(workspace_dir, "baz"))
    
    116 116
     
    
    117 117
     
    
    118
    +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'basic'))
    
    119
    +def test_filter_workspace_open_multi(datafiles, cli, tmpdir):
    
    120
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    121
    +    result = cli.run(cwd=project, project=project, args=['workspace', 'open', 'deps-permitted.bst',
    
    122
    +                                                         'output-orphans.bst'])
    
    123
    +    result.assert_success()
    
    124
    +    assert os.path.exists(os.path.join(project, "input"))
    
    125
    +
    
    126
    +
    
    118 127
     @pytest.mark.datafiles(os.path.join(DATA_DIR, 'basic'))
    
    119 128
     def test_filter_workspace_build(datafiles, cli, tmpdir):
    
    120 129
         project = os.path.join(datafiles.dirname, datafiles.basename)
    
    121 130
         tempdir = os.path.join(tmpdir.dirname, tmpdir.basename)
    
    122 131
         workspace_dir = os.path.join(tempdir, "workspace")
    
    123
    -    result = cli.run(project=project, args=['workspace', 'open', 'output-orphans.bst', workspace_dir])
    
    132
    +    result = cli.run(project=project, args=['workspace', 'open', '--directory', workspace_dir, 'output-orphans.bst'])
    
    124 133
         result.assert_success()
    
    125 134
         src = os.path.join(workspace_dir, "foo")
    
    126 135
         dst = os.path.join(workspace_dir, "quux")
    
    ... ... @@ -138,7 +147,7 @@ def test_filter_workspace_close(datafiles, cli, tmpdir):
    138 147
         project = os.path.join(datafiles.dirname, datafiles.basename)
    
    139 148
         tempdir = os.path.join(tmpdir.dirname, tmpdir.basename)
    
    140 149
         workspace_dir = os.path.join(tempdir, "workspace")
    
    141
    -    result = cli.run(project=project, args=['workspace', 'open', 'output-orphans.bst', workspace_dir])
    
    150
    +    result = cli.run(project=project, args=['workspace', 'open', '--directory', workspace_dir, 'output-orphans.bst'])
    
    142 151
         result.assert_success()
    
    143 152
         src = os.path.join(workspace_dir, "foo")
    
    144 153
         dst = os.path.join(workspace_dir, "quux")
    
    ... ... @@ -158,7 +167,7 @@ def test_filter_workspace_reset(datafiles, cli, tmpdir):
    158 167
         project = os.path.join(datafiles.dirname, datafiles.basename)
    
    159 168
         tempdir = os.path.join(tmpdir.dirname, tmpdir.basename)
    
    160 169
         workspace_dir = os.path.join(tempdir, "workspace")
    
    161
    -    result = cli.run(project=project, args=['workspace', 'open', 'output-orphans.bst', workspace_dir])
    
    170
    +    result = cli.run(project=project, args=['workspace', 'open', '--directory', workspace_dir, 'output-orphans.bst'])
    
    162 171
         result.assert_success()
    
    163 172
         src = os.path.join(workspace_dir, "foo")
    
    164 173
         dst = os.path.join(workspace_dir, "quux")
    

  • tests/plugins/pipeline.py
    ... ... @@ -14,7 +14,7 @@ DATA_DIR = os.path.join(
    14 14
     
    
    15 15
     def create_pipeline(tmpdir, basedir, target):
    
    16 16
         context = Context()
    
    17
    -    context.load()
    
    17
    +    context.load(config=os.devnull)
    
    18 18
         context.deploydir = os.path.join(str(tmpdir), 'deploy')
    
    19 19
         context.artifactdir = os.path.join(str(tmpdir), 'artifact')
    
    20 20
         project = Project(basedir, context)
    



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]