1 2import sys 3from unittest import TestCase 4try: 5 from StringIO import StringIO 6except ImportError: 7 from io import StringIO # doesn't accept 'str' in Py2 8 9from .. import Options 10from ..CmdLine import parse_command_line 11 12 13class CmdLineParserTest(TestCase): 14 def setUp(self): 15 backup = {} 16 for name, value in vars(Options).items(): 17 backup[name] = value 18 self._options_backup = backup 19 20 def tearDown(self): 21 no_value = object() 22 for name, orig_value in self._options_backup.items(): 23 if getattr(Options, name, no_value) != orig_value: 24 setattr(Options, name, orig_value) 25 26 def test_short_options(self): 27 options, sources = parse_command_line([ 28 '-V', '-l', '-+', '-t', '-v', '-v', '-v', '-p', '-D', '-a', '-3', 29 ]) 30 self.assertFalse(sources) 31 self.assertTrue(options.show_version) 32 self.assertTrue(options.use_listing_file) 33 self.assertTrue(options.cplus) 34 self.assertTrue(options.timestamps) 35 self.assertTrue(options.verbose >= 3) 36 self.assertTrue(Options.embed_pos_in_docstring) 37 self.assertFalse(Options.docstrings) 38 self.assertTrue(Options.annotate) 39 self.assertEqual(options.language_level, 3) 40 41 options, sources = parse_command_line([ 42 '-f', '-2', 'source.pyx', 43 ]) 44 self.assertTrue(sources) 45 self.assertTrue(len(sources) == 1) 46 self.assertFalse(options.timestamps) 47 self.assertEqual(options.language_level, 2) 48 49 def test_long_options(self): 50 options, sources = parse_command_line([ 51 '--version', '--create-listing', '--cplus', '--embed', '--timestamps', 52 '--verbose', '--verbose', '--verbose', 53 '--embed-positions', '--no-docstrings', '--annotate', '--lenient', 54 ]) 55 self.assertFalse(sources) 56 self.assertTrue(options.show_version) 57 self.assertTrue(options.use_listing_file) 58 self.assertTrue(options.cplus) 59 self.assertEqual(Options.embed, 'main') 60 self.assertTrue(options.timestamps) 61 self.assertTrue(options.verbose >= 3) 62 self.assertTrue(Options.embed_pos_in_docstring) 63 self.assertFalse(Options.docstrings) 64 self.assertTrue(Options.annotate) 65 self.assertFalse(Options.error_on_unknown_names) 66 self.assertFalse(Options.error_on_uninitialized) 67 68 options, sources = parse_command_line([ 69 '--force', 'source.pyx', 70 ]) 71 self.assertTrue(sources) 72 self.assertTrue(len(sources) == 1) 73 self.assertFalse(options.timestamps) 74 75 def test_options_with_values(self): 76 options, sources = parse_command_line([ 77 '--embed=huhu', 78 '-I/test/include/dir1', '--include-dir=/test/include/dir2', 79 '--include-dir', '/test/include/dir3', 80 '--working=/work/dir', 81 'source.pyx', 82 '--output-file=/output/dir', 83 '--pre-import=/pre/import', 84 '--cleanup=3', 85 '--annotate-coverage=cov.xml', 86 '--gdb-outdir=/gdb/outdir', 87 '--directive=wraparound=false', 88 ]) 89 self.assertEqual(sources, ['source.pyx']) 90 self.assertEqual(Options.embed, 'huhu') 91 self.assertEqual(options.include_path, ['/test/include/dir1', '/test/include/dir2', '/test/include/dir3']) 92 self.assertEqual(options.working_path, '/work/dir') 93 self.assertEqual(options.output_file, '/output/dir') 94 self.assertEqual(Options.pre_import, '/pre/import') 95 self.assertEqual(Options.generate_cleanup_code, 3) 96 self.assertTrue(Options.annotate) 97 self.assertEqual(Options.annotate_coverage_xml, 'cov.xml') 98 self.assertTrue(options.gdb_debug) 99 self.assertEqual(options.output_dir, '/gdb/outdir') 100 101 def test_errors(self): 102 def error(*args): 103 old_stderr = sys.stderr 104 stderr = sys.stderr = StringIO() 105 try: 106 self.assertRaises(SystemExit, parse_command_line, list(args)) 107 finally: 108 sys.stderr = old_stderr 109 self.assertTrue(stderr.getvalue()) 110 111 error('-1') 112 error('-I') 113 error('--version=-a') 114 error('--version=--annotate=true') 115 error('--working') 116 error('--verbose=1') 117 error('--verbose=1') 118 error('--cleanup') 119