def test_filtered(self): stub_stdouts(self) parser = ArgumentParser() with self.assertRaises(SystemExit): parser.error('some random other error') self.assertIn('some random other error', sys.stderr.getvalue())
def test_integration_invalid_default_value(self): argparser = ArgumentParser(prog='prog', add_help=False) with self.assertRaises(ValueError): argparser.add_argument('-p', '--params', action=StoreCommaDelimitedList, default='123')
def test_integration_set_limit(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', action=StoreDelimitedListBase, maxlen=1) parsed, extras = argparser.parse_known_args(['-p', '3,4,5']) self.assertEqual(parsed.params, ['3'])
def test_integration_optional_kwargs(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', action=StoreDelimitedListBase, sep='.') parsed, extras = argparser.parse_known_args(['-p', '3,4.5']) self.assertEqual(parsed.params, ['3,4', '5'])
def test_integration_set_limit(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', action=StoreDelimitedListBase, maxlen=1) parsed, extras = argparser.parse_known_args(['-p', '3,4,5']) self.assertEqual(parsed.params, ['3'])
def test_integration_optional_kwargs(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', action=StoreDelimitedListBase, sep='.') parsed, extras = argparser.parse_known_args(['-p', '3,4.5']) self.assertEqual(parsed.params, ['3,4', '5'])
def test_integration_optional(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', action=StoreCommaDelimitedList, required=False) parsed, extras = argparser.parse_known_args(['-p', '3,4']) self.assertEqual(parsed.params, ['3', '4']) parsed, extras = argparser.parse_known_args() self.assertEqual(parsed.params, None)
def argparser_factory(self): argparser = ArgumentParser( prog=self.prog, description=self.description, ) setattr(argparser, ATTR_ROOT_PKG, self.package_name) return argparser
def argparser_factory(self): """ Produces argparser for this type of Runtime. """ return ArgumentParser( prog=self.prog, description=self.__doc__, add_help=False, )
def test_integration_edge_cases(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', action=StoreCommaDelimitedList, default=['1', '2']) parsed, extras = argparser.parse_known_args(['--params=1,2']) self.assertEqual(parsed.params, ['1', '2']) parsed, extras = argparser.parse_known_args(['--params=8', '-p', '3']) self.assertEqual(parsed.params, ['8', '3']) parsed, extras = argparser.parse_known_args(['--params', '']) self.assertEqual(parsed.params, []) parsed, extras = argparser.parse_known_args(['--params=']) self.assertEqual(parsed.params, []) parsed, extras = argparser.parse_known_args(['--params=,']) self.assertEqual(parsed.params, ['']) parsed, extras = argparser.parse_known_args(['--params=1,']) self.assertEqual(parsed.params, ['1'])
def test_integration_edge_cases(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', action=StoreCommaDelimitedList, default=['1', '2']) parsed, extras = argparser.parse_known_args(['--params=1,2']) self.assertEqual(parsed.params, ['1', '2']) parsed, extras = argparser.parse_known_args(['--params=8', '-p', '3']) self.assertEqual(parsed.params, ['8', '3']) parsed, extras = argparser.parse_known_args(['--params', '']) self.assertEqual(parsed.params, []) parsed, extras = argparser.parse_known_args(['--params=']) self.assertEqual(parsed.params, []) parsed, extras = argparser.parse_known_args(['--params=,']) self.assertEqual(parsed.params, ['']) parsed, extras = argparser.parse_known_args(['--params=1,']) self.assertEqual(parsed.params, ['1'])
def test_integration_optional(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', action=StoreCommaDelimitedList, required=False) parsed, extras = argparser.parse_known_args(['-p', '3,4']) self.assertEqual(parsed.params, ['3', '4']) parsed, extras = argparser.parse_known_args() self.assertEqual(parsed.params, None)
def test_integration(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', action=StoreCommaDelimitedList, default=('1', '2',)) parsed, extras = argparser.parse_known_args(['-p', '3,4']) self.assertEqual(parsed.params, ['3', '4']) parsed, extras = argparser.parse_known_args(['-p', '34']) self.assertEqual(parsed.params, ['34']) parsed, extras = argparser.parse_known_args([]) self.assertEqual(parsed.params, ('1', '2'))
def test_integration(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', action=StoreCommaDelimitedList, default=( '1', '2', )) parsed, extras = argparser.parse_known_args(['-p', '3,4']) self.assertEqual(parsed.params, ['3', '4']) parsed, extras = argparser.parse_known_args(['-p', '34']) self.assertEqual(parsed.params, ['34']) parsed, extras = argparser.parse_known_args([]) self.assertEqual(parsed.params, ('1', '2'))
def test_integration_invalid_default_value(self): argparser = ArgumentParser(prog='prog', add_help=False) with self.assertRaises(ValueError): argparser.add_argument( '-p', '--params', action=StoreCommaDelimitedList, default='123')
def test_error(self): stub_stdouts(self) parser = ArgumentParser() parser.error(_('too few arguments')) self.assertEqual('', sys.stdout.getvalue()) self.assertEqual('', sys.stderr.getvalue())
def setUp(self): self.parser = ArgumentParser() init_argparser_common(self.parser)
def test_deprecation(self): import warnings argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-n', '--normal', action='store') argparser.add_argument( '-d', '--deprecated', action='store', deprecation=True) argparser.add_argument( '--bye', action=StoreDelimitedListBase, deprecation='bye') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') # test that they store stuff args = argparser.parse_args(['-n', 'hello']) self.assertEqual(args.normal, 'hello') args = argparser.parse_args(['-d', 'hello']) self.assertEqual(args.deprecated, 'hello') args = argparser.parse_args(['--deprecated', 'hello']) self.assertEqual(args.deprecated, 'hello') args = argparser.parse_args(['--bye', 'hello,goodbye']) self.assertEqual(args.bye, ['hello', 'goodbye']) # and the warnings are triggered self.assertEqual( "option '-d' is deprecated", str(w[0].message)) self.assertEqual( "option '--deprecated' is deprecated", str(w[1].message)) self.assertEqual( "option '--bye' is deprecated: bye", str(w[2].message)) stream = StringIO() argparser.print_help(file=stream) # deprecated options are not visible on help self.assertNotIn("--deprecated", stream.getvalue()) self.assertNotIn("--bye", stream.getvalue()) self.assertIn("--normal", stream.getvalue())
def test_integration_choices_in_list(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument( '-p', '--params', choices=['1', '2', '3'], action=StoreDelimitedListBase) parsed, extras = argparser.parse_known_args(['-p', '3']) self.assertEqual(parsed.params, ['3']) parsed, extras = argparser.parse_known_args(['-p', '3,2']) self.assertEqual(parsed.params, ['3', '2']) parsed, extras = argparser.parse_known_args(['-p', '3,2,1']) self.assertEqual(parsed.params, ['3', '2', '1']) parsed, extras = argparser.parse_known_args(['-p', '3,3,3']) self.assertEqual(parsed.params, ['3', '3', '3']) stub_stdouts(self) with self.assertRaises(SystemExit): argparser.parse_known_args(['-p', '3,2,1,0']) self.assertIn("(choose from '1', '2', '3')", sys.stderr.getvalue()) with self.assertRaises(SystemExit): argparser.parse_known_args(['-p', '0']) argparser.add_argument( '--dot', choices=['a', 'b', 'c'], action=StoreDelimitedListBase, sep='.') parsed, extras = argparser.parse_known_args(['--dot', 'a.b.c']) self.assertEqual(parsed.dot, ['a', 'b', 'c']) with self.assertRaises(SystemExit): argparser.parse_known_args(['--dot', 'a,b,c'])
def test_integration_choices_in_list(self): argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-p', '--params', choices=['1', '2', '3'], action=StoreDelimitedListBase) parsed, extras = argparser.parse_known_args(['-p', '3']) self.assertEqual(parsed.params, ['3']) parsed, extras = argparser.parse_known_args(['-p', '3,2']) self.assertEqual(parsed.params, ['3', '2']) parsed, extras = argparser.parse_known_args(['-p', '3,2,1']) self.assertEqual(parsed.params, ['3', '2', '1']) parsed, extras = argparser.parse_known_args(['-p', '3,3,3']) self.assertEqual(parsed.params, ['3', '3', '3']) stub_stdouts(self) with self.assertRaises(SystemExit): argparser.parse_known_args(['-p', '3,2,1,0']) self.assertIn("(choose from '1', '2', '3')", sys.stderr.getvalue()) with self.assertRaises(SystemExit): argparser.parse_known_args(['-p', '0']) argparser.add_argument('--dot', choices=['a', 'b', 'c'], action=StoreDelimitedListBase, sep='.') parsed, extras = argparser.parse_known_args(['--dot', 'a.b.c']) self.assertEqual(parsed.dot, ['a', 'b', 'c']) with self.assertRaises(SystemExit): argparser.parse_known_args(['--dot', 'a,b,c'])
def test_deprecation(self): import warnings argparser = ArgumentParser(prog='prog', add_help=False) argparser.add_argument('-n', '--normal', action='store') argparser.add_argument('-d', '--deprecated', action='store', deprecation=True) argparser.add_argument('--bye', action=StoreDelimitedListBase, deprecation='bye') with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') # test that they store stuff args = argparser.parse_args(['-n', 'hello']) self.assertEqual(args.normal, 'hello') args = argparser.parse_args(['-d', 'hello']) self.assertEqual(args.deprecated, 'hello') args = argparser.parse_args(['--deprecated', 'hello']) self.assertEqual(args.deprecated, 'hello') args = argparser.parse_args(['--bye', 'hello,goodbye']) self.assertEqual(args.bye, ['hello', 'goodbye']) # and the warnings are triggered self.assertEqual("option '-d' is deprecated", str(w[0].message)) self.assertEqual("option '--deprecated' is deprecated", str(w[1].message)) self.assertEqual("option '--bye' is deprecated: bye", str(w[2].message)) stream = StringIO() argparser.print_help(file=stream) # deprecated options are not visible on help self.assertNotIn("--deprecated", stream.getvalue()) self.assertNotIn("--bye", stream.getvalue()) self.assertIn("--normal", stream.getvalue())
class TestCommonArgparser(unittest.TestCase): """ Test out some non-trivial parsing options in the common parser """ def setUp(self): self.parser = ArgumentParser() init_argparser_common(self.parser) def parse(self, args): return self.parser.parse_known_args(args)[0] def test_parse_default_coverage_type(self): self.assertIs( DEFAULT_COVER_REPORT_TYPE_OPTIONS, self.parse([]).cover_report_types, ) # default always show this self.assertIn('text', self.parse([]).cover_report_types) # check for deprecated option self.assertIsNone(self.parse([]).coverage_type) def test_parse_default_cover_report_type_legacy(self): import warnings with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') self.assertEqual( 'html', self.parse(['--coverage-type=html']).coverage_type) self.assertIn("will be removed by", str(w[0].message)) # a somewhat more complete test config = {} driver = cli.KarmaDriver() with pretty_logging( logger='calmjs.dev', stream=mocks.StringIO()) as log: # emulate the parsing into a spec spec = Spec(**vars(self.parse(['--coverage-type=html']))) driver._apply_coverage_reporters(spec, config) self.assertIn("WARNING", log.getvalue()) self.assertIn("'coverage_type' is deprecated", log.getvalue()) self.assertEqual({ 'type': 'html', 'dir': realpath('coverage'), }, config['coverageReporter']) def test_parse_default_cover_report_type_legacy_default(self): import warnings with warnings.catch_warnings(record=True): warnings.simplefilter('always') self.assertEqual( 'default', self.parse(['--coverage-type=default']).coverage_type) # a somewhat more complete test config = {} driver = cli.KarmaDriver() with pretty_logging( logger='calmjs.dev', stream=mocks.StringIO()) as log: # emulate the parsing into a spec spec = Spec(**vars(self.parse(['--coverage-type=default']))) driver._apply_coverage_reporters(spec, config) self.assertNotIn("WARNING", log.getvalue()) self.assertNotIn("'coverage_type' is deprecated", log.getvalue()) # the default value actually is supported self.assertEqual( sorted(DEFAULT_COVER_REPORT_TYPE_OPTIONS), sorted([ r['type'] for r in config['coverageReporter']['reporters']]), ) def test_parse_default_cover_report_type_specified(self): self.assertEqual( ['html'], self.parse(['--cover-report-type=html']).cover_report_types) def test_parse_default_cover_report_type_specified_multiple(self): self.assertEqual( ['html', 'text'], self.parse(['--cover-report-type=html,text']).cover_report_types) def test_parse_default_cover_report_type_bad(self): stub_stdouts(self) with self.assertRaises(SystemExit): self.parse(['--cover-report-type=bad'])