Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 1 | """ |
| 2 | Tests of regrtest.py. |
| 3 | """ |
| 4 | |
| 5 | import argparse |
| 6 | import getopt |
| 7 | import unittest |
| 8 | from test import regrtest, support |
| 9 | |
| 10 | def old_parse_args(args): |
| 11 | """Parse arguments as regrtest did strictly prior to 3.4. |
| 12 | |
| 13 | Raises getopt.GetoptError on bad arguments. |
| 14 | """ |
| 15 | return getopt.getopt(args, 'hvqxsoS:rf:lu:t:TD:NLR:FdwWM:nj:Gm:', |
| 16 | ['help', 'verbose', 'verbose2', 'verbose3', 'quiet', |
| 17 | 'exclude', 'single', 'slow', 'randomize', 'fromfile=', 'findleaks', |
| 18 | 'use=', 'threshold=', 'coverdir=', 'nocoverdir', |
| 19 | 'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=', |
| 20 | 'multiprocess=', 'coverage', 'slaveargs=', 'forever', 'debug', |
| 21 | 'start=', 'nowindows', 'header', 'testdir=', 'timeout=', 'wait', |
| 22 | 'failfast', 'match=']) |
| 23 | |
| 24 | class ParseArgsTestCase(unittest.TestCase): |
| 25 | |
Chris Jerdonek | 1573842 | 2013-01-07 17:07:32 -0800 | [diff] [blame] | 26 | """Test that regrtest's parsing code matches the prior getopt behavior.""" |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 27 | |
| 28 | def _parse_args(self, args): |
Chris Jerdonek | 1573842 | 2013-01-07 17:07:32 -0800 | [diff] [blame] | 29 | # This is the same logic as that used in regrtest.main() |
| 30 | parser = regrtest._create_parser() |
| 31 | ns = parser.parse_args(args=args) |
| 32 | opts = regrtest._convert_namespace_to_getopt(ns) |
| 33 | return opts, ns.args |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 34 | |
| 35 | def _check_args(self, args, expected=None): |
| 36 | """ |
| 37 | The expected parameter is for cases when the behavior of the new |
| 38 | parse_args differs from the old (but deliberately so). |
| 39 | """ |
| 40 | if expected is None: |
| 41 | try: |
| 42 | expected = old_parse_args(args) |
| 43 | except getopt.GetoptError: |
| 44 | # Suppress usage string output when an argparse.ArgumentError |
| 45 | # error is raised. |
| 46 | with support.captured_stderr(): |
| 47 | self.assertRaises(SystemExit, self._parse_args, args) |
| 48 | return |
| 49 | # The new parse_args() sorts by long option string. |
| 50 | expected[0].sort() |
| 51 | actual = self._parse_args(args) |
| 52 | self.assertEqual(actual, expected) |
| 53 | |
| 54 | def test_unrecognized_argument(self): |
| 55 | self._check_args(['--xxx']) |
| 56 | |
| 57 | def test_value_not_provided(self): |
| 58 | self._check_args(['--start']) |
| 59 | |
| 60 | def test_short_option(self): |
| 61 | # getopt returns the short option whereas argparse returns the long. |
| 62 | expected = ([('--quiet', '')], []) |
| 63 | self._check_args(['-q'], expected=expected) |
| 64 | |
| 65 | def test_long_option(self): |
| 66 | self._check_args(['--quiet']) |
| 67 | |
| 68 | def test_long_option__partial(self): |
| 69 | self._check_args(['--qui']) |
| 70 | |
| 71 | def test_two_options(self): |
| 72 | self._check_args(['--quiet', '--exclude']) |
| 73 | |
| 74 | def test_option_with_value(self): |
| 75 | self._check_args(['--start', 'foo']) |
| 76 | |
| 77 | def test_option_with_empty_string_value(self): |
| 78 | self._check_args(['--start', '']) |
| 79 | |
| 80 | def test_arg(self): |
| 81 | self._check_args(['foo']) |
| 82 | |
| 83 | def test_option_and_arg(self): |
| 84 | self._check_args(['--quiet', 'foo']) |
| 85 | |
| 86 | def test_fromfile(self): |
| 87 | self._check_args(['--fromfile', 'file']) |
| 88 | |
| 89 | def test_match(self): |
| 90 | self._check_args(['--match', 'pattern']) |
| 91 | |
| 92 | def test_randomize(self): |
| 93 | self._check_args(['--randomize']) |
| 94 | |
| 95 | |
| 96 | def test_main(): |
| 97 | support.run_unittest(__name__) |
| 98 | |
| 99 | if __name__ == '__main__': |
| 100 | test_main() |