From 76ef6c8988624af63432024c139166f2ed35c75c Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Sat, 26 Aug 2023 13:59:48 -0700 Subject: [PATCH] chore: add test coverage for run_binary & expand_variables makevars (#504) --- docs/expand_make_vars.md | 5 ++ docs/run_binary.md | 4 +- lib/private/expand_variables.bzl | 5 ++ lib/private/run_binary.bzl | 8 ++- lib/tests/run_binary_expansions/BUILD.bazel | 62 +++++++++++++++++++ lib/tests/run_binary_expansions/expansions.sh | 12 ++++ .../run_binary_expansions/expansions_golden | 19 ++++++ 7 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 lib/tests/run_binary_expansions/BUILD.bazel create mode 100755 lib/tests/run_binary_expansions/expansions.sh create mode 100644 lib/tests/run_binary_expansions/expansions_golden diff --git a/docs/expand_make_vars.md b/docs/expand_make_vars.md index 21ca544..e95cdcc 100644 --- a/docs/expand_make_vars.md +++ b/docs/expand_make_vars.md @@ -94,6 +94,11 @@ expand_variables(ctx, name | The target name | none | | tool | The tool to run in the action.

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 $(location) expansion in args and env. | none | | srcs | Additional inputs of the action.

These labels are available for $(location) expansion in args and env. | [] | -| args | Command line arguments of the binary.

Subject to $(location) and makevar expansions. | [] | -| env | Environment variables of the action.

Subject to $(location) and makevar expansions. | {} | +| args | Command line arguments of the binary.

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 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.

These labels are available for $(location) expansion in args and env.

Output files cannot be nested within output directories in out_dirs. | [] | | out_dirs | Output directories generated by the action.

These labels are _not_ available for $(location) expansion in args and env since they are not pre-declared labels created via attr.output_list(). Output directories are declared instead by ctx.actions.declare_directory.

Output directories cannot be nested within other output directories in out_dirs. | [] | | mnemonic | A one-word description of the action, for example, CppCompile or GoLink. | "RunBinary" | diff --git a/lib/private/expand_variables.bzl b/lib/private/expand_variables.bzl index 5b88b99..f581944 100644 --- a/lib/private/expand_variables.bzl +++ b/lib/private/expand_variables.bzl @@ -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: diff --git a/lib/private/run_binary.bzl b/lib/private/run_binary.bzl index db8b8bb..692a24a 100644 --- a/lib/private/run_binary.bzl +++ b/lib/private/run_binary.bzl @@ -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. diff --git a/lib/tests/run_binary_expansions/BUILD.bazel b/lib/tests/run_binary_expansions/BUILD.bazel new file mode 100644 index 0000000..5b3db3f --- /dev/null +++ b/lib/tests/run_binary_expansions/BUILD.bazel @@ -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", +) diff --git a/lib/tests/run_binary_expansions/expansions.sh b/lib/tests/run_binary_expansions/expansions.sh new file mode 100755 index 0000000..6816588 --- /dev/null +++ b/lib/tests/run_binary_expansions/expansions.sh @@ -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 diff --git a/lib/tests/run_binary_expansions/expansions_golden b/lib/tests/run_binary_expansions/expansions_golden new file mode 100644 index 0000000..690910e --- /dev/null +++ b/lib/tests/run_binary_expansions/expansions_golden @@ -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