`platform_transition_test`: Use transitioned target platform (#965)

The target platform of a test rule matters for the resolution of the execution platform of the test action, either via test toolchains or `--use_target_platform_for_tests`. With an outgoing transition, tests would run on the wrong platform in these cases, so use an incoming transition for the test rule.
This commit is contained in:
Fabian Meumertzheim 2024-10-15 16:12:19 +02:00 committed by GitHub
parent fa9bbeefdd
commit 64eb5f8cea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 24 deletions

View File

@ -1,5 +1,6 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file") load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@platforms//host:constraints.bzl", "HOST_CONSTRAINTS")
load("//lib:diff_test.bzl", "diff_test") load("//lib:diff_test.bzl", "diff_test")
load( load(
"//lib:transitions.bzl", "//lib:transitions.bzl",
@ -135,22 +136,16 @@ platform_transition_binary(
platform_transition_test( platform_transition_test(
name = "transitioned_go_test_x86_64", name = "transitioned_go_test_x86_64",
binary = ":test_transition_test", binary = ":test_transition_test",
# only run this test on x86_64 platforms # only run this test on an x86_64 host
target_compatible_with = [ target_compatible_with = [] if sorted(HOST_CONSTRAINTS) == ["@platforms//cpu:x86_64", "@platforms//os:linux"] else ["@platforms//:incompatible"],
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
target_platform = "x86_64_linux", target_platform = "x86_64_linux",
) )
platform_transition_test( platform_transition_test(
name = "transitioned_go_test_arm64", name = "transitioned_go_test_arm64",
binary = ":test_transition_test", binary = ":test_transition_test",
# only run this test on arm64 platforms # only run this test on an x86_64 host
target_compatible_with = [ target_compatible_with = [] if sorted(HOST_CONSTRAINTS) == ["@platforms//cpu:aarch64", "@platforms//os:linux"] else ["@platforms//:incompatible"],
"@platforms//cpu:arm64",
"@platforms//os:linux",
],
target_platform = "arm64_linux", target_platform = "arm64_linux",
) )

View File

@ -52,7 +52,12 @@ def _platform_transition_binary_impl(ctx):
# forwarding DefaultInfo. # forwarding DefaultInfo.
result = [] result = []
binary = ctx.attr.binary[0]
# ctx.attr.binary is a singleton list if this rule uses an outgoing transition.
if type(ctx.attr.binary) == type([]):
binary = ctx.attr.binary[0]
else:
binary = ctx.attr.binary
default_info = binary[DefaultInfo] default_info = binary[DefaultInfo]
files = default_info.files files = default_info.files
@ -87,28 +92,36 @@ def _platform_transition_binary_impl(ctx):
return result return result
_platform_transition_attrs = { def _get_platform_transition_attrs(binary_cfg):
"basename": attr.string(), return {
"binary": attr.label(allow_files = True, cfg = _transition_platform), "basename": attr.string(),
"target_platform": attr.label( "binary": attr.label(allow_files = True, cfg = binary_cfg),
doc = "The target platform to transition the binary.", "target_platform": attr.label(
mandatory = True, doc = "The target platform to transition the binary.",
), mandatory = True,
"_allowlist_function_transition": attr.label( ),
default = "@bazel_tools//tools/allowlists/function_transition_allowlist", "_allowlist_function_transition": attr.label(
), default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
} ),
}
platform_transition_binary = rule( platform_transition_binary = rule(
implementation = _platform_transition_binary_impl, implementation = _platform_transition_binary_impl,
attrs = _platform_transition_attrs, # Use an outgoing transition since the target platform of the
# platform_transition_binary doesn't matter and it results in a more
# intuitive output path (matching an untransitioned binary).
attrs = _get_platform_transition_attrs(binary_cfg = _transition_platform),
executable = True, executable = True,
doc = "Transitions the binary to use the provided platform.", doc = "Transitions the binary to use the provided platform.",
) )
platform_transition_test = rule( platform_transition_test = rule(
implementation = _platform_transition_binary_impl, implementation = _platform_transition_binary_impl,
attrs = _platform_transition_attrs, attrs = _get_platform_transition_attrs(binary_cfg = "target"),
# Use an incoming transition since the target platform of the
# platform_transition_test does matter for the exec platform resolution of
# the test action.
cfg = _transition_platform,
test = True, test = True,
doc = "Transitions the test to use the provided platform.", doc = "Transitions the test to use the provided platform.",
) )