Skip to content

Commit

Permalink
chore: add test coverage for run_binary & expand_variables makevars
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan committed Aug 26, 2023
1 parent ef4830b commit ef16fb3
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/expand_make_vars.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/run_binary.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lib/private/expand_variables.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions lib/private/run_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,17 @@ 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](https://docs.aspect.build/rules/aspect_bazel_lib/docs/expand_make_vars#expand_locations)
and
[expand_make_vars](https://docs.aspect.build/rules/aspect_bazel_lib/docs/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](https://docs.aspect.build/rules/aspect_bazel_lib/docs/expand_make_vars#expand_locations)
and
[expand_make_vars](https://docs.aspect.build/rules/aspect_bazel_lib/docs/expand_make_vars).
outs: Output files generated by the action.
Expand Down
56 changes: 56 additions & 0 deletions lib/tests/run_binary_expansions/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"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)",
"$(execpath :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",
)
12 changes: 12 additions & 0 deletions lib/tests/run_binary_expansions/expansions.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions lib/tests/run_binary_expansions/expansions_golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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
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

0 comments on commit ef16fb3

Please sign in to comment.