From 82b95af8199028e7d5e1dcc37adf7f546a8ddda3 Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 8 Apr 2026 05:37:41 -0700 Subject: [PATCH 1/2] DO NOT SUBMIT WIP for rules_cc integration tests PiperOrigin-RevId: 896443857 Change-Id: I1184a36a4b026c773891d44ae6006613791b3606 --- BUILD | 9 ++ MODULE.bazel | 47 ++++++++++ cc/BUILD | 5 +- cc/private/toolchain/BUILD | 4 +- cc/private/toolchain/test/BUILD | 6 ++ cc/proto/BUILD | 6 ++ tests/integration/BUILD | 54 ++++++++++++ tests/integration/bazel_from_env.sh | 3 + .../no_recompile_on_shutdown_test.sh | 10 +++ .../integration/rules_cc_integration_test.bzl | 88 +++++++++++++++++++ tests/integration/simple_compile/BUILD.test | 6 ++ tests/integration/simple_compile/MODULE.bazel | 7 ++ tests/integration/simple_compile/hello.cc | 3 + tests/integration/simple_compile_test.sh | 8 ++ tests/integration/stripped_compile_test.sh | 8 ++ tests/integration/test_launcher.sh | 39 ++++++++ tests/integration/test_utils.sh | 31 +++++++ 17 files changed, 332 insertions(+), 2 deletions(-) create mode 100644 tests/integration/BUILD create mode 100755 tests/integration/bazel_from_env.sh create mode 100755 tests/integration/no_recompile_on_shutdown_test.sh create mode 100644 tests/integration/rules_cc_integration_test.bzl create mode 100644 tests/integration/simple_compile/BUILD.test create mode 100644 tests/integration/simple_compile/MODULE.bazel create mode 100644 tests/integration/simple_compile/hello.cc create mode 100755 tests/integration/simple_compile_test.sh create mode 100755 tests/integration/stripped_compile_test.sh create mode 100755 tests/integration/test_launcher.sh create mode 100644 tests/integration/test_utils.sh diff --git a/BUILD b/BUILD index c284a199a..ffd9a3896 100644 --- a/BUILD +++ b/BUILD @@ -44,3 +44,12 @@ filegroup( ], visibility = ["//:__subpackages__"], ) + +filegroup( + name = "all_files_for_testing", + srcs = [ + "BUILD", + "MODULE.bazel", + "//cc:all_files_for_testing", + ], +) diff --git a/MODULE.bazel b/MODULE.bazel index 6c4eaad8a..678b7d727 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -50,3 +50,50 @@ bazel_dep(name = "stardoc", version = "0.8.0", dev_dependency = True) # Compatibility layer compat = use_extension("//cc:extensions.bzl", "compatibility_proxy") use_repo(compat, "cc_compatibility_proxy") + +bazel_dep( + name = "rules_bazel_integration_test", + version = "0.37.1", + dev_dependency = True, +) + +bazel_binaries = use_extension( + "@rules_bazel_integration_test//:extensions.bzl", + "bazel_binaries", + dev_dependency = True, +) + +# A bazel version which is actually whatever is being used. +bazel_binaries.local( + name = "env", + path = "tests/integration/bazel_from_env.sh", +) + +bazel_binaries.download( + version = "7.x", +) + +bazel_binaries.download( + version = "8.x", +) + +bazel_binaries.download( + version = "9.x", +) + +bazel_binaries.download( + version = "last_green", +) + +bazel_binaries.download( + version = "rolling", +) + +use_repo( + bazel_binaries, + "bazel_binaries", + "build_bazel_bazel_7_x", + "build_bazel_bazel_8_x", + "build_bazel_bazel_9_x", + "build_bazel_bazel_last_green", +) diff --git a/cc/BUILD b/cc/BUILD index 1882fba89..bfbfb296c 100644 --- a/cc/BUILD +++ b/cc/BUILD @@ -24,8 +24,11 @@ licenses(["notice"]) # Apache 2.0 filegroup( name = "all_files_for_testing", srcs = glob(["**"]) + [ - "//cc/private/rules_impl:srcs", + "//cc/common:srcs", + "//cc/private:srcs", "//cc/private/toolchain:srcs", + "//cc/proto:srcs", + "//cc/toolchains:srcs", ], ) diff --git a/cc/private/toolchain/BUILD b/cc/private/toolchain/BUILD index 966da04f6..8f616ec14 100644 --- a/cc/private/toolchain/BUILD +++ b/cc/private/toolchain/BUILD @@ -75,7 +75,9 @@ filegroup( filegroup( name = "srcs", - srcs = glob(["**"]), + srcs = glob(["**"]) + [ + "//cc/private/toolchain/test:srcs", + ], ) filegroup( diff --git a/cc/private/toolchain/test/BUILD b/cc/private/toolchain/test/BUILD index 30bc7b94d..5a0e1a433 100644 --- a/cc/private/toolchain/test/BUILD +++ b/cc/private/toolchain/test/BUILD @@ -11,3 +11,9 @@ compat_toolchain( toolchain_type = "@bazel_tools//tools/cpp:test_runner_toolchain_type", use_target_platform_constraints = True, ) + +filegroup( + name = "srcs", + srcs = glob(["**"]), + visibility = ["//cc/private/toolchain:__pkg__"], +) diff --git a/cc/proto/BUILD b/cc/proto/BUILD index 3be618715..a94df735a 100644 --- a/cc/proto/BUILD +++ b/cc/proto/BUILD @@ -2,3 +2,9 @@ package(default_visibility = ["//visibility:public"]) # Toolchain type provided by proto_lang_toolchain rule and used by cc_proto_library toolchain_type(name = "toolchain_type") + +filegroup( + name = "srcs", + srcs = glob(["**"]), + visibility = ["//cc:__pkg__"], +) diff --git a/tests/integration/BUILD b/tests/integration/BUILD new file mode 100644 index 000000000..94dbcd638 --- /dev/null +++ b/tests/integration/BUILD @@ -0,0 +1,54 @@ +load("@bazel_skylib//rules:common_settings.bzl", "string_flag") +load("@rules_shell//shell:sh_library.bzl", "sh_library") +load("//tests/integration:rules_cc_integration_test.bzl", "gen_workspace", "rules_cc_integration_test") + +BAZEL_VERSIONS = [ + "7", + "8", + "9", + "latest", +] + +string_flag( + name = "bazel_version", + build_setting_default = "latest", + values = BAZEL_VERSIONS, +) + +[ + config_setting( + name = "bazel_" + VERSION, + flag_values = { + ":bazel_version": VERSION, + }, + ) + for VERSION in BAZEL_VERSIONS +] + +sh_library( + name = "test_utils", + srcs = ["test_utils.sh"], +) + +gen_workspace( + "simple_compile", + workspace_files = glob(["simple_compile/**"]), +) + +rules_cc_integration_test( + name = "simple_compile_test", + test_script = "simple_compile_test.sh", + workspace = "simple_compile", +) + +rules_cc_integration_test( + name = "stripped_compile_test", + test_script = "stripped_compile_test.sh", + workspace = "simple_compile", +) + +rules_cc_integration_test( + name = "no_recompile_on_shutdown_test", + test_script = "no_recompile_on_shutdown_test.sh", + workspace = "simple_compile", +) diff --git a/tests/integration/bazel_from_env.sh b/tests/integration/bazel_from_env.sh new file mode 100755 index 000000000..32b0d817f --- /dev/null +++ b/tests/integration/bazel_from_env.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +bazel "$@" diff --git a/tests/integration/no_recompile_on_shutdown_test.sh b/tests/integration/no_recompile_on_shutdown_test.sh new file mode 100755 index 000000000..8d2d04764 --- /dev/null +++ b/tests/integration/no_recompile_on_shutdown_test.sh @@ -0,0 +1,10 @@ +set -euo pipefail +[ -z "$TEST_SRCDIR" ] && { echo "TEST_SRCDIR not set!" >&2; exit 1; } + +source "$(rlocation rules_cc/tests/integration/test_utils.sh)" + +bazel build -s //:hello >& "$TEST_log" || fail "Build failed" +expect_log "Compiling hello.cc" +bazel shutdown +bazel build -s //:hello >& "$TEST_log" || fail "Build failed" +expect_not_log "Compiling hello.cc" diff --git a/tests/integration/rules_cc_integration_test.bzl b/tests/integration/rules_cc_integration_test.bzl new file mode 100644 index 000000000..fef49d9c6 --- /dev/null +++ b/tests/integration/rules_cc_integration_test.bzl @@ -0,0 +1,88 @@ +load("@bazel_binaries//:defs.bzl", "bazel_binaries") +load("@rules_bazel_integration_test//bazel_integration_test:defs.bzl", "bazel_integration_test", "bazel_integration_tests") +load("@rules_shell//shell:sh_binary.bzl", "sh_binary") + +def gen_workspace( + name, + workspace_files = []): + build_file_standins = [f for f in workspace_files if f.endswith("BUILD.test")] + if len(build_file_standins) == 0: + fail("Could not find any BUILD.test files") + build_files = [] + for build_file_standin in build_file_standins: + build_file = build_file_standin[:-len(".test")] + native.genrule( + name = build_file + "_gen", + srcs = [build_file_standin], + outs = [build_file], + cmd = "cp $< $@", + testonly = True, + ) + build_files.append(build_file) + + native.filegroup( + name = name, + srcs = workspace_files + build_files, + testonly = True, + ) + +def rules_cc_integration_test( + name, + test_script, + workspace, + deps = [], + tags = []): + test_binary = name + "_test_runner" + sh_binary( + name = test_binary, + srcs = ["//tests/integration:test_launcher.sh"], + data = deps + [ + test_script, + "//cc:all_files_for_testing", + "//tests/integration:test_utils", + "@rules_shell//shell/runfiles", + "@rules_bazel_integration_test//tools:create_scratch_dir", + ], + deps = [ + "//tests/integration:test_utils", + "@rules_shell//shell/runfiles", + ], + testonly = True, + ) + + bazel_integration_tests( + name = name, + test_runner = test_binary, + bazel_binaries = bazel_binaries, + bazel_versions = bazel_binaries.versions.all, + env = {"TEST_RUNNER": "$(location %s)" % test_script}, + data = [ + test_script, + ], + workspace_path = Label(workspace).name, + workspace_files = [workspace, "//:all_files_for_testing"], + tags = tags + ["manual"], + ) + + selected_bazel = name + "_bazel_selected" + native.alias( + name = selected_bazel, + actual = select({ + "//tests/integration:bazel_7": "@build_bazel_bazel_7_x//:bazel_binary", + "//tests/integration:bazel_8": "@build_bazel_bazel_8_x//:bazel_binary", + "//tests/integration:bazel_9": "@build_bazel_bazel_9_x//:bazel_binary", + "//tests/integration:bazel_latest": "@build_bazel_bazel_last_green//:bazel_binary", + "//conditions:default": "@build_bazel_bazel_last_green//:bazel_binary", + }), + ) + + bazel_integration_test( + name = name, + test_runner = test_binary, + bazel_binary = selected_bazel, + env = {"TEST_RUNNER": "$(location %s)" % test_script}, + data = [test_script], + workspace_path = Label(workspace).name, + workspace_files = [workspace, "//:all_files_for_testing"], + tags = tags, + ) diff --git a/tests/integration/simple_compile/BUILD.test b/tests/integration/simple_compile/BUILD.test new file mode 100644 index 000000000..10ecc986d --- /dev/null +++ b/tests/integration/simple_compile/BUILD.test @@ -0,0 +1,6 @@ +load("@rules_cc//cc:cc_binary.bzl", "cc_binary") + +cc_binary( + name = "hello", + srcs = ["hello.cc"], +) diff --git a/tests/integration/simple_compile/MODULE.bazel b/tests/integration/simple_compile/MODULE.bazel new file mode 100644 index 000000000..e5920359f --- /dev/null +++ b/tests/integration/simple_compile/MODULE.bazel @@ -0,0 +1,7 @@ +module(name = "module_under_test") + +bazel_dep(name = "rules_cc", version = "0.0.0") +local_path_override( + module_name = "rules_cc", + path = "../../..", +) diff --git a/tests/integration/simple_compile/hello.cc b/tests/integration/simple_compile/hello.cc new file mode 100644 index 000000000..1d4229c68 --- /dev/null +++ b/tests/integration/simple_compile/hello.cc @@ -0,0 +1,3 @@ +#include + +int main() { printf("Hit me baby one more time!\n"); } diff --git a/tests/integration/simple_compile_test.sh b/tests/integration/simple_compile_test.sh new file mode 100755 index 000000000..1696e665f --- /dev/null +++ b/tests/integration/simple_compile_test.sh @@ -0,0 +1,8 @@ +set -euo pipefail +[ -z "$TEST_SRCDIR" ] && { echo "TEST_SRCDIR not set!" >&2; exit 1; } + +source "$(rlocation rules_cc/tests/integration/test_utils.sh)" + +bazel build //:hello >& "$TEST_log" || fail "Build failed" +bazel-bin/hello >> "$TEST_log" +expect_log "Hit me baby one more time!" diff --git a/tests/integration/stripped_compile_test.sh b/tests/integration/stripped_compile_test.sh new file mode 100755 index 000000000..b7f2282a5 --- /dev/null +++ b/tests/integration/stripped_compile_test.sh @@ -0,0 +1,8 @@ +set -euo pipefail +[ -z "$TEST_SRCDIR" ] && { echo "TEST_SRCDIR not set!" >&2; exit 1; } + +source "$(rlocation "rules_cc/tests/integration/test_utils.sh")" + +bazel build //:hello.stripped >& "$TEST_log" || fail "Build failed" +bazel-bin/hello.stripped >> "$TEST_log" +expect_log "Hit me baby one more time!" diff --git a/tests/integration/test_launcher.sh b/tests/integration/test_launcher.sh new file mode 100755 index 000000000..ef6a50c74 --- /dev/null +++ b/tests/integration/test_launcher.sh @@ -0,0 +1,39 @@ +# Setup runfiles and the test directory and then run the provided test script. + +set -x +set -euo pipefail + +# --- begin runfiles.bash initialization v3 --- +# Copy-pasted from the Bazel Bash runfiles library v3. +set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash +# shellcheck disable=SC1090 +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e +# --- end runfiles.bash initialization v3 --- + +create_scratch_dir_sh_location=rules_bazel_integration_test/tools/create_scratch_dir.sh +create_scratch_dir_sh="$(rlocation "${create_scratch_dir_sh_location}")" || \ + (echo >&2 "Failed to locate ${create_scratch_dir_sh_location}" && exit 1) + +scratch_dir="$("${create_scratch_dir_sh}" --workspace "${BIT_WORKSPACE_DIR}")" + +# Have the provided bazel sit on PATH +echo "$BIT_BAZEL_BINARY" +BAZEL_BIN_DIR="$(dirname $BIT_BAZEL_BINARY)" +export PATH="$BAZEL_BIN_DIR:$PATH" +ln -s "$BIT_BAZEL_BINARY" "$BAZEL_BIN_DIR/bazel" + +export TEST_log="$scratch_dir/test.log" + +test_runner_path="$(rlocation "rules_cc/${TEST_RUNNER}")" || (echo >&2 "FAILED TO LOAD RUNNER" && exit 1) +test_runner_cmd=( "${test_runner_path}" ) + +# Change into scratch directory +cd "${scratch_dir}" +ls -l +echo "$test_runner_cmd" +"${test_runner_cmd[@]}" diff --git a/tests/integration/test_utils.sh b/tests/integration/test_utils.sh new file mode 100644 index 000000000..846142c8d --- /dev/null +++ b/tests/integration/test_utils.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +function fail() { + __show_log >&2 + echo "FAILED: $*." >&2 + exit 1 +} + +function expect_log() { + local pattern=$1 + local message=${2:-"Expected regexp '$pattern' not found"} + grep -sq -- "$pattern" $TEST_log && return 0 + + fail "$message" + return 1 +} + +function expect_not_log() { + local pattern=$1 + local message=${2:-"Unexpected regexp '$pattern' found"} + grep -sq -- "$pattern" $TEST_log || return 0 + + fail "$message" + return 1 +} + +function __show_log() { + echo "-- Test log: -----------------------------------------------------------" + [[ -e $TEST_log ]] && cat "$TEST_log" || echo "(Log file did not exist.)" + echo "------------------------------------------------------------------------" +} From 3a4521beaa8289b2a09e689c023e990903dd8e2d Mon Sep 17 00:00:00 2001 From: Charles Mita Date: Thu, 21 May 2026 19:44:52 +0200 Subject: [PATCH 2/2] Update to not use rules_bazel_integration_test --- MODULE.bazel | 9 + extensions.bzl | 87 ++++++++++ local_bazel.bzl | 44 +++++ tests/integration/BUILD | 16 +- tests/integration/platform_utils.sh | 32 ++++ .../integration/rules_cc_integration_test.bzl | 158 ++++++++++++++---- tests/integration/test_launcher.sh | 55 ++++-- 7 files changed, 356 insertions(+), 45 deletions(-) create mode 100644 extensions.bzl create mode 100644 local_bazel.bzl create mode 100644 tests/integration/platform_utils.sh diff --git a/MODULE.bazel b/MODULE.bazel index 678b7d727..5f5e50622 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -97,3 +97,12 @@ use_repo( "build_bazel_bazel_9_x", "build_bazel_bazel_last_green", ) + +local_bazel = use_extension("//:extensions.bzl", "local_bazel_extension") +use_repo(local_bazel, "local_bazel") + +remote_bazel_fetcher = use_extension("//:extensions.bzl", "remote_bazel_extension") +remote_bazel_fetcher.download(bazel_version = "bazel_7") +remote_bazel_fetcher.download(bazel_version = "bazel_8") +remote_bazel_fetcher.download(bazel_version = "bazel_9") +use_repo(remote_bazel_fetcher, "bazel_7", "bazel_8", "bazel_9") diff --git a/extensions.bzl b/extensions.bzl new file mode 100644 index 000000000..f6286222a --- /dev/null +++ b/extensions.bzl @@ -0,0 +1,87 @@ +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file") +load("//:local_bazel.bzl", "local_bazel_import") + +SHORT_VERSION_MAPPING = { + "bazel_7": "7.7.1", + "bazel_8": "8.7.0", + "bazel_9": "9.1.0", +} + +SHA_MATRIX = { + "7.7.1": { + "linux_x86_64": "115a1b62be95f29e5821d4dddffba1b058905a48019b499919c285e7f708d5e2", + "linux_arm64": "71df04ec724f1b577f1f47ec9a6b81d13f39683f6c3215cacf45fdaf40b2c5c1", + "darwin_x86_64": "8582aea5ee2d8d0448bbda10fd7034734db1a21cbe4ea351a10012b969aa5d31", + "darwin_arm64": "fe8a1ee9064e94afae075c0dd4efb453db9c1373b9df12fecbff8479d408eb08", + "windows_x86_64": "6d9fb21e806cf4f4e61bfa2bc865df4900ffdc1e9ea90ca1016ba70367ef0de4", + }, + "8.7.0": { + "linux_x86_64": "d7606e679b78067c811096fb3d6cf135225b528835ca396e3a4dddf957859544", + "linux_arm64": "bfe9558bd8a2ecfe4841ec46c0dbccb4b469fe22d81f2f859de0de222b3e7ce3", + "darwin_x86_64": "76f3eb05782098e9f9ddd8247ec969b085195a3ae2978c81721a2235052ccf26", + "darwin_arm64": "575f20fb23955e02f73519befd180df635b4ed0960c60f0e70fcc8d74014a713", + "windows_x86_64": "29f1796f57379933340afa135f02703ffa21dd30135754bea695f8fd15103420", + }, + "9.1.0": { + "linux_x86_64": "a667454f3f4f8878df8199136b82c199f6ada8477b337fae3b1ef854f01e4e2f", + "linux_arm64": "ba933bfc943e4c44f0743a5823aa2312a34b39628532add5dd037e08d8ec27a4", + "darwin_x86_64": "666c6c79eda285cada5f5c39c891c6dd7ee0971b20bff365ea87a4b897271433", + "darwin_arm64": "084a1784fa8f0dcae77fb4e88faa15048d8149a36c947ce198508bffb060e1bb", + "windows_x86_64": "b457dccd36a9bb9be01326cc1d069a201bb50b4b94562a652afb6f43c5148d42", + }, +} + +def _remote_bazel_extension_impl(module_ctx): + os_name = module_ctx.os.name.lower() + os_key = "linux" + exe_extension = "" + if "windows" in os_name: + os_key = "windows" + exe_extension = ".exe" + elif "mac" in os_name or "darwin" in os_name: + os_key = "darwin" + + cpu_arch = module_ctx.os.arch.lower() + if "x86_64" in cpu_arch or "amd64" in cpu_arch: + arch_key = "x86_64" + elif "arm64" in cpu_arch: + arch_key = "arm64" + else: + fail("Unsupported architecture '{}'.".format(cpu_arch)) + + platform_key = "{}_{}".format(os_key, arch_key) + + requested_versions = {} + for mod in module_ctx.modules: + for tag in mod.tags.download: + requested_versions[tag.bazel_version] = True + + for bazel_version in requested_versions: + if bazel_version not in SHORT_VERSION_MAPPING: + fail("Unrecognised version '%s'" % bazel_version) + version = SHORT_VERSION_MAPPING[bazel_version] + filename = "bazel-%s-%s-%s%s" % (version, os_key, arch_key, exe_extension) + url = "https://github.com/bazelbuild/bazel/releases/download/%s/%s" % (version, filename) + + http_file( + name = bazel_version, + downloaded_file_path = "bazel" + exe_extension, + executable = True, + url = url, + sha256 = SHA_MATRIX[version][platform_key], + ) + + +def _local_bazel_extension_impl(module_ctx): + local_bazel_import(name = "local_bazel") + +download_tag = tag_class(attrs = {"bazel_version": attr.string(mandatory = True)}) + +remote_bazel_extension = module_extension( + implementation = _remote_bazel_extension_impl, + tag_classes = {"download": download_tag}, +) + +local_bazel_extension = module_extension( + implementation = _local_bazel_extension_impl, +) diff --git a/local_bazel.bzl b/local_bazel.bzl new file mode 100644 index 000000000..221b8b701 --- /dev/null +++ b/local_bazel.bzl @@ -0,0 +1,44 @@ +def _local_bazel_import_impl(repository_ctx): + if "windows" in repository_ctx.os.name.lower(): + bazel_real = "bazel-real.exe" + bazel_name = "bazel.exe" + else: + bazel_real = "bazel-real" + bazel_name = "bazel" + + # Prioritise bazel-real if it exists since it's much more likely to be an actual executable. + bazel_path = repository_ctx.which(bazel_real) + if bazel_path == None: + bazel_path = repository_ctx.which(bazel_name) + if bazel_path == None: + fail("Neither '%s' or '%s' not found on PATH." % (bazel_real, bazel_name)) + + repository_ctx.symlink(bazel_path, bazel_real) + repository_ctx.file( + "BUILD", + executable = False, + content = """ +load({sh_binary_bzl}, "sh_binary") + +sh_binary( + name = "{bazel_bin}", + srcs = ["{bazel_binary}"], + visibility = ["//visibility:public"], +) + +alias( + name = "bazel_bin", + actual = "{bazel_bin}", + visibility = ["//visibility:public"], +) + """.format( + sh_binary_bzl = repr(str(Label("@rules_shell//shell:sh_binary.bzl"))), + bazel_bin = bazel_name, + bazel_binary = bazel_real, + ), + ) + +local_bazel_import = repository_rule( + implementation = _local_bazel_import_impl, + environ = ["PATH"], +) diff --git a/tests/integration/BUILD b/tests/integration/BUILD index 94dbcd638..ba8d84112 100644 --- a/tests/integration/BUILD +++ b/tests/integration/BUILD @@ -30,25 +30,33 @@ sh_library( srcs = ["test_utils.sh"], ) +sh_library( + name = "platform_utils", + srcs = ["platform_utils.sh"], +) + gen_workspace( - "simple_compile", + "simple_compile_workspace", workspace_files = glob(["simple_compile/**"]), ) rules_cc_integration_test( name = "simple_compile_test", test_script = "simple_compile_test.sh", - workspace = "simple_compile", + workspace = "simple_compile_workspace", + workspace_name = "simple_compile", ) rules_cc_integration_test( name = "stripped_compile_test", test_script = "stripped_compile_test.sh", - workspace = "simple_compile", + workspace = "simple_compile_workspace", + workspace_name = "simple_compile", ) rules_cc_integration_test( name = "no_recompile_on_shutdown_test", test_script = "no_recompile_on_shutdown_test.sh", - workspace = "simple_compile", + workspace = "simple_compile_workspace", + workspace_name = "simple_compile", ) diff --git a/tests/integration/platform_utils.sh b/tests/integration/platform_utils.sh new file mode 100644 index 000000000..56501a626 --- /dev/null +++ b/tests/integration/platform_utils.sh @@ -0,0 +1,32 @@ +case "$(uname -s | tr [:upper:] [:lower:])" in +linux*) + declare -r PLATFORM=linux + ;; +darwin*) + declare -r PLATFORM=darwin + ;; +msys*|mingw*|cygwin*) + declare -r PLATFORM=windows + ;; +*) + declare -r PLATFORM=unknown + ;; +esac + +function is_linux() { + [[ "$PLATFORM" == "linux" ]] +} + +function is_darwin() { + [[ "$PLATFORM" == "darwin" ]] +} + +function is_windows() { + [[ "$PLATFORM" == "windows" ]] +} + +if is_windows; then + declare -r EXE_EXT=".exe" +else + declare -r EXE_EXT="" +fi diff --git a/tests/integration/rules_cc_integration_test.bzl b/tests/integration/rules_cc_integration_test.bzl index fef49d9c6..38d955265 100644 --- a/tests/integration/rules_cc_integration_test.bzl +++ b/tests/integration/rules_cc_integration_test.bzl @@ -1,6 +1,43 @@ load("@bazel_binaries//:defs.bzl", "bazel_binaries") load("@rules_bazel_integration_test//bazel_integration_test:defs.bzl", "bazel_integration_test", "bazel_integration_tests") load("@rules_shell//shell:sh_binary.bzl", "sh_binary") +load("@rules_shell//shell:sh_test.bzl", "sh_test") + +def package_workspace_impl(ctx): + out_dir = ctx.actions.declare_directory(ctx.label.name) + + commands = [] + pkg_root = ctx.label.package + print(pkg_root) + for f in ctx.files.srcs: + f_path = f.short_path + #if f.short_path.startswith(pkg_root + "/"): + # f_path = f_path[len(pkg_root) + 1:] + dest_path = out_dir.path + "/" + f_path + dest_parent = dest_path.rsplit("/", 1)[0] + + commands.append( + "mkdir -p \"{dest_parent}\" && cp \"{src}\" \"{dest}\"".format( + dest_parent = dest_parent, + src = f.path, + dest = dest_path, + ) + ) + + ctx.actions.run_shell( + inputs = ctx.files.srcs, + outputs = [out_dir], + command = " && ".join(commands), + progress_message = "Creating workspace '{}'".format(ctx.label.name) + ) + return DefaultInfo(files = depset([out_dir])) + +package_workspace = rule( + implementation = package_workspace_impl, + attrs = { + "srcs": attr.label_list(allow_files = True), + } +) def gen_workspace( name, @@ -20,16 +57,35 @@ def gen_workspace( ) build_files.append(build_file) - native.filegroup( + #native.filegroup( + # name = name, + # srcs = workspace_files + build_files + ["//:all_files_for_testing"], + # testonly = True, + #) + package_workspace( name = name, - srcs = workspace_files + build_files, + srcs = workspace_files + build_files + ["//:all_files_for_testing"], testonly = True, ) +def common_prefix(files): + if not files: + return "" + walking = files[0] + others = files[1:] + + for idx, c in enumerate(walking.elems()): + for other in others: + if other[idx] != c: + return walking[:idx] + return walking + + def rules_cc_integration_test( name, test_script, workspace, + workspace_name, deps = [], tags = []): test_binary = name + "_test_runner" @@ -50,39 +106,83 @@ def rules_cc_integration_test( testonly = True, ) - bazel_integration_tests( - name = name, - test_runner = test_binary, - bazel_binaries = bazel_binaries, - bazel_versions = bazel_binaries.versions.all, - env = {"TEST_RUNNER": "$(location %s)" % test_script}, - data = [ - test_script, - ], - workspace_path = Label(workspace).name, - workspace_files = [workspace, "//:all_files_for_testing"], - tags = tags + ["manual"], - ) + # bazel_integration_tests( + # name = name, + # test_runner = test_binary, + # bazel_binaries = bazel_binaries, + # bazel_versions = bazel_binaries.versions.all, + # env = {"TEST_RUNNER": "$(location %s)" % test_script}, + # data = [ + # test_script, + # ], + # workspace_path = Label(workspace).name, + # workspace_files = [workspace, "//:all_files_for_testing"], + # tags = tags + ["manual"], + # ) + + # selected_bazel = name + "_bazel_selected" + # native.alias( + # name = selected_bazel, + # actual = select({ + # "//tests/integration:bazel_7": "@build_bazel_bazel_7_x//:bazel_binary", + # "//tests/integration:bazel_8": "@build_bazel_bazel_8_x//:bazel_binary", + # "//tests/integration:bazel_9": "@build_bazel_bazel_9_x//:bazel_binary", + # "//tests/integration:bazel_latest": "@build_bazel_bazel_last_green//:bazel_binary", + # "//conditions:default": "@build_bazel_bazel_last_green//:bazel_binary", + # }), + # ) + + # bazel_integration_test( + # name = name, + # test_runner = test_binary, + # bazel_binary = selected_bazel, + # env = {"TEST_RUNNER": "$(location %s)" % test_script}, + # data = [test_script], + # workspace_path = Label(workspace).name, + # workspace_files = [workspace, "//:all_files_for_testing"], + # tags = tags, + # ) selected_bazel = name + "_bazel_selected" native.alias( name = selected_bazel, actual = select({ - "//tests/integration:bazel_7": "@build_bazel_bazel_7_x//:bazel_binary", - "//tests/integration:bazel_8": "@build_bazel_bazel_8_x//:bazel_binary", - "//tests/integration:bazel_9": "@build_bazel_bazel_9_x//:bazel_binary", - "//tests/integration:bazel_latest": "@build_bazel_bazel_last_green//:bazel_binary", - "//conditions:default": "@build_bazel_bazel_last_green//:bazel_binary", - }), + "//tests/integration:bazel_7": "@bazel_7//file", + "//tests/integration:bazel_8": "@bazel_8//file", + "//tests/integration:bazel_9": "@bazel_9//file", + "//conditions:default": "@local_bazel//:bazel_bin", + }) ) - bazel_integration_test( + #workspace_dir = name + "_workspace" + #package_workspace( + # name = workspace_dir, + # srcs = [workspace], + # testonly = True, + #) + + sh_test( name = name, - test_runner = test_binary, - bazel_binary = selected_bazel, - env = {"TEST_RUNNER": "$(location %s)" % test_script}, - data = [test_script], - workspace_path = Label(workspace).name, - workspace_files = [workspace, "//:all_files_for_testing"], - tags = tags, + srcs = [ + "//tests/integration:test_launcher.sh", + ], + data = [ + selected_bazel, + test_script, + "//tests/integration:test_utils", + "//tests/integration:platform_utils", + "@rules_shell//shell/runfiles", + Label(workspace), + Label(workspace_name), + ], + env = { + "TEST_RUNNER": "$(location %s)" % test_script, + "BAZEL_BINARY": "$(rlocationpath %s)" % selected_bazel, + "WORKSPACE_DIR": "$(location %s)" % Label(workspace), + "WORKSPACE_PATH": "$(location %s)" % Label(workspace_name), + }, + deps = [ + "//tests/integration:test_utils", + "@rules_shell//shell/runfiles", + ], ) diff --git a/tests/integration/test_launcher.sh b/tests/integration/test_launcher.sh index ef6a50c74..55a95d7f4 100755 --- a/tests/integration/test_launcher.sh +++ b/tests/integration/test_launcher.sh @@ -1,6 +1,5 @@ # Setup runfiles and the test directory and then run the provided test script. -set -x set -euo pipefail # --- begin runfiles.bash initialization v3 --- @@ -15,25 +14,57 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e # --- end runfiles.bash initialization v3 --- -create_scratch_dir_sh_location=rules_bazel_integration_test/tools/create_scratch_dir.sh -create_scratch_dir_sh="$(rlocation "${create_scratch_dir_sh_location}")" || \ - (echo >&2 "Failed to locate ${create_scratch_dir_sh_location}" && exit 1) +# create_scratch_dir_sh_location=rules_bazel_integration_test/tools/create_scratch_dir.sh +# create_scratch_dir_sh="$(rlocation "${create_scratch_dir_sh_location}")" || \ +# (echo >&2 "Failed to locate ${create_scratch_dir_sh_location}" && exit 1) +# +# scratch_dir="$("${create_scratch_dir_sh}" --workspace "${BIT_WORKSPACE_DIR}")" +# +# # Have the provided bazel sit on PATH +# echo "$BIT_BAZEL_BINARY" +# BAZEL_BIN_DIR="$(dirname $BIT_BAZEL_BINARY)" +# export PATH="$BAZEL_BIN_DIR:$PATH" +# ln -s "$BIT_BAZEL_BINARY" "$BAZEL_BIN_DIR/bazel" -scratch_dir="$("${create_scratch_dir_sh}" --workspace "${BIT_WORKSPACE_DIR}")" -# Have the provided bazel sit on PATH -echo "$BIT_BAZEL_BINARY" -BAZEL_BIN_DIR="$(dirname $BIT_BAZEL_BINARY)" -export PATH="$BAZEL_BIN_DIR:$PATH" -ln -s "$BIT_BAZEL_BINARY" "$BAZEL_BIN_DIR/bazel" +source "$(rlocation rules_cc/tests/integration/platform_utils.sh)" + +bazel_binary="$(rlocation "$BAZEL_BINARY")" + +scratch_dir="$TEST_TMPDIR/workspace" +mkdir -p $scratch_dir +bazel_bin_dir="$TEST_TMPDIR/bazel" +mkdir -p $bazel_bin_dir + +if is_windows; then + scratch_dir="$(cygpath -u $scratch_dir)" + bazel_bin_dir="$(cygpath -u $bazel_bin_dir)" +fi + +cp "$bazel_binary" "$bazel_bin_dir/bazel" + +ls -l "$bazel_bin_dir" + +export PATH="$bazel_bin_dir:$PATH" export TEST_log="$scratch_dir/test.log" +workspace="$(rlocation "rules_cc/$WORKSPACE_DIR")" + +cp -r "$workspace"/* "$scratch_dir/" + test_runner_path="$(rlocation "rules_cc/${TEST_RUNNER}")" || (echo >&2 "FAILED TO LOAD RUNNER" && exit 1) test_runner_cmd=( "${test_runner_path}" ) -# Change into scratch directory +# Change into scratch workspace cd "${scratch_dir}" -ls -l +echo "$WORKSPACE_PATH" +echo "$(rlocation "$WORKSPACE_PATH")" +cd "$WORKSPACE_PATH" + +echo "WHICH BAZEL" +which bazel +bazel version + echo "$test_runner_cmd" "${test_runner_cmd[@]}"