Skip to content

Commit 5ef52e4

Browse files
authored
Update current_toolchain_files tests to use a dedicated test rule (#1714)
1 parent c75ea6f commit 5ef52e4

File tree

3 files changed

+112
-38
lines changed

3 files changed

+112
-38
lines changed
Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,36 @@
1+
load(":current_toolchain_files_test.bzl", "current_toolchain_files_test")
2+
3+
exports_files([
4+
"current_toolchain_files_test.sh",
5+
])
6+
17
# Executable targets will output a pattern similar to the following
28
# cargo 1.53.0 (4369396ce 2021-04-27)
39
# Also Note, rustc_srcs is too big for this test
410
_FILES = {
5-
"cargo": ("--executable", r"^cargo [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
6-
"clippy": ("--executable", r"^clippy [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
7-
"rust_stdlib": ("--files", r"\.rlib"),
8-
"rustc": ("--executable", r"^rustc [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
9-
"rustc_lib": ("--files", r"rustc_driver"),
10-
"rustdoc": ("--executable", r"^rustdoc [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
11-
"rustfmt": ("--executable", r"^rustfmt [0-9\.]\+\-stable ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
11+
"cargo": ("executable", r"^cargo [0-9\.]\+\(-nightly\)\? ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
12+
"clippy": ("executable", r"^clippy [0-9\.]\+ ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
13+
"rust_stdlib": ("files", r"\.rlib"),
14+
"rustc": ("executable", r"^rustc [0-9\.]\+\(-nightly\)\? ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
15+
"rustc_lib": ("files", r"rustc_driver"),
16+
"rustdoc": ("executable", r"^rustdoc [0-9\.]\+\(\-nightly\)\? ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
17+
"rustfmt": ("executable", r"^rustfmt [0-9\.]\+\-\(stable\|nightly\) ([0-9a-z]\+ [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\})"),
1218
}
1319

14-
# Generate a list manifest for all files in the filegroup
15-
[
16-
genrule(
17-
name = "{}_manifest_genrule".format(files),
18-
srcs = ["//rust/toolchain:current_{}_files".format(files)],
19-
outs = ["{}_manifest".format(files)],
20-
cmd = "for file in $(rootpaths //rust/toolchain:current_{}_files); do echo $$file >> $@; done".format(files),
21-
)
22-
for files in _FILES
23-
if "--files" in _FILES[files]
24-
]
25-
26-
# Test that all toolchain tools are executable targets
20+
# Test that all toolchain tools consumable (executables are executable and filegroups contain expected sources)
2721
[
28-
sh_test(
22+
current_toolchain_files_test(
2923
name = tool + "_test",
30-
srcs = ["current_exec_files_test.sh"],
31-
args = [
32-
"$(rootpath //rust/toolchain:current_{}_files)".format(tool) if "--executable" == arg else "$(rootpath {}_manifest)".format(tool),
33-
arg,
34-
"'{}'".format(pattern),
35-
],
36-
data = [
37-
"//rust/toolchain:current_{}_files".format(tool),
38-
] + (
39-
["{}_manifest".format(tool)] if "--files" == arg else []
40-
),
24+
kind = kind,
25+
pattern = pattern,
26+
# TOOO: Windows requires use of bash which is not guaranteed to be available.
27+
# The test runner should ideally be rewritten in rust so that windows could
28+
# be tested.
29+
target_compatible_with = select({
30+
"@platforms//os:windows": ["@platforms//:incompatible"],
31+
"//conditions:default": [],
32+
}),
33+
tool = "//rust/toolchain:current_{}_files".format(tool),
4134
)
42-
for tool, (arg, pattern) in _FILES.items()
35+
for tool, (kind, pattern) in _FILES.items()
4336
]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
"""Test rules for `current_*_files` targets"""
2+
3+
def _current_toolchain_files_test_impl(ctx):
4+
runfiles_files = []
5+
if ctx.attr.kind == "files":
6+
manifest = ctx.actions.declare_file("{}.files.manifest".format(
7+
ctx.label.name,
8+
))
9+
ctx.actions.write(
10+
output = manifest,
11+
content = "\n".join([file.short_path for file in ctx.files.tool]),
12+
)
13+
runfiles_files.append(manifest)
14+
runfiles_files.extend(ctx.files.tool)
15+
input = manifest
16+
elif ctx.attr.kind == "executable":
17+
tool_files = ctx.files.tool
18+
if len(tool_files) != 1:
19+
fail("Unexpected number of files provided by tool for {}: {}".format(
20+
ctx.label,
21+
tool_files,
22+
))
23+
input = tool_files[0]
24+
runfiles_files.append(input)
25+
else:
26+
fail("Unexpected kind: {}".format(ctx.attr.kind))
27+
28+
extension = ".{}".format(ctx.executable._test_runner.extension) if ctx.executable._test_runner.extension else ""
29+
test_runner = ctx.actions.declare_file("{}{}".format(ctx.label.name, extension))
30+
ctx.actions.symlink(
31+
output = test_runner,
32+
target_file = ctx.executable._test_runner,
33+
)
34+
35+
runfiles = ctx.runfiles(files = runfiles_files)
36+
runfiles = runfiles.merge(ctx.attr._test_runner[DefaultInfo].default_runfiles)
37+
runfiles = runfiles.merge(ctx.attr.tool[DefaultInfo].default_runfiles)
38+
39+
return [
40+
DefaultInfo(
41+
runfiles = runfiles,
42+
executable = test_runner,
43+
),
44+
testing.TestEnvironment({
45+
"CURRENT_TOOLCHAIN_FILES_TEST_INPUT": input.short_path,
46+
"CURRENT_TOOLCHAIN_FILES_TEST_KIND": ctx.attr.kind,
47+
"CURRENT_TOOLCHAIN_FILES_TEST_PATTERN": ctx.attr.pattern,
48+
}),
49+
]
50+
51+
current_toolchain_files_test = rule(
52+
doc = "Test that `current_*_toolchain` tools consumable (executables are executable and filegroups contain expected sources)",
53+
implementation = _current_toolchain_files_test_impl,
54+
attrs = {
55+
"kind": attr.string(
56+
doc = "The kind of the component.",
57+
values = ["executable", "files"],
58+
mandatory = True,
59+
),
60+
"pattern": attr.string(
61+
doc = (
62+
"A pattern used to confirm either executables produce an expected " +
63+
"value or lists of files contain expected contents."
64+
),
65+
mandatory = True,
66+
),
67+
"tool": attr.label(
68+
doc = "The current toolchain component.",
69+
allow_files = True,
70+
mandatory = True,
71+
),
72+
"_test_runner": attr.label(
73+
doc = "A shared test runner for validating targets.",
74+
cfg = "exec",
75+
allow_files = True,
76+
executable = True,
77+
default = Label("//test/current_toolchain_files:current_toolchain_files_test_runner.sh"),
78+
),
79+
},
80+
test = True,
81+
)

test/current_toolchain_files/current_exec_files_test.sh renamed to test/current_toolchain_files/current_toolchain_files_test_runner.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
set -euo pipefail
44

5-
TARGET="$1"
6-
OPTION="$2"
5+
TARGET="${CURRENT_TOOLCHAIN_FILES_TEST_INPUT}"
6+
OPTION="${CURRENT_TOOLCHAIN_FILES_TEST_KIND}"
77

88
# To parse this argument on windows it must be wrapped in quotes but
99
# these quotes should not be passed to grep. Remove them here.
10-
PATTERN="$(echo -n "$3" | sed "s/'//g")"
10+
PATTERN="$(echo -n "${CURRENT_TOOLCHAIN_FILES_TEST_PATTERN}" | sed "s/'//g")"
1111

12-
if [[ "${OPTION}" == "--executable" ]]; then
12+
if [[ "${OPTION}" == "executable" ]]; then
1313
# Clippy requires this environment variable is set
1414
export SYSROOT=""
1515

@@ -18,7 +18,7 @@ if [[ "${OPTION}" == "--executable" ]]; then
1818
exit 0
1919
fi
2020

21-
if [[ "${OPTION}" == "--files" ]]; then
21+
if [[ "${OPTION}" == "files" ]]; then
2222
cat "${TARGET}"
2323
grep "${PATTERN}" "${TARGET}"
2424
exit 0

0 commit comments

Comments
 (0)