Example #1
0
    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)
Example #2
0
    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_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')
Example #5
0
    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_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
Example #8
0
    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'
Example #10
0
    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)
Example #11
0
    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
Example #12
0
    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
Example #13
0
    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
Example #14
0
    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
Example #15
0
    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
Example #16
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)
Example #17
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)
Example #18
0
    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)
Example #19
0
    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)
Example #20
0
    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
Example #22
0
    def test_bad_option(self, myapp):

        with pytest.raises(SystemExit) as excinfo:
            myapp.invoke(_split_cmd_args('--badopt value'))

        assert excinfo.value.code == 2
Example #23
0
    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)
Example #24
0
    def test_unknown_command(self, myapp):

        with pytest.raises(SystemExit) as excinfo:
            myapp.invoke(_split_cmd_args('unknowncmd'))
Example #25
0
    def test_unknown_command(self, myapp):

        with pytest.raises(SystemExit) as excinfo:
            myapp.invoke(_split_cmd_args('unknowncmd'))
Example #26
0
    def test_incorrect(self, myapp):
        with pytest.raises(AssertionError) as excinfo:
            myapp.invoke(_split_cmd_args('badoverride'))

        assert 'Expected preprocess' in str(excinfo.value)
Example #27
0
    def test_bad_option(self, myapp):

        with pytest.raises(SystemExit) as excinfo:
            myapp.invoke(_split_cmd_args('--badopt value'))

        assert excinfo.value.code == 2
Example #28
0
    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)