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.config.get_expanded_path', side_effect=return_expanded_path) as mock_expand: actual = config.get_config_tuple_from_egrc('test/assets/egrc_withdata') target = 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)
def test_list_supported_programs_fails_gracefully_if_no_dirs(): test_config = config.Config(None, None, None, None, None, None, None) actual = util.get_list_of_all_supported_commands(test_config) target = [] assert_equal(actual, target)
def test_get_resolved_config_prioritizes_cli(): """ Options passed in at the command line should override those in the egrc. """ cli_examples_dir = 'test_eg_dir_user_defined' cli_custom_dir = 'test_custom_dir_user_defined' cli_use_color = 'we_should_use_color' cli_pager_cmd = 'command_line_says_pager_with_cat' cli_squeeze = 'command_line_wants_to_squeeze' egrc_config = _create_dummy_egrc_config() expected = config.Config( examples_dir=cli_examples_dir, custom_dir=cli_custom_dir, use_color=cli_use_color, color_config=egrc_config.color_config, pager_cmd=cli_pager_cmd, squeeze=cli_squeeze, subs=egrc_config.subs, editor_cmd=egrc_config.editor_cmd, ) _assert_about_get_resolved_config( cli_egrc_path=None, cli_examples_dir=cli_examples_dir, cli_custom_dir=cli_custom_dir, cli_use_color=cli_use_color, cli_pager_cmd=cli_pager_cmd, cli_squeeze=cli_squeeze, egrc_config=egrc_config, environment_editor_cmd=None, expected_config=expected, )
def test_list_supported_programs_both(): examples_dir = 'example/dir' custom_dir = 'custom/dir' test_config = 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 = util.get_list_of_all_supported_commands(test_config) target = [ 'alpha', 'azy +', 'bar', 'both *', 'examples *', 'zeta +' ] assert_equal(actual, target)
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 = 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.config.get_config_tuple_from_egrc', return_value=def_config): resolved_config = config.get_resolved_config_items( None, None, None, None, None) assert_equal(resolved_config.examples_dir, examples_dir) assert_equal(resolved_config.custom_dir, custom_dir) assert_equal(resolved_config.color_config, test_color_config) assert_equal(resolved_config.use_color, test_use_color) assert_equal(resolved_config.pager_cmd, test_pager_cmd)
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 = config.Config( examples_dir=egrc_examples_dir, custom_dir=egrc_custom_dir, color_config=config.get_default_color_config(), use_color=egrc_use_color, pager_cmd=egrc_pager_cmd) with patch('eg.config.get_config_tuple_from_egrc', return_value=egrc_config): actual = 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)
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' test_config = 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.util.has_default_entry_for_program', return_value=True) as mock_has_default: with patch('eg.util.has_custom_entry_for_program', return_value=True) as mock_has_custom: with patch('eg.util.open_pager_for_file') as mock_open_pager: with patch('eg.util.get_file_path_for_program', side_effect=return_correct_path) as mock_get_file: util.handle_program(program, test_config) mock_has_default.assert_called_once_with( program, test_config) mock_has_custom.assert_called_once_with( program, test_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)
def test_has_custom_entry_when_present(): test_config = 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(test_config, program, path, 'custom', True, True)
def test_has_default_entry_when_present(): test_config = 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(test_config, program, path, 'default', True, True)
def test_has_custom_entry_for_program_no_custom_dir(): test_config = config.Config(examples_dir='examplesdir', custom_dir=None, color_config=None, use_color=False, pager_cmd=None) program = 'find' has_entry = util.has_custom_entry_for_program(program, test_config) assert_equal(False, has_entry)
def test_get_config_tuple_from_egrc_when_present(mock_expand): """ 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' egrc_editor_cmd = 'vim egrc' color_config_from_file = _get_color_config_from_egrc_withdata() egrc_squeeze = True # Order matters--we apply substitutions alphabetically. egrc_subs = [ substitute.Substitution(r' ', r'', False), substitute.Substitution('\n\n\n', '\n\n', True) ] 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 ) mock_expand.side_effect = return_expanded_path actual = config.get_config_tuple_from_egrc(PATH_EGRC_WITH_DATA) expected = 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, squeeze=egrc_squeeze, subs=egrc_subs, editor_cmd=egrc_editor_cmd, ) assert actual == expected mock_expand.assert_any_call(egrc_examples_dir) mock_expand.assert_any_call(egrc_custom_dir)
def test_has_default_entry_for_program_no_examples_dir(): test_config = config.Config(examples_dir=None, custom_dir='customdir', color_config=None, use_color=False, pager_cmd=None, squeeze=False, subs=None) program = 'cp' has_entry = util.has_default_entry_for_program(program, test_config) assert_equal(False, has_entry)
def test_list_supported_programs_only_custom(): example_dir = 'example/dir' custom_dir = 'custom/dir' test_config = config.Config(examples_dir=example_dir, custom_dir=custom_dir, color_config=None, use_color=False, pager_cmd=None, squeeze=False, subs=None) target = ['awk +', 'bar +', 'xor +'] _helper_assert_list_supported_programs(test_config, [], ['awk.md', 'bar.md', 'xor.md'], {}, target)
def test_list_supported_programs_both(): examples_dir = 'example/dir' custom_dir = 'custom/dir' test_config = config.Config(examples_dir=examples_dir, custom_dir=custom_dir, color_config=None, use_color=False, pager_cmd=None, squeeze=False, subs=None) examples_list = ['alpha.md', 'bar.md', 'both.md', 'examples.md'] custom_list = ['azy.md', 'both.md', 'examples.md', 'zeta.md'] target = ['alpha', 'azy +', 'bar', 'both *', 'examples *', 'zeta +'] _helper_assert_list_supported_programs(test_config, examples_list, custom_list, {}, target)
def test_list_supported_programs_only_default(): example_dir = 'example/dir' custom_dir = 'custom/dir' test_config = config.Config(examples_dir=example_dir, custom_dir=custom_dir, color_config=None, use_color=False, pager_cmd=None, squeeze=False, subs=None) examples_list = ['aliases', 'cp.md', 'find.md', 'xargs.md'] custom_list = [] target = ['cp', 'find', 'xargs'] _helper_assert_list_supported_programs(test_config, examples_list, custom_list, {}, target)
def test_get_alias_dict_fails_gracefully_if_not_file(): """ Since users can specify a directory for examples that might not contain the aliases file, we want to fail gracefully if the file doesn't exist. """ contents_of_alias_dict_file = 'should never be reached' config_obj = config.Config(examples_dir='path/to/examples/dir', custom_dir='path/to/custom/dir', use_color=True, color_config='a color config', pager_cmd='less -ismore', squeeze=True, subs=['alpha_sub', 'beta_sub']) alias_file_path = 'path/to/the/alias/file' _helper_assert_get_alias_dict(contents_of_alias_dict_file, {}, config_obj, alias_file_path, False)
def test_get_alias_dict_returns_contents_of_correct_file(): """ get_alias_dict should read data from the file at the default path. """ alias_dict = {'link': 'ln', 'nc': 'netcat'} config_obj = config.Config(examples_dir='path/to/examples/dir', custom_dir='path/to/custom/dir', use_color=True, color_config='a color config', pager_cmd='less -ismore', squeeze=True, subs=['alpha_sub', 'beta_sub']) alias_file_path = 'path/to/alias/file' alias_dict_str = json.dumps(alias_dict) _helper_assert_get_alias_dict(alias_dict_str, alias_dict, config_obj, alias_file_path, True)
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 = config.get_config_tuple_from_egrc('test/assets/egrc_nodata') empty_color_config = config.get_empty_color_config() target = config.Config(examples_dir=None, custom_dir=None, color_config=empty_color_config, use_color=None, pager_cmd=None) assert_equal(actual, target)
def test_config_returns_egrc_values_if_present(mock_get_config, mock_isfile): """ If values are present from an egrc, make sure we take them. Doesn't make sure they are extracted correctly from an egrc file. """ 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' test_editor_cmd = 'vim is the best' test_squeeze = True test_subs = ['alpha', 'beta'] def_config = 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, editor_cmd=test_editor_cmd, squeeze=test_squeeze, subs=test_subs, ) mock_get_config.return_value = def_config resolved_config = config.get_resolved_config( None, None, None, None, None, None, ) assert_equal(resolved_config.examples_dir, examples_dir) assert_equal(resolved_config.custom_dir, custom_dir) assert_equal(resolved_config.color_config, test_color_config) assert_equal(resolved_config.use_color, test_use_color) assert_equal(resolved_config.pager_cmd, test_pager_cmd) assert_equal(resolved_config.editor_cmd, test_editor_cmd) assert_equal(resolved_config.squeeze, test_squeeze) assert_equal(resolved_config.subs, test_subs)
def test_get_alias_file_path(): """ _get_alias_file_path should just join the example dir and the alias file name, to make sure we look in the right place for the file. """ config_obj = config.Config(examples_dir='handy/dandy/examples/dir', custom_dir='path/to/custom/dir', use_color=True, color_config='a color config', pager_cmd='less -ismore', squeeze=True, subs=['alpha_sub', 'beta_sub']) join_result = 'joined path' with patch('os.path.join', return_value=join_result) as mock_join: actual = util._get_alias_file_path(config_obj) assert_equal(actual, join_result) mock_join.assert_called_once_with(config_obj.examples_dir, util.ALIAS_FILE_NAME)
def test_handle_program_no_entries(): program = 'cp' test_config = config.Config(examples_dir=None, custom_dir=None, color_config=None, use_color=False, pager_cmd=None) with patch('eg.util.has_default_entry_for_program', return_value=False) as mock_has_default: with patch('eg.util.has_custom_entry_for_program', return_value=False) as mock_has_custom: with patch('eg.util.open_pager_for_file') as mock_open_pager: util.handle_program(program, test_config) mock_has_default.assert_called_once_with(program, test_config) mock_has_custom.assert_called_once_with(program, test_config) assert_equal(mock_open_pager.call_count, 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 = config.Config( examples_dir='eg_dir', custom_dir='custom_dir', color_config=config.get_empty_color_config(), use_color=False, pager_cmd='less is more') egrc_path = 'test/path/to/egrc' with patch('eg.config.get_config_tuple_from_egrc', return_value=def_config) as mocked_method: config.get_resolved_config_items(egrc_path, None, None, None, None, debug=False) mocked_method.assert_called_once_with(egrc_path)
def test_list_supported_commands_includes_aliases(): examples_dir = 'examples/dir/for/aliases' custom_dir = 'custom/dir/for/aliases' test_config = config.Config(examples_dir=examples_dir, custom_dir=custom_dir, color_config=None, use_color=False, pager_cmd=None, squeeze=False, subs=None) # Things we want to cover: # normal alias # alias that shadows a custom-only declaration # alias that points to a * or + program examples_list = [ 'alpha.md', 'bar.md', 'both.md', 'default-only.md', 'examples.md', 'z-hidden-by-alias.md' ] custom_list = ['aaa.md', 'azy.md', 'both.md', 'examples.md', 'zeta.md'] alias_dict = { 'aaa': 'alpha', 'y-alias-for-both': 'both', 'alias-for-azy': 'azy', 'z-hidden-by-alias': 'azy' } target = [ 'aaa -> alpha', # shadow the custom file 'alias-for-azy -> azy +', 'alpha', 'azy +', 'bar', 'both *', 'default-only', 'examples *', 'y-alias-for-both -> both *', 'z-hidden-by-alias -> azy +', 'zeta +' ] _helper_assert_list_supported_programs(test_config, examples_list, custom_list, alias_dict, target)
def test_has_custom_entry_when_not_present(): test_config = config.Config(examples_dir=None, custom_dir='customdir', color_config=None, use_color=False, pager_cmd=None, squeeze=False, subs=None) program = 'locate' path = '/Users/tyrion/customdir/locate.md' _helper_assert_path_isfile_not_present( test_config, program, path, 'custom', False, False, )
def test_get_resolved_config_falls_back_to_defaults(): """ When no cli arguments or egrc arguments are present, we should use the raw defaults. """ empty_config = config.get_empty_config() expected = config.Config( examples_dir=config.DEFAULT_EXAMPLES_DIR, custom_dir=config.DEFAULT_CUSTOM_DIR, use_color=config.DEFAULT_USE_COLOR, color_config=config.get_default_color_config(), pager_cmd=config.DEFAULT_PAGER_CMD, squeeze=config.DEFAULT_SQUEEZE, subs=config.get_default_subs(), editor_cmd=config.DEFAULT_EDITOR_CMD, ) _assert_about_get_resolved_config(egrc_config=empty_config, environment_editor_cmd=None, expected_config=expected)
def test_handle_program_no_entries(): """ We should do the right thing if there are no entries for a given program. """ program = 'cp' test_config = config.Config(examples_dir=None, custom_dir=None, color_config=None, use_color=False, pager_cmd=None, squeeze=False, subs=None) with patch('eg.util.get_resolved_program', return_value=program) as mock_resolve_program: with patch('eg.util.has_default_entry_for_program', return_value=False) as mock_has_default: with patch('eg.util.has_custom_entry_for_program', return_value=False) as mock_has_custom: with patch('eg.util.get_contents_from_files' ) as mock_get_contents: with patch( 'eg.util.get_formatted_contents') as mock_format: with patch('eg.util.page_string') as mock_page_string: util.handle_program(program, test_config) mock_resolve_program.assert_called_once_with( program, test_config) mock_has_default.assert_called_once_with( program, test_config) mock_has_custom.assert_called_once_with( program, test_config) # We should have aborted and not called any of the # other methods. assert_equal(mock_get_contents.call_count, 0) assert_equal(mock_format.call_count, 0) assert_equal(mock_page_string.call_count, 0)
def test_list_supported_programs_only_custom(): example_dir = 'example/dir' custom_dir = 'custom/dir' test_config = 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 = util.get_list_of_all_supported_commands(test_config) target = ['awk +', 'bar +', 'xor +'] assert_equal(actual, target)
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 = config.get_config_tuple_from_egrc(PATH_EGRC_NO_DATA) empty_color_config = config.get_empty_color_config() target = config.Config( examples_dir=None, custom_dir=None, color_config=empty_color_config, use_color=None, pager_cmd=None, squeeze=None, subs=None, editor_cmd=None, ) assert actual == target
def _create_dummy_egrc_config(): """ Return a dummy Config object as if constructed from an egrc. """ 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_squeeze = 'egrc_says_squeeze' egrc_subs = ['sub1', 'sub2'] egrc_editor_cmd = 'vim from egrc' result = config.Config( examples_dir=egrc_examples_dir, custom_dir=egrc_custom_dir, color_config=config.get_default_color_config(), use_color=egrc_use_color, pager_cmd=egrc_pager_cmd, squeeze=egrc_squeeze, subs=egrc_subs, editor_cmd=egrc_editor_cmd, ) return result
def _create_config( examples_dir=None, custom_dir=None, color_config=None, use_color=True, pager_cmd=None, editor_cmd=None, squeeze=False, subs=None ): """ Create a config.Config object with default values for expediency in testing. """ return config.Config( examples_dir=examples_dir, custom_dir=custom_dir, color_config=color_config, use_color=use_color, pager_cmd=pager_cmd, editor_cmd=editor_cmd, squeeze=squeeze, subs=subs )