TestCmdLine.py 4.3 KB

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