[llvm] r374405 - [lit] Leverage argparse features to remove some code
Julian Lettner via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 10 11:03:37 PDT 2019
Author: yln
Date: Thu Oct 10 11:03:37 2019
New Revision: 374405
URL: http://llvm.org/viewvc/llvm-project?rev=374405&view=rev
Log:
[lit] Leverage argparse features to remove some code
Reviewed By: rnk, serge-sans-paille
Differential Revision: https://reviews.llvm.org/D68589
Modified:
llvm/trunk/utils/lit/lit/cl_arguments.py
llvm/trunk/utils/lit/tests/max-failures.py
llvm/trunk/utils/lit/tests/selecting.py
Modified: llvm/trunk/utils/lit/lit/cl_arguments.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lit/lit/cl_arguments.py?rev=374405&r1=374404&r2=374405&view=diff
==============================================================================
--- llvm/trunk/utils/lit/lit/cl_arguments.py (original)
+++ llvm/trunk/utils/lit/lit/cl_arguments.py Thu Oct 10 11:03:37 2019
@@ -8,7 +8,7 @@ import lit.util
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",
@@ -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",
@@ -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",
@@ -128,8 +126,7 @@ 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")
@@ -137,14 +134,12 @@ def parse_args():
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",
@@ -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")
@@ -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)
Modified: llvm/trunk/utils/lit/tests/max-failures.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lit/tests/max-failures.py?rev=374405&r1=374404&r2=374405&view=diff
==============================================================================
--- llvm/trunk/utils/lit/tests/max-failures.py (original)
+++ llvm/trunk/utils/lit/tests/max-failures.py Thu Oct 10 11:03:37 2019
@@ -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'
Modified: llvm/trunk/utils/lit/tests/selecting.py
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/lit/tests/selecting.py?rev=374405&r1=374404&r2=374405&view=diff
==============================================================================
--- llvm/trunk/utils/lit/tests/selecting.py (original)
+++ llvm/trunk/utils/lit/tests/selecting.py Thu Oct 10 11:03:37 2019
@@ -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
More information about the llvm-commits
mailing list