Skip to content

Commit

Permalink
Fix tests in //api_proto on Windows
Browse files Browse the repository at this point in the history
Fix the Starlark rule generating
//api_proto:api.gen.pb.go_checkshtest so it works
on Windows, where a runfiles tree is not
available.

To fix, the generated shell script now uses the
Bash runfiles library to locate the files to
compare.

See bazelbuild/bazel#5508
  • Loading branch information
laszlocsomor committed Apr 16, 2019
1 parent 6415663 commit 6978420
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion build/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,46 @@ def genfile_check_test(src, gen):
native.genrule(
name = src + "_checksh",
outs = [src + "_check.sh"],
cmd = "echo 'diff $$@' > $@",
cmd = r"""cat >$@ <<'eof'
#!/bin/bash
# Script generated by @com_github_bazelbuild_buildtools//build:build_defs.bzl
# --- begin runfiles.bash initialization ---
# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
set -euo pipefail
if [[ ! -d "$${RUNFILES_DIR:-/dev/null}" && ! -f "$${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
if [[ -f "$$0.runfiles_manifest" ]]; then
export RUNFILES_MANIFEST_FILE="$$0.runfiles_manifest"
elif [[ -f "$$0.runfiles/MANIFEST" ]]; then
export RUNFILES_MANIFEST_FILE="$$0.runfiles/MANIFEST"
elif [[ -f "$$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
export RUNFILES_DIR="$$0.runfiles"
fi
fi
if [[ -f "$${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
source "$${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
elif [[ -f "$${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
source "$$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
"$$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
else
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
exit 1
fi
# --- end runfiles.bash initialization ---
[[ "$$1" = external/* ]] && F1="$${1#external/}" || F1="$$TEST_WORKSPACE/$$1"
[[ "$$2" = external/* ]] && F2="$${2#external/}" || F2="$$TEST_WORKSPACE/$$2"
F1="$$(rlocation "$$F1")"
F2="$$(rlocation "$$F2")"
diff -q "$$F1" "$$F2"
eof
""",
)
native.sh_test(
name = src + "_checkshtest",
size = "small",
srcs = [src + "_check.sh"],
deps = ["@bazel_tools//tools/bash/runfiles"],
data = [src, gen],
args = ["$(location " + src + ")", "$(location " + gen + ")"],
)
Expand Down

0 comments on commit 6978420

Please sign in to comment.