Skip to content

Commit

Permalink
Pass verb class type instead of class instance
Browse files Browse the repository at this point in the history
  • Loading branch information
cottsay committed Jul 25, 2024
1 parent 6f06877 commit 4f3ef46
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
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):
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

0 comments on commit 4f3ef46

Please sign in to comment.