[lit][NFC] Use enum to represent test order
authorJulian Lettner <julian.lettner@apple.com>
Wed, 27 Jan 2021 19:06:58 +0000 (11:06 -0800)
committerJulian Lettner <julian.lettner@apple.com>
Wed, 27 Jan 2021 19:10:17 +0000 (11:10 -0800)
Use an enum to more explicitly represent the test execution order.

llvm/utils/lit/lit/cl_arguments.py
llvm/utils/lit/lit/main.py

index ddb4d25..8111ff7 100644 (file)
@@ -1,4 +1,5 @@
 import argparse
+import enum
 import os
 import shlex
 import sys
@@ -7,6 +8,12 @@ import lit.reports
 import lit.util
 
 
+class TestOrder(enum.Enum):
+    EARLY_TESTS_THEN_BY_NAME = enum.auto()
+    FAILING_FIRST = enum.auto()
+    RANDOM = enum.auto()
+
+
 def parse_args():
     parser = argparse.ArgumentParser(prog='lit')
     parser.add_argument('test_paths',
@@ -187,13 +194,12 @@ def parse_args():
     if opts.echoAllCommands:
         opts.showOutput = True
 
-    # TODO(python3): Could be enum
     if opts.shuffle:
-        opts.order = 'random'
+        opts.order = TestOrder.RANDOM
     elif opts.incremental:
-        opts.order = 'failing-first'
+        opts.order = TestOrder.FAILING_FIRST
     else:
-        opts.order = 'default'
+        opts.order = TestOrder.EARLY_TESTS_THEN_BY_NAME
 
     if opts.numShards or opts.runShard:
         if not opts.numShards or not opts.runShard:
index 3d4f670..801687e 100755 (executable)
@@ -155,16 +155,16 @@ def print_discovered(tests, show_suites, show_tests):
 
 
 def determine_order(tests, order):
-    assert order in ['default', 'random', 'failing-first']
-    if order == 'default':
+    from lit.cl_arguments import TestOrder
+    if order == TestOrder.EARLY_TESTS_THEN_BY_NAME:
         tests.sort(key=lambda t: (not t.isEarlyTest(), t.getFullName()))
-    elif order == 'random':
-        import random
-        random.shuffle(tests)
-    else:
+    elif order == TestOrder.FAILING_FIRST:
         def by_mtime(test):
             return os.path.getmtime(test.getFilePath())
         tests.sort(key=by_mtime, reverse=True)
+    elif order == TestOrder.RANDOM:
+        import random
+        random.shuffle(tests)
 
 
 def touch_file(test):
@@ -203,7 +203,7 @@ def run_tests(tests, lit_config, opts, discovered_tests):
 
     def progress_callback(test):
         display.update(test)
-        if opts.order == 'failing-first':
+        if opts.order == lit.cl_arguments.TestOrder.FAILING_FIRST:
             touch_file(test)
 
     run = lit.run.Run(tests, lit_config, workers, progress_callback,