aboutsummaryrefslogtreecommitdiff
path: root/rules
diff options
context:
space:
mode:
Diffstat (limited to 'rules')
-rw-r--r--rules/BUILD13
-rw-r--r--rules/analysis_test.bzl2
-rw-r--r--rules/build_test.bzl40
-rw-r--r--rules/common_settings.bzl2
-rw-r--r--rules/diff_test.bzl29
-rwxr-xr-xrules/empty_test.sh3
-rw-r--r--rules/native_binary.bzl6
-rw-r--r--rules/private/copy_file_private.bzl7
-rw-r--r--rules/run_binary.bzl4
9 files changed, 63 insertions, 43 deletions
diff --git a/rules/BUILD b/rules/BUILD
index 26cda0c..f7017bb 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"]),
)
@@ -73,14 +68,6 @@ filegroup(
],
)
-filegroup(
- name = "bins",
- srcs = glob(["*.sh"]),
- visibility = [
- "//:__pkg__",
- ],
-)
-
# export bzl files for the documentation
exports_files(
glob(["*.bzl"]),
diff --git a/rules/analysis_test.bzl b/rules/analysis_test.bzl
index 46d32dd..3420d48 100644
--- a/rules/analysis_test.bzl
+++ b/rules/analysis_test.bzl
@@ -16,7 +16,7 @@
def _analysis_test_impl(ctx):
"""Implementation function for analysis_test. """
- _ignore = [ctx]
+ _ignore = [ctx] # @unused
return [AnalysisTestResultInfo(
success = True,
message = "All targets succeeded analysis",
diff --git a/rules/build_test.bzl b/rules/build_test.bzl
index edcb50a..9ec8ab1 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 "#!/usr/bin/env 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:
```
@@ -41,7 +63,7 @@ def build_test(name, targets, **kwargs):
Args:
name: The name of the test rule.
targets: A list of targets to ensure build.
- **kwargs: The <a href="https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
+ **kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
"""
if len(targets) == 0:
fail("targets must be non-empty", "targets")
@@ -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/common_settings.bzl b/rules/common_settings.bzl
index e80ab09..7f56a7f 100644
--- a/rules/common_settings.bzl
+++ b/rules/common_settings.bzl
@@ -18,7 +18,7 @@ These rules return a BuildSettingInfo with the value of the build setting.
For label-typed settings, use the native label_flag and label_setting rules.
More documentation on how to use build settings at
-https://docs.bazel.build/versions/master/skylark/config.html#user-defined-build-settings
+https://docs.bazel.build/versions/main/skylark/config.html#user-defined-build-settings
"""
BuildSettingInfo = provider(
diff --git a/rules/diff_test.bzl b/rules/diff_test.bzl
index 93cf363..49a1968 100644
--- a/rules/diff_test.bzl
+++ b/rules/diff_test.bzl
@@ -44,21 +44,31 @@ for /F "tokens=2* usebackq" %%i in (`findstr.exe /l /c:"!F1! " "%MF%"`) do (
set RF1=!RF1:/=\\!
)
if "!RF1!" equ "" (
- echo>&2 ERROR: !F1! not found
- exit /b 1
+ if exist "{file1}" (
+ set RF1="{file1}"
+ set RF1=!RF1:/=\\!
+ ) else (
+ echo>&2 ERROR: !F1! not found
+ exit /b 1
+ )
)
for /F "tokens=2* usebackq" %%i in (`findstr.exe /l /c:"!F2! " "%MF%"`) do (
set RF2=%%i
set RF2=!RF2:/=\\!
)
if "!RF2!" equ "" (
- echo>&2 ERROR: !F2! not found
- exit /b 1
+ if exist "{file2}" (
+ set RF2="{file2}"
+ set RF2=!RF2:/=\\!
+ ) else (
+ echo>&2 ERROR: !F2! not found
+ exit /b 1
+ )
)
fc.exe 2>NUL 1>NUL /B "!RF1!" "!RF2!"
if %ERRORLEVEL% neq 0 (
if %ERRORLEVEL% equ 1 (
- echo>&2 FAIL: files "{file1}" and "{file2}" differ
+ echo>&2 FAIL: files "{file1}" and "{file2}" differ. {fail_msg}
exit /b 1
) else (
fc.exe /B "!RF1!" "!RF2!"
@@ -66,6 +76,7 @@ if %ERRORLEVEL% neq 0 (
)
)
""".format(
+ fail_msg = ctx.attr.failure_message,
file1 = _runfiles_path(ctx.file.file1),
file2 = _runfiles_path(ctx.file.file2),
),
@@ -75,7 +86,7 @@ if %ERRORLEVEL% neq 0 (
test_bin = ctx.actions.declare_file(ctx.label.name + "-test.sh")
ctx.actions.write(
output = test_bin,
- content = r"""#!/bin/bash
+ content = r"""#!/usr/bin/env bash
set -euo pipefail
F1="{file1}"
F2="{file2}"
@@ -95,10 +106,11 @@ else
exit 1
fi
if ! diff "$RF1" "$RF2"; then
- echo >&2 "FAIL: files \"{file1}\" and \"{file2}\" differ"
+ echo >&2 "FAIL: files \"{file1}\" and \"{file2}\" differ. {fail_msg}"
exit 1
fi
""".format(
+ fail_msg = ctx.attr.failure_message,
file1 = _runfiles_path(ctx.file.file1),
file2 = _runfiles_path(ctx.file.file2),
),
@@ -112,6 +124,7 @@ fi
_diff_test = rule(
attrs = {
+ "failure_message": attr.string(),
"file1": attr.label(
allow_single_file = True,
mandatory = True,
@@ -135,7 +148,7 @@ def diff_test(name, file1, file2, **kwargs):
name: The name of the test rule.
file1: Label of the file to compare to <code>file2</code>.
file2: Label of the file to compare to <code>file1</code>.
- **kwargs: The <a href="https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
+ **kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
"""
_diff_test(
name = name,
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
diff --git a/rules/native_binary.bzl b/rules/native_binary.bzl
index 7d885a0..9a1725f 100644
--- a/rules/native_binary.bzl
+++ b/rules/native_binary.bzl
@@ -72,11 +72,11 @@ def native_binary(name, src, out, data = None, **kwargs):
You can "bazel run" this rule like any other binary rule, and use it as a tool in genrule.tools for example. You can also augment the binary with runfiles.
Args:
- name: The name of the test rule.
+ name: The name of the rule.
src: label; path of the pre-built executable
out: output; an output name for the copy of the binary. (Bazel requires that this rule make a copy of 'src'.)
data: list of labels; data dependencies
- **kwargs: The <a href="https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-binaries">common attributes for binaries</a>.
+ **kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-binaries">common attributes for binaries</a>.
"""
_native_binary(
name = name,
@@ -101,7 +101,7 @@ def native_test(name, src, out, data = None, **kwargs):
src: label; path of the pre-built executable
out: output; an output name for the copy of the binary. (Bazel requires that this rule make a copy of 'src'.)
data: list of labels; data dependencies
- **kwargs: The <a href="https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
+ **kwargs: The <a href="https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes-tests">common attributes for tests</a>.
"""
_native_test(
name = name,
diff --git a/rules/private/copy_file_private.bzl b/rules/private/copy_file_private.bzl
index 75cb027..44f133a 100644
--- a/rules/private/copy_file_private.bzl
+++ b/rules/private/copy_file_private.bzl
@@ -65,11 +65,10 @@ def _copy_file_impl(ctx):
target_file = ctx.file.src,
is_executable = ctx.attr.is_executable,
)
+ elif ctx.attr.is_windows:
+ copy_cmd(ctx, ctx.file.src, ctx.outputs.out)
else:
- if ctx.attr.is_windows:
- copy_cmd(ctx, ctx.file.src, ctx.outputs.out)
- else:
- copy_bash(ctx, ctx.file.src, ctx.outputs.out)
+ copy_bash(ctx, ctx.file.src, ctx.outputs.out)
files = depset(direct = [ctx.outputs.out])
runfiles = ctx.runfiles(files = [ctx.outputs.out])
diff --git a/rules/run_binary.bzl b/rules/run_binary.bzl
index 10f1ab5..c251019 100644
--- a/rules/run_binary.bzl
+++ b/rules/run_binary.bzl
@@ -76,7 +76,7 @@ run_binary = rule(
),
"env": attr.string_dict(
doc = "Environment variables of the action.<br/><br/>Subject to " +
- " <code><a href=\"https://docs.bazel.build/versions/master/be/make-variables.html#location\">$(location)</a></code>" +
+ " <code><a href=\"https://docs.bazel.build/versions/main/be/make-variables.html#location\">$(location)</a></code>" +
" expansion.",
),
"srcs": attr.label_list(
@@ -91,7 +91,7 @@ run_binary = rule(
),
"args": attr.string_list(
doc = "Command line arguments of the binary.<br/><br/>Subject to" +
- "<code><a href=\"https://docs.bazel.build/versions/master/be/make-variables.html#location\">$(location)</a></code>" +
+ "<code><a href=\"https://docs.bazel.build/versions/main/be/make-variables.html#location\">$(location)</a></code>" +
" expansion.",
),
},