Windows support for build_test (#302)

This commit is contained in:
Mansur 2021-09-24 16:43:30 +02:00 committed by GitHub
parent 08398cdc99
commit 14f17ae7f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 15 deletions

View File

@ -49,16 +49,11 @@ bzl_library(
srcs = ["common_settings.bzl"],
)
# Exported for build_test.bzl to make sure of, it is an implementation detail
# of the rule and should not be directly used by anything else.
exports_files(["empty_test.sh"])
filegroup(
name = "test_deps",
testonly = True,
srcs = [
"BUILD",
"empty_test.sh",
] + glob(["*.bzl"]),
)

View File

@ -16,6 +16,31 @@
load("//lib:new_sets.bzl", "sets")
def _empty_test_impl(ctx):
extension = ".bat" if ctx.attr.is_windows else ".sh"
content = "exit 0" if ctx.attr.is_windows else "#!/bin/bash\nexit 0"
executable = ctx.actions.declare_file(ctx.label.name + extension)
ctx.actions.write(
output = executable,
is_executable = True,
content = content,
)
return [DefaultInfo(
files = depset([executable]),
executable = executable,
runfiles = ctx.runfiles(files = ctx.files.data),
)]
_empty_test = rule(
implementation = _empty_test_impl,
attrs = {
"data": attr.label_list(allow_files = True),
"is_windows": attr.bool(mandatory = True),
},
test = True,
)
def build_test(name, targets, **kwargs):
"""Test rule checking that other targets build.
@ -23,9 +48,6 @@ def build_test(name, targets, **kwargs):
the targets it depends on failing to build, and hence failing
the attempt to run this test.
NOTE: At the moment, this won't work on Windows; but someone adding
support would be welcomed.
Typical usage:
```
@ -75,16 +97,18 @@ def build_test(name, targets, **kwargs):
outs = [full_name + ".out"],
testonly = 1,
visibility = ["//visibility:private"],
# TODO: Does this need something else for Windows?
cmd = "touch $@",
cmd_bat = "type nul > $@",
**genrule_args
)
native.sh_test(
_empty_test(
name = name,
# TODO: Does this need something else for Windows?
srcs = ["@bazel_skylib//rules:empty_test.sh"],
data = test_data,
size = kwargs.pop("size", "small"), # Default to small for test size
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
}),
**kwargs
)

View File

@ -1,3 +0,0 @@
#!/bin/bash
# This is a support file for build_test.bzl, it shouldn't be used by anything else.
exit 0