Browse Source

Add convenience .run targets for test files. (#2384)

e.g., for `//explorer/testdata:tuple/no_ending_comma.carbon.test`, `bazel run //explorer/testdata:tuple/no_ending_comma.carbon.run`
Jon Ross-Perkins 3 năm trước cách đây
mục cha
commit
fd455ed36b

+ 5 - 0
bazel/sh_run/BUILD

@@ -0,0 +1,5 @@
+# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+# Exceptions. See /LICENSE for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+exports_files(["exec.sh"])

+ 16 - 0
bazel/sh_run/exec.sh

@@ -0,0 +1,16 @@
+#!/usr/bin/env bash
+#
+# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+# Exceptions. See /LICENSE for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+# Turn any pwd-relative files into absolute paths. Other args may be
+# BUILD_WORKING_DIRECTORY-relative, which will be handled by the executed
+# binary.
+ARGS=("$@")
+for i in "${!ARGS[@]}"; do
+  if [[ -e "${ARGS[$i]}" ]]; then
+    ARGS[$i]="$(realpath ${ARGS[$i]})"
+  fi
+done
+exec "${ARGS[@]}"

+ 31 - 0
bazel/sh_run/rules.bzl

@@ -0,0 +1,31 @@
+# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+# Exceptions. See /LICENSE for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+"""Rules for building fuzz tests."""
+
+def sh_run(name, args, **kwargs):
+    """Produces a target which can run with the given args."""
+
+    native.sh_binary(
+        name = name,
+        srcs = ["//bazel/sh_run:exec.sh"],
+        args = args,
+        **kwargs
+    )
+
+def glob_sh_run(file_exts, args, data, run_ext = "run", **kwargs):
+    """Produces a per-file sh_run."""
+    files = native.glob(
+        ["**"],
+        exclude_directories = 1,
+    )
+    for f in files:
+        if f.split(".")[-1] not in file_exts:
+            continue
+        sh_run(
+            name = "%s.%s" % (f, run_ext),
+            args = args + ["$(location %s)" % f],
+            data = data + [f],
+            **kwargs
+        )

+ 2 - 0
explorer/README.md

@@ -101,7 +101,9 @@ To explain this boilerplate:
 ### Useful commands
 
 -   `./lit_autodupate.py` -- Updates expected output.
+    -   This can be combined with `git diff` to see changes in output.
 -   `bazel test ... --test_output=errors` -- Runs tests and prints any errors.
+-   `bazel run testdata/DIR/FILE.carbon.run` -- Runs explorer on the file.
 
 ### Updating fuzzer logic after making AST changes
 

+ 7 - 0
explorer/testdata/BUILD

@@ -2,6 +2,7 @@
 # Exceptions. See /LICENSE for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+load("//bazel/sh_run:rules.bzl", "glob_sh_run")
 load("//bazel/testing:lit.bzl", "glob_lit_tests")
 
 glob_lit_tests(
@@ -15,6 +16,12 @@ glob_lit_tests(
     test_file_exts = ["carbon"],
 )
 
+glob_sh_run(
+    args = ["$(location //explorer)"],
+    data = ["//explorer"],
+    file_exts = ["carbon"],
+)
+
 filegroup(
     name = "carbon_files",
     srcs = glob(["**/*.carbon"]),

+ 11 - 0
toolchain/lexer/testdata/BUILD

@@ -2,6 +2,7 @@
 # Exceptions. See /LICENSE for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+load("//bazel/sh_run:rules.bzl", "glob_sh_run")
 load("//bazel/testing:lit.bzl", "glob_lit_tests")
 
 glob_lit_tests(
@@ -14,3 +15,13 @@ glob_lit_tests(
     driver = "lit.cfg.py",
     test_file_exts = ["carbon"],
 )
+
+glob_sh_run(
+    args = [
+        "$(location //toolchain/driver:carbon)",
+        "dump",
+        "tokens",
+    ],
+    data = ["//toolchain/driver:carbon"],
+    file_exts = ["carbon"],
+)

+ 11 - 0
toolchain/parser/testdata/BUILD

@@ -2,6 +2,7 @@
 # Exceptions. See /LICENSE for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+load("//bazel/sh_run:rules.bzl", "glob_sh_run")
 load("//bazel/testing:lit.bzl", "glob_lit_tests")
 
 glob_lit_tests(
@@ -14,3 +15,13 @@ glob_lit_tests(
     driver = "lit.cfg.py",
     test_file_exts = ["carbon"],
 )
+
+glob_sh_run(
+    args = [
+        "$(location //toolchain/driver:carbon)",
+        "dump",
+        "parse-tree",
+    ],
+    data = ["//toolchain/driver:carbon"],
+    file_exts = ["carbon"],
+)

+ 23 - 0
toolchain/semantics/testdata/BUILD

@@ -2,6 +2,7 @@
 # Exceptions. See /LICENSE for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+load("//bazel/sh_run:rules.bzl", "glob_sh_run")
 load("//bazel/testing:lit.bzl", "glob_lit_tests")
 
 glob_lit_tests(
@@ -14,3 +15,25 @@ glob_lit_tests(
     driver = "lit.cfg.py",
     test_file_exts = ["carbon"],
 )
+
+glob_sh_run(
+    args = [
+        "$(location //toolchain/driver:carbon)",
+        "dump",
+        "semantics-ir",
+    ],
+    data = ["//toolchain/driver:carbon"],
+    file_exts = ["carbon"],
+)
+
+glob_sh_run(
+    args = [
+        "$(location //toolchain/driver:carbon)",
+        "-v",
+        "dump",
+        "semantics-ir",
+    ],
+    data = ["//toolchain/driver:carbon"],
+    file_exts = ["carbon"],
+    run_ext = "verbose",
+)