Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 1 | """ |
| 2 | Tests of regrtest.py. |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 3 | |
| 4 | Note: test_regrtest cannot be run twice in parallel. |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 5 | """ |
| 6 | |
Victor Stinner | 14b4662 | 2015-10-11 10:04:26 +0200 | [diff] [blame] | 7 | import contextlib |
Charles-François Natali | 833bf1f | 2013-09-08 12:27:33 +0200 | [diff] [blame] | 8 | import faulthandler |
Victor Stinner | 4967146 | 2015-10-11 10:03:28 +0200 | [diff] [blame] | 9 | import io |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 10 | import os.path |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 11 | import platform |
| 12 | import re |
| 13 | import subprocess |
| 14 | import sys |
Victor Stinner | da43ee4 | 2015-11-22 15:18:54 +0100 | [diff] [blame] | 15 | import sysconfig |
Victor Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 16 | import tempfile |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 17 | import textwrap |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 18 | import unittest |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 19 | from test import libregrtest |
| 20 | from test import support |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 21 | |
| 22 | |
| 23 | Py_DEBUG = hasattr(sys, 'getobjects') |
| 24 | ROOT_DIR = os.path.join(os.path.dirname(__file__), '..', '..') |
| 25 | ROOT_DIR = os.path.abspath(os.path.normpath(ROOT_DIR)) |
| 26 | |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 27 | TEST_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 Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 37 | |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 38 | class ParseArgsTestCase(unittest.TestCase): |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 39 | """ |
| 40 | Test regrtest's argument parsing, function _parse_args(). |
| 41 | """ |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 42 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 43 | def checkError(self, args, msg): |
| 44 | with support.captured_stderr() as err, self.assertRaises(SystemExit): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 45 | libregrtest._parse_args(args) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 46 | self.assertIn(msg, err.getvalue()) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 47 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 48 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 53 | libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 54 | self.assertIn('Run Python regression tests.', out.getvalue()) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 55 | |
Charles-François Natali | 833bf1f | 2013-09-08 12:27:33 +0200 | [diff] [blame] | 56 | @unittest.skipUnless(hasattr(faulthandler, 'dump_traceback_later'), |
| 57 | "faulthandler.dump_traceback_later() required") |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 58 | def test_timeout(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 59 | ns = libregrtest._parse_args(['--timeout', '4.2']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 60 | self.assertEqual(ns.timeout, 4.2) |
| 61 | self.checkError(['--timeout'], 'expected one argument') |
| 62 | self.checkError(['--timeout', 'foo'], 'invalid float value') |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 63 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 64 | def test_wait(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 65 | ns = libregrtest._parse_args(['--wait']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 66 | self.assertTrue(ns.wait) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 67 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 68 | def test_slaveargs(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 69 | ns = libregrtest._parse_args(['--slaveargs', '[[], {}]']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 70 | self.assertEqual(ns.slaveargs, '[[], {}]') |
| 71 | self.checkError(['--slaveargs'], 'expected one argument') |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 72 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 73 | def test_start(self): |
| 74 | for opt in '-S', '--start': |
| 75 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 76 | ns = libregrtest._parse_args([opt, 'foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 77 | self.assertEqual(ns.start, 'foo') |
| 78 | self.checkError([opt], 'expected one argument') |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 79 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 80 | def test_verbose(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 81 | ns = libregrtest._parse_args(['-v']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 82 | self.assertEqual(ns.verbose, 1) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 83 | ns = libregrtest._parse_args(['-vvv']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 84 | self.assertEqual(ns.verbose, 3) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 85 | ns = libregrtest._parse_args(['--verbose']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 86 | self.assertEqual(ns.verbose, 1) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 87 | ns = libregrtest._parse_args(['--verbose'] * 3) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 88 | self.assertEqual(ns.verbose, 3) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 89 | ns = libregrtest._parse_args([]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 90 | self.assertEqual(ns.verbose, 0) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 91 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 92 | def test_verbose2(self): |
| 93 | for opt in '-w', '--verbose2': |
| 94 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 95 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 96 | self.assertTrue(ns.verbose2) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 97 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 98 | def test_verbose3(self): |
| 99 | for opt in '-W', '--verbose3': |
| 100 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 101 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 102 | self.assertTrue(ns.verbose3) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 103 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 104 | def test_quiet(self): |
| 105 | for opt in '-q', '--quiet': |
| 106 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 107 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 108 | self.assertTrue(ns.quiet) |
| 109 | self.assertEqual(ns.verbose, 0) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 110 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 111 | def test_slow(self): |
Victor Stinner | 6c44619 | 2016-08-17 11:25:43 +0200 | [diff] [blame] | 112 | for opt in '-o', '--slowest': |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 113 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 114 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 115 | self.assertTrue(ns.print_slow) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 116 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 117 | def test_header(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 118 | ns = libregrtest._parse_args(['--header']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 119 | self.assertTrue(ns.header) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 120 | |
| 121 | def test_randomize(self): |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 122 | for opt in '-r', '--randomize': |
| 123 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 124 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 125 | self.assertTrue(ns.randomize) |
| 126 | |
| 127 | def test_randseed(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 128 | ns = libregrtest._parse_args(['--randseed', '12345']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 129 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 137 | ns = libregrtest._parse_args([opt, 'foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 138 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 145 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 146 | self.assertTrue(ns.exclude) |
| 147 | |
| 148 | def test_single(self): |
| 149 | for opt in '-s', '--single': |
| 150 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 151 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 152 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 158 | ns = libregrtest._parse_args([opt, 'pattern']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 159 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 165 | ns = libregrtest._parse_args([opt, '-v']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 166 | self.assertTrue(ns.failfast) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 167 | ns = libregrtest._parse_args([opt, '-W']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 168 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 174 | ns = libregrtest._parse_args([opt, 'gui,network']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 175 | self.assertEqual(ns.use_resources, ['gui', 'network']) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 176 | ns = libregrtest._parse_args([opt, 'gui,none,network']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 177 | self.assertEqual(ns.use_resources, ['network']) |
Victor Stinner | 98de534 | 2015-09-26 09:43:45 +0200 | [diff] [blame] | 178 | expected = list(libregrtest.RESOURCE_NAMES) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 179 | expected.remove('gui') |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 180 | ns = libregrtest._parse_args([opt, 'all,-gui']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 181 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 188 | ns = libregrtest._parse_args([opt, '4G']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 189 | self.assertEqual(ns.memlimit, '4G') |
| 190 | self.checkError([opt], 'expected one argument') |
| 191 | |
| 192 | def test_testdir(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 193 | ns = libregrtest._parse_args(['--testdir', 'foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 194 | self.assertEqual(ns.testdir, os.path.join(support.SAVEDCWD, 'foo')) |
| 195 | self.checkError(['--testdir'], 'expected one argument') |
| 196 | |
Benjamin Peterson | 95536b8 | 2013-09-29 15:18:43 -0400 | [diff] [blame] | 197 | def test_runleaks(self): |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 198 | for opt in '-L', '--runleaks': |
| 199 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 200 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 201 | self.assertTrue(ns.runleaks) |
| 202 | |
Benjamin Peterson | 95536b8 | 2013-09-29 15:18:43 -0400 | [diff] [blame] | 203 | def test_huntrleaks(self): |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 204 | for opt in '-R', '--huntrleaks': |
| 205 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 206 | ns = libregrtest._parse_args([opt, ':']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 207 | self.assertEqual(ns.huntrleaks, (5, 4, 'reflog.txt')) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 208 | ns = libregrtest._parse_args([opt, '6:']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 209 | self.assertEqual(ns.huntrleaks, (6, 4, 'reflog.txt')) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 210 | ns = libregrtest._parse_args([opt, ':3']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 211 | self.assertEqual(ns.huntrleaks, (5, 3, 'reflog.txt')) |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 212 | ns = libregrtest._parse_args([opt, '6:3:leaks.log']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 213 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 223 | ns = libregrtest._parse_args([opt, '2']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 224 | 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 Zhang | 270a21f | 2016-12-19 21:01:33 +0800 | [diff] [blame] | 229 | self.checkError([opt, '0', '-T'], "don't go together") |
| 230 | self.checkError([opt, '0', '-l'], "don't go together") |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 231 | |
Benjamin Peterson | 95536b8 | 2013-09-29 15:18:43 -0400 | [diff] [blame] | 232 | def test_coverage(self): |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 233 | for opt in '-T', '--coverage': |
| 234 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 235 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 236 | self.assertTrue(ns.trace) |
| 237 | |
| 238 | def test_coverdir(self): |
| 239 | for opt in '-D', '--coverdir': |
| 240 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 241 | ns = libregrtest._parse_args([opt, 'foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 242 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 249 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 250 | self.assertIsNone(ns.coverdir) |
| 251 | |
| 252 | def test_threshold(self): |
| 253 | for opt in '-t', '--threshold': |
| 254 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 255 | ns = libregrtest._parse_args([opt, '1000']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 256 | 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 Stinner | 14b4662 | 2015-10-11 10:04:26 +0200 | [diff] [blame] | 263 | with contextlib.redirect_stderr(io.StringIO()) as stderr: |
| 264 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 265 | self.assertTrue(ns.nowindows) |
Victor Stinner | 14b4662 | 2015-10-11 10:04:26 +0200 | [diff] [blame] | 266 | err = stderr.getvalue() |
| 267 | self.assertIn('the --nowindows (-n) option is deprecated', err) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 268 | |
| 269 | def test_forever(self): |
| 270 | for opt in '-F', '--forever': |
| 271 | with self.subTest(opt=opt): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 272 | ns = libregrtest._parse_args([opt]) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 273 | self.assertTrue(ns.forever) |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 274 | |
| 275 | |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 276 | def test_unrecognized_argument(self): |
| 277 | self.checkError(['--xxx'], 'usage:') |
| 278 | |
| 279 | def test_long_option__partial(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 280 | ns = libregrtest._parse_args(['--qui']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 281 | self.assertTrue(ns.quiet) |
| 282 | self.assertEqual(ns.verbose, 0) |
| 283 | |
| 284 | def test_two_options(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 285 | ns = libregrtest._parse_args(['--quiet', '--exclude']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 286 | 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 Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 291 | ns = libregrtest._parse_args(['--start', '']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 292 | self.assertEqual(ns.start, '') |
| 293 | |
| 294 | def test_arg(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 295 | ns = libregrtest._parse_args(['foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 296 | self.assertEqual(ns.args, ['foo']) |
| 297 | |
| 298 | def test_option_and_arg(self): |
Victor Stinner | 3844fe5 | 2015-09-26 10:38:01 +0200 | [diff] [blame] | 299 | ns = libregrtest._parse_args(['--quiet', 'foo']) |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 300 | self.assertTrue(ns.quiet) |
| 301 | self.assertEqual(ns.verbose, 0) |
| 302 | self.assertEqual(ns.args, ['foo']) |
| 303 | |
Victor Stinner | 1f6b69b | 2016-10-17 18:11:03 +0200 | [diff] [blame] | 304 | def test_arg_option_arg(self): |
Victor Stinner | a506a93 | 2016-10-17 18:13:46 +0200 | [diff] [blame] | 305 | ns = libregrtest._parse_args(['test_unaryop', '-v', 'test_binop']) |
Victor Stinner | 1f6b69b | 2016-10-17 18:11:03 +0200 | [diff] [blame] | 306 | 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 Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 313 | |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 314 | class BaseTestCase(unittest.TestCase): |
| 315 | TEST_UNIQUE_ID = 1 |
| 316 | TESTNAME_PREFIX = 'test_regrtest_' |
Victor Stinner | 69649f2 | 2016-03-23 12:14:10 +0100 | [diff] [blame] | 317 | TESTNAME_REGEX = r'test_[a-zA-Z0-9_]+' |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 318 | |
| 319 | def setUp(self): |
Victor Stinner | da43ee4 | 2015-11-22 15:18:54 +0100 | [diff] [blame] | 320 | self.testdir = os.path.realpath(os.path.dirname(__file__)) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 321 | |
Victor Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 322 | self.tmptestdir = tempfile.mkdtemp() |
| 323 | self.addCleanup(support.rmtree, self.tmptestdir) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 324 | |
| 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 Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 332 | name = self.TESTNAME_PREFIX + name |
| 333 | path = os.path.join(self.tmptestdir, name + '.py') |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 334 | |
| 335 | self.addCleanup(support.unlink, path) |
| 336 | # Use 'x' mode to ensure that we do not override existing tests |
Victor Stinner | da43ee4 | 2015-11-22 15:18:54 +0100 | [diff] [blame] | 337 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 344 | 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 Stinner | 69649f2 | 2016-03-23 12:14:10 +0100 | [diff] [blame] | 357 | regex = (r'^[0-9]+:[0-9]+:[0-9]+ \[ *[0-9]+(?:/ *[0-9]+)?\] (%s)' |
| 358 | % self.TESTNAME_REGEX) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 359 | parser = re.finditer(regex, output, re.MULTILINE) |
| 360 | return list(match.group(1) for match in parser) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 361 | |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 362 | def check_executed_tests(self, output, tests, skipped=(), failed=(), |
Victor Stinner | 6368603 | 2016-08-17 16:12:16 +0200 | [diff] [blame] | 363 | omitted=(), randomize=False, interrupted=False): |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 364 | if isinstance(tests, str): |
| 365 | tests = [tests] |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 366 | if isinstance(skipped, str): |
| 367 | skipped = [skipped] |
| 368 | if isinstance(failed, str): |
| 369 | failed = [failed] |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 370 | if isinstance(omitted, str): |
| 371 | omitted = [omitted] |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 372 | ntest = len(tests) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 373 | nskipped = len(skipped) |
| 374 | nfailed = len(failed) |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 375 | nomitted = len(omitted) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 376 | |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 377 | executed = self.parse_executed_tests(output) |
| 378 | if randomize: |
| 379 | self.assertEqual(set(executed), set(tests), output) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 380 | else: |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 381 | 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 Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 401 | 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 Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 406 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 411 | |
Victor Stinner | 6368603 | 2016-08-17 16:12:16 +0200 | [diff] [blame] | 412 | 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 Stinner | f2abf5c | 2016-08-19 17:54:25 +0200 | [diff] [blame] | 421 | self.check_line(output, 'Tests result: %s' % result) |
Victor Stinner | 6368603 | 2016-08-17 16:12:16 +0200 | [diff] [blame] | 422 | |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 423 | 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 Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 429 | def run_command(self, args, input=None, exitcode=0, **kw): |
Victor Stinner | 4277134 | 2015-09-29 14:17:09 +0200 | [diff] [blame] | 430 | if not input: |
| 431 | input = '' |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 432 | if 'stderr' not in kw: |
| 433 | kw['stderr'] = subprocess.PIPE |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 434 | proc = subprocess.run(args, |
| 435 | universal_newlines=True, |
| 436 | input=input, |
| 437 | stdout=subprocess.PIPE, |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 438 | **kw) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 439 | if proc.returncode != exitcode: |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 440 | 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 Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 455 | return proc |
Victor Stinner | 4277134 | 2015-09-29 14:17:09 +0200 | [diff] [blame] | 456 | |
| 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 463 | |
| 464 | class 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 Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 479 | self.regrtest_args = ['-uall', '-rwW', |
| 480 | '--testdir=%s' % self.tmptestdir] |
Victor Stinner | 4967146 | 2015-10-11 10:03:28 +0200 | [diff] [blame] | 481 | if hasattr(faulthandler, 'dump_traceback_later'): |
| 482 | self.regrtest_args.extend(('--timeout', '3600', '-j4')) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 483 | if sys.platform == 'win32': |
| 484 | self.regrtest_args.append('-n') |
| 485 | |
| 486 | def check_output(self, output): |
| 487 | self.parse_random_seed(output) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 488 | self.check_executed_tests(output, self.tests, randomize=True) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 489 | |
| 490 | def run_tests(self, args): |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 491 | output = self.run_python(args) |
| 492 | self.check_output(output) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 493 | |
| 494 | def test_script_regrtest(self): |
| 495 | # Lib/test/regrtest.py |
Victor Stinner | da43ee4 | 2015-11-22 15:18:54 +0100 | [diff] [blame] | 496 | script = os.path.join(self.testdir, 'regrtest.py') |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 497 | |
| 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 Stinner | da43ee4 | 2015-11-22 15:18:54 +0100 | [diff] [blame] | 528 | script = os.path.join(self.testdir, 'autotest.py') |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 529 | args = [*self.python_args, script, *self.regrtest_args, *self.tests] |
| 530 | self.run_tests(args) |
| 531 | |
Victor Stinner | da43ee4 | 2015-11-22 15:18:54 +0100 | [diff] [blame] | 532 | @unittest.skipUnless(sysconfig.is_python_build(), |
| 533 | 'run_tests.py script is not installed') |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 534 | 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 Stinner | 8a34d41 | 2016-03-30 08:51:15 +0200 | [diff] [blame] | 537 | args = [script, *self.regrtest_args, *self.tests] |
Victor Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 538 | self.run_tests(args) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 539 | |
Victor Stinner | e143c1f | 2015-09-29 01:02:37 +0200 | [diff] [blame] | 540 | def run_batch(self, *args): |
Victor Stinner | 4277134 | 2015-09-29 14:17:09 +0200 | [diff] [blame] | 541 | proc = self.run_command(args) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 542 | self.check_output(proc.stdout) |
| 543 | |
Victor Stinner | da43ee4 | 2015-11-22 15:18:54 +0100 | [diff] [blame] | 544 | @unittest.skipUnless(sysconfig.is_python_build(), |
| 545 | 'test.bat script is not installed') |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 546 | @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 Stinner | 732599f | 2016-03-30 08:38:05 +0200 | [diff] [blame] | 550 | test_args = ['--testdir=%s' % self.tmptestdir] |
Victor Stinner | e143c1f | 2015-09-29 01:02:37 +0200 | [diff] [blame] | 551 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 556 | |
| 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 Stinner | e143c1f | 2015-09-29 01:02:37 +0200 | [diff] [blame] | 561 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 567 | |
| 568 | |
| 569 | class ArgsTestCase(BaseTestCase): |
| 570 | """ |
| 571 | Test arguments of the Python test suite. |
| 572 | """ |
| 573 | |
Victor Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 574 | def run_tests(self, *testargs, **kw): |
| 575 | cmdargs = ['-m', 'test', '--testdir=%s' % self.tmptestdir, *testargs] |
| 576 | return self.run_python(cmdargs, **kw) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 577 | |
| 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 Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 587 | test_ok = self.create_test('ok') |
| 588 | test_failing = self.create_test('failing', code=code) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 589 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 593 | |
| 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 Stinner | 5de16e8 | 2016-03-24 09:43:00 +0100 | [diff] [blame] | 648 | |
| 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 664 | 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 Stinner | 5de16e8 | 2016-03-24 09:43:00 +0100 | [diff] [blame] | 671 | # 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 Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 679 | def test_interrupted(self): |
| 680 | code = TEST_INTERRUPTED |
Victor Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 681 | test = self.create_test('sigint', code=code) |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 682 | output = self.run_tests(test, exitcode=1) |
Victor Stinner | 6368603 | 2016-08-17 16:12:16 +0200 | [diff] [blame] | 683 | self.check_executed_tests(output, test, omitted=test, |
| 684 | interrupted=True) |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 685 | |
Victor Stinner | 435eaf4 | 2016-08-17 12:22:52 +0200 | [diff] [blame] | 686 | def test_slowest(self): |
Victor Stinner | 6c44619 | 2016-08-17 11:25:43 +0200 | [diff] [blame] | 687 | # test --slowest |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 688 | tests = [self.create_test() for index in range(3)] |
Victor Stinner | 6c44619 | 2016-08-17 11:25:43 +0200 | [diff] [blame] | 689 | output = self.run_tests("--slowest", *tests) |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 690 | self.check_executed_tests(output, tests) |
| 691 | regex = ('10 slowest tests:\n' |
Victor Stinner | 435eaf4 | 2016-08-17 12:22:52 +0200 | [diff] [blame] | 692 | '(?:- %s: .*\n){%s}' |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 693 | % (self.TESTNAME_REGEX, len(tests))) |
| 694 | self.check_line(output, regex) |
| 695 | |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 696 | def test_slow_interrupted(self): |
Victor Stinner | 6c44619 | 2016-08-17 11:25:43 +0200 | [diff] [blame] | 697 | # Issue #25373: test --slowest with an interrupted test |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 698 | code = TEST_INTERRUPTED |
| 699 | test = self.create_test("sigint", code=code) |
| 700 | |
Martin Panter | 56b2cf5 | 2016-10-20 21:45:49 +0000 | [diff] [blame] | 701 | try: |
| 702 | import threading |
| 703 | tests = (False, True) |
| 704 | except ImportError: |
| 705 | tests = (False,) |
| 706 | for multiprocessing in tests: |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 707 | if multiprocessing: |
Victor Stinner | 6c44619 | 2016-08-17 11:25:43 +0200 | [diff] [blame] | 708 | args = ("--slowest", "-j2", test) |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 709 | else: |
Victor Stinner | 6c44619 | 2016-08-17 11:25:43 +0200 | [diff] [blame] | 710 | args = ("--slowest", test) |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 711 | output = self.run_tests(*args, exitcode=1) |
Victor Stinner | 6368603 | 2016-08-17 16:12:16 +0200 | [diff] [blame] | 712 | self.check_executed_tests(output, test, |
| 713 | omitted=test, interrupted=True) |
| 714 | |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 715 | regex = ('10 slowest tests:\n') |
| 716 | self.check_line(output, regex) |
Victor Stinner | 3909e58 | 2015-10-11 10:37:25 +0200 | [diff] [blame] | 717 | |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 718 | def test_coverage(self): |
| 719 | # test --coverage |
Victor Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 720 | test = self.create_test('coverage') |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 721 | output = self.run_tests("--coverage", test) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 722 | self.check_executed_tests(output, [test]) |
R David Murray | 44b548d | 2016-09-08 13:59:53 -0400 | [diff] [blame] | 723 | regex = (r'lines +cov% +module +\(path\)\n' |
| 724 | r'(?: *[0-9]+ *[0-9]{1,2}% *[^ ]+ +\([^)]+\)+)+') |
Victor Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 725 | self.check_line(output, regex) |
| 726 | |
Victor Stinner | 4277134 | 2015-09-29 14:17:09 +0200 | [diff] [blame] | 727 | def test_wait(self): |
| 728 | # test --wait |
Victor Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 729 | test = self.create_test('wait') |
Victor Stinner | 4277134 | 2015-09-29 14:17:09 +0200 | [diff] [blame] | 730 | output = self.run_tests("--wait", test, input='key') |
| 731 | self.check_line(output, 'Press any key to continue') |
| 732 | |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 733 | def test_forever(self): |
| 734 | # test --forever |
| 735 | code = textwrap.dedent(""" |
Victor Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 736 | import builtins |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 737 | import unittest |
| 738 | |
| 739 | class ForeverTester(unittest.TestCase): |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 740 | def test_run(self): |
Victor Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 741 | # 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 Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 749 | """) |
Victor Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 750 | test = self.create_test('forever', code=code) |
Victor Stinner | 9a14214 | 2015-09-30 13:51:17 +0200 | [diff] [blame] | 751 | output = self.run_tests('--forever', test, exitcode=1) |
| 752 | self.check_executed_tests(output, [test]*3, failed=test) |
| 753 | |
Victor Stinner | 46d7535 | 2015-10-03 02:21:35 +0200 | [diff] [blame] | 754 | @unittest.skipUnless(Py_DEBUG, 'need a debug build') |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 755 | 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 Stinner | 20d2118 | 2015-10-03 21:40:21 +0200 | [diff] [blame] | 761 | # 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 Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 773 | 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 Stinner | d6e2502 | 2016-03-30 02:33:52 +0200 | [diff] [blame] | 778 | test = self.create_test('huntrleaks', code=code) |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 779 | |
| 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 Stinner | 254ad58 | 2016-09-19 00:11:30 +0200 | [diff] [blame] | 791 | self.assertIn(line2, output) |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 792 | |
| 793 | with open(filename) as fp: |
| 794 | reflog = fp.read() |
Victor Stinner | 254ad58 | 2016-09-19 00:11:30 +0200 | [diff] [blame] | 795 | self.assertIn(line2, reflog) |
Victor Stinner | 076fc87 | 2015-10-03 00:20:56 +0200 | [diff] [blame] | 796 | |
Victor Stinner | 5f9d3ac | 2015-10-03 00:21:12 +0200 | [diff] [blame] | 797 | 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 Stinner | 6b41510 | 2015-09-28 23:16:17 +0200 | [diff] [blame] | 804 | |
Chris Jerdonek | d6c18dc | 2012-12-27 18:53:12 -0800 | [diff] [blame] | 805 | if __name__ == '__main__': |
Serhiy Storchaka | 64f7c4e | 2013-08-29 12:26:23 +0300 | [diff] [blame] | 806 | unittest.main() |