From 984c90bbbad637cabac65f2e11a84e6375148ae2 Mon Sep 17 00:00:00 2001 From: Julian Lettner Date: Thu, 10 Oct 2019 18:03:37 +0000 Subject: [PATCH] [lit] Leverage argparse features to remove some code Reviewed By: rnk, serge-sans-paille Differential Revision: https://reviews.llvm.org/D68589 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@374405 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/lit/lit/cl_arguments.py | 58 +++++++++++++++------------------ utils/lit/tests/max-failures.py | 2 +- utils/lit/tests/selecting.py | 2 +- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/utils/lit/lit/cl_arguments.py b/utils/lit/lit/cl_arguments.py index a1ceee6aabb..49992609a6a 100644 --- a/utils/lit/lit/cl_arguments.py +++ b/utils/lit/lit/cl_arguments.py @@ -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) diff --git a/utils/lit/tests/max-failures.py b/utils/lit/tests/max-failures.py index a1c337fd605..3b85ae18611 100644 --- a/utils/lit/tests/max-failures.py +++ b/utils/lit/tests/max-failures.py @@ -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' diff --git a/utils/lit/tests/selecting.py b/utils/lit/tests/selecting.py index 25ac299d865..0d6fa938e4a 100644 --- a/utils/lit/tests/selecting.py +++ b/utils/lit/tests/selecting.py @@ -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 -- 2.40.0