Skip to content

Commit faaada2

Browse files
authored
feat: add platform_transition_test (#770)
* Add platform_transition_test Signed-off-by: Thomas Lam <thomaslam@canva.com> * Set target platform constraints for tests Signed-off-by: Thomas Lam <thomaslam@canva.com> * Update docs Signed-off-by: Thomas Lam <thomaslam@canva.com> --------- Signed-off-by: Thomas Lam <thomaslam@canva.com>
1 parent 7716549 commit faaada2

File tree

4 files changed

+103
-13
lines changed

4 files changed

+103
-13
lines changed

docs/transitions.md

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/tests/transitions/BUILD.bazel

+52-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
load("@bazel_skylib//rules:write_file.bzl", "write_file")
2-
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
2+
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
33
load("//lib:diff_test.bzl", "diff_test")
4-
load("//lib:transitions.bzl", "platform_transition_binary", "platform_transition_filegroup")
4+
load(
5+
"//lib:transitions.bzl",
6+
"platform_transition_binary",
7+
"platform_transition_filegroup",
8+
"platform_transition_test",
9+
)
510

611
platform(
712
name = "armv7_linux",
@@ -109,6 +114,11 @@ go_binary(
109114
visibility = ["//visibility:public"],
110115
)
111116

117+
go_test(
118+
name = "test_transition_test",
119+
srcs = ["simple_test.go"],
120+
)
121+
112122
platform_transition_binary(
113123
name = "transitioned_go_binary_x86_64",
114124
binary = ":test_transition_binary",
@@ -121,6 +131,26 @@ platform_transition_binary(
121131
target_platform = "arm64_linux",
122132
)
123133

134+
platform_transition_test(
135+
name = "transitioned_go_test_x86_64",
136+
binary = ":test_transition_test",
137+
# only run this test on x86_64 platforms
138+
target_compatible_with = [
139+
"@platforms//cpu:x86_64",
140+
],
141+
target_platform = "x86_64_linux",
142+
)
143+
144+
platform_transition_test(
145+
name = "transitioned_go_test_arm64",
146+
binary = ":test_transition_test",
147+
# only run this test on arm64 platforms
148+
target_compatible_with = [
149+
"@platforms//cpu:arm64",
150+
],
151+
target_platform = "arm64_linux",
152+
)
153+
124154
sh_test(
125155
name = "test_go_binary_is_x86_64",
126156
srcs = ["test_file_type_contains.sh"],
@@ -141,6 +171,26 @@ sh_test(
141171
data = [":transitioned_go_binary_arm64"],
142172
)
143173

174+
sh_test(
175+
name = "test_go_test_is_x86_64",
176+
srcs = ["test_file_type_contains.sh"],
177+
args = [
178+
"$(rootpath :transitioned_go_test_x86_64)",
179+
"x86-64",
180+
],
181+
data = [":transitioned_go_test_x86_64"],
182+
)
183+
184+
sh_test(
185+
name = "test_go_test_is_arm64",
186+
srcs = ["test_file_type_contains.sh"],
187+
args = [
188+
"$(rootpath :transitioned_go_test_arm64)",
189+
"aarch64",
190+
],
191+
data = [":transitioned_go_test_arm64"],
192+
)
193+
144194
go_library(
145195
name = "transitions_lib",
146196
srcs = ["main.go"],

lib/tests/transitions/simple_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package simple_test
2+
3+
import "testing"
4+
5+
func TestAdd(t *testing.T) {
6+
result := 1 + 2
7+
if result != 3 {
8+
t.Errorf("got %q, wanted %q", result, 3)
9+
}
10+
}

lib/transitions.bzl

+20-11
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,28 @@ def _platform_transition_binary_impl(ctx):
8787

8888
return result
8989

90+
_platform_transition_attrs = {
91+
"basename": attr.string(),
92+
"binary": attr.label(allow_files = True, cfg = _transition_platform),
93+
"target_platform": attr.label(
94+
doc = "The target platform to transition the binary.",
95+
mandatory = True,
96+
),
97+
"_allowlist_function_transition": attr.label(
98+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
99+
),
100+
}
101+
90102
platform_transition_binary = rule(
91103
implementation = _platform_transition_binary_impl,
92-
attrs = {
93-
"basename": attr.string(),
94-
"binary": attr.label(allow_files = True, cfg = _transition_platform),
95-
"target_platform": attr.label(
96-
doc = "The target platform to transition the binary.",
97-
mandatory = True,
98-
),
99-
"_allowlist_function_transition": attr.label(
100-
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
101-
),
102-
},
104+
attrs = _platform_transition_attrs,
103105
executable = True,
104106
doc = "Transitions the binary to use the provided platform.",
105107
)
108+
109+
platform_transition_test = rule(
110+
implementation = _platform_transition_binary_impl,
111+
attrs = _platform_transition_attrs,
112+
test = True,
113+
doc = "Transitions the test to use the provided platform.",
114+
)

0 commit comments

Comments
 (0)