Skip to content

Commit 68e0994

Browse files
committed
Update to not use rules_bazel_integration_test
1 parent 82b95af commit 68e0994

6 files changed

Lines changed: 320 additions & 45 deletions

File tree

MODULE.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@ use_repo(
9797
"build_bazel_bazel_9_x",
9898
"build_bazel_bazel_last_green",
9999
)
100+
101+
local_bazel = use_extension("//:extensions.bzl", "local_bazel_extension")
102+
use_repo(local_bazel, "local_bazel")
103+
104+
remote_bazel_fetcher = use_extension("//:extensions.bzl", "remote_bazel_extension")
105+
remote_bazel_fetcher.download(bazel_version = "bazel_7")
106+
remote_bazel_fetcher.download(bazel_version = "bazel_8")
107+
remote_bazel_fetcher.download(bazel_version = "bazel_9")
108+
use_repo(remote_bazel_fetcher, "bazel_7", "bazel_8", "bazel_9")

extensions.bzl

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
2+
load("//:local_bazel.bzl", "local_bazel_import")
3+
4+
SHORT_VERSION_MAPPING = {
5+
"bazel_7": "7.7.1",
6+
"bazel_8": "8.7.0",
7+
"bazel_9": "9.1.0",
8+
}
9+
10+
SHA_MATRIX = {
11+
"7.7.1": {
12+
"linux_x86_64": "115a1b62be95f29e5821d4dddffba1b058905a48019b499919c285e7f708d5e2",
13+
"linux_arm64": "71df04ec724f1b577f1f47ec9a6b81d13f39683f6c3215cacf45fdaf40b2c5c1",
14+
"darwin_x86_64": "8582aea5ee2d8d0448bbda10fd7034734db1a21cbe4ea351a10012b969aa5d31",
15+
"darwin_arm64": "fe8a1ee9064e94afae075c0dd4efb453db9c1373b9df12fecbff8479d408eb08",
16+
"windows_x86_64": "6d9fb21e806cf4f4e61bfa2bc865df4900ffdc1e9ea90ca1016ba70367ef0de4",
17+
},
18+
"8.7.0": {
19+
"linux_x86_64": "d7606e679b78067c811096fb3d6cf135225b528835ca396e3a4dddf957859544",
20+
"linux_arm64": "bfe9558bd8a2ecfe4841ec46c0dbccb4b469fe22d81f2f859de0de222b3e7ce3",
21+
"darwin_x86_64": "76f3eb05782098e9f9ddd8247ec969b085195a3ae2978c81721a2235052ccf26",
22+
"darwin_arm64": "575f20fb23955e02f73519befd180df635b4ed0960c60f0e70fcc8d74014a713",
23+
"windows_x86_64": "29f1796f57379933340afa135f02703ffa21dd30135754bea695f8fd15103420",
24+
},
25+
"9.1.0": {
26+
"linux_x86_64": "a667454f3f4f8878df8199136b82c199f6ada8477b337fae3b1ef854f01e4e2f",
27+
"linux_arm64": "ba933bfc943e4c44f0743a5823aa2312a34b39628532add5dd037e08d8ec27a4",
28+
"darwin_x86_64": "666c6c79eda285cada5f5c39c891c6dd7ee0971b20bff365ea87a4b897271433",
29+
"darwin_arm64": "084a1784fa8f0dcae77fb4e88faa15048d8149a36c947ce198508bffb060e1bb",
30+
"windows_x86_64": "b457dccd36a9bb9be01326cc1d069a201bb50b4b94562a652afb6f43c5148d42",
31+
},
32+
}
33+
34+
def _remote_bazel_extension_impl(module_ctx):
35+
os_name = module_ctx.os.name.lower()
36+
os_key = "linux"
37+
exe_extension = ""
38+
if "windows" in os_name:
39+
os_key = "windows"
40+
exe_extension = ".exe"
41+
elif "mac" in os_name or "darwin" in os_name:
42+
os_key = "darwin"
43+
44+
cpu_arch = module_ctx.os.arch.lower()
45+
if "x86_64" in cpu_arch or "amd64" in cpu_arch:
46+
arch_key = "x86_64"
47+
elif "arm64" in cpu_arch:
48+
arch_key = "arm64"
49+
else:
50+
fail("Unsupported architecture '{}'.".format(cpu_arch))
51+
52+
platform_key = "{}_{}".format(os_key, arch_key)
53+
54+
requested_versions = {}
55+
for mod in module_ctx.modules:
56+
for tag in mod.tags.download:
57+
requested_versions[tag.bazel_version] = True
58+
59+
for bazel_version in requested_versions:
60+
if bazel_version not in SHORT_VERSION_MAPPING:
61+
fail("Unrecognised version '%s'" % bazel_version)
62+
version = SHORT_VERSION_MAPPING[bazel_version]
63+
filename = "bazel-%s-%s-%s%s" % (version, os_key, arch_key, exe_extension)
64+
url = "https://github.com/bazelbuild/bazel/releases/download/%s/%s" % (version, filename)
65+
66+
http_file(
67+
name = bazel_version,
68+
downloaded_file_path = "bazel" + exe_extension,
69+
executable = True,
70+
url = url,
71+
sha256 = SHA_MATRIX[version][platform_key],
72+
)
73+
74+
75+
def _local_bazel_extension_impl(module_ctx):
76+
local_bazel_import(name = "local_bazel")
77+
78+
download_tag = tag_class(attrs = {"bazel_version": attr.string(mandatory = True)})
79+
80+
remote_bazel_extension = module_extension(
81+
implementation = _remote_bazel_extension_impl,
82+
tag_classes = {"download": download_tag},
83+
)
84+
85+
local_bazel_extension = module_extension(
86+
implementation = _local_bazel_extension_impl,
87+
)

local_bazel.bzl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
def _local_bazel_import_impl(repository_ctx):
2+
if "windows" in repository_ctx.os.name.lower():
3+
bazel_real = "bazel-real.exe"
4+
bazel_name = "bazel.exe"
5+
else:
6+
bazel_real = "bazel-real"
7+
bazel_name = "bazel"
8+
9+
# Prioritise bazel-real if it exists since it's much more likely to be an actual executable.
10+
bazel_path = repository_ctx.which(bazel_real)
11+
if bazel_path == None:
12+
bazel_path = repository_ctx.which(bazel_name)
13+
if bazel_path == None:
14+
fail("Neither '%s' or '%s' not found on PATH." % (bazel_real, bazel_name))
15+
16+
repository_ctx.symlink(bazel_path, bazel_real)
17+
repository_ctx.file(
18+
"BUILD",
19+
executable = False,
20+
content = """
21+
load({sh_binary_bzl}, "sh_binary")
22+
23+
sh_binary(
24+
name = "{bazel_bin}",
25+
srcs = ["{bazel_binary}"],
26+
visibility = ["//visibility:public"],
27+
)
28+
29+
alias(
30+
name = "bazel_bin",
31+
actual = "{bazel_bin}",
32+
visibility = ["//visibility:public"],
33+
)
34+
""".format(
35+
sh_binary_bzl = repr(str(Label("@rules_shell//shell:sh_binary.bzl"))),
36+
bazel_bin = bazel_name,
37+
bazel_binary = bazel_real,
38+
),
39+
)
40+
41+
local_bazel_import = repository_rule(
42+
implementation = _local_bazel_import_impl,
43+
environ = ["PATH"],
44+
)

tests/integration/BUILD

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,27 @@ sh_library(
3131
)
3232

3333
gen_workspace(
34-
"simple_compile",
34+
"simple_compile_workspace",
3535
workspace_files = glob(["simple_compile/**"]),
3636
)
3737

3838
rules_cc_integration_test(
3939
name = "simple_compile_test",
4040
test_script = "simple_compile_test.sh",
41-
workspace = "simple_compile",
41+
workspace = "simple_compile_workspace",
42+
workspace_name = "simple_compile",
4243
)
4344

4445
rules_cc_integration_test(
4546
name = "stripped_compile_test",
4647
test_script = "stripped_compile_test.sh",
47-
workspace = "simple_compile",
48+
workspace = "simple_compile_workspace",
49+
workspace_name = "simple_compile",
4850
)
4951

5052
rules_cc_integration_test(
5153
name = "no_recompile_on_shutdown_test",
5254
test_script = "no_recompile_on_shutdown_test.sh",
53-
workspace = "simple_compile",
55+
workspace = "simple_compile_workspace",
56+
workspace_name = "simple_compile",
5457
)
Lines changed: 128 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
load("@bazel_binaries//:defs.bzl", "bazel_binaries")
22
load("@rules_bazel_integration_test//bazel_integration_test:defs.bzl", "bazel_integration_test", "bazel_integration_tests")
33
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
4+
load("@rules_shell//shell:sh_test.bzl", "sh_test")
5+
6+
def package_workspace_impl(ctx):
7+
out_dir = ctx.actions.declare_directory(ctx.label.name)
8+
9+
commands = []
10+
pkg_root = ctx.label.package
11+
print(pkg_root)
12+
for f in ctx.files.srcs:
13+
f_path = f.short_path
14+
#if f.short_path.startswith(pkg_root + "/"):
15+
# f_path = f_path[len(pkg_root) + 1:]
16+
dest_path = out_dir.path + "/" + f_path
17+
dest_parent = dest_path.rsplit("/", 1)[0]
18+
19+
commands.append(
20+
"mkdir -p \"{dest_parent}\" && cp \"{src}\" \"{dest}\"".format(
21+
dest_parent = dest_parent,
22+
src = f.path,
23+
dest = dest_path,
24+
)
25+
)
26+
27+
ctx.actions.run_shell(
28+
inputs = ctx.files.srcs,
29+
outputs = [out_dir],
30+
command = " && ".join(commands),
31+
progress_message = "Creating workspace '{}'".format(ctx.label.name)
32+
)
33+
return DefaultInfo(files = depset([out_dir]))
34+
35+
package_workspace = rule(
36+
implementation = package_workspace_impl,
37+
attrs = {
38+
"srcs": attr.label_list(allow_files = True),
39+
}
40+
)
441

542
def gen_workspace(
643
name,
@@ -20,16 +57,35 @@ def gen_workspace(
2057
)
2158
build_files.append(build_file)
2259

23-
native.filegroup(
60+
#native.filegroup(
61+
# name = name,
62+
# srcs = workspace_files + build_files + ["//:all_files_for_testing"],
63+
# testonly = True,
64+
#)
65+
package_workspace(
2466
name = name,
25-
srcs = workspace_files + build_files,
67+
srcs = workspace_files + build_files + ["//:all_files_for_testing"],
2668
testonly = True,
2769
)
2870

71+
def common_prefix(files):
72+
if not files:
73+
return ""
74+
walking = files[0]
75+
others = files[1:]
76+
77+
for idx, c in enumerate(walking.elems()):
78+
for other in others:
79+
if other[idx] != c:
80+
return walking[:idx]
81+
return walking
82+
83+
2984
def rules_cc_integration_test(
3085
name,
3186
test_script,
3287
workspace,
88+
workspace_name,
3389
deps = [],
3490
tags = []):
3591
test_binary = name + "_test_runner"
@@ -50,39 +106,82 @@ def rules_cc_integration_test(
50106
testonly = True,
51107
)
52108

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-
)
109+
# bazel_integration_tests(
110+
# name = name,
111+
# test_runner = test_binary,
112+
# bazel_binaries = bazel_binaries,
113+
# bazel_versions = bazel_binaries.versions.all,
114+
# env = {"TEST_RUNNER": "$(location %s)" % test_script},
115+
# data = [
116+
# test_script,
117+
# ],
118+
# workspace_path = Label(workspace).name,
119+
# workspace_files = [workspace, "//:all_files_for_testing"],
120+
# tags = tags + ["manual"],
121+
# )
122+
123+
# selected_bazel = name + "_bazel_selected"
124+
# native.alias(
125+
# name = selected_bazel,
126+
# actual = select({
127+
# "//tests/integration:bazel_7": "@build_bazel_bazel_7_x//:bazel_binary",
128+
# "//tests/integration:bazel_8": "@build_bazel_bazel_8_x//:bazel_binary",
129+
# "//tests/integration:bazel_9": "@build_bazel_bazel_9_x//:bazel_binary",
130+
# "//tests/integration:bazel_latest": "@build_bazel_bazel_last_green//:bazel_binary",
131+
# "//conditions:default": "@build_bazel_bazel_last_green//:bazel_binary",
132+
# }),
133+
# )
134+
135+
# bazel_integration_test(
136+
# name = name,
137+
# test_runner = test_binary,
138+
# bazel_binary = selected_bazel,
139+
# env = {"TEST_RUNNER": "$(location %s)" % test_script},
140+
# data = [test_script],
141+
# workspace_path = Label(workspace).name,
142+
# workspace_files = [workspace, "//:all_files_for_testing"],
143+
# tags = tags,
144+
# )
66145

67146
selected_bazel = name + "_bazel_selected"
68147
native.alias(
69148
name = selected_bazel,
70149
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-
}),
150+
"//tests/integration:bazel_7": "@bazel_7//file",
151+
"//tests/integration:bazel_8": "@bazel_8//file",
152+
"//tests/integration:bazel_9": "@bazel_9//file",
153+
"//conditions:default": "@local_bazel//:bazel_bin",
154+
})
77155
)
78156

79-
bazel_integration_test(
157+
#workspace_dir = name + "_workspace"
158+
#package_workspace(
159+
# name = workspace_dir,
160+
# srcs = [workspace],
161+
# testonly = True,
162+
#)
163+
164+
sh_test(
80165
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,
166+
srcs = [
167+
"//tests/integration:test_launcher.sh",
168+
],
169+
data = [
170+
selected_bazel,
171+
test_script,
172+
"//tests/integration:test_utils",
173+
"@rules_shell//shell/runfiles",
174+
Label(workspace),
175+
Label(workspace_name),
176+
],
177+
env = {
178+
"TEST_RUNNER": "$(location %s)" % test_script,
179+
"BAZEL_BINARY": "$(location %s)" % selected_bazel,
180+
"WORKSPACE_DIR": "$(location %s)" % Label(workspace),
181+
"WORKSPACE_PATH": "$(location %s)" % Label(workspace_name),
182+
},
183+
deps = [
184+
"//tests/integration:test_utils",
185+
"@rules_shell//shell/runfiles",
186+
],
88187
)

0 commit comments

Comments
 (0)