reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
import argparse
import os
import shlex
import sys

import lit.util

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('test_paths',
            nargs='+',
            help='Files or paths to include in the test suite')

    parser.add_argument("--version",
            dest="show_version",
            help="Show version and exit",
            action="store_true",
            default=False)
    parser.add_argument("-j", "--threads", "--workers",
            dest="numWorkers",
            metavar="N",
            help="Number of workers used for testing",
            type=_positive_int,
            default=lit.util.detectCPUs())
    parser.add_argument("--config-prefix",
            dest="configPrefix",
            metavar="NAME",
            help="Prefix for 'lit' config files",
            default=None)
    parser.add_argument("-D", "--param",
            dest="user_params",
            metavar="NAME=VAL",
            help="Add 'NAME' = 'VAL' to the user defined parameters",
            type=str,
            action="append",
            default=[])

    format_group = parser.add_argument_group("Output Format")
    # FIXME: I find these names very confusing, although I like the
    # functionality.
    format_group.add_argument("-q", "--quiet",
            help="Suppress no error output",
            action="store_true",
            default=False)
    format_group.add_argument("-s", "--succinct",
            help="Reduce amount of output",
            action="store_true",
            default=False)
    format_group.add_argument("-v", "--verbose",
            dest="showOutput",
            help="Show test output for failures",
            action="store_true",
            default=False)
    format_group.add_argument("-vv", "--echo-all-commands",
            dest="echoAllCommands",
            action="store_true",
            default=False,
            help="Echo all commands as they are executed to stdout. In case of "
                 "failure, last command shown will be the failing one.")
    format_group.add_argument("-a", "--show-all",
            dest="showAllOutput",
            help="Display all commandlines and output",
            action="store_true",
            default=False)
    format_group.add_argument("-o", "--output",
            dest="output_path",
            help="Write test results to the provided path",
            metavar="PATH")
    format_group.add_argument("--no-progress-bar",
            dest="useProgressBar",
            help="Do not use curses based progress bar",
            action="store_false",
            default=True)
    format_group.add_argument("--show-unsupported",
            help="Show unsupported tests",
            action="store_true",
            default=False)
    format_group.add_argument("--show-xfail",
            help="Show tests that were expected to fail",
            action="store_true",
            default=False)

    execution_group = parser.add_argument_group("Test Execution")
    execution_group.add_argument("--path",
            help="Additional paths to add to testing environment",
            action="append",
            type=str,
            default=[])
    execution_group.add_argument("--vg",
            dest="useValgrind",
            help="Run tests under valgrind",
            action="store_true",
            default=False)
    execution_group.add_argument("--vg-leak",
            dest="valgrindLeakCheck",
            help="Check for memory leaks under valgrind",
            action="store_true",
            default=False)
    execution_group.add_argument("--vg-arg",
            dest="valgrindArgs",
            metavar="ARG",
            help="Specify an extra argument for valgrind",
            type=str,
            action="append",
            default=[])
    execution_group.add_argument("--time-tests",
            dest="timeTests",
            help="Track elapsed wall time for each test",
            action="store_true",
            default=False)
    execution_group.add_argument("--no-execute",
            dest="noExecute",
            help="Don't execute any tests (assume PASS)",
            action="store_true",
            default=False)
    execution_group.add_argument("--xunit-xml-output",
            dest="xunit_output_file",
            help="Write XUnit-compatible XML test reports to the specified file",
            default=None)
    execution_group.add_argument("--timeout",
            dest="maxIndividualTestTime",
            help="Maximum time to spend running a single test (in seconds). "
                 "0 means no time limit. [Default: 0]",
            type=_non_negative_int,
            default=None)
    execution_group.add_argument("--max-failures",
            dest="maxFailures",
            help="Stop execution after the given number of failures.",
            type=_positive_int,
            default=None)

    selection_group = parser.add_argument_group("Test Selection")
    selection_group.add_argument("--max-tests",
            dest="maxTests",
            metavar="N",
            help="Maximum number of tests to run",
            type=int,
            default=None)
    selection_group.add_argument("--max-time",
            dest="maxTime",
            metavar="N",
            help="Maximum time to spend testing (in seconds)",
            type=float,
            default=None)
    selection_group.add_argument("--shuffle",
            help="Run tests in random order",
            action="store_true",
            default=False)
    selection_group.add_argument("-i", "--incremental",
            help="Run modified and failing tests first (updates mtimes)",
            action="store_true",
            default=False)
    selection_group.add_argument("--filter",
            metavar="REGEX",
            type=_case_insensitive_regex,
            help="Only run tests with paths matching the given regular expression",
            default=os.environ.get("LIT_FILTER"))
    selection_group.add_argument("--num-shards",
            dest="numShards",
            metavar="M",
            help="Split testsuite into M pieces and only run one",
            type=_positive_int,
            default=os.environ.get("LIT_NUM_SHARDS"))
    selection_group.add_argument("--run-shard",
            dest="runShard",
            metavar="N",
            help="Run shard #N of the testsuite",
            type=_positive_int,
            default=os.environ.get("LIT_RUN_SHARD"))

    debug_group = parser.add_argument_group("Debug and Experimental Options")
    debug_group.add_argument("--debug",
            help="Enable debugging (for 'lit' development)",
            action="store_true",
            default=False)
    debug_group.add_argument("--show-suites",
            dest="showSuites",
            help="Show discovered test suites",
            action="store_true",
            default=False)
    debug_group.add_argument("--show-tests",
            dest="showTests",
            help="Show all discovered tests",
            action="store_true",
            default=False)

    # LIT is special: environment variables override command line arguments.
    env_args = shlex.split(os.environ.get("LIT_OPTS", ""))
    args = sys.argv[1:] + env_args
    opts = parser.parse_args(args)

    # Validate command line options
    if opts.echoAllCommands:
        opts.showOutput = True

    if opts.numShards or opts.runShard:
        if not opts.numShards or not opts.runShard:
            parser.error("--num-shards and --run-shard must be used together")
        if opts.runShard > opts.numShards:
            parser.error("--run-shard must be between 1 and --num-shards (inclusive)")
        opts.shard = (opts.runShard, opts.numShards)
    else:
      opts.shard = None

    return opts

def _positive_int(arg):
    return _int(arg, 'positive', lambda i: i > 0)

def _non_negative_int(arg):
    return _int(arg, 'non-negative', lambda i: i >= 0)

def _int(arg, kind, pred):
    desc = "requires {} integer, but found '{}'"
    try:
        i = int(arg)
    except ValueError:
        raise _error(desc, kind, arg)
    if not pred(i):
        raise _error(desc, kind, arg)
    return i

def _case_insensitive_regex(arg):
    import re
    try:
        return re.compile(arg, re.IGNORECASE)
    except re.error as reason:
        raise _error("invalid regular expression: '{}', {}", arg, reason)

def _error(desc, *args):
    msg = desc.format(*args)
    return argparse.ArgumentTypeError(msg)