diff --git a/docs/transitions.md b/docs/transitions.md index 83b656179..85250d3b3 100644 --- a/docs/transitions.md +++ b/docs/transitions.md @@ -43,3 +43,24 @@ Transitions the srcs to use the provided platform. The filegroup will contain ar | target_platform | The target platform to transition the srcs. | Label | required | | + + +## platform_transition_test + +
+platform_transition_test(name, basename, binary, target_platform)
+
+ +Transitions the test to use the provided platform. + +**ATTRIBUTES** + + +| Name | Description | Type | Mandatory | Default | +| :------------- | :------------- | :------------- | :------------- | :------------- | +| name | A unique name for this target. | Name | required | | +| basename | - | String | optional | "" | +| binary | - | Label | optional | None | +| target_platform | The target platform to transition the binary. | Label | required | | + + diff --git a/lib/tests/transitions/BUILD.bazel b/lib/tests/transitions/BUILD.bazel index 3908c8f33..db1f31b39 100644 --- a/lib/tests/transitions/BUILD.bazel +++ b/lib/tests/transitions/BUILD.bazel @@ -1,7 +1,12 @@ load("@bazel_skylib//rules:write_file.bzl", "write_file") -load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test") load("//lib:diff_test.bzl", "diff_test") -load("//lib:transitions.bzl", "platform_transition_binary", "platform_transition_filegroup") +load( + "//lib:transitions.bzl", + "platform_transition_binary", + "platform_transition_filegroup", + "platform_transition_test", +) platform( name = "armv7_linux", @@ -109,6 +114,11 @@ go_binary( visibility = ["//visibility:public"], ) +go_test( + name = "test_transition_test", + srcs = ["simple_test.go"], +) + platform_transition_binary( name = "transitioned_go_binary_x86_64", binary = ":test_transition_binary", @@ -121,6 +131,26 @@ platform_transition_binary( target_platform = "arm64_linux", ) +platform_transition_test( + name = "transitioned_go_test_x86_64", + binary = ":test_transition_test", + # only run this test on x86_64 platforms + target_compatible_with = [ + "@platforms//cpu:x86_64", + ], + target_platform = "x86_64_linux", +) + +platform_transition_test( + name = "transitioned_go_test_arm64", + binary = ":test_transition_test", + # only run this test on arm64 platforms + target_compatible_with = [ + "@platforms//cpu:arm64", + ], + target_platform = "arm64_linux", +) + sh_test( name = "test_go_binary_is_x86_64", srcs = ["test_file_type_contains.sh"], @@ -141,6 +171,26 @@ sh_test( data = [":transitioned_go_binary_arm64"], ) +sh_test( + name = "test_go_test_is_x86_64", + srcs = ["test_file_type_contains.sh"], + args = [ + "$(rootpath :transitioned_go_test_x86_64)", + "x86-64", + ], + data = [":transitioned_go_test_x86_64"], +) + +sh_test( + name = "test_go_test_is_arm64", + srcs = ["test_file_type_contains.sh"], + args = [ + "$(rootpath :transitioned_go_test_arm64)", + "aarch64", + ], + data = [":transitioned_go_test_arm64"], +) + go_library( name = "transitions_lib", srcs = ["main.go"], diff --git a/lib/tests/transitions/simple_test.go b/lib/tests/transitions/simple_test.go new file mode 100644 index 000000000..4b1bee0ab --- /dev/null +++ b/lib/tests/transitions/simple_test.go @@ -0,0 +1,10 @@ +package simple_test + +import "testing" + +func TestAdd(t *testing.T) { + result := 1 + 2 + if result != 3 { + t.Errorf("got %q, wanted %q", result, 3) + } +} diff --git a/lib/transitions.bzl b/lib/transitions.bzl index b01e75f23..9fda22c58 100644 --- a/lib/transitions.bzl +++ b/lib/transitions.bzl @@ -87,19 +87,28 @@ def _platform_transition_binary_impl(ctx): return result +_platform_transition_attrs = { + "basename": attr.string(), + "binary": attr.label(allow_files = True, cfg = _transition_platform), + "target_platform": attr.label( + doc = "The target platform to transition the binary.", + mandatory = True, + ), + "_allowlist_function_transition": attr.label( + default = "@bazel_tools//tools/allowlists/function_transition_allowlist", + ), +} + platform_transition_binary = rule( implementation = _platform_transition_binary_impl, - attrs = { - "basename": attr.string(), - "binary": attr.label(allow_files = True, cfg = _transition_platform), - "target_platform": attr.label( - doc = "The target platform to transition the binary.", - mandatory = True, - ), - "_allowlist_function_transition": attr.label( - default = "@bazel_tools//tools/allowlists/function_transition_allowlist", - ), - }, + attrs = _platform_transition_attrs, executable = True, doc = "Transitions the binary to use the provided platform.", ) + +platform_transition_test = rule( + implementation = _platform_transition_binary_impl, + attrs = _platform_transition_attrs, + test = True, + doc = "Transitions the test to use the provided platform.", +)