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', squeeze='squeeze from fake egrc', subs=['foo', 'bar'] ) egrc_path = 'test/path/to/egrc' with patch( 'eg.config.get_config_tuple_from_egrc', return_value=def_config ) as mocked_get_config_from_egrc: config.get_resolved_config_items( egrc_path, None, None, None, None, None, debug=False ) mocked_get_config_from_egrc.assert_called_once_with(egrc_path)
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_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_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_config_returns_defaults_if_all_none_and_no_egrc(): with patch('os.path.isfile', return_value=False): resolved_config = config.get_resolved_config_items(None, None, None, None, None, debug=False) default_color_config = config.get_default_color_config() assert_equal(resolved_config.examples_dir, config.DEFAULT_EXAMPLES_DIR) assert_equal(resolved_config.custom_dir, None) assert_equal(resolved_config.color_config, default_color_config) assert_equal(resolved_config.use_color, config.DEFAULT_USE_COLOR) assert_equal(resolved_config.pager_cmd, config.DEFAULT_PAGER_CMD)
def test_config_returns_defaults_if_all_none_and_no_egrc(): with patch('os.path.isfile', return_value=False): resolved_config = config.get_resolved_config_items( None, None, None, None, None, debug=False ) default_color_config = config.get_default_color_config() assert_equal(resolved_config.examples_dir, config.DEFAULT_EXAMPLES_DIR) assert_equal(resolved_config.custom_dir, None) assert_equal(resolved_config.color_config, default_color_config) assert_equal(resolved_config.use_color, config.DEFAULT_USE_COLOR) assert_equal(resolved_config.pager_cmd, config.DEFAULT_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' command_line_squeeze = 'command_line_wants_to_squeeze' 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_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, squeeze=egrc_squeeze, subs=egrc_subs ) 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, command_line_squeeze, 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) assert_equal(actual.squeeze, command_line_squeeze)
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' 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, squeeze=test_squeeze, subs=test_subs ) 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, 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.squeeze, test_squeeze) assert_equal(resolved_config.subs, test_subs)
def test_config_returns_defaults_if_all_none_and_no_egrc(): """No config file and no egrc should return default values.""" with patch('os.path.isfile', return_value=False): resolved_config = config.get_resolved_config_items(None, None, None, None, None, None, debug=False) default_color_config = config.get_default_color_config() assert_equal(resolved_config.examples_dir, config.DEFAULT_EXAMPLES_DIR) assert_equal(resolved_config.custom_dir, None) assert_equal(resolved_config.color_config, default_color_config) assert_equal(resolved_config.use_color, config.DEFAULT_USE_COLOR) assert_equal(resolved_config.pager_cmd, config.DEFAULT_PAGER_CMD) assert_equal(resolved_config.squeeze, config.DEFAULT_SQUEEZE) assert_equal(resolved_config.subs, config.get_default_subs())
def test_config_returns_defaults_if_all_none_and_no_egrc(): """No config file and no egrc should return default values.""" with patch('os.path.isfile', return_value=False): resolved_config = config.get_resolved_config_items( None, None, None, None, None, None, debug=False ) default_color_config = config.get_default_color_config() assert_equal(resolved_config.examples_dir, config.DEFAULT_EXAMPLES_DIR) assert_equal(resolved_config.custom_dir, None) assert_equal(resolved_config.color_config, default_color_config) assert_equal(resolved_config.use_color, config.DEFAULT_USE_COLOR) assert_equal(resolved_config.pager_cmd, config.DEFAULT_PAGER_CMD) assert_equal(resolved_config.squeeze, config.DEFAULT_SQUEEZE) assert_equal(resolved_config.subs, config.get_default_subs())
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 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)
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)