Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 1 | """ |
| 2 | Tests of regrtest.py. |
| 3 | """ |
| 4 | |
| 5 | import argparse |
Charles-François Natali | 833bf1f | 2013-09-08 12:27:33 +0200 | [diff] [blame] | 6 | import faulthandler |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 7 | import getopt |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 8 | import os.path |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 9 | import unittest |
| 10 | from test import regrtest, support |
| 11 | |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 12 | class ParseArgsTestCase(unittest.TestCase): |
| 13 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 14 | """Test regrtest's argument parsing.""" |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 15 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 16 | def checkError(self, args, msg): |
| 17 | with support.captured_stderr() as err, self.assertRaises(SystemExit): |
| 18 | regrtest._parse_args(args) |
| 19 | self.assertIn(msg, err.getvalue()) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 20 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 21 | def test_help(self): |
| 22 | for opt in '-h', '--help': |
| 23 | with self.subTest(opt=opt): |
| 24 | with support.captured_stdout() as out, \ |
| 25 | self.assertRaises(SystemExit): |
| 26 | regrtest._parse_args([opt]) |
| 27 | self.assertIn('Run Python regression tests.', out.getvalue()) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 28 | |
Charles-François Natali | 833bf1f | 2013-09-08 12:27:33 +0200 | [diff] [blame] | 29 | @unittest.skipUnless(hasattr(faulthandler, 'dump_traceback_later'), |
| 30 | "faulthandler.dump_traceback_later() required") |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 31 | def test_timeout(self): |
| 32 | ns = regrtest._parse_args(['--timeout', '4.2']) |
| 33 | self.assertEqual(ns.timeout, 4.2) |
| 34 | self.checkError(['--timeout'], 'expected one argument') |
| 35 | self.checkError(['--timeout', 'foo'], 'invalid float value') |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 36 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 37 | def test_wait(self): |
| 38 | ns = regrtest._parse_args(['--wait']) |
| 39 | self.assertTrue(ns.wait) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 40 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 41 | def test_slaveargs(self): |
| 42 | ns = regrtest._parse_args(['--slaveargs', '[[], {}]']) |
| 43 | self.assertEqual(ns.slaveargs, '[[], {}]') |
| 44 | self.checkError(['--slaveargs'], 'expected one argument') |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 45 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 46 | def test_start(self): |
| 47 | for opt in '-S', '--start': |
| 48 | with self.subTest(opt=opt): |
| 49 | ns = regrtest._parse_args([opt, 'foo']) |
| 50 | self.assertEqual(ns.start, 'foo') |
| 51 | self.checkError([opt], 'expected one argument') |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 52 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 53 | def test_verbose(self): |
| 54 | ns = regrtest._parse_args(['-v']) |
| 55 | self.assertEqual(ns.verbose, 1) |
| 56 | ns = regrtest._parse_args(['-vvv']) |
| 57 | self.assertEqual(ns.verbose, 3) |
| 58 | ns = regrtest._parse_args(['--verbose']) |
| 59 | self.assertEqual(ns.verbose, 1) |
| 60 | ns = regrtest._parse_args(['--verbose'] * 3) |
| 61 | self.assertEqual(ns.verbose, 3) |
| 62 | ns = regrtest._parse_args([]) |
| 63 | self.assertEqual(ns.verbose, 0) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 64 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 65 | def test_verbose2(self): |
| 66 | for opt in '-w', '--verbose2': |
| 67 | with self.subTest(opt=opt): |
| 68 | ns = regrtest._parse_args([opt]) |
| 69 | self.assertTrue(ns.verbose2) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 70 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 71 | def test_verbose3(self): |
| 72 | for opt in '-W', '--verbose3': |
| 73 | with self.subTest(opt=opt): |
| 74 | ns = regrtest._parse_args([opt]) |
| 75 | self.assertTrue(ns.verbose3) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 76 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 77 | def test_quiet(self): |
| 78 | for opt in '-q', '--quiet': |
| 79 | with self.subTest(opt=opt): |
| 80 | ns = regrtest._parse_args([opt]) |
| 81 | self.assertTrue(ns.quiet) |
| 82 | self.assertEqual(ns.verbose, 0) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 83 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 84 | def test_slow(self): |
| 85 | for opt in '-o', '--slow': |
| 86 | with self.subTest(opt=opt): |
| 87 | ns = regrtest._parse_args([opt]) |
| 88 | self.assertTrue(ns.print_slow) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 89 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 90 | def test_header(self): |
| 91 | ns = regrtest._parse_args(['--header']) |
| 92 | self.assertTrue(ns.header) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 93 | |
| 94 | def test_randomize(self): |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 95 | for opt in '-r', '--randomize': |
| 96 | with self.subTest(opt=opt): |
| 97 | ns = regrtest._parse_args([opt]) |
| 98 | self.assertTrue(ns.randomize) |
| 99 | |
| 100 | def test_randseed(self): |
| 101 | ns = regrtest._parse_args(['--randseed', '12345']) |
| 102 | self.assertEqual(ns.random_seed, 12345) |
| 103 | self.assertTrue(ns.randomize) |
| 104 | self.checkError(['--randseed'], 'expected one argument') |
| 105 | self.checkError(['--randseed', 'foo'], 'invalid int value') |
| 106 | |
| 107 | def test_fromfile(self): |
| 108 | for opt in '-f', '--fromfile': |
| 109 | with self.subTest(opt=opt): |
| 110 | ns = regrtest._parse_args([opt, 'foo']) |
| 111 | self.assertEqual(ns.fromfile, 'foo') |
| 112 | self.checkError([opt], 'expected one argument') |
| 113 | self.checkError([opt, 'foo', '-s'], "don't go together") |
| 114 | |
| 115 | def test_exclude(self): |
| 116 | for opt in '-x', '--exclude': |
| 117 | with self.subTest(opt=opt): |
| 118 | ns = regrtest._parse_args([opt]) |
| 119 | self.assertTrue(ns.exclude) |
| 120 | |
| 121 | def test_single(self): |
| 122 | for opt in '-s', '--single': |
| 123 | with self.subTest(opt=opt): |
| 124 | ns = regrtest._parse_args([opt]) |
| 125 | self.assertTrue(ns.single) |
| 126 | self.checkError([opt, '-f', 'foo'], "don't go together") |
| 127 | |
| 128 | def test_match(self): |
| 129 | for opt in '-m', '--match': |
| 130 | with self.subTest(opt=opt): |
| 131 | ns = regrtest._parse_args([opt, 'pattern']) |
| 132 | self.assertEqual(ns.match_tests, 'pattern') |
| 133 | self.checkError([opt], 'expected one argument') |
| 134 | |
| 135 | def test_failfast(self): |
| 136 | for opt in '-G', '--failfast': |
| 137 | with self.subTest(opt=opt): |
| 138 | ns = regrtest._parse_args([opt, '-v']) |
| 139 | self.assertTrue(ns.failfast) |
| 140 | ns = regrtest._parse_args([opt, '-W']) |
| 141 | self.assertTrue(ns.failfast) |
| 142 | self.checkError([opt], '-G/--failfast needs either -v or -W') |
| 143 | |
| 144 | def test_use(self): |
| 145 | for opt in '-u', '--use': |
| 146 | with self.subTest(opt=opt): |
| 147 | ns = regrtest._parse_args([opt, 'gui,network']) |
| 148 | self.assertEqual(ns.use_resources, ['gui', 'network']) |
| 149 | ns = regrtest._parse_args([opt, 'gui,none,network']) |
| 150 | self.assertEqual(ns.use_resources, ['network']) |
| 151 | expected = list(regrtest.RESOURCE_NAMES) |
| 152 | expected.remove('gui') |
| 153 | ns = regrtest._parse_args([opt, 'all,-gui']) |
| 154 | self.assertEqual(ns.use_resources, expected) |
| 155 | self.checkError([opt], 'expected one argument') |
| 156 | self.checkError([opt, 'foo'], 'invalid resource') |
| 157 | |
| 158 | def test_memlimit(self): |
| 159 | for opt in '-M', '--memlimit': |
| 160 | with self.subTest(opt=opt): |
| 161 | ns = regrtest._parse_args([opt, '4G']) |
| 162 | self.assertEqual(ns.memlimit, '4G') |
| 163 | self.checkError([opt], 'expected one argument') |
| 164 | |
| 165 | def test_testdir(self): |
| 166 | ns = regrtest._parse_args(['--testdir', 'foo']) |
| 167 | self.assertEqual(ns.testdir, os.path.join(support.SAVEDCWD, 'foo')) |
| 168 | self.checkError(['--testdir'], 'expected one argument') |
| 169 | |
| 170 | def test_findleaks(self): |
| 171 | for opt in '-l', '--findleaks': |
| 172 | with self.subTest(opt=opt): |
| 173 | ns = regrtest._parse_args([opt]) |
| 174 | self.assertTrue(ns.findleaks) |
| 175 | |
| 176 | def test_findleaks(self): |
| 177 | for opt in '-L', '--runleaks': |
| 178 | with self.subTest(opt=opt): |
| 179 | ns = regrtest._parse_args([opt]) |
| 180 | self.assertTrue(ns.runleaks) |
| 181 | |
| 182 | def test_findleaks(self): |
| 183 | for opt in '-R', '--huntrleaks': |
| 184 | with self.subTest(opt=opt): |
| 185 | ns = regrtest._parse_args([opt, ':']) |
| 186 | self.assertEqual(ns.huntrleaks, (5, 4, 'reflog.txt')) |
| 187 | ns = regrtest._parse_args([opt, '6:']) |
| 188 | self.assertEqual(ns.huntrleaks, (6, 4, 'reflog.txt')) |
| 189 | ns = regrtest._parse_args([opt, ':3']) |
| 190 | self.assertEqual(ns.huntrleaks, (5, 3, 'reflog.txt')) |
| 191 | ns = regrtest._parse_args([opt, '6:3:leaks.log']) |
| 192 | self.assertEqual(ns.huntrleaks, (6, 3, 'leaks.log')) |
| 193 | self.checkError([opt], 'expected one argument') |
| 194 | self.checkError([opt, '6'], |
| 195 | 'needs 2 or 3 colon-separated arguments') |
| 196 | self.checkError([opt, 'foo:'], 'invalid huntrleaks value') |
| 197 | self.checkError([opt, '6:foo'], 'invalid huntrleaks value') |
| 198 | |
| 199 | def test_multiprocess(self): |
| 200 | for opt in '-j', '--multiprocess': |
| 201 | with self.subTest(opt=opt): |
| 202 | ns = regrtest._parse_args([opt, '2']) |
| 203 | self.assertEqual(ns.use_mp, 2) |
| 204 | self.checkError([opt], 'expected one argument') |
| 205 | self.checkError([opt, 'foo'], 'invalid int value') |
| 206 | self.checkError([opt, '2', '-T'], "don't go together") |
| 207 | self.checkError([opt, '2', '-l'], "don't go together") |
| 208 | self.checkError([opt, '2', '-M', '4G'], "don't go together") |
| 209 | |
| 210 | def test_findleaks(self): |
| 211 | for opt in '-T', '--coverage': |
| 212 | with self.subTest(opt=opt): |
| 213 | ns = regrtest._parse_args([opt]) |
| 214 | self.assertTrue(ns.trace) |
| 215 | |
| 216 | def test_coverdir(self): |
| 217 | for opt in '-D', '--coverdir': |
| 218 | with self.subTest(opt=opt): |
| 219 | ns = regrtest._parse_args([opt, 'foo']) |
| 220 | self.assertEqual(ns.coverdir, |
| 221 | os.path.join(support.SAVEDCWD, 'foo')) |
| 222 | self.checkError([opt], 'expected one argument') |
| 223 | |
| 224 | def test_nocoverdir(self): |
| 225 | for opt in '-N', '--nocoverdir': |
| 226 | with self.subTest(opt=opt): |
| 227 | ns = regrtest._parse_args([opt]) |
| 228 | self.assertIsNone(ns.coverdir) |
| 229 | |
| 230 | def test_threshold(self): |
| 231 | for opt in '-t', '--threshold': |
| 232 | with self.subTest(opt=opt): |
| 233 | ns = regrtest._parse_args([opt, '1000']) |
| 234 | self.assertEqual(ns.threshold, 1000) |
| 235 | self.checkError([opt], 'expected one argument') |
| 236 | self.checkError([opt, 'foo'], 'invalid int value') |
| 237 | |
| 238 | def test_nowindows(self): |
| 239 | for opt in '-n', '--nowindows': |
| 240 | with self.subTest(opt=opt): |
| 241 | ns = regrtest._parse_args([opt]) |
| 242 | self.assertTrue(ns.nowindows) |
| 243 | |
| 244 | def test_forever(self): |
| 245 | for opt in '-F', '--forever': |
| 246 | with self.subTest(opt=opt): |
| 247 | ns = regrtest._parse_args([opt]) |
| 248 | self.assertTrue(ns.forever) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 249 | |
| 250 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 251 | def test_unrecognized_argument(self): |
| 252 | self.checkError(['--xxx'], 'usage:') |
| 253 | |
| 254 | def test_long_option__partial(self): |
| 255 | ns = regrtest._parse_args(['--qui']) |
| 256 | self.assertTrue(ns.quiet) |
| 257 | self.assertEqual(ns.verbose, 0) |
| 258 | |
| 259 | def test_two_options(self): |
| 260 | ns = regrtest._parse_args(['--quiet', '--exclude']) |
| 261 | self.assertTrue(ns.quiet) |
| 262 | self.assertEqual(ns.verbose, 0) |
| 263 | self.assertTrue(ns.exclude) |
| 264 | |
| 265 | def test_option_with_empty_string_value(self): |
| 266 | ns = regrtest._parse_args(['--start', '']) |
| 267 | self.assertEqual(ns.start, '') |
| 268 | |
| 269 | def test_arg(self): |
| 270 | ns = regrtest._parse_args(['foo']) |
| 271 | self.assertEqual(ns.args, ['foo']) |
| 272 | |
| 273 | def test_option_and_arg(self): |
| 274 | ns = regrtest._parse_args(['--quiet', 'foo']) |
| 275 | self.assertTrue(ns.quiet) |
| 276 | self.assertEqual(ns.verbose, 0) |
| 277 | self.assertEqual(ns.args, ['foo']) |
| 278 | |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 279 | |
| 280 | if __name__ == '__main__': |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 281 | unittest.main() |