| 1 | n/a | import argparse |
|---|
| 2 | n/a | import os |
|---|
| 3 | n/a | import sys |
|---|
| 4 | n/a | from test import support |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | USAGE = """\ |
|---|
| 8 | n/a | python -m test [options] [test_name1 [test_name2 ...]] |
|---|
| 9 | n/a | python path/to/Lib/test/regrtest.py [options] [test_name1 [test_name2 ...]] |
|---|
| 10 | n/a | """ |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | DESCRIPTION = """\ |
|---|
| 13 | n/a | Run Python regression tests. |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | If no arguments or options are provided, finds all files matching |
|---|
| 16 | n/a | the pattern "test_*" in the Lib/test subdirectory and runs |
|---|
| 17 | n/a | them in alphabetical order (but see -M and -u, below, for exceptions). |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | For more rigorous testing, it is useful to use the following |
|---|
| 20 | n/a | command line: |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | python -E -Wd -m test [options] [test_name1 ...] |
|---|
| 23 | n/a | """ |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | EPILOG = """\ |
|---|
| 26 | n/a | Additional option details: |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | -r randomizes test execution order. You can use --randseed=int to provide an |
|---|
| 29 | n/a | int seed value for the randomizer; this is useful for reproducing troublesome |
|---|
| 30 | n/a | test orders. |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | -s On the first invocation of regrtest using -s, the first test file found |
|---|
| 33 | n/a | or the first test file given on the command line is run, and the name of |
|---|
| 34 | n/a | the next test is recorded in a file named pynexttest. If run from the |
|---|
| 35 | n/a | Python build directory, pynexttest is located in the 'build' subdirectory, |
|---|
| 36 | n/a | otherwise it is located in tempfile.gettempdir(). On subsequent runs, |
|---|
| 37 | n/a | the test in pynexttest is run, and the next test is written to pynexttest. |
|---|
| 38 | n/a | When the last test has been run, pynexttest is deleted. In this way it |
|---|
| 39 | n/a | is possible to single step through the test files. This is useful when |
|---|
| 40 | n/a | doing memory analysis on the Python interpreter, which process tends to |
|---|
| 41 | n/a | consume too many resources to run the full regression test non-stop. |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | -S is used to continue running tests after an aborted run. It will |
|---|
| 44 | n/a | maintain the order a standard run (ie, this assumes -r is not used). |
|---|
| 45 | n/a | This is useful after the tests have prematurely stopped for some external |
|---|
| 46 | n/a | reason and you want to start running from where you left off rather |
|---|
| 47 | n/a | than starting from the beginning. |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | -f reads the names of tests from the file given as f's argument, one |
|---|
| 50 | n/a | or more test names per line. Whitespace is ignored. Blank lines and |
|---|
| 51 | n/a | lines beginning with '#' are ignored. This is especially useful for |
|---|
| 52 | n/a | whittling down failures involving interactions among tests. |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | -L causes the leaks(1) command to be run just before exit if it exists. |
|---|
| 55 | n/a | leaks(1) is available on Mac OS X and presumably on some other |
|---|
| 56 | n/a | FreeBSD-derived systems. |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | -R runs each test several times and examines sys.gettotalrefcount() to |
|---|
| 59 | n/a | see if the test appears to be leaking references. The argument should |
|---|
| 60 | n/a | be of the form stab:run:fname where 'stab' is the number of times the |
|---|
| 61 | n/a | test is run to let gettotalrefcount settle down, 'run' is the number |
|---|
| 62 | n/a | of times further it is run and 'fname' is the name of the file the |
|---|
| 63 | n/a | reports are written to. These parameters all have defaults (5, 4 and |
|---|
| 64 | n/a | "reflog.txt" respectively), and the minimal invocation is '-R :'. |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | -M runs tests that require an exorbitant amount of memory. These tests |
|---|
| 67 | n/a | typically try to ascertain containers keep working when containing more than |
|---|
| 68 | n/a | 2 billion objects, which only works on 64-bit systems. There are also some |
|---|
| 69 | n/a | tests that try to exhaust the address space of the process, which only makes |
|---|
| 70 | n/a | sense on 32-bit systems with at least 2Gb of memory. The passed-in memlimit, |
|---|
| 71 | n/a | which is a string in the form of '2.5Gb', determines howmuch memory the |
|---|
| 72 | n/a | tests will limit themselves to (but they may go slightly over.) The number |
|---|
| 73 | n/a | shouldn't be more memory than the machine has (including swap memory). You |
|---|
| 74 | n/a | should also keep in mind that swap memory is generally much, much slower |
|---|
| 75 | n/a | than RAM, and setting memlimit to all available RAM or higher will heavily |
|---|
| 76 | n/a | tax the machine. On the other hand, it is no use running these tests with a |
|---|
| 77 | n/a | limit of less than 2.5Gb, and many require more than 20Gb. Tests that expect |
|---|
| 78 | n/a | to use more than memlimit memory will be skipped. The big-memory tests |
|---|
| 79 | n/a | generally run very, very long. |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | -u is used to specify which special resource intensive tests to run, |
|---|
| 82 | n/a | such as those requiring large file support or network connectivity. |
|---|
| 83 | n/a | The argument is a comma-separated list of words indicating the |
|---|
| 84 | n/a | resources to test. Currently only the following are defined: |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | all - Enable all special resources. |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | none - Disable all special resources (this is the default). |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | audio - Tests that use the audio device. (There are known |
|---|
| 91 | n/a | cases of broken audio drivers that can crash Python or |
|---|
| 92 | n/a | even the Linux kernel.) |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | curses - Tests that use curses and will modify the terminal's |
|---|
| 95 | n/a | state and output modes. |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | largefile - It is okay to run some test that may create huge |
|---|
| 98 | n/a | files. These tests can take a long time and may |
|---|
| 99 | n/a | consume >2GB of disk space temporarily. |
|---|
| 100 | n/a | |
|---|
| 101 | n/a | network - It is okay to run tests that use external network |
|---|
| 102 | n/a | resource, e.g. testing SSL support for sockets. |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | decimal - Test the decimal module against a large suite that |
|---|
| 105 | n/a | verifies compliance with standards. |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | cpu - Used for certain CPU-heavy tests. |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | subprocess Run all tests for the subprocess module. |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | urlfetch - It is okay to download files required on testing. |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | gui - Run tests that require a running GUI. |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | tzdata - Run tests that require timezone data. |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | To enable all resources except one, use '-uall,-<resource>'. For |
|---|
| 118 | n/a | example, to run all the tests except for the gui tests, give the |
|---|
| 119 | n/a | option '-uall,-gui'. |
|---|
| 120 | n/a | """ |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | |
|---|
| 123 | n/a | RESOURCE_NAMES = ('audio', 'curses', 'largefile', 'network', |
|---|
| 124 | n/a | 'decimal', 'cpu', 'subprocess', 'urlfetch', 'gui', 'tzdata') |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | class _ArgParser(argparse.ArgumentParser): |
|---|
| 127 | n/a | |
|---|
| 128 | n/a | def error(self, message): |
|---|
| 129 | n/a | super().error(message + "\nPass -h or --help for complete help.") |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | def _create_parser(): |
|---|
| 133 | n/a | # Set prog to prevent the uninformative "__main__.py" from displaying in |
|---|
| 134 | n/a | # error messages when using "python -m test ...". |
|---|
| 135 | n/a | parser = _ArgParser(prog='regrtest.py', |
|---|
| 136 | n/a | usage=USAGE, |
|---|
| 137 | n/a | description=DESCRIPTION, |
|---|
| 138 | n/a | epilog=EPILOG, |
|---|
| 139 | n/a | add_help=False, |
|---|
| 140 | n/a | formatter_class=argparse.RawDescriptionHelpFormatter) |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | # Arguments with this clause added to its help are described further in |
|---|
| 143 | n/a | # the epilog's "Additional option details" section. |
|---|
| 144 | n/a | more_details = ' See the section at bottom for more details.' |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | group = parser.add_argument_group('General options') |
|---|
| 147 | n/a | # We add help explicitly to control what argument group it renders under. |
|---|
| 148 | n/a | group.add_argument('-h', '--help', action='help', |
|---|
| 149 | n/a | help='show this help message and exit') |
|---|
| 150 | n/a | group.add_argument('--timeout', metavar='TIMEOUT', type=float, |
|---|
| 151 | n/a | help='dump the traceback and exit if a test takes ' |
|---|
| 152 | n/a | 'more than TIMEOUT seconds; disabled if TIMEOUT ' |
|---|
| 153 | n/a | 'is negative or equals to zero') |
|---|
| 154 | n/a | group.add_argument('--wait', action='store_true', |
|---|
| 155 | n/a | help='wait for user input, e.g., allow a debugger ' |
|---|
| 156 | n/a | 'to be attached') |
|---|
| 157 | n/a | group.add_argument('--slaveargs', metavar='ARGS') |
|---|
| 158 | n/a | group.add_argument('-S', '--start', metavar='START', |
|---|
| 159 | n/a | help='the name of the test at which to start.' + |
|---|
| 160 | n/a | more_details) |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | group = parser.add_argument_group('Verbosity') |
|---|
| 163 | n/a | group.add_argument('-v', '--verbose', action='count', |
|---|
| 164 | n/a | help='run tests in verbose mode with output to stdout') |
|---|
| 165 | n/a | group.add_argument('-w', '--verbose2', action='store_true', |
|---|
| 166 | n/a | help='re-run failed tests in verbose mode') |
|---|
| 167 | n/a | group.add_argument('-W', '--verbose3', action='store_true', |
|---|
| 168 | n/a | help='display test output on failure') |
|---|
| 169 | n/a | group.add_argument('-q', '--quiet', action='store_true', |
|---|
| 170 | n/a | help='no output unless one or more tests fail') |
|---|
| 171 | n/a | group.add_argument('-o', '--slowest', action='store_true', dest='print_slow', |
|---|
| 172 | n/a | help='print the slowest 10 tests') |
|---|
| 173 | n/a | group.add_argument('--header', action='store_true', |
|---|
| 174 | n/a | help='print header with interpreter info') |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | group = parser.add_argument_group('Selecting tests') |
|---|
| 177 | n/a | group.add_argument('-r', '--randomize', action='store_true', |
|---|
| 178 | n/a | help='randomize test execution order.' + more_details) |
|---|
| 179 | n/a | group.add_argument('--randseed', metavar='SEED', |
|---|
| 180 | n/a | dest='random_seed', type=int, |
|---|
| 181 | n/a | help='pass a random seed to reproduce a previous ' |
|---|
| 182 | n/a | 'random run') |
|---|
| 183 | n/a | group.add_argument('-f', '--fromfile', metavar='FILE', |
|---|
| 184 | n/a | help='read names of tests to run from a file.' + |
|---|
| 185 | n/a | more_details) |
|---|
| 186 | n/a | group.add_argument('-x', '--exclude', action='store_true', |
|---|
| 187 | n/a | help='arguments are tests to *exclude*') |
|---|
| 188 | n/a | group.add_argument('-s', '--single', action='store_true', |
|---|
| 189 | n/a | help='single step through a set of tests.' + |
|---|
| 190 | n/a | more_details) |
|---|
| 191 | n/a | group.add_argument('-m', '--match', metavar='PAT', |
|---|
| 192 | n/a | dest='match_tests', |
|---|
| 193 | n/a | help='match test cases and methods with glob pattern PAT') |
|---|
| 194 | n/a | group.add_argument('-G', '--failfast', action='store_true', |
|---|
| 195 | n/a | help='fail as soon as a test fails (only with -v or -W)') |
|---|
| 196 | n/a | group.add_argument('-u', '--use', metavar='RES1,RES2,...', |
|---|
| 197 | n/a | action='append', type=resources_list, |
|---|
| 198 | n/a | help='specify which special resource intensive tests ' |
|---|
| 199 | n/a | 'to run.' + more_details) |
|---|
| 200 | n/a | group.add_argument('-M', '--memlimit', metavar='LIMIT', |
|---|
| 201 | n/a | help='run very large memory-consuming tests.' + |
|---|
| 202 | n/a | more_details) |
|---|
| 203 | n/a | group.add_argument('--testdir', metavar='DIR', |
|---|
| 204 | n/a | type=relative_filename, |
|---|
| 205 | n/a | help='execute test files in the specified directory ' |
|---|
| 206 | n/a | '(instead of the Python stdlib test suite)') |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | group = parser.add_argument_group('Special runs') |
|---|
| 209 | n/a | group.add_argument('-l', '--findleaks', action='store_true', |
|---|
| 210 | n/a | help='if GC is available detect tests that leak memory') |
|---|
| 211 | n/a | group.add_argument('-L', '--runleaks', action='store_true', |
|---|
| 212 | n/a | help='run the leaks(1) command just before exit.' + |
|---|
| 213 | n/a | more_details) |
|---|
| 214 | n/a | group.add_argument('-R', '--huntrleaks', metavar='RUNCOUNTS', |
|---|
| 215 | n/a | type=huntrleaks, |
|---|
| 216 | n/a | help='search for reference leaks (needs debug build, ' |
|---|
| 217 | n/a | 'very slow).' + more_details) |
|---|
| 218 | n/a | group.add_argument('-j', '--multiprocess', metavar='PROCESSES', |
|---|
| 219 | n/a | dest='use_mp', type=int, |
|---|
| 220 | n/a | help='run PROCESSES processes at once') |
|---|
| 221 | n/a | group.add_argument('-T', '--coverage', action='store_true', |
|---|
| 222 | n/a | dest='trace', |
|---|
| 223 | n/a | help='turn on code coverage tracing using the trace ' |
|---|
| 224 | n/a | 'module') |
|---|
| 225 | n/a | group.add_argument('-D', '--coverdir', metavar='DIR', |
|---|
| 226 | n/a | type=relative_filename, |
|---|
| 227 | n/a | help='directory where coverage files are put') |
|---|
| 228 | n/a | group.add_argument('-N', '--nocoverdir', |
|---|
| 229 | n/a | action='store_const', const=None, dest='coverdir', |
|---|
| 230 | n/a | help='put coverage files alongside modules') |
|---|
| 231 | n/a | group.add_argument('-t', '--threshold', metavar='THRESHOLD', |
|---|
| 232 | n/a | type=int, |
|---|
| 233 | n/a | help='call gc.set_threshold(THRESHOLD)') |
|---|
| 234 | n/a | group.add_argument('-n', '--nowindows', action='store_true', |
|---|
| 235 | n/a | help='suppress error message boxes on Windows') |
|---|
| 236 | n/a | group.add_argument('-F', '--forever', action='store_true', |
|---|
| 237 | n/a | help='run the specified tests in a loop, until an ' |
|---|
| 238 | n/a | 'error happens') |
|---|
| 239 | n/a | group.add_argument('--list-tests', action='store_true', |
|---|
| 240 | n/a | help="only write the name of tests that will be run, " |
|---|
| 241 | n/a | "don't execute them") |
|---|
| 242 | n/a | group.add_argument('-P', '--pgo', dest='pgo', action='store_true', |
|---|
| 243 | n/a | help='enable Profile Guided Optimization training') |
|---|
| 244 | n/a | |
|---|
| 245 | n/a | return parser |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | def relative_filename(string): |
|---|
| 249 | n/a | # CWD is replaced with a temporary dir before calling main(), so we |
|---|
| 250 | n/a | # join it with the saved CWD so it ends up where the user expects. |
|---|
| 251 | n/a | return os.path.join(support.SAVEDCWD, string) |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | def huntrleaks(string): |
|---|
| 255 | n/a | args = string.split(':') |
|---|
| 256 | n/a | if len(args) not in (2, 3): |
|---|
| 257 | n/a | raise argparse.ArgumentTypeError( |
|---|
| 258 | n/a | 'needs 2 or 3 colon-separated arguments') |
|---|
| 259 | n/a | nwarmup = int(args[0]) if args[0] else 5 |
|---|
| 260 | n/a | ntracked = int(args[1]) if args[1] else 4 |
|---|
| 261 | n/a | fname = args[2] if len(args) > 2 and args[2] else 'reflog.txt' |
|---|
| 262 | n/a | return nwarmup, ntracked, fname |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | def resources_list(string): |
|---|
| 266 | n/a | u = [x.lower() for x in string.split(',')] |
|---|
| 267 | n/a | for r in u: |
|---|
| 268 | n/a | if r == 'all' or r == 'none': |
|---|
| 269 | n/a | continue |
|---|
| 270 | n/a | if r[0] == '-': |
|---|
| 271 | n/a | r = r[1:] |
|---|
| 272 | n/a | if r not in RESOURCE_NAMES: |
|---|
| 273 | n/a | raise argparse.ArgumentTypeError('invalid resource: ' + r) |
|---|
| 274 | n/a | return u |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | |
|---|
| 277 | n/a | def _parse_args(args, **kwargs): |
|---|
| 278 | n/a | # Defaults |
|---|
| 279 | n/a | ns = argparse.Namespace(testdir=None, verbose=0, quiet=False, |
|---|
| 280 | n/a | exclude=False, single=False, randomize=False, fromfile=None, |
|---|
| 281 | n/a | findleaks=False, use_resources=None, trace=False, coverdir='coverage', |
|---|
| 282 | n/a | runleaks=False, huntrleaks=False, verbose2=False, print_slow=False, |
|---|
| 283 | n/a | random_seed=None, use_mp=None, verbose3=False, forever=False, |
|---|
| 284 | n/a | header=False, failfast=False, match_tests=None, pgo=False) |
|---|
| 285 | n/a | for k, v in kwargs.items(): |
|---|
| 286 | n/a | if not hasattr(ns, k): |
|---|
| 287 | n/a | raise TypeError('%r is an invalid keyword argument ' |
|---|
| 288 | n/a | 'for this function' % k) |
|---|
| 289 | n/a | setattr(ns, k, v) |
|---|
| 290 | n/a | if ns.use_resources is None: |
|---|
| 291 | n/a | ns.use_resources = [] |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | parser = _create_parser() |
|---|
| 294 | n/a | # Issue #14191: argparse doesn't support "intermixed" positional and |
|---|
| 295 | n/a | # optional arguments. Use parse_known_args() as workaround. |
|---|
| 296 | n/a | ns.args = parser.parse_known_args(args=args, namespace=ns)[1] |
|---|
| 297 | n/a | for arg in ns.args: |
|---|
| 298 | n/a | if arg.startswith('-'): |
|---|
| 299 | n/a | parser.error("unrecognized arguments: %s" % arg) |
|---|
| 300 | n/a | sys.exit(1) |
|---|
| 301 | n/a | |
|---|
| 302 | n/a | if ns.single and ns.fromfile: |
|---|
| 303 | n/a | parser.error("-s and -f don't go together!") |
|---|
| 304 | n/a | if ns.use_mp is not None and ns.trace: |
|---|
| 305 | n/a | parser.error("-T and -j don't go together!") |
|---|
| 306 | n/a | if ns.use_mp is not None and ns.findleaks: |
|---|
| 307 | n/a | parser.error("-l and -j don't go together!") |
|---|
| 308 | n/a | if ns.failfast and not (ns.verbose or ns.verbose3): |
|---|
| 309 | n/a | parser.error("-G/--failfast needs either -v or -W") |
|---|
| 310 | n/a | if ns.pgo and (ns.verbose or ns.verbose2 or ns.verbose3): |
|---|
| 311 | n/a | parser.error("--pgo/-v don't go together!") |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | if ns.nowindows: |
|---|
| 314 | n/a | print("Warning: the --nowindows (-n) option is deprecated. " |
|---|
| 315 | n/a | "Use -vv to display assertions in stderr.", file=sys.stderr) |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | if ns.quiet: |
|---|
| 318 | n/a | ns.verbose = 0 |
|---|
| 319 | n/a | if ns.timeout is not None: |
|---|
| 320 | n/a | if ns.timeout <= 0: |
|---|
| 321 | n/a | ns.timeout = None |
|---|
| 322 | n/a | if ns.use_mp is not None: |
|---|
| 323 | n/a | if ns.use_mp <= 0: |
|---|
| 324 | n/a | # Use all cores + extras for tests that like to sleep |
|---|
| 325 | n/a | ns.use_mp = 2 + (os.cpu_count() or 1) |
|---|
| 326 | n/a | if ns.use: |
|---|
| 327 | n/a | for a in ns.use: |
|---|
| 328 | n/a | for r in a: |
|---|
| 329 | n/a | if r == 'all': |
|---|
| 330 | n/a | ns.use_resources[:] = RESOURCE_NAMES |
|---|
| 331 | n/a | continue |
|---|
| 332 | n/a | if r == 'none': |
|---|
| 333 | n/a | del ns.use_resources[:] |
|---|
| 334 | n/a | continue |
|---|
| 335 | n/a | remove = False |
|---|
| 336 | n/a | if r[0] == '-': |
|---|
| 337 | n/a | remove = True |
|---|
| 338 | n/a | r = r[1:] |
|---|
| 339 | n/a | if remove: |
|---|
| 340 | n/a | if r in ns.use_resources: |
|---|
| 341 | n/a | ns.use_resources.remove(r) |
|---|
| 342 | n/a | elif r not in ns.use_resources: |
|---|
| 343 | n/a | ns.use_resources.append(r) |
|---|
| 344 | n/a | if ns.random_seed is not None: |
|---|
| 345 | n/a | ns.randomize = True |
|---|
| 346 | n/a | |
|---|
| 347 | n/a | return ns |
|---|