def test_works(p_exec_script_command: Mock, app_conf: conf.Config): p_exec_script_command.return_value = 0 options: Dict[str, Any] = {} script = Script.from_config('test', {'command': 'fake-cmd'}) run_script(script, options) p_exec_script_command.assert_called_once()
def test_supports_passing_scalar_to_success_exit_codes(): script = Script.from_config('test', { 'command': 'fake_cmd', 'success_exit_codes': 5, }) assert script.name == 'test' assert script.command == 'fake_cmd' assert script.success_exit_codes == [5]
def test_works_with_just_name_and_command(): script = Script.from_config('test', {'command': 'echo test'}) assert script assert script.name == 'test' assert script.command == 'echo test' assert script.about == '' assert script.files is None assert script.success_exit_codes == [0]
def test_uses_cli_group_command_decorator(): p_run_cli = Mock() p_command_decorator = Mock() p_run_cli.command.return_value = p_command_decorator script = Script.from_config('fake', {'command': 'fake'}) script.register(p_run_cli) p_run_cli.command.assert_called_with(script.name) p_command_decorator.assert_called_once()
def test_exits_with_script_return_code_if_its_non_zero( p_exec_script_command: Mock, p_exit: Mock, app_conf: conf.Config, ): p_exec_script_command.return_value = -99 options: Dict[str, Any] = {} script = Script.from_config('test', {'command': 'fake-cmd'}) run_script(script, options) p_exit.assert_called_once_with(-99)
def test_prints_return_code_if_verbose_lvl_ge_3( p_exec_script_command: Mock, p_cprint: Mock, app_conf: conf.Config, ): RunContext().set('verbose', 1) p_exec_script_command.return_value = -99 options: Dict[str, Any] = {} script = Script.from_config('test', {'command': 'fake-cmd'}) run_script(script, options) assert next((True for x in p_cprint.call_args_list if 'Script exited with code: <33>-99' in x[0][0]), False)
def test_creates_all_options_defined_in_the_script_config(p_click): script = Script.from_config( 'fake', { 'command': 'fake', 'options': [ { 'name': '--fake1', 'is_flag': True }, { 'name': '--fake2', 'type': 'int' }, ] }) script.register(Mock()) assert p_click.option.call_count == 2 assert len(p_click.option.call_args_list) == 2 fake1 = next( (args for args in p_click.option.call_args_list if args[0][0] == '--fake1'), None, ) fake2 = next( (args for args in p_click.option.call_args_list if args[0][0] == '--fake2'), None, ) assert fake1 is not None _, fake1_kw = fake1 assert fake1_kw['is_flag'] is True assert fake1_kw['count'] is False assert fake1_kw['help'] == '' assert fake1_kw['default'] is None assert fake1_kw['type'] is str assert fake2 is not None _, fake2_kw = fake2 assert fake2_kw['is_flag'] is False assert fake2_kw['count'] is False assert fake2_kw['help'] == '' assert fake2_kw['default'] is None assert fake2_kw['type'] == int
def test_raises_ValueError_if_command_or_command_files_is_missing( app_conf: conf.Config): """ GIVEN A script with neither command nor command_file defined WHEN I run the script THEN it raises ValueError. """ options: Dict[str, Any] = {} script = Script( name='test', command='', command_file='', ) with pytest.raises(ValueError): run_script(script, options)
def test_uses_command_file_if_given(p_open: Mock, command: str, command_file: str, app_conf: conf.Config): """ GIVEN A command_file is defined for the script WHEN I run the script THEN It will always use it no matter if 'command' is also defined or not """ options: Dict[str, Any] = {} script = Script( name='test', command=command, command_file=command_file, ) run_script(script, options) p_open.assert_called_once_with(conf.proj_path('fake/file'))
def test_includes_files_if_specified_in_config(): options = {'fake_opt': 'hello'} script = Script.from_config('test', { 'command': 'fake-cmd', 'files': { 'paths': 'fake_path' }, }) RunContext().set('verbose', 3) RunContext().set('pretend', False) result = build_template_context(script, options) assert 'files' in result assert result['files'] == ['file1', 'file2', 'file3'] RunContext().set('verbose', 0)
def test_works(): options = {'fake_opt': 'hello'} script = Script.from_config('test', {'command': 'fake-cmd'}) RunContext().set('verbose', 3) RunContext().set('pretend', False) result = build_template_context(script, options) assert result['ctx'] == RunContext().values assert result['conf'] == {'pelconf': 'hello'} assert result['proj_path'] == conf.proj_path assert result['script'] == attr.asdict(script) assert result['opts'] == { 'verbose': 3, 'pretend': False, 'fake_opt': 'hello', } assert 'files' not in result
def test_works(): script = Script.from_config( 'test', { 'about': 'Test script', 'command': 'echo test', 'success_exit_codes': [0, 5], 'root_cli': True, 'files': { 'paths': ['src'], }, 'options': [{ 'name': ['-f', '--force'], 'about': 'Force something', 'is_flag': True, }], }) assert script.name == 'test' assert script.about == 'Test script' assert script.root_cli is True assert script.success_exit_codes == [0, 5] assert script.command == 'echo test' assert isinstance(script.files, types.FilesCollection) assert script.files.paths == ['src'] assert isinstance(script.options, list) assert len(script.options) == 1 assert isinstance(script.options[0], ScriptOption) assert script.options[0].name == ['-f', '--force'] assert script.options[0].about == "Force something" assert script.options[0].is_flag is True assert script.options[0].default is None
def test_raises_ValueError_if_command_is_not_defined(): with pytest.raises(ValueError): Script.from_config('test', {})
def test_every_script_has_pretend_option(p_pretend_option): script = Script.from_config('fake', {'command': 'fake'}) script.register(Mock()) p_pretend_option.assert_called_once()