def test_chained_combinations(self, myapp, cmdargs, x_cgrp_opt, x_results): myapp.invoke(_split_cmd_args(cmdargs)) self._common_group_assertions(myapp) chainedgrp = myapp.invoked_subcommand.invoked_subcommand assert isinstance(chainedgrp, smclip.ChainedCommandGroup) assert chainedgrp.invoked_subcommand is True assert len(chainedgrp.invoked_subcommands) == len(x_results) chainedgrp.preprocess.assert_called_once_with(vieweditopt=x_cgrp_opt) assert chainedgrp.this_action.call_count == 0 expected_rvs = smclip.ChainedOutputResults() for i, chainedcmd in enumerate(chainedgrp.invoked_subcommands): x_name, x_values, x_rv = x_results[i] assert chainedcmd.name == x_name chainedcmd.preprocess.assert_called_once_with(**x_values) chainedcmd.this_action.assert_called_once_with(**x_values) expected_rvs.add_result(chainedcmd, x_rv) assert chainedgrp.results_callback.call_count == 1 assert len(chainedgrp.results_callback.call_args[0]) == 1 result_obj = chainedgrp.results_callback.call_args[0][0] assert result_obj.results == expected_rvs.results assert iter(result_obj)
def test_correct(self, myapp): myapp.invoke(_split_cmd_args('override --toreplace value')) overridecmd = myapp.invoked_subcommand overridecmd.preprocess.assert_called_once_with(toreplace='value') overridecmd.this_action.assert_called_once_with(replaced='othervalue')
def test_help(self, myapp): with pytest.raises(SystemExit): myapp.invoke(_split_cmd_args('listdefault 1234 change --help')) chainedgrp = myapp.invoked_subcommand.invoked_subcommand assert chainedgrp.invoked_subcommands, 'Chained subcommand is not marked as called' first_chained_cmd = chainedgrp.invoked_subcommands[0] assert first_chained_cmd.parent is chainedgrp
def test_last_cmd_get_parent_names(self, myapp, cmdargs, real_names_only, expected): myapp.invoke(_split_cmd_args(cmdargs)) lastcmd = myapp.invoked_subcommand while hasattr(lastcmd, 'invoked_subcommand') and lastcmd.invoked_subcommand: lastcmd = lastcmd.invoked_subcommand parent_names = lastcmd.get_parent_names(real_names_only=real_names_only) assert parent_names == expected
def test_correct(self, myapp, cmdargs, x_app_opt, x_sub_opt): myapp.invoke(_split_cmd_args(cmdargs)) myapp.preprocess.assert_called_once_with(appopt=x_app_opt) assert myapp.this_action.call_count == 0 assert isinstance(myapp.invoked_subcommand, SimpleCommand) helpcmd = myapp.invoked_subcommand helpcmd.preprocess.assert_called_once_with(helpopt=x_sub_opt) helpcmd.this_action.assert_called_once_with(helpopt=x_sub_opt)
def test_possible_commands(myapp, cmdargs, current_command_name, subcommand_names): args = _split_cmd_args(cmdargs) cmd_names = myapp.possible_command_names(args) assert cmd_names is not None assert set(cmd_names) == set(subcommand_names) possible_commands = myapp.commands_for_args(args) assert len(possible_commands) > 0 assert possible_commands[0].default_name == current_command_name, \ 'Current command was not included'
def test_group_invoke(self, myapp, cmdargs, x_app_opt, x_grp_opt): myapp.invoke(_split_cmd_args(cmdargs)) myapp.preprocess.assert_called_once_with(appopt=x_app_opt) assert myapp.this_action.call_count == 0 groupcmd = myapp.invoked_subcommand assert isinstance(groupcmd, ItemGroupCommand) groupcmd.preprocess.assert_called_once_with(groupopt=x_grp_opt) groupcmd.this_action.assert_called_once_with(groupopt=x_grp_opt) assert not groupcmd.invoked_subcommand
def test_last_cmd_get_parent_names(self, myapp, cmdargs, real_names_only, expected): myapp.invoke(_split_cmd_args(cmdargs)) lastcmd = myapp.invoked_subcommand while hasattr(lastcmd, 'invoked_subcommand') and lastcmd.invoked_subcommand: lastcmd = lastcmd.invoked_subcommand parent_names = lastcmd.get_parent_names( real_names_only=real_names_only) assert parent_names == expected
def test_direct_call(self, myapp, cmdargs, x_cmd_alias, x_cgrp_opt): myapp.invoke(_split_cmd_args(cmdargs)) self._common_group_assertions(myapp) chainedgrp = myapp.invoked_subcommand.invoked_subcommand assert isinstance(chainedgrp, smclip.ChainedCommandGroup) assert chainedgrp.alias == x_cmd_alias # tests fallback command assert not chainedgrp.invoked_subcommand chainedgrp.preprocess.assert_called_once_with(vieweditopt=x_cgrp_opt) chainedgrp.this_action.assert_called_once_with(vieweditopt=x_cgrp_opt) assert chainedgrp.results_callback.call_count == 0
def test_group_subcommand(self, myapp, cmdargs, x_app_opt, x_grp_opt, x_sub_opt): myapp.invoke(_split_cmd_args(cmdargs)) myapp.preprocess.assert_called_once_with(appopt=x_app_opt) assert myapp.this_action.call_count == 0 groupcmd = myapp.invoked_subcommand groupcmd.preprocess.assert_called_once_with(groupopt=x_grp_opt) assert groupcmd.this_action.call_count == 0 subcmd = groupcmd.invoked_subcommand assert isinstance(subcmd, CreateCommand) subcmd.preprocess.assert_called_once_with(createopt=x_sub_opt) subcmd.this_action.assert_called_once_with(createopt=x_sub_opt)
def test_group_default_invoke(self, myapp, cmdargs, x_app_opt, x_sub_opt): myapp.invoke(_split_cmd_args(cmdargs)) myapp.preprocess.assert_called_once_with(appopt=x_app_opt) assert myapp.this_action.call_count == 0 groupcmd = myapp.invoked_subcommand assert isinstance(groupcmd, ItemGroupCommandDefault) # Item group of chained commands with default command does not call its callbacks # when invoking default command assert groupcmd.preprocess.call_count == 0 assert groupcmd.this_action.call_count == 0 defaultgrpcmd = groupcmd.invoked_subcommand assert isinstance(defaultgrpcmd, ListCommand) assert groupcmd._default_subcmd_cls is defaultgrpcmd.__class__ defaultgrpcmd.preprocess.assert_called_once_with(listopt=x_sub_opt) defaultgrpcmd.this_action.assert_called_once_with(listopt=x_sub_opt)
def test_incorrect(self, myapp): with pytest.raises(AssertionError) as excinfo: myapp.invoke(_split_cmd_args('badoverride')) assert 'Expected preprocess' in str(excinfo.value)
def test_bad_arguments_for_command_names(myapp, cmdargs): cmd_names = myapp.possible_command_names(_split_cmd_args(cmdargs)) assert cmd_names is None
def test_bad_option(self, myapp): with pytest.raises(SystemExit) as excinfo: myapp.invoke(_split_cmd_args('--badopt value')) assert excinfo.value.code == 2
def test_correct(self, myapp, cmdargs, expected): myapp.invoke(_split_cmd_args(cmdargs)) myapp.preprocess.assert_called_once_with(appopt=expected) myapp.this_action.assert_called_once_with(appopt=expected)
def test_unknown_command(self, myapp): with pytest.raises(SystemExit) as excinfo: myapp.invoke(_split_cmd_args('unknowncmd'))