Skip to content

Commit 82b95af

Browse files
c-mitacopybara-github
authored andcommitted
DO NOT SUBMIT
WIP for rules_cc integration tests PiperOrigin-RevId: 896443857 Change-Id: I1184a36a4b026c773891d44ae6006613791b3606
1 parent 4733851 commit 82b95af

17 files changed

Lines changed: 332 additions & 2 deletions

BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,12 @@ filegroup(
4444
],
4545
visibility = ["//:__subpackages__"],
4646
)
47+
48+
filegroup(
49+
name = "all_files_for_testing",
50+
srcs = [
51+
"BUILD",
52+
"MODULE.bazel",
53+
"//cc:all_files_for_testing",
54+
],
55+
)

MODULE.bazel

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,50 @@ bazel_dep(name = "stardoc", version = "0.8.0", dev_dependency = True)
5050
# Compatibility layer
5151
compat = use_extension("//cc:extensions.bzl", "compatibility_proxy")
5252
use_repo(compat, "cc_compatibility_proxy")
53+
54+
bazel_dep(
55+
name = "rules_bazel_integration_test",
56+
version = "0.37.1",
57+
dev_dependency = True,
58+
)
59+
60+
bazel_binaries = use_extension(
61+
"@rules_bazel_integration_test//:extensions.bzl",
62+
"bazel_binaries",
63+
dev_dependency = True,
64+
)
65+
66+
# A bazel version which is actually whatever is being used.
67+
bazel_binaries.local(
68+
name = "env",
69+
path = "tests/integration/bazel_from_env.sh",
70+
)
71+
72+
bazel_binaries.download(
73+
version = "7.x",
74+
)
75+
76+
bazel_binaries.download(
77+
version = "8.x",
78+
)
79+
80+
bazel_binaries.download(
81+
version = "9.x",
82+
)
83+
84+
bazel_binaries.download(
85+
version = "last_green",
86+
)
87+
88+
bazel_binaries.download(
89+
version = "rolling",
90+
)
91+
92+
use_repo(
93+
bazel_binaries,
94+
"bazel_binaries",
95+
"build_bazel_bazel_7_x",
96+
"build_bazel_bazel_8_x",
97+
"build_bazel_bazel_9_x",
98+
"build_bazel_bazel_last_green",
99+
)

cc/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ licenses(["notice"]) # Apache 2.0
2424
filegroup(
2525
name = "all_files_for_testing",
2626
srcs = glob(["**"]) + [
27-
"//cc/private/rules_impl:srcs",
27+
"//cc/common:srcs",
28+
"//cc/private:srcs",
2829
"//cc/private/toolchain:srcs",
30+
"//cc/proto:srcs",
31+
"//cc/toolchains:srcs",
2932
],
3033
)
3134

cc/private/toolchain/BUILD

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ filegroup(
7575

7676
filegroup(
7777
name = "srcs",
78-
srcs = glob(["**"]),
78+
srcs = glob(["**"]) + [
79+
"//cc/private/toolchain/test:srcs",
80+
],
7981
)
8082

8183
filegroup(

cc/private/toolchain/test/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ compat_toolchain(
1111
toolchain_type = "@bazel_tools//tools/cpp:test_runner_toolchain_type",
1212
use_target_platform_constraints = True,
1313
)
14+
15+
filegroup(
16+
name = "srcs",
17+
srcs = glob(["**"]),
18+
visibility = ["//cc/private/toolchain:__pkg__"],
19+
)

cc/proto/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ package(default_visibility = ["//visibility:public"])
22

33
# Toolchain type provided by proto_lang_toolchain rule and used by cc_proto_library
44
toolchain_type(name = "toolchain_type")
5+
6+
filegroup(
7+
name = "srcs",
8+
srcs = glob(["**"]),
9+
visibility = ["//cc:__pkg__"],
10+
)

tests/integration/BUILD

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
2+
load("@rules_shell//shell:sh_library.bzl", "sh_library")
3+
load("//tests/integration:rules_cc_integration_test.bzl", "gen_workspace", "rules_cc_integration_test")
4+
5+
BAZEL_VERSIONS = [
6+
"7",
7+
"8",
8+
"9",
9+
"latest",
10+
]
11+
12+
string_flag(
13+
name = "bazel_version",
14+
build_setting_default = "latest",
15+
values = BAZEL_VERSIONS,
16+
)
17+
18+
[
19+
config_setting(
20+
name = "bazel_" + VERSION,
21+
flag_values = {
22+
":bazel_version": VERSION,
23+
},
24+
)
25+
for VERSION in BAZEL_VERSIONS
26+
]
27+
28+
sh_library(
29+
name = "test_utils",
30+
srcs = ["test_utils.sh"],
31+
)
32+
33+
gen_workspace(
34+
"simple_compile",
35+
workspace_files = glob(["simple_compile/**"]),
36+
)
37+
38+
rules_cc_integration_test(
39+
name = "simple_compile_test",
40+
test_script = "simple_compile_test.sh",
41+
workspace = "simple_compile",
42+
)
43+
44+
rules_cc_integration_test(
45+
name = "stripped_compile_test",
46+
test_script = "stripped_compile_test.sh",
47+
workspace = "simple_compile",
48+
)
49+
50+
rules_cc_integration_test(
51+
name = "no_recompile_on_shutdown_test",
52+
test_script = "no_recompile_on_shutdown_test.sh",
53+
workspace = "simple_compile",
54+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
bazel "$@"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set -euo pipefail
2+
[ -z "$TEST_SRCDIR" ] && { echo "TEST_SRCDIR not set!" >&2; exit 1; }
3+
4+
source "$(rlocation rules_cc/tests/integration/test_utils.sh)"
5+
6+
bazel build -s //:hello >& "$TEST_log" || fail "Build failed"
7+
expect_log "Compiling hello.cc"
8+
bazel shutdown
9+
bazel build -s //:hello >& "$TEST_log" || fail "Build failed"
10+
expect_not_log "Compiling hello.cc"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
load("@bazel_binaries//:defs.bzl", "bazel_binaries")
2+
load("@rules_bazel_integration_test//bazel_integration_test:defs.bzl", "bazel_integration_test", "bazel_integration_tests")
3+
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
4+
5+
def gen_workspace(
6+
name,
7+
workspace_files = []):
8+
build_file_standins = [f for f in workspace_files if f.endswith("BUILD.test")]
9+
if len(build_file_standins) == 0:
10+
fail("Could not find any BUILD.test files")
11+
build_files = []
12+
for build_file_standin in build_file_standins:
13+
build_file = build_file_standin[:-len(".test")]
14+
native.genrule(
15+
name = build_file + "_gen",
16+
srcs = [build_file_standin],
17+
outs = [build_file],
18+
cmd = "cp $< $@",
19+
testonly = True,
20+
)
21+
build_files.append(build_file)
22+
23+
native.filegroup(
24+
name = name,
25+
srcs = workspace_files + build_files,
26+
testonly = True,
27+
)
28+
29+
def rules_cc_integration_test(
30+
name,
31+
test_script,
32+
workspace,
33+
deps = [],
34+
tags = []):
35+
test_binary = name + "_test_runner"
36+
sh_binary(
37+
name = test_binary,
38+
srcs = ["//tests/integration:test_launcher.sh"],
39+
data = deps + [
40+
test_script,
41+
"//cc:all_files_for_testing",
42+
"//tests/integration:test_utils",
43+
"@rules_shell//shell/runfiles",
44+
"@rules_bazel_integration_test//tools:create_scratch_dir",
45+
],
46+
deps = [
47+
"//tests/integration:test_utils",
48+
"@rules_shell//shell/runfiles",
49+
],
50+
testonly = True,
51+
)
52+
53+
bazel_integration_tests(
54+
name = name,
55+
test_runner = test_binary,
56+
bazel_binaries = bazel_binaries,
57+
bazel_versions = bazel_binaries.versions.all,
58+
env = {"TEST_RUNNER": "$(location %s)" % test_script},
59+
data = [
60+
test_script,
61+
],
62+
workspace_path = Label(workspace).name,
63+
workspace_files = [workspace, "//:all_files_for_testing"],
64+
tags = tags + ["manual"],
65+
)
66+
67+
selected_bazel = name + "_bazel_selected"
68+
native.alias(
69+
name = selected_bazel,
70+
actual = select({
71+
"//tests/integration:bazel_7": "@build_bazel_bazel_7_x//:bazel_binary",
72+
"//tests/integration:bazel_8": "@build_bazel_bazel_8_x//:bazel_binary",
73+
"//tests/integration:bazel_9": "@build_bazel_bazel_9_x//:bazel_binary",
74+
"//tests/integration:bazel_latest": "@build_bazel_bazel_last_green//:bazel_binary",
75+
"//conditions:default": "@build_bazel_bazel_last_green//:bazel_binary",
76+
}),
77+
)
78+
79+
bazel_integration_test(
80+
name = name,
81+
test_runner = test_binary,
82+
bazel_binary = selected_bazel,
83+
env = {"TEST_RUNNER": "$(location %s)" % test_script},
84+
data = [test_script],
85+
workspace_path = Label(workspace).name,
86+
workspace_files = [workspace, "//:all_files_for_testing"],
87+
tags = tags,
88+
)

0 commit comments

Comments
 (0)