diff --git a/colcon_core/command.py b/colcon_core/command.py index 6d8565a5..41548dc3 100644 --- a/colcon_core/command.py +++ b/colcon_core/command.py @@ -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 """ @@ -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) @@ -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' diff --git a/test/test_command.py b/test/test_command.py index b03616a4..e4339fe3 100644 --- a/test/test_command.py +++ b/test/test_command.py @@ -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():