Browse Source

Cleanup up lit_test and require explicit tool paths (#1049)

I think lit_test accrued a bit of cruft as I switched approaches... I still may go further, but this is particularly fixing a bug where `FileCheck` (instead of `%{FileCheck}`) should've failed in tests.
Jon Meow 4 năm trước cách đây
mục cha
commit
9009ae5e1b

+ 1 - 1
bazel/testing/lit_test.bzl

@@ -31,6 +31,6 @@ def lit_test(name, test_dir, data = None, **kwargs):
         srcs = ["//bazel/testing:lit_test.py"],
         main = "//bazel/testing:lit_test.py",
         data = data + native.glob([test_dir + "/**"]),
-        args = [test_dir, "--"],
+        args = ["--package_name=%s" % native.package_name(), "--test_dir=%s" % test_dir, "--"],
         **kwargs
     )

+ 18 - 43
bazel/testing/lit_test.py

@@ -8,16 +8,18 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
 import argparse
 import os
+from pathlib import Path
 import subprocess
 
-_PASSTHROUGH_FLAGS = ["filter", "filter-out"]
-
 
 def _parse_args():
     """Parses command line arguments, returning the result."""
     arg_parser = argparse.ArgumentParser(description=__doc__)
     arg_parser.add_argument(
-        "test_dir", help="The directory containing tests to run."
+        "--package_name", help="The directory containing tests to run."
+    )
+    arg_parser.add_argument(
+        "--test_dir", help="The directory containing tests to run."
     )
     arg_parser.add_argument(
         "lit_args", nargs="*", help="Arguments to pass through to lit."
@@ -25,54 +27,27 @@ def _parse_args():
     return arg_parser.parse_args()
 
 
-def _normalize(relative_base, target):
-    """Given a target, normalizes it to a relative path."""
-    assert target
-    if target.startswith(":"):
-        # Local target; :foo -> my/dir/foo
-        return os.path.join(relative_base, target[1:])
-    elif target[0].isalpha():
-        # Local target; foo -> my/dir/foo
-        return os.path.join(relative_base, target)
-
-    if ":" in target:
-        # Specified target; //foo:bar -> //foo/bar
-        target = target.replace(":", "/")
-    else:
-        # Default target; //foo -> //foo/foo
-        target = os.path.join(target, os.path.basename(target))
-
-    if target.startswith("@"):
-        return os.path.join("external/", target[1:])
-    elif target.startswith("//"):
-        return target[2:]
-    else:
-        raise ValueError("Unhandled target path: %s" % target)
-
-
 def main():
     parsed_args = _parse_args()
 
-    bin_dir = os.getcwd()
-
-    # Figure out the actual path for the test_dir.
-    relative_base = os.path.dirname(_normalize("", os.environ["TEST_TARGET"]))
-    test_dir = os.path.join(
-        bin_dir, _normalize(relative_base, parsed_args.test_dir)
-    )
-
     args = [
-        os.path.join(os.environ["TEST_SRCDIR"], "llvm-project/llvm/lit"),
-        "--path=%s" % bin_dir,
-        test_dir,
+        str(Path(os.environ["TEST_SRCDIR"]).joinpath("llvm-project/llvm/lit")),
+        str(
+            Path.cwd().joinpath(parsed_args.package_name, parsed_args.test_dir)
+        ),
         "-sv",
     ]
 
+    # Force tests to be explicit about command paths.
+    env = os.environ.copy()
+    del env["PATH"]
+
     # Run lit.
-    p = subprocess.run(args=args + parsed_args.lit_args)
-    # Do this instead of check_call to hide stack traces.
-    if p.returncode != 0:
-        exit("lit failed, exit code %d" % p.returncode)
+    try:
+        subprocess.check_call(args=args + parsed_args.lit_args, env=env)
+    except subprocess.CalledProcessError as e:
+        # Print without the stack trace.
+        print(e)
 
 
 if __name__ == "__main__":

+ 1 - 1
executable_semantics/testdata/struct/fail_equality_type.carbon

@@ -2,7 +2,7 @@
 // Exceptions. See /LICENSE for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
-// RUN: %{not} %{executable_semantics} %s 2>&1 2>&1 | FileCheck %s
+// RUN: %{not} %{executable_semantics} %s 2>&1 2>&1 | %{FileCheck} %s
 // AUTOUPDATE: %{executable_semantics} %s
 // CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/struct/fail_equality_type.carbon:16: type error in ==
 // CHECK: expected: {.x: i32, .y: i32}

+ 1 - 1
executable_semantics/testdata/tuple/fail_equality_type.carbon

@@ -2,7 +2,7 @@
 // Exceptions. See /LICENSE for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
-// RUN: %{not} %{executable_semantics} %s 2>&1 2>&1 | FileCheck %s
+// RUN: %{not} %{executable_semantics} %s 2>&1 2>&1 | %{FileCheck} %s
 // AUTOUPDATE: %{executable_semantics} %s
 // CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/tuple/fail_equality_type.carbon:16: type error in ==
 // CHECK: expected: (i32, i32)