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_list_supported_programs_fails_gracefully_if_no_dirs(): test_config = _create_config() actual = util.get_list_of_all_supported_commands(test_config) target = [] assert actual == target
def test_list_supported_programs_fails_gracefully_if_no_dirs(): test_config = _create_config() actual = util.get_list_of_all_supported_commands(test_config) target = [] assert_equal(actual, target)
def _helper_assert_list_supported_programs( config_obj, default_list, custom_list, alias_dict, target_list ): """ config_obj: Config object to be passed to get_list function default_list: the list of default programs custom_list: the list of programs with custom programs alias_dict: dict of aliases target_list: list of string that should be returned """ def give_list(*args, **kwargs): dir_name = args[0] if dir_name == config_obj.custom_dir: return custom_list elif dir_name == config_obj.examples_dir: return default_list else: raise NameError('Not the default or custom dir: ' + dir_name) with patch('os.path.isdir', return_value=True): with patch('os.listdir', side_effect=give_list): with patch('eg.util.get_alias_dict', return_value=alias_dict): actual = util.get_list_of_all_supported_commands(config_obj) assert_equal(actual, target_list)
def _helper_assert_list_supported_programs( config_obj, default_list, custom_list, alias_dict, target_list ): """ config_obj: Config object to be passed to get_list function default_list: the list of default programs custom_list: the list of programs with custom programs alias_dict: dict of aliases target_list: list of string that should be returned """ def give_list(*args, **kwargs): dir_name = args[0] if dir_name == config_obj.custom_dir: return custom_list elif dir_name == config_obj.examples_dir: return default_list else: raise NameError('Not the default or custom dir: ' + dir_name) with patch('os.path.isdir', return_value=True): with patch('os.listdir', side_effect=give_list): with patch('eg.util.get_alias_dict', return_value=alias_dict): actual = util.get_list_of_all_supported_commands(config_obj) assert_equal(actual, target_list)
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_get_list_of_all_supported_commands(tmpdir): dir_example = tmpdir.mkdir('examples') dir_custom = tmpdir.mkdir('custom') config = _create_config( examples_dir=str(dir_example), custom_dir=str(dir_custom), ) expected = [ 'a-only-default', 'b-both *', 'c-only-custom +', 'd-only-custom-nested +', 'e-only-default-nested', 'f-default-custom-nested', 'g-both-different-levels *', 't-a-only-default-alias -> a-only-default', 'u-b-both-alias -> b-both *', 'v-c-only-custom-alias -> c-only-custom +' ] aliases = { 't-a-only-default-alias': 'a-only-default', 'u-b-both-alias': 'b-both', 'v-c-only-custom-alias': 'c-only-custom' } # Make the directory structure we expect. dir_example_nested = dir_example.mkdir('default-nested') dir_custom_nested = dir_custom.mkdir('custom-nested') dir_example.join('a-only-default.md').write('foo') dir_example.join('b-both.md').write('foo') dir_custom.join('b-both.md').write('foo') dir_custom.join('c-only-custom.md').write('foo') dir_custom_nested.join('d-only-custom-nested.md').write('foo') dir_example_nested.join('e-only-default-nested.md').write('foo') dir_example_nested.join('f-default-custom-nested.md').write('foo') dir_example.join('g-both-different-levels.md').write('foo') dir_custom_nested.join('g-both-different-levels.md').write('foo') # Use the 'with' context manager rather than the @decorator, because the # tmpdir fixture doesn't play nice with the decorator. with patch('eg.util.get_alias_dict') as mock_get_alias: mock_get_alias.return_value = aliases actual = util.get_list_of_all_supported_commands(config) assert actual == expected mock_get_alias.assert_called_once_with(config)
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 _show_list_message(resolved_config): """ Show the message for when a user has passed in --list. """ # Show what's available. supported_programs = util.get_list_of_all_supported_commands( resolved_config) msg_line_1 = 'Legend: ' msg_line_2 = (' ' + util.FLAG_ONLY_CUSTOM + ' only custom files') msg_line_3 = (' ' + util.FLAG_CUSTOM_AND_DEFAULT + ' custom and default files') msg_line_4 = ' ' + ' only default files (no symbol)' msg_line_5 = '' msg_line_6 = 'Programs supported by eg: ' preamble = [ msg_line_1, msg_line_2, msg_line_3, msg_line_4, msg_line_5, msg_line_6 ] complete_message = '\n'.join(preamble) complete_message += '\n' + '\n'.join(supported_programs) pydoc.pager(complete_message)
def _show_list_message(resolved_config): """ Show the message for when a user has passed in --list. """ # Show what's available. supported_programs = util.get_list_of_all_supported_commands( resolved_config ) msg_line_1 = 'Legend: ' msg_line_2 = ( ' ' + util.FLAG_ONLY_CUSTOM + ' only custom files' ) msg_line_3 = ( ' ' + util.FLAG_CUSTOM_AND_DEFAULT + ' custom and default files' ) msg_line_4 = ' ' + ' only default files (no symbol)' msg_line_5 = '' msg_line_6 = 'Programs supported by eg: ' preamble = [ msg_line_1, msg_line_2, msg_line_3, msg_line_4, msg_line_5, msg_line_6 ] complete_message = '\n'.join(preamble) complete_message += '\n' + '\n'.join(supported_programs) pydoc.pager(complete_message)
def run_eg(): parser = argparse.ArgumentParser( description='eg provides examples of common command usage.' ) parser.add_argument( '-v', '--version', action='store_true', help='Display version information about eg' ) parser.add_argument( '--config-file', help='Path to the .egrc file, if it is not in the default location.' ) parser.add_argument( '--examples-dir', help='The location to the examples/ dir that ships with eg' ) parser.add_argument( '--custom-dir', help='Path to a directory containing user-defined examples.' ) parser.add_argument( '--pager-cmd', help='String literal that will be invoked to page output.' ) parser.add_argument( '--list', action='store_true', help='Show all the programs with eg entries.' ) parser.add_argument( '--color', action='store_true', dest='use_color', default=None, help='Colorize output.' ) parser.add_argument( '--no-color', action='store_false', dest='use_color', help='Do not colorize output.' ) parser.add_argument( 'program', nargs='?', help='The program for which to display examples.' ) args = parser.parse_args() if len(sys.argv) < 2: parser.print_help() elif not args.version and not args.list and not args.program: print( 'you must specify a program or pass the --list or --version flags' ) else: resolved_config = config.get_resolved_config_items( egrc_path=args.config_file, examples_dir=args.examples_dir, custom_dir=args.custom_dir, use_color=args.use_color, pager_cmd=args.pager_cmd ) if args.list: # Show what's available. supported_programs = util.get_list_of_all_supported_commands( resolved_config ) msg_line_1 = 'Legend: ' msg_line_2 = (' ' + util.FLAG_ONLY_CUSTOM + ' only custom files' ) msg_line_3 = (' ' + util.FLAG_CUSTOM_AND_DEFAULT + ' custom and default files' ) msg_line_4 = ' ' + ' only default files (no symbol)' msg_line_5 = '' msg_line_6 = 'Programs supported by eg: ' preamble = [ msg_line_1, msg_line_2, msg_line_3, msg_line_4, msg_line_5, msg_line_6 ] complete_message = '\n'.join(preamble) complete_message += '\n' + '\n'.join(supported_programs) pydoc.pager(complete_message) elif args.version: print(util.VERSION) else: util.handle_program(args.program, resolved_config)
def run_eg(): parser = argparse.ArgumentParser( description='eg provides examples of common command usage.') parser.add_argument('-v', '--version', action='store_true', help='Display version information about eg') parser.add_argument( '--config-file', help='Path to the .egrc file, if it is not in the default location.') parser.add_argument( '--examples-dir', help='The location to the examples/ dir that ships with eg') parser.add_argument( '--custom-dir', help='Path to a directory containing user-defined examples.') parser.add_argument( '--pager-cmd', help='String literal that will be invoked to page output.') parser.add_argument('--list', action='store_true', help='Show all the programs with eg entries.') parser.add_argument('--color', action='store_true', dest='use_color', default=None, help='Colorize output.') parser.add_argument('--no-color', action='store_false', dest='use_color', help='Do not colorize output.') parser.add_argument('program', nargs='?', help='The program for which to display examples.') args = parser.parse_args() if len(sys.argv) < 2: parser.print_help() elif not args.version and not args.list and not args.program: print( 'you must specify a program or pass the --list or --version flags') else: resolved_config = config.get_resolved_config_items( egrc_path=args.config_file, examples_dir=args.examples_dir, custom_dir=args.custom_dir, use_color=args.use_color, pager_cmd=args.pager_cmd) if args.list: # Show what's available. supported_programs = util.get_list_of_all_supported_commands( resolved_config) msg_line_1 = 'Legend: ' msg_line_2 = (' ' + util.FLAG_ONLY_CUSTOM + ' only custom files') msg_line_3 = (' ' + util.FLAG_CUSTOM_AND_DEFAULT + ' custom and default files') msg_line_4 = ' ' + ' only default files (no symbol)' msg_line_5 = '' msg_line_6 = 'Programs supported by eg: ' preamble = [ msg_line_1, msg_line_2, msg_line_3, msg_line_4, msg_line_5, msg_line_6 ] complete_message = '\n'.join(preamble) complete_message += '\n' + '\n'.join(supported_programs) pydoc.pager(complete_message) elif args.version: print(util.VERSION) else: util.handle_program(args.program, resolved_config)