diff --git a/rules/BUILD b/rules/BUILD index 26cda0c..6bb55b9 100644 --- a/rules/BUILD +++ b/rules/BUILD @@ -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"]), ) diff --git a/rules/build_test.bzl b/rules/build_test.bzl index f06e0d4..697875b 100644 --- a/rules/build_test.bzl +++ b/rules/build_test.bzl @@ -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 ) diff --git a/rules/empty_test.sh b/rules/empty_test.sh deleted file mode 100755 index fde58b3..0000000 --- a/rules/empty_test.sh +++ /dev/null @@ -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