Skip to content

Commit

Permalink
[lit] Leverage argparse features to remove some code
Browse files Browse the repository at this point in the history
Reviewed By: rnk, serge-sans-paille

Differential Revision: https://reviews.llvm.org/D68589

llvm-svn: 374405
  • Loading branch information
yln committed Oct 10, 2019
1 parent e80a261 commit 822946c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
58 changes: 26 additions & 32 deletions llvm/utils/lit/lit/cl_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('test_paths',
nargs='*',
nargs='+',
help='Files or paths to include in the test suite')

parser.add_argument("--version",
Expand All @@ -20,13 +20,12 @@ def parse_args():
dest="numWorkers",
metavar="N",
help="Number of workers used for testing",
type=int,
default=None)
type=_positive_int,
default=lit.util.detectCPUs())
parser.add_argument("--config-prefix",
dest="configPrefix",
metavar="NAME",
help="Prefix for 'lit' config files",
action="store",
default=None)
parser.add_argument("-D", "--param",
dest="userParameters",
Expand Down Expand Up @@ -66,7 +65,6 @@ def parse_args():
format_group.add_argument("-o", "--output",
dest="output_path",
help="Write test results to the provided path",
action="store",
metavar="PATH")
format_group.add_argument("--no-progress-bar",
dest="useProgressBar",
Expand Down Expand Up @@ -128,23 +126,20 @@ def parse_args():
execution_group.add_argument("--max-failures",
dest="maxFailures",
help="Stop execution after the given number of failures.",
action="store",
type=int,
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",
action="store",
type=int,
default=None)
selection_group.add_argument("--max-time",
dest="maxTime",
metavar="N",
help="Maximum time to spend testing (in seconds)",
action="store",
type=float,
default=None)
selection_group.add_argument("--shuffle",
Expand All @@ -158,19 +153,18 @@ def parse_args():
selection_group.add_argument("--filter",
metavar="REGEX",
help="Only run tests with paths matching the given regular expression",
action="store",
default=os.environ.get("LIT_FILTER"))
selection_group.add_argument("--num-shards", dest="numShards", metavar="M",
selection_group.add_argument("--num-shards",
dest="numShards",
metavar="M",
help="Split testsuite into M pieces and only run one",
action="store",
type=int,
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",
action="store",
type=int,
type=_positive_int,
default=os.environ.get("LIT_RUN_SHARD"))

debug_group = parser.add_argument_group("Debug and Experimental Options")
Expand All @@ -192,27 +186,27 @@ def parse_args():
opts = parser.parse_args(sys.argv[1:] +
shlex.split(os.environ.get("LIT_OPTS", "")))

# Validate options
if not opts.test_paths:
parser.error('No inputs specified')

if opts.numWorkers is None:
opts.numWorkers = lit.util.detectCPUs()
elif opts.numWorkers <= 0:
parser.error("Option '--workers' or '-j' requires positive integer")

if opts.maxFailures is not None and opts.maxFailures <= 0:
parser.error("Option '--max-failures' requires positive integer")

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

if (opts.numShards is not None) or (opts.runShard is not None):
if (opts.numShards is None) or (opts.runShard is None):
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.numShards <= 0:
parser.error("--num-shards must be positive")
if (opts.runShard < 1) or (opts.runShard > opts.numShards):
if opts.runShard > opts.numShards:
parser.error("--run-shard must be between 1 and --num-shards (inclusive)")

return opts

def _positive_int(arg):
try:
n = int(arg)
except ValueError:
raise _arg_error('positive integer', arg)
if n <= 0:
raise _arg_error('positive integer', arg)
return n

def _arg_error(desc, arg):
msg = "requires %s, but found '%s'" % (desc, arg)
return argparse.ArgumentTypeError(msg)
2 changes: 1 addition & 1 deletion llvm/utils/lit/tests/max-failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# CHECK: Failing Tests (31)
# CHECK: Failing Tests (1)
# CHECK: Failing Tests (2)
# CHECK: error: Option '--max-failures' requires positive integer
# CHECK: error: argument --max-failures: requires positive integer, but found '0'
2 changes: 1 addition & 1 deletion llvm/utils/lit/tests/selecting.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
#
# RUN: not %{lit} --num-shards 0 --run-shard 2 %{inputs}/discovery >%t.out 2>%t.err
# RUN: FileCheck --check-prefix=CHECK-SHARD-ERR < %t.err %s
# CHECK-SHARD-ERR: error: --num-shards must be positive
# CHECK-SHARD-ERR: error: argument --num-shards: requires positive integer, but found '0'
#
# RUN: not %{lit} --num-shards 3 --run-shard 4 %{inputs}/discovery >%t.out 2>%t.err
# RUN: FileCheck --check-prefix=CHECK-SHARD-ERR2 < %t.err %s
Expand Down

0 comments on commit 822946c

Please sign in to comment.