Skip to content

Commit

Permalink
Windows support for build_test (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
stdbug committed Sep 24, 2021
1 parent 08398cd commit 14f17ae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
5 changes: 0 additions & 5 deletions rules/BUILD
Expand Up @@ -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"]),
)

Expand Down
38 changes: 31 additions & 7 deletions rules/build_test.bzl
Expand Up @@ -16,16 +16,38 @@

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.
This works not by an instance of this test failing, but instead by
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:
```
Expand Down Expand Up @@ -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
)
3 changes: 0 additions & 3 deletions rules/empty_test.sh

This file was deleted.

0 comments on commit 14f17ae

Please sign in to comment.