diff options
author | Tristan Van Berkom <tristan.van.berkom@gmail.com> | 2019-01-14 20:24:57 +0000 |
---|---|---|
committer | Tristan Van Berkom <tristan.van.berkom@gmail.com> | 2019-01-14 20:24:57 +0000 |
commit | 273b0f5562ef993902395bee212079cbc6a71a91 (patch) | |
tree | 327fa3d1e6fd16a4e0be03fa1096bc6fc5c4bf20 | |
parent | 10b3ee621df65884c4a76096a5242aa861f8f8ce (diff) | |
parent | ff666e76c30bf48d01bd675e564163a77068e60c (diff) | |
download | buildstream-273b0f5562ef993902395bee212079cbc6a71a91.tar.gz |
Merge branch 'jennis/add_artifacts_completion' into 'master'
completions.py: Add a test for our artifact ref autocompletions
See merge request BuildStream/buildstream!1054
-rw-r--r-- | buildstream/_frontend/cli.py | 27 | ||||
-rw-r--r-- | tests/completions/completions.py | 41 |
2 files changed, 61 insertions, 7 deletions
diff --git a/buildstream/_frontend/cli.py b/buildstream/_frontend/cli.py index bdcf3ca27..b3e48d583 100644 --- a/buildstream/_frontend/cli.py +++ b/buildstream/_frontend/cli.py @@ -2,6 +2,7 @@ import os import sys from contextlib import ExitStack from fnmatch import fnmatch +from functools import partial from tempfile import TemporaryDirectory import click @@ -111,14 +112,25 @@ def complete_target(args, incomplete): return complete_list -def complete_artifact(args, incomplete): +def complete_artifact(orig_args, args, incomplete): from .._context import Context ctx = Context() config = None - for i, arg in enumerate(args): - if arg in ('-c', '--config'): - config = args[i + 1] + if orig_args: + for i, arg in enumerate(orig_args): + if arg in ('-c', '--config'): + try: + config = orig_args[i + 1] + except IndexError: + pass + if args: + for i, arg in enumerate(args): + if arg in ('-c', '--config'): + try: + config = args[i + 1] + except IndexError: + pass ctx.load(config) # element targets are valid artifact names @@ -128,8 +140,9 @@ def complete_artifact(args, incomplete): return complete_list -def override_completions(cmd, cmd_param, args, incomplete): +def override_completions(orig_args, cmd, cmd_param, args, incomplete): """ + :param orig_args: original, non-completion args :param cmd_param: command definition :param args: full list of args typed before the incomplete arg :param incomplete: the incomplete text to autocomplete @@ -150,7 +163,7 @@ def override_completions(cmd, cmd_param, args, incomplete): cmd_param.opts == ['--track-except']): return complete_target(args, incomplete) if cmd_param.name == 'artifacts': - return complete_artifact(args, incomplete) + return complete_artifact(orig_args, args, incomplete) raise CompleteUnhandled() @@ -161,7 +174,7 @@ def override_main(self, args=None, prog_name=None, complete_var=None, # Hook for the Bash completion. This only activates if the Bash # completion is actually enabled, otherwise this is quite a fast # noop. - if main_bashcomplete(self, prog_name, override_completions): + if main_bashcomplete(self, prog_name, partial(override_completions, args)): # If we're running tests we cant just go calling exit() # from the main process. diff --git a/tests/completions/completions.py b/tests/completions/completions.py index 372ed7840..6c75b31e2 100644 --- a/tests/completions/completions.py +++ b/tests/completions/completions.py @@ -281,3 +281,44 @@ def test_argument_element_invalid(datafiles, cli, project, cmd, word_idx, expect ]) def test_help_commands(cli, cmd, word_idx, expected): assert_completion(cli, cmd, word_idx, expected) + + +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'project')) +def test_argument_artifact(cli, tmpdir, datafiles): + project = os.path.join(datafiles.dirname, datafiles.basename) + + # Build an import element with no dependencies (as there will only be ONE cache key) + result = cli.run(project=project, args=['build', 'import-bin.bst']) # Has no dependencies + result.assert_success() + + # Get the key and the artifact ref ($project/$element_name/$key) + key = cli.get_element_key(project, 'import-bin.bst') + artifact = os.path.join('test', 'import-bin', key) + + # Test autocompletion of the artifact + cmds = [ + 'bst artifact log ', + 'bst artifact log t', + 'bst artifact log test/' + ] + + for i, cmd in enumerate(cmds): + word_idx = 3 + result = cli.run(project=project, cwd=project, env={ + '_BST_COMPLETION': 'complete', + 'COMP_WORDS': cmd, + 'COMP_CWORD': str(word_idx) + }) + words = [] + if result.output: + words = result.output.splitlines() # This leaves an extra space on each e.g. ['foo.bst '] + words = [word.strip() for word in words] + + if i == 0: + expected = PROJECT_ELEMENTS + [artifact] # We should now be able to see the artifact + elif i == 1: + expected = ['target.bst', artifact] + elif i == 2: + expected = [artifact] + + assert expected == words |