def test_logs_traceback_on_exception_in_dispatch_if_traceback( api_instance_mock, handle_args_mock, dispatch_command_mock, no_config_mock, logger_exception_mock, ): msg = "oh this is bad!!" args_with_traceback = dict(VALID_PARSED_ARGS) args_with_traceback["traceback"] = True parsed_args = argparse.Namespace(**args_with_traceback) handle_args_mock.return_value = parsed_args, api_instance_mock def raise_(): raise ValueError(msg) sys_args = ["repobee", *CLONE_ARGS, "--traceback"] dispatch_command_mock.side_effect = lambda *args, **kwargs: raise_() with pytest.raises(SystemExit) as exc_info: main.main(sys_args) assert exc_info.value.code == 1 assert logger_exception_mock.called handle_args_mock.assert_called_once_with([*CLONE_ARGS, "--traceback"], config=ANY)
def test_happy_path(api_instance_mock, handle_args_mock, dispatch_command_mock, no_config_mock): sys_args = ["just some made up arguments".split()] main.main(sys_args) handle_args_mock.assert_called_once_with(sys_args[1:], ANY) dispatch_command_mock.assert_called_once_with(PARSED_ARGS, api_instance_mock, ANY)
def test_plug_arg_incompatible_with_no_plugins(dispatch_command_mock, init_plugins_mock): sys_args = ["repobee", "-p", "javac", "--no-plugins", *CLONE_ARGS] with pytest.raises(SystemExit): main.main(sys_args) assert not init_plugins_mock.called assert not dispatch_command_mock.called
def test_does_not_log_error_when_command_is_used_incorrectly(mocker): """There should be no error log when a command is invoked incorrectly (e.g. misspelling the category). """ errlog_mock = mocker.patch("repobee_plug.log.error") with pytest.raises(SystemExit): # note that the category is misspelled main.main("repbee isues -h".split()) assert not errlog_mock.called
def test_does_not_raise_on_exception_in_dispatch( api_instance_mock, handle_args_mock, dispatch_command_mock, no_config_mock, logger_exception_mock, ): sys_args = ["repobee", *CLONE_ARGS] main.main(sys_args) assert not logger_exception_mock.called
def test_prints_url_to_faq_on_error(capsys, parse_preparser_options_mock, handle_args_mock, no_config_mock): def raise_(): raise ValueError() parse_preparser_options_mock.side_effect = raise_ with pytest.raises(SystemExit): main.main("repobee -h".split()) assert ("https://repobee.readthedocs.io/en/stable/faq.html" in capsys.readouterr().err)
def test_no_plugins_arg(handle_args_mock, dispatch_command_mock, init_plugins_mock): """Default plugins still need to be loaded, so initialize_plugins should be called only with the default plugin. """ sys_args = ["repobee", "--no-plugins", *CLONE_ARGS] main.main(sys_args) init_plugins_mock.assert_called_once_with(DEFAULT_PLUGIN_NAMES, allow_qualified=True) handle_args_mock.assert_called_once_with(CLONE_ARGS, config=ANY)
def test_exit_status_1_on_exception_in_handling_parsed_args( api_instance_mock, handle_args_mock, dispatch_command_mock): """should just log, but not raise.""" msg = "some nice error message" dispatch_command_mock.side_effect = raise_(Exception(msg)) sys_args = ["just some made up arguments".split()] with pytest.raises(SystemExit) as exc_info: main.main(sys_args) assert exc_info.value.code == 1 handle_args_mock.assert_called_once_with(sys_args[1:], config=ANY)
def test_no_plugins_with_configured_plugins(handle_args_mock, dispatch_command_mock, init_plugins_mock, config_mock): """Test that --no-plugins causes any plugins listed in the config file to NOT be loaded. """ sys_args = ["repobee", "--no-plugins", *CLONE_ARGS] main.main(sys_args) init_plugins_mock.assert_called_once_with(DEFAULT_PLUGIN_NAMES, allow_qualified=True) handle_args_mock.assert_called_once_with(CLONE_ARGS, config=ANY)
def test_happy_path( api_instance_mock, handle_args_mock, dispatch_command_mock, no_config_mock ): sys_args = ["just some made up arguments".split()] main.main(sys_args) handle_args_mock.assert_called_once_with( sys_args[1:], config_file=_repobee.constants.DEFAULT_CONFIG_FILE ) dispatch_command_mock.assert_called_once_with( PARSED_ARGS, api_instance_mock, _repobee.constants.DEFAULT_CONFIG_FILE )
def test_non_zero_exit_status_on_exception(handle_args_mock, parse_preparser_options_mock, no_config_mock): def raise_(*args, **kwargs): raise ValueError() handle_args_mock.side_effect = raise_ sys_args = ["repobee", *CLONE_ARGS] with pytest.raises(SystemExit) as exc_info: main.main(sys_args) assert exc_info.value.code == 1
def test_plugin_with_subparser_name(handle_args_mock, dispatch_command_mock, init_plugins_mock): sys_args = ["repobee", "-p", "javac", "-p", "clone", *CLONE_ARGS] main.main(sys_args) init_plugins_mock.assert_has_calls( [ call(["javac", "clone"], allow_filepath=True), call(DEFAULT_PLUGIN_NAMES, allow_qualified=True), ], any_order=True, ) handle_args_mock.assert_called_once_with(CLONE_ARGS, config=ANY)
def test_plugins_args(handle_args_mock, dispatch_command_mock, init_plugins_mock): plugin_args = "-p javac -p pylint".split() sys_args = ["repobee", *plugin_args, *CLONE_ARGS] main.main(sys_args) init_plugins_mock.assert_has_calls( [ call(["javac", "pylint"], allow_filepath=True), call(DEFAULT_PLUGIN_NAMES, allow_qualified=True), ], any_order=True, ) handle_args_mock.assert_called_once_with(CLONE_ARGS, config=ANY)
def test_exit_status_1_on_exception_in_parsing( api_instance_mock, handle_args_mock, dispatch_command_mock, no_config_mock ): msg = "some nice error message" handle_args_mock.side_effect = raise_(Exception(msg)) sys_args = ["just some made up arguments".split()] with pytest.raises(SystemExit) as exc_info: main.main(sys_args) assert exc_info.value.code == 1 handle_args_mock.assert_called_once_with( sys_args[1:], config_file=_repobee.constants.DEFAULT_CONFIG_FILE ) assert not dispatch_command_mock.called
def test_dist_plugins_are_loaded_when_dist_install(monkeypatch): dist_plugin_qualnames = plugin.get_qualified_module_names( _repobee.ext.dist) sys_args = "repobee -h".split() monkeypatch.setattr("_repobee.distinfo.DIST_INSTALL", True) with pytest.raises(SystemExit): # calling -h always causes a SystemExit main.main(sys_args, unload_plugins=False) qualnames = { p.__name__ for p in plug.manager.get_plugins() if isinstance(p, types.ModuleType) } assert qualnames.issuperset(dist_plugin_qualnames)
def test_invalid_plug_options(dispatch_command_mock, init_plugins_mock): """-f is not a valid option for plugins and should be bumped to the main parser Note that the default plugins must be loaded for this test to work. """ plugin.initialize_plugins( plugin.get_qualified_module_names(_repobee.ext.defaults), allow_qualified=True, ) sys_args = ["repobee", "-p", "javac", "-f", *CLONE_ARGS] with pytest.raises(SystemExit): main.main(sys_args) init_plugins_mock.assert_has_calls( [ call(["javac"], allow_filepath=True), call(DEFAULT_PLUGIN_NAMES, allow_qualified=True), ], any_order=True, ) assert not dispatch_command_mock.called