Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support for build_test #302

Merged
merged 4 commits into from Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
36 changes: 29 additions & 7 deletions rules/build_test.bzl
Expand Up @@ -16,16 +16,40 @@

load("//lib:new_sets.bzl", "sets")

def _empty_test_impl(ctx):
is_windows = select({
"@bazel_tools//src/conditions:host_windows": True,
"//conditions:default": False,
})
extension = ".bat" if is_windows else ".sh"
stdbug marked this conversation as resolved.
Show resolved Hide resolved
executable = ctx.actions.declare_file(ctx.label.name + extension)
ctx.actions.write(
output = executable,
is_executable = True,
content = "exit 0",
)

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),
stdbug marked this conversation as resolved.
Show resolved Hide resolved
},
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,15 +99,13 @@ 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
stdbug marked this conversation as resolved.
Show resolved Hide resolved
**kwargs
Expand Down
3 changes: 0 additions & 3 deletions rules/empty_test.sh

This file was deleted.