/usr/share/pyshared/zope/testrunner/options.py is in python-zope.testrunner 4.0.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 | ##############################################################################
#
# Copyright (c) 2004-2008 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Command-line option parsing
"""
import optparse
import re
import os
import sys
import pkg_resources
from zope.testrunner.profiling import available_profilers
from zope.testrunner.formatter import (
OutputFormatter,
ColorfulOutputFormatter,
SubunitOutputFormatter,
)
from zope.testrunner.formatter import terminal_has_colors
parser = optparse.OptionParser("Usage: %prog [options] [MODULE] [TEST]")
######################################################################
# Searching and filtering
searching = optparse.OptionGroup(parser, "Searching and filtering", """\
Options in this group are used to define which tests to run.
""")
searching.add_option(
'--package', '--dir', '-s', action="append", dest='package',
help="""\
Search the given package's directories for tests. This can be
specified more than once to run tests in multiple parts of the source
tree. For example, if refactoring interfaces, you don't want to see
the way you have broken setups for tests in other packages. You *just*
want to run the interface tests.
Packages are supplied as dotted names. For compatibility with the old
test runner, forward and backward slashed in package names are
converted to dots.
(In the special case of packages spread over multiple directories,
only directories within the test search path are searched. See the
--path option.)
""")
searching.add_option(
'--module', '-m', action="append", dest='module',
help="""\
Specify a test-module filter as a regular expression. This is a
case-sensitive regular expression, used in search (not match) mode, to
limit which test modules are searched for tests. The regular
expressions are checked against dotted module names. In an extension
of Python regexp notation, a leading "!" is stripped and causes the
sense of the remaining regexp to be negated (so "!bc" matches any
string that does not match "bc", and vice versa). The option can be
specified multiple test-module filters. Test modules matching any of
the test filters are searched. If no test-module filter is specified,
then all test modules are used.
""")
searching.add_option(
'--test', '-t', action="append", dest='test',
help="""\
Specify a test filter as a regular expression. This is a
case-sensitive regular expression, used in search (not match) mode, to
limit which tests are run. In an extension of Python regexp notation,
a leading "!" is stripped and causes the sense of the remaining regexp
to be negated (so "!bc" matches any string that does not match "bc",
and vice versa). The option can be specified multiple test filters.
Tests matching any of the test filters are included. If no test
filter is specified, then all tests are run.
""")
searching.add_option(
'--unit', '-u', action="store_true", dest='unit',
help="""\
Run only unit tests, ignoring any layer options.
""")
searching.add_option(
'--non-unit', '-f', action="store_true", dest='non_unit',
help="""\
Run tests other than unit tests.
""")
searching.add_option(
'--layer', action="append", dest='layer',
help="""\
Specify a test layer to run. The option can be given multiple times
to specify more than one layer. If not specified, all layers are run.
It is common for the running script to provide default values for this
option. Layers are specified regular expressions, used in search
mode, for dotted names of objects that define a layer. In an
extension of Python regexp notation, a leading "!" is stripped and
causes the sense of the remaining regexp to be negated (so "!bc"
matches any string that does not match "bc", and vice versa). The
layer named 'zope.testrunner.layer.UnitTests' is reserved for
unit tests, however, take note of the --unit and non-unit options.
""")
searching.add_option(
'-a', '--at-level', type='int', dest='at_level',
help="""\
Run the tests at the given level. Any test at a level at or below
this is run, any test at a level above this is not run. Level 0
runs all tests.
""")
searching.add_option(
'--all', action="store_true", dest='all',
help="Run tests at all levels.")
searching.add_option(
'--list-tests', action="store_true", dest='list_tests',
help="List all tests that matched your filters. Do not run any tests.")
parser.add_option_group(searching)
######################################################################
# Reporting
reporting = optparse.OptionGroup(parser, "Reporting", """\
Reporting options control basic aspects of test-runner output
""")
reporting.add_option(
'--verbose', '-v', action="count", dest='verbose',
help="""\
Make output more verbose.
Increment the verbosity level.
""")
reporting.add_option(
'--quiet', '-q', action="store_true", dest='quiet',
help="""\
Make the output minimal, overriding any verbosity options.
""")
reporting.add_option(
'--progress', '-p', action="store_true", dest='progress',
help="""\
Output progress status
""")
reporting.add_option(
'--no-progress',action="store_false", dest='progress',
help="""\
Do not output progress status. This is the default, but can be used to
counter a previous use of --progress or -p.
""")
# We use a noop callback because the actual processing will be done in the
# get_options function, but we want optparse to generate appropriate help info
# for us, so we add an option anyway.
reporting.add_option(
'--auto-progress', action="callback", callback=lambda *args: None,
help="""\
Output progress status, but only when stdout is a terminal.
""")
reporting.add_option(
'--color', '-c', action="store_true", dest='color',
help="""\
Colorize the output.
""")
reporting.add_option(
'--no-color', '-C', action="store_false", dest='color',
help="""\
Do not colorize the output. This is the default, but can be used to
counter a previous use of --color or -c.
""")
# We use a noop callback because the actual processing will be done in the
# get_options function, but we want optparse to generate appropriate help info
# for us, so we add an option anyway.
reporting.add_option(
'--auto-color', action="callback", callback=lambda *args: None,
help="""\
Colorize the output, but only when stdout is a terminal.
""")
reporting.add_option(
'--subunit', action="store_true", dest='subunit',
help="""\
Use subunit output. Will not be colorized.
""")
reporting.add_option(
'--slow-test', type='float', dest='slow_test_threshold', metavar='N',
help="""\
With -c and -vvv, highlight tests that take longer than N seconds (default:
%default).
""")
reporting.add_option(
'-1', '--hide-secondary-failures',
action="store_true", dest='report_only_first_failure',
help="""\
Report only the first failure in a doctest. (Examples after the
failure are still executed, in case they do any cleanup.)
""")
reporting.add_option(
'--show-secondary-failures',
action="store_false", dest='report_only_first_failure',
help="""\
Report all failures in a doctest. This is the default, but can
be used to counter a default use of -1 or --hide-secondary-failures.
""")
reporting.add_option(
'--ndiff', action="store_true", dest="ndiff",
help="""\
When there is a doctest failure, show it as a diff using the ndiff.py utility.
""")
reporting.add_option(
'--udiff', action="store_true", dest="udiff",
help="""\
When there is a doctest failure, show it as a unified diff.
""")
reporting.add_option(
'--cdiff', action="store_true", dest="cdiff",
help="""\
When there is a doctest failure, show it as a context diff.
""")
parser.add_option_group(reporting)
######################################################################
# Analysis
analysis = optparse.OptionGroup(parser, "Analysis", """\
Analysis options provide tools for analysing test output.
""")
analysis.add_option(
'--stop-on-error', '--stop', '-x', action="store_true",
dest='stop_on_error',
help="Stop running tests after first test failure or error."
)
analysis.add_option(
'--post-mortem', '--pdb', '-D', action="store_true", dest='post_mortem',
help="Enable post-mortem debugging of test failures"
)
analysis.add_option(
'--gc', '-g', action="append", dest='gc', type="int",
help="""\
Set the garbage collector generation threshold. This can be used
to stress memory and gc correctness. Some crashes are only
reproducible when the threshold is set to 1 (aggressive garbage
collection). Do "--gc 0" to disable garbage collection altogether.
The --gc option can be used up to 3 times to specify up to 3 of the 3
Python gc_threshold settings.
""")
analysis.add_option(
'--gc-option', '-G', action="append", dest='gc_option', type="choice",
choices=['DEBUG_STATS', 'DEBUG_COLLECTABLE', 'DEBUG_UNCOLLECTABLE',
'DEBUG_INSTANCES', 'DEBUG_OBJECTS', 'DEBUG_SAVEALL',
'DEBUG_LEAK'],
help="""\
Set a Python gc-module debug flag. This option can be used more than
once to set multiple flags.
""")
analysis.add_option(
'--repeat', '-N', action="store", type="int", dest='repeat',
help="""\
Repeat the tests the given number of times. This option is used to
make sure that tests leave their environment in the state they found
it and, with the --report-refcounts option to look for memory leaks.
""")
analysis.add_option(
'--report-refcounts', '-r', action="store_true", dest='report_refcounts',
help="""\
After each run of the tests, output a report summarizing changes in
refcounts by object type. This option that requires that Python was
built with the --with-pydebug option to configure.
""")
analysis.add_option(
'--coverage', action="store", type='string', dest='coverage',
help="""\
Perform code-coverage analysis, saving trace data to the directory
with the given name. A code coverage summary is printed to standard
out.
""")
analysis.add_option(
'--profile', action="store", dest='profile', type="choice",
choices=available_profilers.keys(),
help="""\
Run the tests under cProfiler or hotshot and display the top 50 stats, sorted
by cumulative time and number of calls.
""")
def do_pychecker(*args):
if not os.environ.get("PYCHECKER"):
os.environ["PYCHECKER"] = "-q"
import pychecker.checker
analysis.add_option(
'--pychecker', action="callback", callback=do_pychecker,
help="""\
Run the tests under pychecker
""")
parser.add_option_group(analysis)
######################################################################
# Setup
setup = optparse.OptionGroup(parser, "Setup", """\
Setup options are normally supplied by the testrunner script, although
they can be overridden by users.
""")
setup.add_option(
'--path', action="append", dest='path',
help="""\
Specify a path to be added to Python's search path. This option can
be used multiple times to specify multiple search paths. The path is
usually specified by the test-runner script itself, rather than by
users of the script, although it can be overridden by users. Only
tests found in the path will be run.
This option also specifies directories to be searched for tests.
See the search_directory.
""")
setup.add_option(
'--test-path', action="append", dest='test_path',
help="""\
Specify a path to be searched for tests, but not added to the Python
search path. This option can be used multiple times to specify
multiple search paths. The path is usually specified by the
test-runner script itself, rather than by users of the script,
although it can be overridden by users. Only tests found in the path
will be run.
""")
setup.add_option(
'--package-path', action="append", dest='package_path', nargs=2,
help="""\
Specify a path to be searched for tests, but not added to the Python
search path. Also specify a package for files found in this path.
This is used to deal with directories that are stitched into packages
that are not otherwise searched for tests.
This option takes 2 arguments. The first is a path name. The second is
the package name.
This option can be used multiple times to specify
multiple search paths. The path is usually specified by the
test-runner script itself, rather than by users of the script,
although it can be overridden by users. Only tests found in the path
will be run.
""")
setup.add_option(
'--tests-pattern', action="store", dest='tests_pattern',
help="""\
The test runner looks for modules containing tests. It uses this
pattern to identify these modules. The modules may be either packages
or python files.
If a test module is a package, it uses the value given by the
test-file-pattern to identify python files within the package
containing tests.
""")
setup.add_option(
'--suite-name', action="store", dest='suite_name',
help="""\
Specify the name of the object in each test_module that contains the
module's test suite.
""")
setup.add_option(
'--test-file-pattern', action="store", dest='test_file_pattern',
help="""\
Specify a pattern for identifying python files within a tests package.
See the documentation for the --tests-pattern option.
""")
setup.add_option(
'--ignore_dir', action="append", dest='ignore_dir',
help="""\
Specifies the name of a directory to ignore when looking for tests.
""")
setup.add_option(
'--shuffle', action="store_true", dest='shuffle',
help="""\
Shuffles the order in which tests are ran.
""")
setup.add_option(
'--shuffle-seed', action="store", dest='shuffle_seed', type="int",
help="""\
Value used to initialize the tests shuffler. Specify a value to create
repeatable random ordered tests.
""")
parser.add_option_group(setup)
######################################################################
# Other
other = optparse.OptionGroup(parser, "Other", "Other options")
other.add_option(
'--version', action="store_true", dest='showversion',
help="Print the version of the testrunner, and exit.")
other.add_option(
'-j', action="store", type="int", dest='processes',
help="""\
Use up to given number of parallel processes to execute tests. May decrease
test run time substantially. Defaults to %default.
""")
other.add_option(
'--keepbytecode', '-k', action="store_true", dest='keepbytecode',
help="""\
Normally, the test runner scans the test paths and the test
directories looking for and deleting pyc or pyo files without
corresponding py files. This is to prevent spurious test failures due
to finding compiled modules where source modules have been deleted.
This scan can be time consuming. Using this option disables this
scan. If you know you haven't removed any modules since last running
the tests, can make the test run go much faster.
""")
other.add_option(
'--usecompiled', action="store_true", dest='usecompiled',
help="""\
Normally, a package must contain an __init__.py file, and only .py files
can contain test code. When this option is specified, compiled Python
files (.pyc and .pyo) can be used instead: a directory containing
__init__.pyc or __init__.pyo is also considered to be a package, and if
file XYZ.py contains tests but is absent while XYZ.pyc or XYZ.pyo exists
then the compiled files will be used. This is necessary when running
tests against a tree where the .py files have been removed after
compilation to .pyc/.pyo. Use of this option implies --keepbytecode.
""")
other.add_option(
'--exit-with-status', action="store_true", dest='exitwithstatus',
help="""DEPRECATED: The test runner will always exit with a status.\
""")
parser.add_option_group(other)
######################################################################
# Default values
parser.set_defaults(
ignore_dir=['.svn', 'CVS', '{arch}', '.arch-ids', '_darcs'],
tests_pattern='^tests$',
at_level=1,
test_file_pattern='^test',
suite_name='test_suite',
list_tests=False,
slow_test_threshold=10,
processes=1,
verbose=0,
repeat=1,
)
######################################################################
# Command-line processing
def compile_filter(pattern):
if pattern.startswith('!'):
pattern = re.compile(pattern[1:]).search
return (lambda s: not pattern(s))
return re.compile(pattern).search
def merge_options(options, defaults):
odict = options.__dict__
for name, value in defaults.__dict__.items():
if (value is not None) and (odict[name] is None):
odict[name] = value
def get_options(args=None, defaults=None):
# Because we want to inspect stdout and decide to colorize or not, we
# replace the --auto-color option with the appropriate --color or
# --no-color option. That way the subprocess doesn't have to decide (which
# it would do incorrectly anyway because stdout would be a pipe).
def apply_auto_color(args):
if args and '--auto-color' in args:
if sys.stdout.isatty() and terminal_has_colors():
colorization = '--color'
else:
colorization = '--no-color'
args[:] = [arg.replace('--auto-color', colorization)
for arg in args]
# The comment of apply_auto_color applies here as well
def apply_auto_progress(args):
if args and '--auto-progress' in args:
if sys.stdout.isatty():
progress = '--progress'
else:
progress = '--no-progress'
args[:] = [arg.replace('--auto-progress', progress)
for arg in args]
apply_auto_color(args)
apply_auto_color(defaults)
apply_auto_progress(args)
apply_auto_progress(defaults)
if defaults:
defaults, _ = parser.parse_args(defaults)
assert not _
else:
defaults = None
if args is None:
args = sys.argv
options, positional = parser.parse_args(args[1:], defaults)
options.original_testrunner_args = args
if options.showversion:
dist = pkg_resources.require('zope.testrunner')[0]
print 'zope.app.testrunner version %s' % dist.version
options.fail = True
return options
if options.subunit:
try:
import subunit
except ImportError:
print """\
Subunit is not installed. Please install Subunit
to generate subunit output.
"""
options.fail = True
return options
else:
options.output = SubunitOutputFormatter(options)
elif options.color:
options.output = ColorfulOutputFormatter(options)
options.output.slow_test_threshold = options.slow_test_threshold
else:
options.output = OutputFormatter(options)
options.fail = False
if positional:
module_filter = positional.pop(0)
if module_filter != '.':
if options.module:
options.module.append(module_filter)
else:
options.module = [module_filter]
if positional:
test_filter = positional.pop(0)
if options.test:
options.test.append(test_filter)
else:
options.test = [test_filter]
if positional:
parser.error("Too many positional arguments")
options.ignore_dir = dict([(d,1) for d in options.ignore_dir])
options.test_file_pattern = re.compile(options.test_file_pattern).search
options.tests_pattern = re.compile(options.tests_pattern).search
options.test = map(compile_filter, options.test or ('.'))
options.module = map(compile_filter, options.module or ('.'))
options.path = map(os.path.abspath, options.path or ())
options.test_path = map(os.path.abspath, options.test_path or ())
options.test_path += options.path
options.test_path = ([(path, '') for path in options.test_path]
+
[(os.path.abspath(path), package)
for (path, package) in options.package_path or ()
])
if options.package:
pkgmap = dict(options.test_path)
options.package = [normalize_package(p, pkgmap)
for p in options.package]
options.prefix = [(path + os.path.sep, package)
for (path, package) in options.test_path]
if options.all:
options.at_level = sys.maxint
if options.unit and options.non_unit:
# The test runner interprets this as "run only those tests that are
# both unit and non-unit at the same time". The user, however, wants
# to run both unit and non-unit tests. Disable the filtering so that
# the user will get what she wants:
options.unit = options.non_unit = False
if options.unit:
# XXX Argh.
options.layer = ['zope.testrunner.layer.UnitTests']
if options.layer:
options.layer = map(compile_filter, options.layer)
options.layer = options.layer and dict([(l, 1) for l in options.layer])
if options.usecompiled:
options.keepbytecode = options.usecompiled
if options.quiet:
options.verbose = 0
if options.report_refcounts and options.repeat < 2:
print """\
You must use the --repeat (-N) option to specify a repeat
count greater than 1 when using the --report_refcounts (-r)
option.
"""
options.fail = True
return options
if options.report_refcounts and not hasattr(sys, "gettotalrefcount"):
print """\
The Python you are running was not configured
with --with-pydebug. This is required to use
the --report-refcounts option.
"""
options.fail = True
return options
return options
def normalize_package(package, package_map={}):
r"""Normalize package name passed to the --package option.
>>> normalize_package('zope.testrunner')
'zope.testrunner'
Converts path names into package names for compatibility with the old
test runner.
>>> normalize_package('zope/testrunner')
'zope.testrunner'
>>> normalize_package('zope/testrunner/')
'zope.testrunner'
>>> normalize_package('zope\\testrunner')
'zope.testrunner'
Can use a map of absolute pathnames to package names
>>> a = os.path.abspath
>>> normalize_package('src/zope/testrunner/',
... {a('src'): ''})
'zope.testrunner'
>>> normalize_package('src/zope_testrunner/',
... {a('src/zope_testrunner'): 'zope.testrunner'})
'zope.testrunner'
>>> normalize_package('src/zope_something/tests',
... {a('src/zope_something'): 'zope.something',
... a('src'): ''})
'zope.something.tests'
"""
package = package.replace('\\', '/')
if package.endswith('/'):
package = package[:-1]
bits = package.split('/')
for n in range(len(bits), 0, -1):
pkg = package_map.get(os.path.abspath('/'.join(bits[:n])))
if pkg is not None:
bits = bits[n:]
if pkg:
bits = [pkg] + bits
return '.'.join(bits)
return package.replace('/', '.')
|