Esempio n. 1
0
def _helper_run_eg_responds_to_args_correctly(
    args,
    mock_resolved_config,
    mock_show_list,
    mock_show_version,
    mock_handle_program,
    mock_parse_args,
    mock_no_editor,
    mock_edit_custom,
    resolved_config='stand-in-config',
    call_show_list=False,
    call_show_version=False,
    call_handle_program=False,
    call_no_editor=False,
    call_edit_custom=False,
):
    """
    Helper method for verifying the results of calls to run_eg.
    """
    mock_resolved_config.return_value = resolved_config
    mock_parse_args.return_value = args

    core.run_eg()

    if (call_show_list):
        mock_show_list.assert_called_once_with(resolved_config)
    else:
        assert mock_show_list.call_args_list == []

    if (call_show_version):
        mock_show_version.assert_called_once_with()
    else:
        assert mock_show_version.call_args_list == []

    if (call_no_editor):
        mock_no_editor.assert_called_once_with()
    else:
        assert mock_no_editor.call_args_list == []

    if (call_edit_custom):
        mock_edit_custom.assert_called_once_with(args.program, resolved_config)
    else:
        assert mock_edit_custom.call_args_list == []

    if (call_handle_program):
        mock_handle_program.assert_called_once_with(
            args.program, resolved_config
        )
    else:
        assert mock_handle_program.call_args_list == []
Esempio n. 2
0
def _helper_run_eg_responds_to_args_correctly(
    args,
    mock_resolved_config,
    mock_show_list,
    mock_show_version,
    mock_handle_program,
    mock_parse_args,
    mock_no_editor,
    mock_edit_custom,
    resolved_config='stand-in-config',
    call_show_list=False,
    call_show_version=False,
    call_handle_program=False,
    call_no_editor=False,
    call_edit_custom=False,
):
    """
    Helper method for verifying the results of calls to run_eg.
    """
    mock_resolved_config.return_value = resolved_config
    mock_parse_args.return_value = args

    core.run_eg()

    if (call_show_list):
        mock_show_list.assert_called_once_with(resolved_config)
    else:
        assert_equal(mock_show_list.call_args_list, [])

    if (call_show_version):
        mock_show_version.assert_called_once_with()
    else:
        assert_equal(mock_show_version.call_args_list, [])

    if (call_no_editor):
        mock_no_editor.assert_called_once_with()
    else:
        assert_equal(mock_no_editor.call_args_list, [])

    if (call_edit_custom):
        mock_edit_custom.assert_called_once_with(args.program, resolved_config)
    else:
        assert_equal(mock_edit_custom.call_args_list, [])

    if (call_handle_program):
        mock_handle_program.assert_called_once_with(args.program,
                                                    resolved_config)
    else:
        assert_equal(mock_handle_program.call_args_list, [])
Esempio n. 3
0
def test_fewer_than_two_args_fails():
    """
    You always need at least two arguments in argv: eg and a command.
    """
    with patch('sys.argv', ['eg']):
        with patch('argparse.ArgumentParser.print_help') as mock_help:
            core.run_eg()
            mock_help.assert_called_once_with()
    _helper_assert_about_invocation(argv=['eg'],
                                    num_print_help_calls=1,
                                    num_show_list_calls=0,
                                    num_show_version_calls=0,
                                    num_handle_program_calls=0,
                                    num_handle_insufficient_args_calls=0,
                                    resolved_config='config',
                                    handle_program_args='not applicable',
                                    egrc_path=None,
                                    examples_dir=None,
                                    custom_dir=None,
                                    use_color=None,
                                    pager_cmd=None,
                                    squeeze=None)
Esempio n. 4
0
def test_fewer_than_two_args_fails():
    """
    You always need at least two arguments in argv: eg and a command.
    """
    with patch('sys.argv', ['eg']):
        with patch('argparse.ArgumentParser.print_help') as mock_help:
            core.run_eg()
            mock_help.assert_called_once_with()
    _helper_assert_about_invocation(
        argv=['eg'],
        num_print_help_calls=1,
        num_show_list_calls=0,
        num_show_version_calls=0,
        num_handle_program_calls=0,
        num_handle_insufficient_args_calls=0,
        resolved_config='config',
        handle_program_args='not applicable',
        egrc_path=None,
        examples_dir=None,
        custom_dir=None,
        use_color=None,
        pager_cmd=None,
        squeeze=None
    )
Esempio n. 5
0
#!/usr/bin/python
from eg import core


core.run_eg()
Esempio n. 6
0
def _helper_assert_about_invocation(
    argv,
    num_print_help_calls,
    num_show_list_calls,
    num_show_version_calls,
    num_handle_program_calls,
    num_handle_insufficient_args_calls,
    resolved_config,
    handle_program_args,
    egrc_path,
    examples_dir,
    custom_dir,
    use_color,
    pager_cmd,
    squeeze
):
    """
    Helper method for calling core.run_eg() and making assertions as if it were
    called from the command line.

    argv: an array to be interpreted as argv
    num_print_help_calls: the number of calls to print_help
    num_show_list_calls: the number of calls to _show_list_message. Called with
        resolved_config as a paramter.
    num_show_version_calls: the number of calls to _show_version
    num_handle_program_calls: the number of calls to handle_program
    resolved_config: the resolved config object
    handle_program_args: an array of arguments that will be exploded and passed
        individually to handle_program if num_handle_program_calls > 0
    egrc_path: the path we should parse from the command line
    examples_dir: the examples dir we should parse from the command line
    custom_dir: the custom dir we should parse from the command line
    use_color: the use_color value we should parse from the command line
    pager_cmd: the pager_cmd we should parse from the command line
    squeeze: the squeeze command we should parse from the command line
    """
    with patch('sys.argv', argv):
        with patch('argparse.ArgumentParser.print_help') as mock_help:
            with patch('eg.core._show_list_message') as mock_show_list:
                with patch('eg.core._show_version') as mock_version:
                    with patch('eg.util.handle_program') as mock_handle:
                        with patch(
                            'eg.config.get_resolved_config_items',
                            return_value=resolved_config
                        ) as mock_resolve_config:
                            with patch(
                                'eg.core._handle_insufficient_args'
                            ) as mock_bad_args:
                                core.run_eg()

                                assert_equal(
                                    mock_help.call_count,
                                    num_print_help_calls
                                )
                                assert_equal(
                                    mock_version.call_count,
                                    num_show_version_calls
                                )
                                assert_equal(
                                    mock_bad_args.call_count,
                                    num_handle_insufficient_args_calls
                                )

                                should_resolve_config = False

                                if num_show_version_calls > 0:
                                    should_resolve_config = True

                                if num_show_list_calls > 0:
                                    should_resolve_config = True
                                    mock_show_list.assert_called_once_with(
                                        resolved_config
                                    )
                                else:
                                    assert_equal(mock_show_list.call_count, 0)

                                if num_handle_program_calls > 0:
                                    should_resolve_config = True
                                    mock_handle.assert_called_once_with(
                                        *handle_program_args
                                    )
                                else:
                                    assert_equal(mock_handle.call_count, 0)

                                if should_resolve_config:
                                    mock_resolve_config.assert_called_once_with(
                                        egrc_path=egrc_path,
                                        examples_dir=examples_dir,
                                        custom_dir=custom_dir,
                                        use_color=use_color,
                                        pager_cmd=pager_cmd,
                                        squeeze=squeeze
                                    )
                                else:
                                    assert_equal(
                                        mock_resolve_config.call_count,
                                        0
                                    )
Esempio n. 7
0
def _helper_assert_about_invocation(
        argv, num_print_help_calls, num_show_list_calls,
        num_show_version_calls, num_handle_program_calls,
        num_handle_insufficient_args_calls, resolved_config,
        handle_program_args, egrc_path, examples_dir, custom_dir, use_color,
        pager_cmd, squeeze):
    """
    Helper method for calling core.run_eg() and making assertions as if it were
    called from the command line.

    argv: an array to be interpreted as argv
    num_print_help_calls: the number of calls to print_help
    num_show_list_calls: the number of calls to _show_list_message. Called with
        resolved_config as a paramter.
    num_show_version_calls: the number of calls to _show_version
    num_handle_program_calls: the number of calls to handle_program
    resolved_config: the resolved config object
    handle_program_args: an array of arguments that will be exploded and passed
        individually to handle_program if num_handle_program_calls > 0
    egrc_path: the path we should parse from the command line
    examples_dir: the examples dir we should parse from the command line
    custom_dir: the custom dir we should parse from the command line
    use_color: the use_color value we should parse from the command line
    pager_cmd: the pager_cmd we should parse from the command line
    squeeze: the squeeze command we should parse from the command line
    """
    with patch('sys.argv', argv):
        with patch('argparse.ArgumentParser.print_help') as mock_help:
            with patch('eg.core._show_list_message') as mock_show_list:
                with patch('eg.core._show_version') as mock_version:
                    with patch('eg.util.handle_program') as mock_handle:
                        with patch('eg.config.get_resolved_config_items',
                                   return_value=resolved_config
                                   ) as mock_resolve_config:
                            with patch('eg.core._handle_insufficient_args'
                                       ) as mock_bad_args:
                                core.run_eg()

                                assert_equal(mock_help.call_count,
                                             num_print_help_calls)
                                assert_equal(mock_version.call_count,
                                             num_show_version_calls)
                                assert_equal(
                                    mock_bad_args.call_count,
                                    num_handle_insufficient_args_calls)

                                should_resolve_config = False

                                if num_show_version_calls > 0:
                                    should_resolve_config = True

                                if num_show_list_calls > 0:
                                    should_resolve_config = True
                                    mock_show_list.assert_called_once_with(
                                        resolved_config)
                                else:
                                    assert_equal(mock_show_list.call_count, 0)

                                if num_handle_program_calls > 0:
                                    should_resolve_config = True
                                    mock_handle.assert_called_once_with(
                                        *handle_program_args)
                                else:
                                    assert_equal(mock_handle.call_count, 0)

                                if should_resolve_config:
                                    mock_resolve_config.assert_called_once_with(
                                        egrc_path=egrc_path,
                                        examples_dir=examples_dir,
                                        custom_dir=custom_dir,
                                        use_color=use_color,
                                        pager_cmd=pager_cmd,
                                        squeeze=squeeze)
                                else:
                                    assert_equal(
                                        mock_resolve_config.call_count, 0)
Esempio n. 8
0
#!/usr/bin/python
from eg import core

core.run_eg()