Commit 69e59443 authored by Chandan Singh's avatar Chandan Singh Committed by Tristan Van Berkom

_frontend/cli.py: Try to autocomplete element paths when running from a subdirectory

The previous commit added support for running bst commands form
subdirectories of the project root. Make autocomplete also work in a
similar way.
parent a5ff465d
Pipeline #23369765 passed with stages
in 29 minutes and 7 seconds
......@@ -20,6 +20,18 @@ def complete_target(args, incomplete):
:return: all the possible user-specified completions for the param
"""
project_conf = 'project.conf'
def ensure_project_dir(directory):
directory = os.path.abspath(directory)
while not os.path.isfile(os.path.join(directory, project_conf)):
parent_dir = os.path.dirname(directory)
if directory == parent_dir:
break
directory = parent_dir
return directory
# First resolve the directory, in case there is an
# active --directory/-C option
#
......@@ -35,10 +47,14 @@ def complete_target(args, incomplete):
if idx >= 0 and len(args) > idx + 1:
base_directory = args[idx + 1]
else:
# Check if this directory or any of its parent directories
# contain a project config file
base_directory = ensure_project_dir(base_directory)
# Now parse the project.conf just to find the element path,
# this is unfortunately a bit heavy.
project_file = os.path.join(base_directory, 'project.conf')
project_file = os.path.join(base_directory, project_conf)
try:
project = _yaml.load(project_file)
except LoadError:
......
......@@ -174,8 +174,9 @@ def test_option_directory(datafiles, cli, cmd, word_idx, expected, subdir):
['compose-all.bst ', 'compose-include-bin.bst ', 'compose-exclude-dev.bst '], None),
# When running from the files subdir
('project', 'bst show ', 2, [], 'files'),
('project', 'bst build com', 2, [], 'files'),
('project', 'bst show ', 2, [e + ' ' for e in PROJECT_ELEMENTS], 'files'),
('project', 'bst build com', 2,
['compose-all.bst ', 'compose-include-bin.bst ', 'compose-exclude-dev.bst '], 'files'),
# When passing the project directory
('project', 'bst --directory ../ show ', 4, [e + ' ' for e in PROJECT_ELEMENTS], 'files'),
......@@ -193,8 +194,10 @@ def test_option_directory(datafiles, cli, cmd, word_idx, expected, subdir):
['compose-all.bst ', 'compose-include-bin.bst ', 'compose-exclude-dev.bst '], None),
# When running from the files subdir
('no-element-path', 'bst show ', 2, [], 'files'),
('no-element-path', 'bst build com', 2, [], 'files'),
('no-element-path', 'bst show ', 2,
[e + ' ' for e in (PROJECT_ELEMENTS + ['project.conf'])] + ['files/'], 'files'),
('no-element-path', 'bst build com', 2,
['compose-all.bst ', 'compose-include-bin.bst ', 'compose-exclude-dev.bst '], 'files'),
# When passing the project directory
('no-element-path', 'bst --directory ../ show ', 4,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment