Windows: tests now work with native test wrapper (#263)

Windows: tests now work with native test wrapper

See https://github.com/bazelbuild/bazel/issues/6622
This commit is contained in:
László Csomor 2019-05-17 11:52:28 +02:00 committed by irengrig
parent 50e2679dee
commit 9cfeff53b5
3 changed files with 53 additions and 8 deletions

View File

@ -89,8 +89,9 @@ cc_binary(
sh_test(
name = "test_hello",
srcs = ["test_hello.sh"],
args = ["libhello_example"],
args = ["$(location libhello_example)"],
data = [":libhello_example"],
deps = ["@bazel_tools//tools/bash/runfiles"],
tags = ["windows"],
visibility = ["//:__pkg__"],
)
@ -98,8 +99,9 @@ sh_test(
sh_test(
name = "test_hello_ninja",
srcs = ["test_hello.sh"],
args = ["libhello_example_ninja"],
args = ["$(location libhello_example_ninja)"],
data = [":libhello_example_ninja"],
deps = ["@bazel_tools//tools/bash/runfiles"],
tags = ["windows"],
visibility = ["//:__pkg__"],
)
@ -107,8 +109,9 @@ sh_test(
sh_test(
name = "test_hello_nmake",
srcs = ["test_hello.sh"],
args = ["libhello_example_nmake"],
args = ["$(location libhello_example_nmake)"],
data = [":libhello_example_nmake"],
deps = ["@bazel_tools//tools/bash/runfiles"],
tags = ["windows"],
visibility = ["//:__pkg__"],
)

View File

@ -1,3 +1,29 @@
#!/usr/bin/env bash
$(rlocation rules_foreign_cc/examples/cmake_hello_world_lib/$1)
# --- begin runfiles.bash initialization ---
# The runfiles library itself defines rlocation which you would need to look
# up the library's runtime location, thus we have a chicken-and-egg problem.
#
# 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 ---
$(rlocation $TEST_WORKSPACE/$1)

View File

@ -19,12 +19,17 @@ def _impl(ctx):
if "-fblah3" in flags.cxx_linker_static:
fail("Static linker flags should not contain '-fblah3'")
# to satisfy test rule requirement to be executable
exe = ctx.outputs.out
ctx.actions.write(
output = ctx.outputs.executable,
content = "",
output = exe,
is_executable = True,
# The file must not be empty because running an empty .bat file as a
# subprocess fails on Windows, so we write one space to it.
content = " ",
)
return [DefaultInfo(files = depset([exe]), executable = exe)]
def assert_contains_once(arr, value):
cnt = 0
for elem in arr:
@ -35,12 +40,23 @@ def assert_contains_once(arr, value):
if cnt > 1:
fail("Value is included multiple times: " + value)
flags_test = rule(
_flags_test = rule(
implementation = _impl,
attrs = {
"_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
"out": attr.output(),
},
toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
fragments = ["cpp"],
test = True,
)
def flags_test(name, **kwargs):
_flags_test(
name = name,
# On Windows we need the ".bat" extension.
# On other platforms the extension doesn't matter.
# Therefore we can use ".bat" on every platform.
out = name + ".bat",
**kwargs
)