def test_handle_program_no_entries( mock_resolve_program, mock_get_contents, mock_format, mock_page_string, ): """ We should do the right thing if there are no entries for a given program. """ program = 'cp' test_config = _create_config() mock_resolve_program.return_value = program util.handle_program(program, test_config) mock_resolve_program.assert_called_once_with( program, test_config ) # We should have aborted and not called any of the # other methods. assert mock_get_contents.call_count == 0 assert mock_format.call_count == 0 assert mock_page_string.call_count == 0
def run_eg(): args = _parse_arguments() resolved_config = config.get_resolved_config( 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, squeeze=args.squeeze) if args.list: _show_list_message(resolved_config) elif args.version: _show_version() elif args.edit: if not resolved_config.editor_cmd: _handle_no_editor() else: util.edit_custom_examples(args.program, resolved_config) elif args.labels: _show_labels(args.program) elif args.add_label: _add_label(args.program, args.add_label) elif args.find_file: _find_file(args.find_file) elif args.remove_label: _remove_label(args.program, args.remove_label) else: util.handle_program(args.program, resolved_config)
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_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_handle_program_no_entries(): """ We should do the right thing if there are no entries for a given program. """ program = 'cp' test_config = _create_config() 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_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 run_eg(): args = _parse_arguments() resolved_config = config.get_resolved_config( 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, squeeze=args.squeeze ) if args.list: _show_list_message(resolved_config) elif args.version: _show_version() elif args.edit: if not resolved_config.editor_cmd: _handle_no_editor() else: util.edit_custom_examples(args.program, resolved_config) 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( '-f', '--config-file', help='Path to the .egrc file, if it is not in the default location.') parser.add_argument( '-e', '--examples-dir', help='The location to the examples/ dir that ships with eg') parser.add_argument( '-c', '--custom-dir', help='Path to a directory containing user-defined examples.') parser.add_argument( '-p', '--pager-cmd', help='String literal that will be invoked to page output.') parser.add_argument('-l', '--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('-s', '--squeeze', action='store_true', default=None, help='Show fewer blank lines in 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: _handle_insufficient_args() 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, squeeze=args.squeeze) if args.list: _show_list_message(resolved_config) elif args.version: _show_version() else: util.handle_program(args.program, resolved_config)
def test_handle_program_finds_paths_and_calls_open_pager_with_alias( mock_page, mock_format, mock_get_paths, mock_get_contents, mock_resolve, ): """ If there are entries for the program, handle_program needs to get the paths, get the contents, format the contents, and page the resulting string. """ alias_for_program = 'link' resolved_program = 'ln' examples_dir = 'test-eg-dir' custom_dir = 'test-custom-dir' color_config = None use_color = False pager_cmd = 'foo bar' squeeze = False subs = ['foo', 'bar'] file_contents = 'I am the contents of ln.md.' formatted_contents = 'and I am the formatted contents of ln.md.' test_config = _create_config( examples_dir=examples_dir, custom_dir=custom_dir, color_config=color_config, use_color=use_color, pager_cmd=pager_cmd, squeeze=squeeze, subs=subs ) default_paths = ['test-eg-dir/ln.md'] custom_paths = ['test-custom-dir/ln.md'] def return_correct_path(*args, **kwargs): program_param = args[0] dir_param = args[1] if program_param != resolved_program: raise NameError( 'expected ' + resolved_program + ', got ' + program_param ) if dir_param == examples_dir: return default_paths elif dir_param == custom_dir: return custom_paths else: raise NameError( 'got ' + dir_param + ', expected ' + examples_dir + ' or ' + custom_dir) mock_format.return_value = formatted_contents mock_get_paths.side_effect = return_correct_path mock_get_contents.return_value = file_contents mock_resolve.return_value = resolved_program util.handle_program( alias_for_program, test_config ) mock_resolve.assert_called_once_with( alias_for_program, test_config ) mock_get_paths.assert_any_call( resolved_program, examples_dir ) mock_get_paths.assert_any_call( resolved_program, custom_dir, ) mock_get_contents.assert_called_once_with( custom_paths[0], default_paths[0] ) mock_format.assert_called_once_with( file_contents, use_color=test_config.use_color, color_config=test_config.color_config, squeeze=test_config.squeeze, subs=test_config.subs ) mock_page.assert_called_once_with( formatted_contents, test_config.pager_cmd )
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 test_handle_program_finds_paths_and_calls_open_pager_with_alias(): """ If there are entries for the program, handle_program needs to get the paths, get the contents, format the contents, and page the resulting string. """ alias_for_program = 'link' resolved_program = 'ln' examples_dir = 'test-eg-dir' custom_dir = 'test-custom-dir' color_config = None use_color = False pager_cmd = 'foo bar' squeeze = False subs = ['foo', 'bar'] file_contents = 'I am the contents of ln.md.' formatted_contents = 'and I am the formatted contents of ln.md.' test_config = config.Config( examples_dir=examples_dir, custom_dir=custom_dir, color_config=color_config, use_color=use_color, pager_cmd=pager_cmd, squeeze=squeeze, subs=subs ) default_path = 'test-eg-dir/ln.md' custom_path = 'test-custom-dir/ln.md' def return_correct_path(*args, **kwargs): program_param = args[0] dir_param = args[1] if program_param != resolved_program: raise NameError( 'expected ' + resolved_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.get_resolved_program', return_value=resolved_program ) as mock_resolve: 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.get_contents_from_files', return_value=file_contents ) as mock_get_contents: with patch( 'eg.util.get_file_path_for_program', side_effect=return_correct_path ) as mock_get_file: with patch( 'eg.util.get_formatted_contents', return_value=formatted_contents ) as mock_format: with patch('eg.util.page_string') as mock_page: util.handle_program( alias_for_program, test_config ) mock_resolve.assert_called_once_with( alias_for_program, test_config ) mock_has_default.assert_called_once_with( resolved_program, test_config ) mock_has_custom.assert_called_once_with( resolved_program, test_config ) mock_get_file.assert_any_call( resolved_program, examples_dir ) mock_get_file.assert_any_call( resolved_program, custom_dir, ) mock_get_contents.assert_called_once_with( default_path, custom_path ) mock_format.assert_called_once_with( file_contents, use_color=test_config.use_color, color_config=test_config.color_config, squeeze=test_config.squeeze, subs=test_config.subs ) mock_page.assert_called_once_with( formatted_contents, test_config.pager_cmd )
def test_handle_program_finds_paths_and_calls_open_pager_with_alias(): """ If there are entries for the program, handle_program needs to get the paths, get the contents, format the contents, and page the resulting string. """ alias_for_program = 'link' resolved_program = 'ln' examples_dir = 'test-eg-dir' custom_dir = 'test-custom-dir' color_config = None use_color = False pager_cmd = 'foo bar' squeeze = False subs = ['foo', 'bar'] file_contents = 'I am the contents of ln.md.' formatted_contents = 'and I am the formatted contents of ln.md.' test_config = _create_config( examples_dir=examples_dir, custom_dir=custom_dir, color_config=color_config, use_color=use_color, pager_cmd=pager_cmd, squeeze=squeeze, subs=subs ) default_path = 'test-eg-dir/ln.md' custom_path = 'test-custom-dir/ln.md' def return_correct_path(*args, **kwargs): program_param = args[0] dir_param = args[1] if program_param != resolved_program: raise NameError( 'expected ' + resolved_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.get_resolved_program', return_value=resolved_program ) as mock_resolve: 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.get_contents_from_files', return_value=file_contents ) as mock_get_contents: with patch( 'eg.util.get_file_path_for_program', side_effect=return_correct_path ) as mock_get_file: with patch( 'eg.util.get_formatted_contents', return_value=formatted_contents ) as mock_format: with patch('eg.util.page_string') as mock_page: util.handle_program( alias_for_program, test_config ) mock_resolve.assert_called_once_with( alias_for_program, test_config ) mock_has_default.assert_called_once_with( resolved_program, test_config ) mock_has_custom.assert_called_once_with( resolved_program, test_config ) mock_get_file.assert_any_call( resolved_program, examples_dir ) mock_get_file.assert_any_call( resolved_program, custom_dir, ) mock_get_contents.assert_called_once_with( default_path, custom_path ) mock_format.assert_called_once_with( file_contents, use_color=test_config.use_color, color_config=test_config.color_config, squeeze=test_config.squeeze, subs=test_config.subs ) mock_page.assert_called_once_with( formatted_contents, test_config.pager_cmd )
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( '-f', '--config-file', help='Path to the .egrc file, if it is not in the default location.' ) parser.add_argument( '-e', '--examples-dir', help='The location to the examples/ dir that ships with eg' ) parser.add_argument( '-c', '--custom-dir', help='Path to a directory containing user-defined examples.' ) parser.add_argument( '-p', '--pager-cmd', help='String literal that will be invoked to page output.' ) parser.add_argument( '-l', '--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( '-s', '--squeeze', action='store_true', default=None, help='Show fewer blank lines in 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: _handle_insufficient_args() 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, squeeze=args.squeeze ) if args.list: _show_list_message(resolved_config) elif args.version: _show_version() else: util.handle_program(args.program, resolved_config)