Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for command invocation with a 'default verb' #656

Merged
merged 5 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions colcon_core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def main(
:param str verb_group_name: The extension point group name for verbs
:param str environment_variable_group_name: The extension point group name
for environment variables
:param VerbExtensionPoint default_verb: The verb to invoke if no explicit
:param Type default_verb: The verb class type to invoke if no explicit
verb was provided on the command line
:returns: The return code
"""
Expand Down Expand Up @@ -159,10 +159,11 @@ def _main(
parser = create_parser(environment_variable_group_name)

if default_verb is not None:
default_verb_instance = default_verb()
parser.set_defaults(
verb_parser=parser, verb_extension=default_verb,
main=default_verb.main)
add_parser_arguments(parser, default_verb)
verb_parser=parser, verb_extension=default_verb_instance,
main=default_verb_instance.main)
add_parser_arguments(parser, default_verb_instance)

verb_extensions = get_verb_extensions(group_name=verb_group_name)

Expand All @@ -189,7 +190,7 @@ def _main(
colcon_logger.debug(f'Parsed command line arguments: {args}')

# error: no verb provided
if args.verb_name is None and default_verb is None:
if not getattr(args, 'main', None):
cottsay marked this conversation as resolved.
Show resolved Hide resolved
print(parser.format_usage())
return 'Error: No verb provided'

Expand Down
15 changes: 7 additions & 8 deletions test/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,26 @@ def test_main_no_verbs_or_env():


def test_main_default_verb():
default_verb = Extension2()
default_verb.main = Mock(wraps=default_verb.main)
with ExtensionPointContext():
with patch(
'colcon_core.argument_parser.get_argument_parser_extensions',
return_value={}
):
with pytest.raises(SystemExit) as e:
main(argv=['--help'], default_verb=default_verb)
main(argv=['--help'], default_verb=Extension1)
assert e.value.code == 0

with pytest.raises(SystemExit) as e:
main(
argv=['--log-level', 'invalid'],
default_verb=default_verb)
default_verb=Extension1)
assert e.value.code == 2

assert not main(
argv=['--log-base', '/dev/null'],
default_verb=default_verb)
default_verb.main.assert_called_once()
with patch.object(Extension1, 'main', return_value=0) as mock_main:
assert not main(
argv=['--log-base', '/dev/null'],
default_verb=Extension1)
mock_main.assert_called_once()


def test_create_parser():
Expand Down
Loading