chore: add test coverage for run_binary & expand_variables makevars (#504)

This commit is contained in:
Greg Magolan 2023-08-26 13:59:48 -07:00 committed by GitHub
parent ef4830bf25
commit 76ef6c8988
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 111 additions and 4 deletions

View File

@ -94,6 +94,11 @@ expand_variables(<a href="#expand_variables-ctx">ctx</a>, <a href="#expand_varia
Expand make variables and substitute like genrule does.
Bazel [pre-defined variables](https://bazel.build/reference/be/make-variables#predefined_variables)
are expanded however only `$@`, `$(@D)` and `$(RULEDIR)` of
[pre-defined genrule variables](https://bazel.build/reference/be/make-variables#predefined_genrule_variables)
are supported.
This function is the same as ctx.expand_make_variables with the additional
genrule-like substitutions of:

4
docs/run_binary.md generated
View File

@ -27,8 +27,8 @@ This rule does not require Bash (unlike `native.genrule`).
| <a id="run_binary-name"></a>name | The target name | none |
| <a id="run_binary-tool"></a>tool | The tool to run in the action.<br><br>Must be the label of a *_binary rule of a rule that generates an executable file, or of a file that can be executed as a subprocess (e.g. an .exe or .bat file on Windows or a binary with executable permission on Linux). This label is available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | none |
| <a id="run_binary-srcs"></a>srcs | Additional inputs of the action.<br><br>These labels are available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>. | <code>[]</code> |
| <a id="run_binary-args"></a>args | Command line arguments of the binary.<br><br>Subject to <code>$(location)</code> and makevar expansions. | <code>[]</code> |
| <a id="run_binary-env"></a>env | Environment variables of the action.<br><br>Subject to <code>$(location)</code> and makevar expansions. | <code>{}</code> |
| <a id="run_binary-args"></a>args | Command line arguments of the binary.<br><br>Subject to <code>$(location)</code> and make variable expansions via [expand_location](./expand_make_vars#expand_locations) and [expand_make_vars](./expand_make_vars). | <code>[]</code> |
| <a id="run_binary-env"></a>env | Environment variables of the action.<br><br>Subject to <code>$(location)</code> and make variable expansions via [expand_location](./expand_make_vars#expand_locations) and [expand_make_vars](./expand_make_vars). | <code>{}</code> |
| <a id="run_binary-outs"></a>outs | Output files generated by the action.<br><br>These labels are available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code>.<br><br>Output files cannot be nested within output directories in out_dirs. | <code>[]</code> |
| <a id="run_binary-out_dirs"></a>out_dirs | Output directories generated by the action.<br><br>These labels are _not_ available for <code>$(location)</code> expansion in <code>args</code> and <code>env</code> since they are not pre-declared labels created via <code>attr.output_list()</code>. Output directories are declared instead by <code>ctx.actions.declare_directory</code>.<br><br>Output directories cannot be nested within other output directories in out_dirs. | <code>[]</code> |
| <a id="run_binary-mnemonic"></a>mnemonic | A one-word description of the action, for example, CppCompile or GoLink. | <code>"RunBinary"</code> |

View File

@ -5,6 +5,11 @@ load("@bazel_skylib//lib:paths.bzl", _spaths = "paths")
def expand_variables(ctx, s, outs = [], output_dir = False, attribute_name = "args"):
"""Expand make variables and substitute like genrule does.
Bazel [pre-defined variables](https://bazel.build/reference/be/make-variables#predefined_variables)
are expanded however only `$@`, `$(@D)` and `$(RULEDIR)` of
[pre-defined genrule variables](https://bazel.build/reference/be/make-variables#predefined_genrule_variables)
are supported.
This function is the same as ctx.expand_make_variables with the additional
genrule-like substitutions of:

View File

@ -141,11 +141,15 @@ def run_binary(
args: Command line arguments of the binary.
Subject to `$(location)` and makevar expansions.
Subject to `$(location)` and make variable expansions via
[expand_location](./expand_make_vars#expand_locations)
and [expand_make_vars](./expand_make_vars).
env: Environment variables of the action.
Subject to `$(location)` and makevar expansions.
Subject to `$(location)` and make variable expansions via
[expand_location](./expand_make_vars#expand_locations)
and [expand_make_vars](./expand_make_vars).
outs: Output files generated by the action.

View File

@ -0,0 +1,62 @@
"tests for run_binary"
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_file")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//lib:run_binary.bzl", "run_binary")
sh_binary(
name = "expansions_sh",
srcs = [":expansions.sh"],
)
write_file(
name = "gen_src_1",
out = "src_1",
content = ["src1"],
)
# target-under-test
run_binary(
name = "expansions",
srcs = [
":gen_src_1",
],
outs = ["expansions_out"],
args = [
"$@",
"$(@D)",
"$(rootpath :gen_src_1)",
"$(rootpaths :gen_src_1)",
"$(execpath :gen_src_1)",
"$(execpaths :gen_src_1)",
"$(rlocationpath :gen_src_1)",
"$(rlocationpaths :gen_src_1)",
"$(location :gen_src_1)",
"$(locations :gen_src_1)",
# Bazel built-in pre-defined variables
# https://bazel.build/reference/be/make-variables#predefined_variables
"$(COMPILATION_MODE)",
"$(BINDIR)",
"$(GENDIR)",
"$(TARGET_CPU)",
# Additional variables handled by aspect_bazel_lib expand_variables
# used by run_binary
# https://docs.aspect.build/rules/aspect_bazel_lib/docs/expand_expansions#expand_variables
"$(BUILD_FILE_PATH)",
"$(VERSION_FILE)",
"$(INFO_FILE)",
"$(TARGET)",
"$(WORKSPACE)",
],
execution_requirements = {
"no-cache": "1",
},
progress_message = "doing some work to make %{output}",
tool = ":expansions_sh",
)
write_source_file(
name = "write_expansions_golden",
in_file = "expansions_out",
out_file = "expansions_golden",
)

View File

@ -0,0 +1,12 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
mkdir -p $(dirname $1)
outfile=$1
rm -f $outfile
for each in $@
do
sanitized=${each/darwin/PLATFORM}
sanitized=${sanitized/k8/PLATFORM}
echo $sanitized >> $outfile
done

View File

@ -0,0 +1,19 @@
bazel-out/PLATFORM-fastbuild/bin/lib/tests/run_binary_expansions/expansions_out
bazel-out/PLATFORM-fastbuild/bin/lib/tests/run_binary_expansions
lib/tests/run_binary_expansions/src_1
lib/tests/run_binary_expansions/src_1
bazel-out/PLATFORM-fastbuild/bin/lib/tests/run_binary_expansions/src_1
bazel-out/PLATFORM-fastbuild/bin/lib/tests/run_binary_expansions/src_1
aspect_bazel_lib/lib/tests/run_binary_expansions/src_1
aspect_bazel_lib/lib/tests/run_binary_expansions/src_1
bazel-out/PLATFORM-fastbuild/bin/lib/tests/run_binary_expansions/src_1
bazel-out/PLATFORM-fastbuild/bin/lib/tests/run_binary_expansions/src_1
fastbuild
bazel-out/PLATFORM-fastbuild/bin
bazel-out/PLATFORM-fastbuild/bin
PLATFORM
lib/tests/run_binary_expansions/BUILD.bazel
bazel-out/volatile-status.txt
bazel-out/stable-status.txt
//lib/tests/run_binary_expansions:expansions
aspect_bazel_lib