From 9cfeff53b5c15d9f4f7b6eea5347b25982ec51d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Csomor?= Date: Fri, 17 May 2019 11:52:28 +0200 Subject: [PATCH] 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 --- examples/cmake_hello_world_lib/static/BUILD | 9 ++++-- .../static/test_hello.sh | 28 ++++++++++++++++++- test/standard_cxx_flags_test/tests.bzl | 24 +++++++++++++--- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/examples/cmake_hello_world_lib/static/BUILD b/examples/cmake_hello_world_lib/static/BUILD index 99e04883..d462a517 100644 --- a/examples/cmake_hello_world_lib/static/BUILD +++ b/examples/cmake_hello_world_lib/static/BUILD @@ -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__"], ) diff --git a/examples/cmake_hello_world_lib/static/test_hello.sh b/examples/cmake_hello_world_lib/static/test_hello.sh index 06092d63..1f575316 100755 --- a/examples/cmake_hello_world_lib/static/test_hello.sh +++ b/examples/cmake_hello_world_lib/static/test_hello.sh @@ -1,3 +1,29 @@ #!/usr/bin/env bash -$(rlocation rules_foreign_cc/examples/cmake_hello_world_lib/$1) \ No newline at end of file +# --- 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) diff --git a/test/standard_cxx_flags_test/tests.bzl b/test/standard_cxx_flags_test/tests.bzl index 77be56e2..4a247b52 100644 --- a/test/standard_cxx_flags_test/tests.bzl +++ b/test/standard_cxx_flags_test/tests.bzl @@ -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 + )