Skip to content

Commit

Permalink
feat: add platform_transition_test (#770)
Browse files Browse the repository at this point in the history
* Add platform_transition_test

Signed-off-by: Thomas Lam <[email protected]>

* Set target platform constraints for tests

Signed-off-by: Thomas Lam <[email protected]>

* Update docs

Signed-off-by: Thomas Lam <[email protected]>

---------

Signed-off-by: Thomas Lam <[email protected]>
  • Loading branch information
lamcw authored Apr 3, 2024
1 parent 7716549 commit faaada2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 13 deletions.
21 changes: 21 additions & 0 deletions docs/transitions.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 52 additions & 2 deletions lib/tests/transitions/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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"],
Expand All @@ -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"],
Expand Down
10 changes: 10 additions & 0 deletions lib/tests/transitions/simple_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
31 changes: 20 additions & 11 deletions lib/transitions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
)

0 comments on commit faaada2

Please sign in to comment.