示例#1
0
def test_get_config_tuple_from_egrc_when_present():
    """Make sure we extract values correctly from the egrc."""
    # These are the values hardcoded into the files.
    egrc_examples_dir = 'test/example/dir/in/egrc_withdata'
    egrc_custom_dir = 'test/custom/dir/in/egrc_withdata'
    egrc_use_color = True
    egrc_pager_cmd = 'more egrc'
    color_config_from_file = _get_color_config_from_egrc_withdata()

    def return_expanded_path(*args, **kwargs):
        if args[0] == egrc_examples_dir:
            return egrc_examples_dir
        elif args[0] == egrc_custom_dir:
            return egrc_custom_dir
        else:
            raise TypeError(args[0] + ' was an unexpected path--should be ' +
                            egrc_examples_dir + ' or ' + egrc_custom_dir)

    with patch('eg.eg_config.get_expanded_path',
               side_effect=return_expanded_path) as mock_expand:

        actual = eg_config.get_config_tuple_from_egrc(
            'test/assets/egrc_withdata')

        target = eg_config.Config(examples_dir=egrc_examples_dir,
                                  custom_dir=egrc_custom_dir,
                                  color_config=color_config_from_file,
                                  use_color=egrc_use_color,
                                  pager_cmd=egrc_pager_cmd)
        assert_equal(actual, target)

        mock_expand.assert_any_call(egrc_examples_dir)
        mock_expand.assert_any_call(egrc_custom_dir)
示例#2
0
def test_config_returns_values_passed_at_command_line():
    """
    Options passed in at the command line should override those in the egrc.
    """
    with patch('os.path.isfile', return_value=True):
        command_line_examples_dir = 'test_eg_dir_user_defined'
        command_line_custom_dir = 'test_custom_dir_user_defined'
        command_line_use_color = 'we_should_use_color'
        command_line_pager_cmd = 'command_line_says_pager_with_cat'
        egrc_examples_dir = 'egrc_examples_dir'
        egrc_custom_dir = 'egrc_custom_dir'
        egrc_use_color = 'the_egrc_says_yes_color'
        egrc_pager_cmd = 'the_egrc_pages_with_more'
        egrc_config = eg_config.Config(
            examples_dir=egrc_examples_dir,
            custom_dir=egrc_custom_dir,
            color_config=eg_config.get_default_color_config(),
            use_color=egrc_use_color,
            pager_cmd=egrc_pager_cmd)
        with patch('eg.eg_config.get_config_tuple_from_egrc',
                   return_value=egrc_config):
            actual = eg_config.get_resolved_config_items(
                None,
                command_line_examples_dir,
                command_line_custom_dir,
                command_line_use_color,
                command_line_pager_cmd,
                debug=False)
            assert_equal(actual.examples_dir, command_line_examples_dir)
            assert_equal(actual.custom_dir, command_line_custom_dir)
            assert_equal(actual.use_color, command_line_use_color)
            assert_equal(actual.pager_cmd, command_line_pager_cmd)
示例#3
0
def test_config_returns_egrc_values_if_present():
    """
    If values are present from an egrc, make sure we take them.

    Doesn't make sure they are extracted correctly from an egrc file.
    """
    with patch('os.path.isfile', return_value=True):
        examples_dir = 'test_eg_dir_from_egrc'
        custom_dir = 'test_custom_dir_from_egrc'
        test_color_config = _get_color_config_from_egrc_withdata()
        test_use_color = True
        test_pager_cmd = 'more baby'

        def_config = eg_config.Config(examples_dir=examples_dir,
                                      custom_dir=custom_dir,
                                      color_config=test_color_config,
                                      use_color=test_use_color,
                                      pager_cmd=test_pager_cmd)
        with patch('eg.eg_config.get_config_tuple_from_egrc',
                   return_value=def_config):
            config = eg_config.get_resolved_config_items(
                None, None, None, None, None)
            assert_equal(config.examples_dir, examples_dir)
            assert_equal(config.custom_dir, custom_dir)
            assert_equal(config.color_config, test_color_config)
            assert_equal(config.use_color, test_use_color)
            assert_equal(config.pager_cmd, test_pager_cmd)
示例#4
0
文件: eg_util_test.py 项目: statik/eg
def test_list_supported_programs_fails_gracefully_if_no_dirs():
    config = eg_config.Config(None, None, None, None, None)

    actual = eg_util.get_list_of_all_supported_commands(config)
    target = []

    assert_equal(actual, target)
示例#5
0
文件: eg_util_test.py 项目: statik/eg
def test_list_supported_programs_both():
    examples_dir = 'example/dir'
    custom_dir = 'custom/dir'

    config = eg_config.Config(examples_dir=examples_dir,
                              custom_dir=custom_dir,
                              color_config=None,
                              use_color=False,
                              pager_cmd=None)

    def give_list(*args, **kwargs):
        if args[0] == examples_dir:
            return ['alpha', 'bar.md', 'both.md', 'examples']
        else:
            # custom_dir
            return ['azy.md', 'both.md', 'examples', 'zeta']

    with patch('os.path.isdir', return_value=True):
        with patch('os.listdir', side_effect=give_list):
            actual = eg_util.get_list_of_all_supported_commands(config)

            target = [
                'alpha', 'azy +', 'bar', 'both *', 'examples *', 'zeta +'
            ]

            assert_equal(actual, target)
示例#6
0
文件: eg_util_test.py 项目: statik/eg
def test_handle_program_finds_paths_and_calls_open_pager():
    program = 'mv'

    examples_dir = 'test-eg-dir'
    custom_dir = 'test-custom-dir'
    color_config = None
    use_color = False
    pager_cmd = 'foo bar'

    config = eg_config.Config(examples_dir=examples_dir,
                              custom_dir=custom_dir,
                              color_config=color_config,
                              use_color=use_color,
                              pager_cmd=pager_cmd)

    default_path = 'test-eg-dir/mv.md'
    custom_path = 'test-custom-dir/mv.md'

    def return_correct_path(*args, **kwargs):
        program_param = args[0]
        dir_param = args[1]
        if program_param != program:
            raise NameError('expected ' + program + ', got ' + program_param)
        if dir_param == examples_dir:
            return default_path
        elif dir_param == custom_dir:
            return custom_path
        else:
            raise NameError('got ' + dir_param + ', expected ' + examples_dir +
                            ' or ' + custom_dir)

    with patch('eg.eg_util.has_default_entry_for_program',
               return_value=True) as mock_has_default:
        with patch('eg.eg_util.has_custom_entry_for_program',
                   return_value=True) as mock_has_custom:
            with patch('eg.eg_util.open_pager_for_file') as mock_open_pager:
                with patch('eg.eg_util.get_file_path_for_program',
                           side_effect=return_correct_path) as mock_get_file:
                    eg_util.handle_program(program, config)

                    mock_has_default.assert_called_once_with(program, config)
                    mock_has_custom.assert_called_once_with(program, config)

                    mock_get_file.assert_any_call(program, examples_dir)
                    mock_get_file.assert_any_call(
                        program,
                        custom_dir,
                    )

                    mock_open_pager.assert_called_once_with(
                        default_file_path=default_path,
                        custom_file_path=custom_path,
                        use_color=use_color,
                        color_config=color_config,
                        pager_cmd=pager_cmd)
示例#7
0
文件: eg_util_test.py 项目: statik/eg
def test_has_custom_entry_when_present():
    config = eg_config.Config(examples_dir=None,
                              custom_dir='customdir',
                              color_config=None,
                              use_color=False,
                              pager_cmd=None)
    program = 'find'

    path = '/Users/tyrion/customdir/find.md'

    _helper_assert_path_isfile_not_present(config, program, path, 'custom',
                                           True, True)
示例#8
0
文件: eg_util_test.py 项目: statik/eg
def test_has_default_entry_when_present():
    config = eg_config.Config(examples_dir='examplesdir',
                              custom_dir=None,
                              color_config=None,
                              use_color=False,
                              pager_cmd=None)
    program = 'mv'

    path = '/Users/tyrion/examplesdir/mv.md'

    _helper_assert_path_isfile_not_present(config, program, path, 'default',
                                           True, True)
示例#9
0
文件: eg_util_test.py 项目: statik/eg
def test_has_custom_entry_for_program_no_custom_dir():
    config = eg_config.Config(examples_dir='examplesdir',
                              custom_dir=None,
                              color_config=None,
                              use_color=False,
                              pager_cmd=None)

    program = 'find'

    has_entry = eg_util.has_custom_entry_for_program(program, config)

    assert_equal(False, has_entry)
示例#10
0
def test_get_config_tuple_from_egrc_all_none_when_not_present():
    """
    Return correct data if the egrc has no data.

    We should return None for all values and an empty color_config if there is
    no data in the egrc.
    """
    actual = eg_config.get_config_tuple_from_egrc('test/assets/egrc_nodata')

    empty_color_config = eg_config.get_empty_color_config()

    target = eg_config.Config(examples_dir=None,
                              custom_dir=None,
                              color_config=empty_color_config,
                              use_color=None,
                              pager_cmd=None)
    assert_equal(actual, target)
示例#11
0
def test_config_uses_custom_egrc_path():
    """Make sure we use the passed in egrc path rather than the default."""
    with patch('os.path.isfile', return_value=True):
        def_config = eg_config.Config(
            examples_dir='eg_dir',
            custom_dir='custom_dir',
            color_config=eg_config.get_empty_color_config(),
            use_color=False,
            pager_cmd='less is more')
        egrc_path = 'test/path/to/egrc'
        with patch('eg.eg_config.get_config_tuple_from_egrc',
                   return_value=def_config) as mocked_method:
            eg_config.get_resolved_config_items(egrc_path,
                                                None,
                                                None,
                                                None,
                                                None,
                                                debug=False)
            mocked_method.assert_called_once_with(egrc_path)
示例#12
0
文件: eg_util_test.py 项目: statik/eg
def test_handle_program_no_entries():
    program = 'cp'
    config = eg_config.Config(examples_dir=None,
                              custom_dir=None,
                              color_config=None,
                              use_color=False,
                              pager_cmd=None)

    with patch('eg.eg_util.has_default_entry_for_program',
               return_value=False) as mock_has_default:
        with patch('eg.eg_util.has_custom_entry_for_program',
                   return_value=False) as mock_has_custom:
            with patch('eg.eg_util.open_pager_for_file') as mock_open_pager:
                eg_util.handle_program(program, config)

                mock_has_default.assert_called_once_with(program, config)
                mock_has_custom.assert_called_once_with(program, config)

                assert_equal(mock_open_pager.call_count, 0)
示例#13
0
文件: eg_util_test.py 项目: statik/eg
def test_list_supported_programs_only_custom():
    example_dir = 'example/dir'
    custom_dir = 'custom/dir'

    config = eg_config.Config(examples_dir=example_dir,
                              custom_dir=custom_dir,
                              color_config=None,
                              use_color=False,
                              pager_cmd=None)

    def give_list(*args, **kwargs):
        if args[0] == custom_dir:
            return ['awk.md', 'bar.md', 'xor.md']
        else:
            return []

    with patch('os.path.isdir', return_value=True):
        with patch('os.listdir', side_effect=give_list):
            actual = eg_util.get_list_of_all_supported_commands(config)
            target = ['awk +', 'bar +', 'xor +']

            assert_equal(actual, target)