Skip to content
Snippets Groups Projects
Commit 273b0f55 authored by Tristan Van Berkom's avatar Tristan Van Berkom
Browse files

Merge branch 'jennis/add_artifacts_completion' into 'master'

completions.py: Add a test for our artifact ref autocompletions

See merge request !1054
parents 10b3ee62 ff666e76
No related branches found
No related tags found
1 merge request!1054completions.py: Add a test for our artifact ref autocompletions
Pipeline #43334450 passed
......@@ -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.
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment