[lit] Refactor ordering of tests
authorJulian Lettner <julian.lettner@gmail.com>
Mon, 25 Feb 2019 05:49:42 +0000 (21:49 -0800)
committerJulian Lettner <julian.lettner@apple.com>
Wed, 30 Oct 2019 00:49:23 +0000 (17:49 -0700)
llvm/utils/lit/lit/cl_arguments.py
llvm/utils/lit/lit/main.py

index 152c510..7b15041 100644 (file)
@@ -121,10 +121,10 @@ def parse_args():
             metavar="N",
             help="Maximum time to spend testing (in seconds)",
             type=_positive_int)
-    selection_group.add_argument("--shuffle",
-            help="Run tests in random order",
+    selection_group.add_argument("--shuffle",   # TODO(yln): --order=random
+            help="Run tests in random order",   # default or 'by-path' (+ isEarlyTest())
             action="store_true")
-    selection_group.add_argument("-i", "--incremental",
+    selection_group.add_argument("-i", "--incremental",  # TODO(yln): --order=failing-first
             help="Run modified and failing tests first (updates mtimes)",
             action="store_true")
     selection_group.add_argument("--filter",
@@ -167,6 +167,14 @@ def parse_args():
     if opts.echoAllCommands:
         opts.showOutput = True
 
+    # TODO(python3): Could be enum
+    if opts.shuffle:
+        opts.order = 'random'
+    elif opts.incremental:
+        opts.order = 'failing-first'
+    else:
+        opts.order = 'default'
+
     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")
@@ -174,7 +182,7 @@ def parse_args():
             parser.error("--run-shard must be between 1 and --num-shards (inclusive)")
         opts.shard = (opts.runShard, opts.numShards)
     else:
-      opts.shard = None
+        opts.shard = None
 
     return opts
 
index 6c38c5d..1276fda 100755 (executable)
@@ -63,7 +63,7 @@ def main(builtin_params = {}):
     if opts.filter:
         tests = [t for t in tests if opts.filter.search(t.getFullName())]
 
-    determine_order(tests, opts)
+    determine_order(tests, opts.order)
 
     if opts.shard:
         (run, shards) = opts.shard
@@ -132,19 +132,23 @@ def print_suites_or_tests(tests, opts):
             for test in ts_tests:
                 print('  %s' % (test.getFullName(),))
 
-def determine_order(tests, opts):
-    if opts.shuffle:
+
+def determine_order(tests, order):
+    assert order in ['default', 'random', 'failing-first']
+    if order == 'default':
+        tests.sort(key=lambda t: (not t.isEarlyTest(), t.getFullName()))
+    elif order == 'random':
         import random
         random.shuffle(tests)
-    elif opts.incremental:
+    else:
         def by_mtime(test):
-            try:
-                return os.path.getmtime(test.getFilePath())
-            except:
-                return 0
+            return os.path.getmtime(test.getFilePath())
         tests.sort(key=by_mtime, reverse=True)
-    else:
-        tests.sort(key=lambda t: (not t.isEarlyTest(), t.getFullName()))
+
+
+def touch_file(test):
+    if test.result.code.isFailure:
+        os.utime(test.getFilePath(), None)
 
 def filter_by_shard(tests, run, shards, litConfig):
     test_ixs = range(run - 1, len(tests), shards)
@@ -164,19 +168,13 @@ def filter_by_shard(tests, run, shards, litConfig):
     litConfig.note(msg)
     return selected_tests
 
-def update_incremental_cache(test):
-    if not test.result.code.isFailure:
-        return
-    fname = test.getFilePath()
-    os.utime(fname, None)
-
 def run_tests(tests, litConfig, opts, numTotalTests):
     display = lit.display.create_display(opts, len(tests), numTotalTests,
                                          opts.numWorkers)
     def progress_callback(test):
         display.update(test)
-        if opts.incremental:
-            update_incremental_cache(test)
+        if opts.order == 'failing-first':
+            touch_file(test)
 
     run = lit.run.create_run(tests, litConfig, opts.numWorkers,
                              progress_callback, opts.timeout)