blob: 751df1537d2c9b33e6763f590647cef7724d166e [file] [log] [blame]
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -08001"""
2Tests of regrtest.py.
Victor Stinner6b415102015-09-28 23:16:17 +02003
4Note: test_regrtest cannot be run twice in parallel.
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -08005"""
6
Victor Stinner14b46622015-10-11 10:04:26 +02007import contextlib
Charles-François Natali833bf1f2013-09-08 12:27:33 +02008import faulthandler
Victor Stinner49671462015-10-11 10:03:28 +02009import io
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030010import os.path
Victor Stinner6b415102015-09-28 23:16:17 +020011import platform
12import re
13import subprocess
14import sys
Victor Stinnerda43ee42015-11-22 15:18:54 +010015import sysconfig
Victor Stinnerd6e25022016-03-30 02:33:52 +020016import tempfile
Victor Stinner6b415102015-09-28 23:16:17 +020017import textwrap
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080018import unittest
Victor Stinner3844fe52015-09-26 10:38:01 +020019from test import libregrtest
20from test import support
Victor Stinner6b415102015-09-28 23:16:17 +020021
22
23Py_DEBUG = hasattr(sys, 'getobjects')
24ROOT_DIR = os.path.join(os.path.dirname(__file__), '..', '..')
25ROOT_DIR = os.path.abspath(os.path.normpath(ROOT_DIR))
26
Victor Stinner3909e582015-10-11 10:37:25 +020027TEST_INTERRUPTED = textwrap.dedent("""
28 from signal import SIGINT
29 try:
30 from _testcapi import raise_signal
31 raise_signal(SIGINT)
32 except ImportError:
33 import os
34 os.kill(os.getpid(), SIGINT)
35 """)
36
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080037
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080038class ParseArgsTestCase(unittest.TestCase):
Victor Stinner6b415102015-09-28 23:16:17 +020039 """
40 Test regrtest's argument parsing, function _parse_args().
41 """
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080042
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030043 def checkError(self, args, msg):
44 with support.captured_stderr() as err, self.assertRaises(SystemExit):
Victor Stinner3844fe52015-09-26 10:38:01 +020045 libregrtest._parse_args(args)
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030046 self.assertIn(msg, err.getvalue())
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080047
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030048 def test_help(self):
49 for opt in '-h', '--help':
50 with self.subTest(opt=opt):
51 with support.captured_stdout() as out, \
52 self.assertRaises(SystemExit):
Victor Stinner3844fe52015-09-26 10:38:01 +020053 libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030054 self.assertIn('Run Python regression tests.', out.getvalue())
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080055
Charles-François Natali833bf1f2013-09-08 12:27:33 +020056 @unittest.skipUnless(hasattr(faulthandler, 'dump_traceback_later'),
57 "faulthandler.dump_traceback_later() required")
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030058 def test_timeout(self):
Victor Stinner3844fe52015-09-26 10:38:01 +020059 ns = libregrtest._parse_args(['--timeout', '4.2'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030060 self.assertEqual(ns.timeout, 4.2)
61 self.checkError(['--timeout'], 'expected one argument')
62 self.checkError(['--timeout', 'foo'], 'invalid float value')
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080063
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030064 def test_wait(self):
Victor Stinner3844fe52015-09-26 10:38:01 +020065 ns = libregrtest._parse_args(['--wait'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030066 self.assertTrue(ns.wait)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080067
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030068 def test_slaveargs(self):
Victor Stinner3844fe52015-09-26 10:38:01 +020069 ns = libregrtest._parse_args(['--slaveargs', '[[], {}]'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030070 self.assertEqual(ns.slaveargs, '[[], {}]')
71 self.checkError(['--slaveargs'], 'expected one argument')
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080072
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030073 def test_start(self):
74 for opt in '-S', '--start':
75 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +020076 ns = libregrtest._parse_args([opt, 'foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030077 self.assertEqual(ns.start, 'foo')
78 self.checkError([opt], 'expected one argument')
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080079
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030080 def test_verbose(self):
Victor Stinner3844fe52015-09-26 10:38:01 +020081 ns = libregrtest._parse_args(['-v'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030082 self.assertEqual(ns.verbose, 1)
Victor Stinner3844fe52015-09-26 10:38:01 +020083 ns = libregrtest._parse_args(['-vvv'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030084 self.assertEqual(ns.verbose, 3)
Victor Stinner3844fe52015-09-26 10:38:01 +020085 ns = libregrtest._parse_args(['--verbose'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030086 self.assertEqual(ns.verbose, 1)
Victor Stinner3844fe52015-09-26 10:38:01 +020087 ns = libregrtest._parse_args(['--verbose'] * 3)
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030088 self.assertEqual(ns.verbose, 3)
Victor Stinner3844fe52015-09-26 10:38:01 +020089 ns = libregrtest._parse_args([])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030090 self.assertEqual(ns.verbose, 0)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080091
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030092 def test_verbose2(self):
93 for opt in '-w', '--verbose2':
94 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +020095 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030096 self.assertTrue(ns.verbose2)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -080097
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +030098 def test_verbose3(self):
99 for opt in '-W', '--verbose3':
100 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200101 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300102 self.assertTrue(ns.verbose3)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800103
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300104 def test_quiet(self):
105 for opt in '-q', '--quiet':
106 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200107 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300108 self.assertTrue(ns.quiet)
109 self.assertEqual(ns.verbose, 0)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800110
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300111 def test_slow(self):
Victor Stinner6c446192016-08-17 11:25:43 +0200112 for opt in '-o', '--slowest':
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300113 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200114 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300115 self.assertTrue(ns.print_slow)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800116
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300117 def test_header(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200118 ns = libregrtest._parse_args(['--header'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300119 self.assertTrue(ns.header)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800120
121 def test_randomize(self):
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300122 for opt in '-r', '--randomize':
123 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200124 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300125 self.assertTrue(ns.randomize)
126
127 def test_randseed(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200128 ns = libregrtest._parse_args(['--randseed', '12345'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300129 self.assertEqual(ns.random_seed, 12345)
130 self.assertTrue(ns.randomize)
131 self.checkError(['--randseed'], 'expected one argument')
132 self.checkError(['--randseed', 'foo'], 'invalid int value')
133
134 def test_fromfile(self):
135 for opt in '-f', '--fromfile':
136 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200137 ns = libregrtest._parse_args([opt, 'foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300138 self.assertEqual(ns.fromfile, 'foo')
139 self.checkError([opt], 'expected one argument')
140 self.checkError([opt, 'foo', '-s'], "don't go together")
141
142 def test_exclude(self):
143 for opt in '-x', '--exclude':
144 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200145 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300146 self.assertTrue(ns.exclude)
147
148 def test_single(self):
149 for opt in '-s', '--single':
150 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200151 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300152 self.assertTrue(ns.single)
153 self.checkError([opt, '-f', 'foo'], "don't go together")
154
155 def test_match(self):
156 for opt in '-m', '--match':
157 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200158 ns = libregrtest._parse_args([opt, 'pattern'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300159 self.assertEqual(ns.match_tests, 'pattern')
160 self.checkError([opt], 'expected one argument')
161
162 def test_failfast(self):
163 for opt in '-G', '--failfast':
164 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200165 ns = libregrtest._parse_args([opt, '-v'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300166 self.assertTrue(ns.failfast)
Victor Stinner3844fe52015-09-26 10:38:01 +0200167 ns = libregrtest._parse_args([opt, '-W'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300168 self.assertTrue(ns.failfast)
169 self.checkError([opt], '-G/--failfast needs either -v or -W')
170
171 def test_use(self):
172 for opt in '-u', '--use':
173 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200174 ns = libregrtest._parse_args([opt, 'gui,network'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300175 self.assertEqual(ns.use_resources, ['gui', 'network'])
Victor Stinner3844fe52015-09-26 10:38:01 +0200176 ns = libregrtest._parse_args([opt, 'gui,none,network'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300177 self.assertEqual(ns.use_resources, ['network'])
Victor Stinner98de5342015-09-26 09:43:45 +0200178 expected = list(libregrtest.RESOURCE_NAMES)
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300179 expected.remove('gui')
Victor Stinner3844fe52015-09-26 10:38:01 +0200180 ns = libregrtest._parse_args([opt, 'all,-gui'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300181 self.assertEqual(ns.use_resources, expected)
182 self.checkError([opt], 'expected one argument')
183 self.checkError([opt, 'foo'], 'invalid resource')
184
185 def test_memlimit(self):
186 for opt in '-M', '--memlimit':
187 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200188 ns = libregrtest._parse_args([opt, '4G'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300189 self.assertEqual(ns.memlimit, '4G')
190 self.checkError([opt], 'expected one argument')
191
192 def test_testdir(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200193 ns = libregrtest._parse_args(['--testdir', 'foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300194 self.assertEqual(ns.testdir, os.path.join(support.SAVEDCWD, 'foo'))
195 self.checkError(['--testdir'], 'expected one argument')
196
Benjamin Peterson95536b82013-09-29 15:18:43 -0400197 def test_runleaks(self):
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300198 for opt in '-L', '--runleaks':
199 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200200 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300201 self.assertTrue(ns.runleaks)
202
Benjamin Peterson95536b82013-09-29 15:18:43 -0400203 def test_huntrleaks(self):
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300204 for opt in '-R', '--huntrleaks':
205 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200206 ns = libregrtest._parse_args([opt, ':'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300207 self.assertEqual(ns.huntrleaks, (5, 4, 'reflog.txt'))
Victor Stinner3844fe52015-09-26 10:38:01 +0200208 ns = libregrtest._parse_args([opt, '6:'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300209 self.assertEqual(ns.huntrleaks, (6, 4, 'reflog.txt'))
Victor Stinner3844fe52015-09-26 10:38:01 +0200210 ns = libregrtest._parse_args([opt, ':3'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300211 self.assertEqual(ns.huntrleaks, (5, 3, 'reflog.txt'))
Victor Stinner3844fe52015-09-26 10:38:01 +0200212 ns = libregrtest._parse_args([opt, '6:3:leaks.log'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300213 self.assertEqual(ns.huntrleaks, (6, 3, 'leaks.log'))
214 self.checkError([opt], 'expected one argument')
215 self.checkError([opt, '6'],
216 'needs 2 or 3 colon-separated arguments')
217 self.checkError([opt, 'foo:'], 'invalid huntrleaks value')
218 self.checkError([opt, '6:foo'], 'invalid huntrleaks value')
219
220 def test_multiprocess(self):
221 for opt in '-j', '--multiprocess':
222 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200223 ns = libregrtest._parse_args([opt, '2'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300224 self.assertEqual(ns.use_mp, 2)
225 self.checkError([opt], 'expected one argument')
226 self.checkError([opt, 'foo'], 'invalid int value')
227 self.checkError([opt, '2', '-T'], "don't go together")
228 self.checkError([opt, '2', '-l'], "don't go together")
Xiang Zhang270a21f2016-12-19 21:01:33 +0800229 self.checkError([opt, '0', '-T'], "don't go together")
230 self.checkError([opt, '0', '-l'], "don't go together")
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300231
Benjamin Peterson95536b82013-09-29 15:18:43 -0400232 def test_coverage(self):
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300233 for opt in '-T', '--coverage':
234 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200235 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300236 self.assertTrue(ns.trace)
237
238 def test_coverdir(self):
239 for opt in '-D', '--coverdir':
240 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200241 ns = libregrtest._parse_args([opt, 'foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300242 self.assertEqual(ns.coverdir,
243 os.path.join(support.SAVEDCWD, 'foo'))
244 self.checkError([opt], 'expected one argument')
245
246 def test_nocoverdir(self):
247 for opt in '-N', '--nocoverdir':
248 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200249 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300250 self.assertIsNone(ns.coverdir)
251
252 def test_threshold(self):
253 for opt in '-t', '--threshold':
254 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200255 ns = libregrtest._parse_args([opt, '1000'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300256 self.assertEqual(ns.threshold, 1000)
257 self.checkError([opt], 'expected one argument')
258 self.checkError([opt, 'foo'], 'invalid int value')
259
260 def test_nowindows(self):
261 for opt in '-n', '--nowindows':
262 with self.subTest(opt=opt):
Victor Stinner14b46622015-10-11 10:04:26 +0200263 with contextlib.redirect_stderr(io.StringIO()) as stderr:
264 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300265 self.assertTrue(ns.nowindows)
Victor Stinner14b46622015-10-11 10:04:26 +0200266 err = stderr.getvalue()
267 self.assertIn('the --nowindows (-n) option is deprecated', err)
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300268
269 def test_forever(self):
270 for opt in '-F', '--forever':
271 with self.subTest(opt=opt):
Victor Stinner3844fe52015-09-26 10:38:01 +0200272 ns = libregrtest._parse_args([opt])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300273 self.assertTrue(ns.forever)
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800274
275
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300276 def test_unrecognized_argument(self):
277 self.checkError(['--xxx'], 'usage:')
278
279 def test_long_option__partial(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200280 ns = libregrtest._parse_args(['--qui'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300281 self.assertTrue(ns.quiet)
282 self.assertEqual(ns.verbose, 0)
283
284 def test_two_options(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200285 ns = libregrtest._parse_args(['--quiet', '--exclude'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300286 self.assertTrue(ns.quiet)
287 self.assertEqual(ns.verbose, 0)
288 self.assertTrue(ns.exclude)
289
290 def test_option_with_empty_string_value(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200291 ns = libregrtest._parse_args(['--start', ''])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300292 self.assertEqual(ns.start, '')
293
294 def test_arg(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200295 ns = libregrtest._parse_args(['foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300296 self.assertEqual(ns.args, ['foo'])
297
298 def test_option_and_arg(self):
Victor Stinner3844fe52015-09-26 10:38:01 +0200299 ns = libregrtest._parse_args(['--quiet', 'foo'])
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300300 self.assertTrue(ns.quiet)
301 self.assertEqual(ns.verbose, 0)
302 self.assertEqual(ns.args, ['foo'])
303
Victor Stinner1f6b69b2016-10-17 18:11:03 +0200304 def test_arg_option_arg(self):
Victor Stinnera506a932016-10-17 18:13:46 +0200305 ns = libregrtest._parse_args(['test_unaryop', '-v', 'test_binop'])
Victor Stinner1f6b69b2016-10-17 18:11:03 +0200306 self.assertEqual(ns.verbose, 1)
307 self.assertEqual(ns.args, ['test_unaryop', 'test_binop'])
308
309 def test_unknown_option(self):
310 self.checkError(['--unknown-option'],
311 'unrecognized arguments: --unknown-option')
312
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800313
Victor Stinner6b415102015-09-28 23:16:17 +0200314class BaseTestCase(unittest.TestCase):
315 TEST_UNIQUE_ID = 1
316 TESTNAME_PREFIX = 'test_regrtest_'
Victor Stinner69649f22016-03-23 12:14:10 +0100317 TESTNAME_REGEX = r'test_[a-zA-Z0-9_]+'
Victor Stinner6b415102015-09-28 23:16:17 +0200318
319 def setUp(self):
Victor Stinnerda43ee42015-11-22 15:18:54 +0100320 self.testdir = os.path.realpath(os.path.dirname(__file__))
Victor Stinner6b415102015-09-28 23:16:17 +0200321
Victor Stinnerd6e25022016-03-30 02:33:52 +0200322 self.tmptestdir = tempfile.mkdtemp()
323 self.addCleanup(support.rmtree, self.tmptestdir)
Victor Stinner6b415102015-09-28 23:16:17 +0200324
325 def create_test(self, name=None, code=''):
326 if not name:
327 name = 'noop%s' % BaseTestCase.TEST_UNIQUE_ID
328 BaseTestCase.TEST_UNIQUE_ID += 1
329
330 # test_regrtest cannot be run twice in parallel because
331 # of setUp() and create_test()
Victor Stinnerd6e25022016-03-30 02:33:52 +0200332 name = self.TESTNAME_PREFIX + name
333 path = os.path.join(self.tmptestdir, name + '.py')
Victor Stinner6b415102015-09-28 23:16:17 +0200334
335 self.addCleanup(support.unlink, path)
336 # Use 'x' mode to ensure that we do not override existing tests
Victor Stinnerda43ee42015-11-22 15:18:54 +0100337 try:
338 with open(path, 'x', encoding='utf-8') as fp:
339 fp.write(code)
340 except PermissionError as exc:
341 if not sysconfig.is_python_build():
342 self.skipTest("cannot write %s: %s" % (path, exc))
343 raise
Victor Stinner6b415102015-09-28 23:16:17 +0200344 return name
345
346 def regex_search(self, regex, output):
347 match = re.search(regex, output, re.MULTILINE)
348 if not match:
349 self.fail("%r not found in %r" % (regex, output))
350 return match
351
352 def check_line(self, output, regex):
353 regex = re.compile(r'^' + regex, re.MULTILINE)
354 self.assertRegex(output, regex)
355
356 def parse_executed_tests(self, output):
Victor Stinner69649f22016-03-23 12:14:10 +0100357 regex = (r'^[0-9]+:[0-9]+:[0-9]+ \[ *[0-9]+(?:/ *[0-9]+)?\] (%s)'
358 % self.TESTNAME_REGEX)
Victor Stinner9a142142015-09-30 13:51:17 +0200359 parser = re.finditer(regex, output, re.MULTILINE)
360 return list(match.group(1) for match in parser)
Victor Stinner6b415102015-09-28 23:16:17 +0200361
Victor Stinner9a142142015-09-30 13:51:17 +0200362 def check_executed_tests(self, output, tests, skipped=(), failed=(),
Victor Stinner63686032016-08-17 16:12:16 +0200363 omitted=(), randomize=False, interrupted=False):
Victor Stinner6b415102015-09-28 23:16:17 +0200364 if isinstance(tests, str):
365 tests = [tests]
Victor Stinner9a142142015-09-30 13:51:17 +0200366 if isinstance(skipped, str):
367 skipped = [skipped]
368 if isinstance(failed, str):
369 failed = [failed]
Victor Stinner3909e582015-10-11 10:37:25 +0200370 if isinstance(omitted, str):
371 omitted = [omitted]
Victor Stinner6b415102015-09-28 23:16:17 +0200372 ntest = len(tests)
Victor Stinner9a142142015-09-30 13:51:17 +0200373 nskipped = len(skipped)
374 nfailed = len(failed)
Victor Stinner3909e582015-10-11 10:37:25 +0200375 nomitted = len(omitted)
Victor Stinner6b415102015-09-28 23:16:17 +0200376
Victor Stinner9a142142015-09-30 13:51:17 +0200377 executed = self.parse_executed_tests(output)
378 if randomize:
379 self.assertEqual(set(executed), set(tests), output)
Victor Stinner6b415102015-09-28 23:16:17 +0200380 else:
Victor Stinner9a142142015-09-30 13:51:17 +0200381 self.assertEqual(executed, tests, output)
382
383 def plural(count):
384 return 's' if count != 1 else ''
385
386 def list_regex(line_format, tests):
387 count = len(tests)
388 names = ' '.join(sorted(tests))
389 regex = line_format % (count, plural(count))
390 regex = r'%s:\n %s$' % (regex, names)
391 return regex
392
393 if skipped:
394 regex = list_regex('%s test%s skipped', skipped)
395 self.check_line(output, regex)
396
397 if failed:
398 regex = list_regex('%s test%s failed', failed)
399 self.check_line(output, regex)
400
Victor Stinner3909e582015-10-11 10:37:25 +0200401 if omitted:
402 regex = list_regex('%s test%s omitted', omitted)
403 self.check_line(output, regex)
404
405 good = ntest - nskipped - nfailed - nomitted
Victor Stinner9a142142015-09-30 13:51:17 +0200406 if good:
407 regex = r'%s test%s OK\.$' % (good, plural(good))
408 if not skipped and not failed and good > 1:
409 regex = 'All %s' % regex
410 self.check_line(output, regex)
Victor Stinner6b415102015-09-28 23:16:17 +0200411
Victor Stinner63686032016-08-17 16:12:16 +0200412 if interrupted:
413 self.check_line(output, 'Test suite interrupted by signal SIGINT.')
414
415 if nfailed:
416 result = 'FAILURE'
417 elif interrupted:
418 result = 'INTERRUPTED'
419 else:
420 result = 'SUCCESS'
Victor Stinnerf2abf5c2016-08-19 17:54:25 +0200421 self.check_line(output, 'Tests result: %s' % result)
Victor Stinner63686032016-08-17 16:12:16 +0200422
Victor Stinner6b415102015-09-28 23:16:17 +0200423 def parse_random_seed(self, output):
424 match = self.regex_search(r'Using random seed ([0-9]+)', output)
425 randseed = int(match.group(1))
426 self.assertTrue(0 <= randseed <= 10000000, randseed)
427 return randseed
428
Victor Stinner076fc872015-10-03 00:20:56 +0200429 def run_command(self, args, input=None, exitcode=0, **kw):
Victor Stinner42771342015-09-29 14:17:09 +0200430 if not input:
431 input = ''
Victor Stinner076fc872015-10-03 00:20:56 +0200432 if 'stderr' not in kw:
433 kw['stderr'] = subprocess.PIPE
Victor Stinner9a142142015-09-30 13:51:17 +0200434 proc = subprocess.run(args,
435 universal_newlines=True,
436 input=input,
437 stdout=subprocess.PIPE,
Victor Stinner076fc872015-10-03 00:20:56 +0200438 **kw)
Victor Stinner9a142142015-09-30 13:51:17 +0200439 if proc.returncode != exitcode:
Victor Stinner076fc872015-10-03 00:20:56 +0200440 msg = ("Command %s failed with exit code %s\n"
441 "\n"
442 "stdout:\n"
443 "---\n"
444 "%s\n"
445 "---\n"
446 % (str(args), proc.returncode, proc.stdout))
447 if proc.stderr:
448 msg += ("\n"
449 "stderr:\n"
450 "---\n"
451 "%s"
452 "---\n"
453 % proc.stderr)
454 self.fail(msg)
Victor Stinner9a142142015-09-30 13:51:17 +0200455 return proc
Victor Stinner42771342015-09-29 14:17:09 +0200456
457
458 def run_python(self, args, **kw):
459 args = [sys.executable, '-X', 'faulthandler', '-I', *args]
460 proc = self.run_command(args, **kw)
461 return proc.stdout
462
Victor Stinner6b415102015-09-28 23:16:17 +0200463
464class ProgramsTestCase(BaseTestCase):
465 """
466 Test various ways to run the Python test suite. Use options close
467 to options used on the buildbot.
468 """
469
470 NTEST = 4
471
472 def setUp(self):
473 super().setUp()
474
475 # Create NTEST tests doing nothing
476 self.tests = [self.create_test() for index in range(self.NTEST)]
477
478 self.python_args = ['-Wd', '-E', '-bb']
Victor Stinnerd6e25022016-03-30 02:33:52 +0200479 self.regrtest_args = ['-uall', '-rwW',
480 '--testdir=%s' % self.tmptestdir]
Victor Stinner49671462015-10-11 10:03:28 +0200481 if hasattr(faulthandler, 'dump_traceback_later'):
482 self.regrtest_args.extend(('--timeout', '3600', '-j4'))
Victor Stinner6b415102015-09-28 23:16:17 +0200483 if sys.platform == 'win32':
484 self.regrtest_args.append('-n')
485
486 def check_output(self, output):
487 self.parse_random_seed(output)
Victor Stinner9a142142015-09-30 13:51:17 +0200488 self.check_executed_tests(output, self.tests, randomize=True)
Victor Stinner6b415102015-09-28 23:16:17 +0200489
490 def run_tests(self, args):
Victor Stinner9a142142015-09-30 13:51:17 +0200491 output = self.run_python(args)
492 self.check_output(output)
Victor Stinner6b415102015-09-28 23:16:17 +0200493
494 def test_script_regrtest(self):
495 # Lib/test/regrtest.py
Victor Stinnerda43ee42015-11-22 15:18:54 +0100496 script = os.path.join(self.testdir, 'regrtest.py')
Victor Stinner6b415102015-09-28 23:16:17 +0200497
498 args = [*self.python_args, script, *self.regrtest_args, *self.tests]
499 self.run_tests(args)
500
501 def test_module_test(self):
502 # -m test
503 args = [*self.python_args, '-m', 'test',
504 *self.regrtest_args, *self.tests]
505 self.run_tests(args)
506
507 def test_module_regrtest(self):
508 # -m test.regrtest
509 args = [*self.python_args, '-m', 'test.regrtest',
510 *self.regrtest_args, *self.tests]
511 self.run_tests(args)
512
513 def test_module_autotest(self):
514 # -m test.autotest
515 args = [*self.python_args, '-m', 'test.autotest',
516 *self.regrtest_args, *self.tests]
517 self.run_tests(args)
518
519 def test_module_from_test_autotest(self):
520 # from test import autotest
521 code = 'from test import autotest'
522 args = [*self.python_args, '-c', code,
523 *self.regrtest_args, *self.tests]
524 self.run_tests(args)
525
526 def test_script_autotest(self):
527 # Lib/test/autotest.py
Victor Stinnerda43ee42015-11-22 15:18:54 +0100528 script = os.path.join(self.testdir, 'autotest.py')
Victor Stinner6b415102015-09-28 23:16:17 +0200529 args = [*self.python_args, script, *self.regrtest_args, *self.tests]
530 self.run_tests(args)
531
Victor Stinnerda43ee42015-11-22 15:18:54 +0100532 @unittest.skipUnless(sysconfig.is_python_build(),
533 'run_tests.py script is not installed')
Victor Stinner6b415102015-09-28 23:16:17 +0200534 def test_tools_script_run_tests(self):
535 # Tools/scripts/run_tests.py
536 script = os.path.join(ROOT_DIR, 'Tools', 'scripts', 'run_tests.py')
Victor Stinner8a34d412016-03-30 08:51:15 +0200537 args = [script, *self.regrtest_args, *self.tests]
Victor Stinnerd6e25022016-03-30 02:33:52 +0200538 self.run_tests(args)
Victor Stinner6b415102015-09-28 23:16:17 +0200539
Victor Stinnere143c1f2015-09-29 01:02:37 +0200540 def run_batch(self, *args):
Victor Stinner42771342015-09-29 14:17:09 +0200541 proc = self.run_command(args)
Victor Stinner6b415102015-09-28 23:16:17 +0200542 self.check_output(proc.stdout)
543
Victor Stinnerda43ee42015-11-22 15:18:54 +0100544 @unittest.skipUnless(sysconfig.is_python_build(),
545 'test.bat script is not installed')
Victor Stinner6b415102015-09-28 23:16:17 +0200546 @unittest.skipUnless(sys.platform == 'win32', 'Windows only')
547 def test_tools_buildbot_test(self):
548 # Tools\buildbot\test.bat
549 script = os.path.join(ROOT_DIR, 'Tools', 'buildbot', 'test.bat')
Victor Stinner732599f2016-03-30 08:38:05 +0200550 test_args = ['--testdir=%s' % self.tmptestdir]
Victor Stinnere143c1f2015-09-29 01:02:37 +0200551 if platform.architecture()[0] == '64bit':
552 test_args.append('-x64') # 64-bit build
553 if not Py_DEBUG:
554 test_args.append('+d') # Release build, use python.exe
555 self.run_batch(script, *test_args, *self.tests)
Victor Stinner6b415102015-09-28 23:16:17 +0200556
557 @unittest.skipUnless(sys.platform == 'win32', 'Windows only')
558 def test_pcbuild_rt(self):
559 # PCbuild\rt.bat
560 script = os.path.join(ROOT_DIR, r'PCbuild\rt.bat')
Victor Stinnere143c1f2015-09-29 01:02:37 +0200561 rt_args = ["-q"] # Quick, don't run tests twice
562 if platform.architecture()[0] == '64bit':
563 rt_args.append('-x64') # 64-bit build
564 if Py_DEBUG:
565 rt_args.append('-d') # Debug build, use python_d.exe
566 self.run_batch(script, *rt_args, *self.regrtest_args, *self.tests)
Victor Stinner6b415102015-09-28 23:16:17 +0200567
568
569class ArgsTestCase(BaseTestCase):
570 """
571 Test arguments of the Python test suite.
572 """
573
Victor Stinnerd6e25022016-03-30 02:33:52 +0200574 def run_tests(self, *testargs, **kw):
575 cmdargs = ['-m', 'test', '--testdir=%s' % self.tmptestdir, *testargs]
576 return self.run_python(cmdargs, **kw)
Victor Stinner9a142142015-09-30 13:51:17 +0200577
578 def test_failing_test(self):
579 # test a failing test
580 code = textwrap.dedent("""
581 import unittest
582
583 class FailingTest(unittest.TestCase):
584 def test_failing(self):
585 self.fail("bug")
586 """)
Victor Stinnerd6e25022016-03-30 02:33:52 +0200587 test_ok = self.create_test('ok')
588 test_failing = self.create_test('failing', code=code)
Victor Stinner9a142142015-09-30 13:51:17 +0200589 tests = [test_ok, test_failing]
590
591 output = self.run_tests(*tests, exitcode=1)
592 self.check_executed_tests(output, tests, failed=test_failing)
Victor Stinner6b415102015-09-28 23:16:17 +0200593
594 def test_resources(self):
595 # test -u command line option
596 tests = {}
597 for resource in ('audio', 'network'):
598 code = 'from test import support\nsupport.requires(%r)' % resource
599 tests[resource] = self.create_test(resource, code)
600 test_names = sorted(tests.values())
601
602 # -u all: 2 resources enabled
603 output = self.run_tests('-u', 'all', *test_names)
604 self.check_executed_tests(output, test_names)
605
606 # -u audio: 1 resource enabled
607 output = self.run_tests('-uaudio', *test_names)
608 self.check_executed_tests(output, test_names,
609 skipped=tests['network'])
610
611 # no option: 0 resources enabled
612 output = self.run_tests(*test_names)
613 self.check_executed_tests(output, test_names,
614 skipped=test_names)
615
616 def test_random(self):
617 # test -r and --randseed command line option
618 code = textwrap.dedent("""
619 import random
620 print("TESTRANDOM: %s" % random.randint(1, 1000))
621 """)
622 test = self.create_test('random', code)
623
624 # first run to get the output with the random seed
625 output = self.run_tests('-r', test)
626 randseed = self.parse_random_seed(output)
627 match = self.regex_search(r'TESTRANDOM: ([0-9]+)', output)
628 test_random = int(match.group(1))
629
630 # try to reproduce with the random seed
631 output = self.run_tests('-r', '--randseed=%s' % randseed, test)
632 randseed2 = self.parse_random_seed(output)
633 self.assertEqual(randseed2, randseed)
634
635 match = self.regex_search(r'TESTRANDOM: ([0-9]+)', output)
636 test_random2 = int(match.group(1))
637 self.assertEqual(test_random2, test_random)
638
639 def test_fromfile(self):
640 # test --fromfile
641 tests = [self.create_test() for index in range(5)]
642
643 # Write the list of files using a format similar to regrtest output:
644 # [1/2] test_1
645 # [2/2] test_2
646 filename = support.TESTFN
647 self.addCleanup(support.unlink, filename)
Victor Stinner5de16e82016-03-24 09:43:00 +0100648
649 # test format '0:00:00 [2/7] test_opcodes -- test_grammar took 0 sec'
650 with open(filename, "w") as fp:
651 previous = None
652 for index, name in enumerate(tests, 1):
653 line = ("00:00:%02i [%s/%s] %s"
654 % (index, index, len(tests), name))
655 if previous:
656 line += " -- %s took 0 sec" % previous
657 print(line, file=fp)
658 previous = name
659
660 output = self.run_tests('--fromfile', filename)
661 self.check_executed_tests(output, tests)
662
663 # test format '[2/7] test_opcodes'
Victor Stinner6b415102015-09-28 23:16:17 +0200664 with open(filename, "w") as fp:
665 for index, name in enumerate(tests, 1):
666 print("[%s/%s] %s" % (index, len(tests), name), file=fp)
667
668 output = self.run_tests('--fromfile', filename)
669 self.check_executed_tests(output, tests)
670
Victor Stinner5de16e82016-03-24 09:43:00 +0100671 # test format 'test_opcodes'
672 with open(filename, "w") as fp:
673 for name in tests:
674 print(name, file=fp)
675
676 output = self.run_tests('--fromfile', filename)
677 self.check_executed_tests(output, tests)
678
Victor Stinner3909e582015-10-11 10:37:25 +0200679 def test_interrupted(self):
680 code = TEST_INTERRUPTED
Victor Stinnerd6e25022016-03-30 02:33:52 +0200681 test = self.create_test('sigint', code=code)
Victor Stinner3909e582015-10-11 10:37:25 +0200682 output = self.run_tests(test, exitcode=1)
Victor Stinner63686032016-08-17 16:12:16 +0200683 self.check_executed_tests(output, test, omitted=test,
684 interrupted=True)
Victor Stinner3909e582015-10-11 10:37:25 +0200685
Victor Stinner435eaf42016-08-17 12:22:52 +0200686 def test_slowest(self):
Victor Stinner6c446192016-08-17 11:25:43 +0200687 # test --slowest
Victor Stinner6b415102015-09-28 23:16:17 +0200688 tests = [self.create_test() for index in range(3)]
Victor Stinner6c446192016-08-17 11:25:43 +0200689 output = self.run_tests("--slowest", *tests)
Victor Stinner6b415102015-09-28 23:16:17 +0200690 self.check_executed_tests(output, tests)
691 regex = ('10 slowest tests:\n'
Victor Stinner435eaf42016-08-17 12:22:52 +0200692 '(?:- %s: .*\n){%s}'
Victor Stinner6b415102015-09-28 23:16:17 +0200693 % (self.TESTNAME_REGEX, len(tests)))
694 self.check_line(output, regex)
695
Victor Stinner3909e582015-10-11 10:37:25 +0200696 def test_slow_interrupted(self):
Victor Stinner6c446192016-08-17 11:25:43 +0200697 # Issue #25373: test --slowest with an interrupted test
Victor Stinner3909e582015-10-11 10:37:25 +0200698 code = TEST_INTERRUPTED
699 test = self.create_test("sigint", code=code)
700
Martin Panter56b2cf52016-10-20 21:45:49 +0000701 try:
702 import threading
703 tests = (False, True)
704 except ImportError:
705 tests = (False,)
706 for multiprocessing in tests:
Victor Stinner3909e582015-10-11 10:37:25 +0200707 if multiprocessing:
Victor Stinner6c446192016-08-17 11:25:43 +0200708 args = ("--slowest", "-j2", test)
Victor Stinner3909e582015-10-11 10:37:25 +0200709 else:
Victor Stinner6c446192016-08-17 11:25:43 +0200710 args = ("--slowest", test)
Victor Stinner3909e582015-10-11 10:37:25 +0200711 output = self.run_tests(*args, exitcode=1)
Victor Stinner63686032016-08-17 16:12:16 +0200712 self.check_executed_tests(output, test,
713 omitted=test, interrupted=True)
714
Victor Stinner3909e582015-10-11 10:37:25 +0200715 regex = ('10 slowest tests:\n')
716 self.check_line(output, regex)
Victor Stinner3909e582015-10-11 10:37:25 +0200717
Victor Stinner6b415102015-09-28 23:16:17 +0200718 def test_coverage(self):
719 # test --coverage
Victor Stinnerd6e25022016-03-30 02:33:52 +0200720 test = self.create_test('coverage')
Victor Stinner6b415102015-09-28 23:16:17 +0200721 output = self.run_tests("--coverage", test)
Victor Stinner9a142142015-09-30 13:51:17 +0200722 self.check_executed_tests(output, [test])
R David Murray44b548d2016-09-08 13:59:53 -0400723 regex = (r'lines +cov% +module +\(path\)\n'
724 r'(?: *[0-9]+ *[0-9]{1,2}% *[^ ]+ +\([^)]+\)+)+')
Victor Stinner6b415102015-09-28 23:16:17 +0200725 self.check_line(output, regex)
726
Victor Stinner42771342015-09-29 14:17:09 +0200727 def test_wait(self):
728 # test --wait
Victor Stinnerd6e25022016-03-30 02:33:52 +0200729 test = self.create_test('wait')
Victor Stinner42771342015-09-29 14:17:09 +0200730 output = self.run_tests("--wait", test, input='key')
731 self.check_line(output, 'Press any key to continue')
732
Victor Stinner9a142142015-09-30 13:51:17 +0200733 def test_forever(self):
734 # test --forever
735 code = textwrap.dedent("""
Victor Stinnerd6e25022016-03-30 02:33:52 +0200736 import builtins
Victor Stinner9a142142015-09-30 13:51:17 +0200737 import unittest
738
739 class ForeverTester(unittest.TestCase):
Victor Stinner9a142142015-09-30 13:51:17 +0200740 def test_run(self):
Victor Stinnerd6e25022016-03-30 02:33:52 +0200741 # Store the state in the builtins module, because the test
742 # module is reload at each run
743 if 'RUN' in builtins.__dict__:
744 builtins.__dict__['RUN'] += 1
745 if builtins.__dict__['RUN'] >= 3:
746 self.fail("fail at the 3rd runs")
747 else:
748 builtins.__dict__['RUN'] = 1
Victor Stinner9a142142015-09-30 13:51:17 +0200749 """)
Victor Stinnerd6e25022016-03-30 02:33:52 +0200750 test = self.create_test('forever', code=code)
Victor Stinner9a142142015-09-30 13:51:17 +0200751 output = self.run_tests('--forever', test, exitcode=1)
752 self.check_executed_tests(output, [test]*3, failed=test)
753
Victor Stinner46d75352015-10-03 02:21:35 +0200754 @unittest.skipUnless(Py_DEBUG, 'need a debug build')
Victor Stinner076fc872015-10-03 00:20:56 +0200755 def test_huntrleaks_fd_leak(self):
756 # test --huntrleaks for file descriptor leak
757 code = textwrap.dedent("""
758 import os
759 import unittest
760
Victor Stinner20d21182015-10-03 21:40:21 +0200761 # Issue #25306: Disable popups and logs to stderr on assertion
762 # failures in MSCRT
763 try:
764 import msvcrt
765 msvcrt.CrtSetReportMode
766 except (ImportError, AttributeError):
767 # no Windows, o release build
768 pass
769 else:
770 for m in [msvcrt.CRT_WARN, msvcrt.CRT_ERROR, msvcrt.CRT_ASSERT]:
771 msvcrt.CrtSetReportMode(m, 0)
772
Victor Stinner076fc872015-10-03 00:20:56 +0200773 class FDLeakTest(unittest.TestCase):
774 def test_leak(self):
775 fd = os.open(__file__, os.O_RDONLY)
776 # bug: never cloes the file descriptor
777 """)
Victor Stinnerd6e25022016-03-30 02:33:52 +0200778 test = self.create_test('huntrleaks', code=code)
Victor Stinner076fc872015-10-03 00:20:56 +0200779
780 filename = 'reflog.txt'
781 self.addCleanup(support.unlink, filename)
782 output = self.run_tests('--huntrleaks', '3:3:', test,
783 exitcode=1,
784 stderr=subprocess.STDOUT)
785 self.check_executed_tests(output, [test], failed=test)
786
787 line = 'beginning 6 repetitions\n123456\n......\n'
788 self.check_line(output, re.escape(line))
789
790 line2 = '%s leaked [1, 1, 1] file descriptors, sum=3\n' % test
Victor Stinner254ad582016-09-19 00:11:30 +0200791 self.assertIn(line2, output)
Victor Stinner076fc872015-10-03 00:20:56 +0200792
793 with open(filename) as fp:
794 reflog = fp.read()
Victor Stinner254ad582016-09-19 00:11:30 +0200795 self.assertIn(line2, reflog)
Victor Stinner076fc872015-10-03 00:20:56 +0200796
Victor Stinner5f9d3ac2015-10-03 00:21:12 +0200797 def test_list_tests(self):
798 # test --list-tests
799 tests = [self.create_test() for i in range(5)]
800 output = self.run_tests('--list-tests', *tests)
801 self.assertEqual(output.rstrip().splitlines(),
802 tests)
803
Victor Stinner6b415102015-09-28 23:16:17 +0200804
Chris Jerdonekd6c18dc2012-12-27 18:53:12 -0800805if __name__ == '__main__':
Serhiy Storchaka64f7c4e2013-08-29 12:26:23 +0300806 unittest.main()