[Notes] [Git][BuildStream/buildstream][jennis/add_artifacts_completion] 2 commits: _frontend/cli.py: Also check original args for --config in bash completion



Title: GitLab

Jürg Billeter pushed to branch jennis/add_artifacts_completion at BuildStream / buildstream

Commits:

2 changed files:

Changes:

  • buildstream/_frontend/cli.py
    ... ... @@ -2,6 +2,7 @@ import os
    2 2
     import sys
    
    3 3
     from contextlib import ExitStack
    
    4 4
     from fnmatch import fnmatch
    
    5
    +from functools import partial
    
    5 6
     from tempfile import TemporaryDirectory
    
    6 7
     
    
    7 8
     import click
    
    ... ... @@ -111,14 +112,17 @@ def complete_target(args, incomplete):
    111 112
         return complete_list
    
    112 113
     
    
    113 114
     
    
    114
    -def complete_artifact(args, incomplete):
    
    115
    +def complete_artifact(orig_args, args, incomplete):
    
    115 116
         from .._context import Context
    
    116 117
         ctx = Context()
    
    117 118
     
    
    118 119
         config = None
    
    119
    -    for i, arg in enumerate(args):
    
    120
    +    for i, arg in enumerate(orig_args):
    
    120 121
             if arg in ('-c', '--config'):
    
    121 122
                 config = args[i + 1]
    
    123
    +    for i, arg in enumerate(args):
    
    124
    +        if arg in ('-c', '--config'):
    
    125
    +            config = comp_args[i + 1]
    
    122 126
         ctx.load(config)
    
    123 127
     
    
    124 128
         # element targets are valid artifact names
    
    ... ... @@ -128,8 +132,9 @@ def complete_artifact(args, incomplete):
    128 132
         return complete_list
    
    129 133
     
    
    130 134
     
    
    131
    -def override_completions(cmd, cmd_param, args, incomplete):
    
    135
    +def override_completions(orig_args, cmd, cmd_param, args, incomplete):
    
    132 136
         """
    
    137
    +    :param orig_args: original, non-completion args
    
    133 138
         :param cmd_param: command definition
    
    134 139
         :param args: full list of args typed before the incomplete arg
    
    135 140
         :param incomplete: the incomplete text to autocomplete
    
    ... ... @@ -150,7 +155,7 @@ def override_completions(cmd, cmd_param, args, incomplete):
    150 155
                     cmd_param.opts == ['--track-except']):
    
    151 156
                 return complete_target(args, incomplete)
    
    152 157
             if cmd_param.name == 'artifacts':
    
    153
    -            return complete_artifact(args, incomplete)
    
    158
    +            return complete_artifact(orig_args, args, incomplete)
    
    154 159
     
    
    155 160
         raise CompleteUnhandled()
    
    156 161
     
    
    ... ... @@ -161,7 +166,7 @@ def override_main(self, args=None, prog_name=None, complete_var=None,
    161 166
         # Hook for the Bash completion.  This only activates if the Bash
    
    162 167
         # completion is actually enabled, otherwise this is quite a fast
    
    163 168
         # noop.
    
    164
    -    if main_bashcomplete(self, prog_name, override_completions):
    
    169
    +    if main_bashcomplete(self, prog_name, partial(override_completions, args)):
    
    165 170
     
    
    166 171
             # If we're running tests we cant just go calling exit()
    
    167 172
             # from the main process.
    

  • tests/completions/completions.py
    ... ... @@ -281,3 +281,44 @@ def test_argument_element_invalid(datafiles, cli, project, cmd, word_idx, expect
    281 281
     ])
    
    282 282
     def test_help_commands(cli, cmd, word_idx, expected):
    
    283 283
         assert_completion(cli, cmd, word_idx, expected)
    
    284
    +
    
    285
    +
    
    286
    +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'project'))
    
    287
    +def test_argument_artifact(cli, tmpdir, datafiles):
    
    288
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    289
    +
    
    290
    +    # Build an import element with no dependencies (as there will only be ONE cache key)
    
    291
    +    result = cli.run(project=project, args=['build', 'import-bin.bst'])  # Has no dependencies
    
    292
    +    result.assert_success()
    
    293
    +
    
    294
    +    # Get the key and the artifact ref ($project/$element_name/$key)
    
    295
    +    key = cli.get_element_key(project, 'import-bin.bst')
    
    296
    +    artifact = os.path.join('test', 'import-bin', key)
    
    297
    +
    
    298
    +    # Test autocompletion of the artifact
    
    299
    +    cmds = [
    
    300
    +        'bst artifact log ',
    
    301
    +        'bst artifact log t',
    
    302
    +        'bst artifact log test/'
    
    303
    +    ]
    
    304
    +
    
    305
    +    for i, cmd in enumerate(cmds):
    
    306
    +        word_idx = 3
    
    307
    +        result = cli.run(project=project, cwd=project, env={
    
    308
    +            '_BST_COMPLETION': 'complete',
    
    309
    +            'COMP_WORDS': cmd,
    
    310
    +            'COMP_CWORD': str(word_idx)
    
    311
    +        })
    
    312
    +        words = []
    
    313
    +        if result.output:
    
    314
    +            words = result.output.splitlines()  # This leaves an extra space on each e.g. 'foo.bst ']
    
    315
    +            words = [word.strip() for word in words]
    
    316
    +
    
    317
    +            if i == 0:
    
    318
    +                expected = PROJECT_ELEMENTS + [artifact]  # We should now be able to see the artifact
    
    319
    +            elif i == 1:
    
    320
    +                expected = ['target.bst', artifact]
    
    321
    +            elif i == 2:
    
    322
    +                expected = [artifact]
    
    323
    +
    
    324
    +            assert expected == words



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