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_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_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(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_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_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_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'])
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'])