diff options
author | Spandan Das <spandandas@google.com> | 2023-06-14 21:55:21 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2023-06-14 21:55:21 +0000 |
commit | 711a453236752a1786e93fbde5929b92008fc7ff (patch) | |
tree | d9fac15bb5a835ae6ba757dc5eaf6ef597ea44cf /tests/core/cross | |
parent | 9803cf8403d7105bddc1d5304d6e694b781a6605 (diff) | |
parent | 49dcd021242b91376524f85bfec457f8e81051e4 (diff) | |
download | bazelbuild-rules_go-711a453236752a1786e93fbde5929b92008fc7ff.tar.gz |
Merge remote-tracking branch 'aosp/upstream-master' into merge_rules_go am: 49dcd02124platform-tools-34.0.5platform-tools-34.0.4
Original change: https://android-review.googlesource.com/c/platform/external/bazelbuild-rules_go/+/2625353
Change-Id: I9f9788cf0500a3062862c617370cef7b915bbbc1
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
Diffstat (limited to 'tests/core/cross')
-rw-r--r-- | tests/core/cross/BUILD.bazel | 143 | ||||
-rw-r--r-- | tests/core/cross/README.rst | 62 | ||||
-rw-r--r-- | tests/core/cross/asm.s | 15 | ||||
-rw-r--r-- | tests/core/cross/cross_test.go | 91 | ||||
-rw-r--r-- | tests/core/cross/def.bzl | 24 | ||||
-rw-r--r-- | tests/core/cross/ios_select_test.go | 66 | ||||
-rw-r--r-- | tests/core/cross/lib_darwin.go | 3 | ||||
-rw-r--r-- | tests/core/cross/lib_linux.go | 3 | ||||
-rw-r--r-- | tests/core/cross/lib_windows.go | 3 | ||||
-rw-r--r-- | tests/core/cross/main.go | 26 | ||||
-rw-r--r-- | tests/core/cross/non_executable_test.go | 106 | ||||
-rw-r--r-- | tests/core/cross/proto_test.go | 126 | ||||
-rw-r--r-- | tests/core/cross/sdk_version_test.go | 135 |
13 files changed, 803 insertions, 0 deletions
diff --git a/tests/core/cross/BUILD.bazel b/tests/core/cross/BUILD.bazel new file mode 100644 index 00000000..ec179984 --- /dev/null +++ b/tests/core/cross/BUILD.bazel @@ -0,0 +1,143 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_cross_binary", "go_library", "go_test") +load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test") +load(":def.bzl", "no_context_info") + +test_suite( + name = "cross", +) + +go_binary( + name = "windows_cross", + srcs = ["main.go"], + goarch = "amd64", + goos = "windows", + pure = "on", + deps = [":platform_lib"], +) + +go_binary( + name = "linux_cross", + srcs = ["main.go"], + goarch = "amd64", + goos = "linux", + pure = "on", + deps = [":platform_lib"], +) + +go_binary( + name = "darwin_cross", + srcs = ["main.go"], + goarch = "amd64", + goos = "darwin", + pure = "on", + deps = [":platform_lib"], +) + +go_binary( + name = "asm_cross", + srcs = ["asm.s", "main.go"], + goarch = "386", + goos = "linux", + deps = [":platform_lib"], +) + +go_binary( + name = "native_bin", + srcs = ["main.go"], + pure = "on", + deps = [":platform_lib"], +) + +go_cross_binary( + name = "windows_go_cross", + platform = "@io_bazel_rules_go//go/toolchain:windows_amd64", + target = ":native_bin", +) + +go_cross_binary( + name = "linux_go_cross", + platform = "@io_bazel_rules_go//go/toolchain:linux_amd64", + target = ":native_bin", +) + +go_cross_binary( + name = "darwin_go_cross", + platform = "@io_bazel_rules_go//go/toolchain:darwin_amd64", + target = ":native_bin", +) + +go_library( + name = "platform_lib", + srcs = select({ + "//go/platform:darwin": ["lib_darwin.go"], + "//go/platform:linux": ["lib_linux.go"], + "//go/platform:windows": ["lib_windows.go"], + }), + importpath = "github.com/bazelbuild/rules_go/tests/core/cross/platform_lib", +) + +go_test( + name = "cross_test", + size = "small", + srcs = ["cross_test.go"], + args = [ + "-darwin", + "$(location :darwin_cross)", + "-linux", + "$(location :linux_cross)", + "-windows", + "$(location :windows_cross)", + ], + data = [ + ":darwin_cross", + ":linux_cross", + ":windows_cross", + ], + rundir = ".", + deps = ["//go/tools/bazel:go_default_library"], +) + +go_test( + name = "go_cross_binary_test", + size = "small", + srcs = ["cross_test.go"], + args = [ + "-darwin", + "$(location :darwin_go_cross)", + "-linux", + "$(location :linux_go_cross)", + "-windows", + "$(location :windows_go_cross)", + ], + data = [ + ":darwin_go_cross", + ":linux_go_cross", + ":windows_go_cross", + ], + rundir = ".", + deps = ["//go/tools/bazel:go_default_library"], +) + +go_bazel_test( + name = "ios_select_test", + srcs = ["ios_select_test.go"], +) + +go_bazel_test( + name = "proto_test", + srcs = ["proto_test.go"], +) + +go_bazel_test( + name = "sdk_version_test", + srcs = ["sdk_version_test.go"], +) + +go_bazel_test( + name = "non_executable_test", + srcs = ["non_executable_test.go"], +) + +no_context_info( + name = "no_context_info", +) diff --git a/tests/core/cross/README.rst b/tests/core/cross/README.rst new file mode 100644 index 00000000..283eb264 --- /dev/null +++ b/tests/core/cross/README.rst @@ -0,0 +1,62 @@ +Cross compilation +================= + +.. _go_binary: /docs/go/core/rules.md#go_binary +.. _go_library: /docs/go/core/rules.md#go_library +.. _go_cross_binary: /docs/go/core/rules.md#go_cross_binary +.. _#2523: https://github.com/bazelbuild/rules_go/issues/2523 + +Tests to ensure that cross compilation is working as expected. + +.. contents:: + +cross_test +---------- + + +Tests that cross compilation controlled by the ``goos`` and ``goarch`` +attributes on a `go_binary`_ produces executables for the correct platform. + +This builds binaries using `main.go <main.go>`_ in multiple configurations, and +then passes them as data to a test `written in go <cross_test.go>`_. + +The test executes the unix command "file" on the binaries to determine their +type, and checks they were built for the expected architecture. + +The test also checks that `go_library`_ packages imoprted by `go_binary`_ with +``goos`` set are built in the correct configuration, and ``select`` is applied +in that configuration. Each binary depends on ``platform_lib``, which has a +different source file (determined by ``select``) for each platform. The source +files have a ``goos`` suffix, so they will only be built on the right platform. +If the wrong source file is used or if all files are filtered out, the +`go_binary`_ will not build. + +go_cross_test +------------- + +Indentical test to ``cross_test`` except tests using a `go_cross_binary`_ rule wrapping a `go_binary`_ instead of the ``goos`` and ``goarch`` attributes on a `go_binary`_. + +sdk_version_test +---------------- +Tests that a `go_binary`_ wrapped in a `go_cross_binary`_ rule, with the ``sdk_version`` attribute set, produces an executable built with the correct Go SDK version. + +ios_select_test +--------------- + +Tests that we can cross-compile a library for iOS. We should be able to select +a dependency using ``@io_bazel_rules_go//go/platform:darwin``, which is true +when building for iOS (tested by ``ios_select_test``) and macOS +(tested by ``use_ios_lib``). + +proto_test +---------- + +Tests that a ``go_proto_library`` can be cross-compiled, both with +``--platforms`` and with mode attributes. + +no_context_info +--------------- + +Tests that a rule that uses ``@io_bazel_rules_go//go:toolchain`` but does not +depend on any other target can call ``go_context`` without error. Verifies +`#2523`_. diff --git a/tests/core/cross/asm.s b/tests/core/cross/asm.s new file mode 100644 index 00000000..7a5d5219 --- /dev/null +++ b/tests/core/cross/asm.s @@ -0,0 +1,15 @@ +// Example assembly copied from https://github.com/rpccloud/goid + +#include "go_asm.h" +#include "textflag.h" + +#ifdef GOARCH_386 +#define get_tls(r) MOVL TLS, r +#define g(r) 0(r)(TLS*1) +#endif + +TEXT ·getg(SB), NOSPLIT, $0-4 + get_tls(CX) + MOVL g(CX), AX + MOVL AX, ret+0(FP) + RET diff --git a/tests/core/cross/cross_test.go b/tests/core/cross/cross_test.go new file mode 100644 index 00000000..c8ff3b72 --- /dev/null +++ b/tests/core/cross/cross_test.go @@ -0,0 +1,91 @@ +/* Copyright 2017 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package cross_test + +import ( + "flag" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/bazelbuild/rules_go/go/tools/bazel" +) + +type check struct { + file *string + info []string +} + +var darwin = flag.String("darwin", "", "The darwin binary") +var linux = flag.String("linux", "", "The linux binary") +var windows = flag.String("windows", "", "The windows binary") + +var checks = []check{ + {darwin, []string{ + "Mach-O", + "64-bit", + "executable", + "x86_64", + }}, + {linux, []string{ + "ELF", + "64-bit", + "executable", + "x86-64", + }}, + {windows, []string{ + "PE32+", + "Windows", + "executable", + "console", + "x86-64", + }}, +} + +func TestCross(t *testing.T) { + for _, c := range checks { + path, err := bazel.Runfile(*c.file) + if err != nil { + t.Fatalf("Could not find runfile %s: %q", *c.file, err) + } + + if _, err := os.Stat(path); os.IsNotExist(err) { + t.Fatalf("Missing binary %v", path) + } + file, err := filepath.EvalSymlinks(path) + if err != nil { + t.Fatalf("Invalid filename %v", path) + } + cmd := exec.Command("file", file) + cmd.Stderr = os.Stderr + res, err := cmd.Output() + if err != nil { + t.Fatalf("failed running 'file': %v", err) + } + output := string(res) + if index := strings.Index(output, ":"); index >= 0 { + output = output[index+1:] + } + output = strings.TrimSpace(output) + for _, info := range c.info { + if !strings.Contains(output, info) { + t.Errorf("incorrect type for %v\nExpected %v\nGot %v", file, info, output) + } + } + } +} diff --git a/tests/core/cross/def.bzl b/tests/core/cross/def.bzl new file mode 100644 index 00000000..99ef7c40 --- /dev/null +++ b/tests/core/cross/def.bzl @@ -0,0 +1,24 @@ +# Copyright 2020 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("//go:def.bzl", "go_context") + +def _no_context_info_impl(ctx): + go_context(ctx) + # do nothing and pass if that succeeds + +no_context_info = rule( + implementation = _no_context_info_impl, + toolchains = ["@io_bazel_rules_go//go:toolchain"], +) diff --git a/tests/core/cross/ios_select_test.go b/tests/core/cross/ios_select_test.go new file mode 100644 index 00000000..00bf0032 --- /dev/null +++ b/tests/core/cross/ios_select_test.go @@ -0,0 +1,66 @@ +// Copyright 2019 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ios_select_test + +import ( + "testing" + + "github.com/bazelbuild/rules_go/go/tools/bazel_testing" +) + +func TestMain(m *testing.M) { + bazel_testing.TestMain(m, bazel_testing.Args{ + Main: ` +-- BUILD.bazel -- +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "use_ios_lib", + importpath = "github.com/bazelbuild/rules_go/tests/core/cross/use_ios_lib", + deps = select({ + ":is_osx": [":ios_lib"], + "//conditions:default": [], + }), +) + +config_setting( + name = "is_osx", + constraint_values = ["@platforms//os:osx"], +) + +go_library( + name = "ios_lib", + srcs = select({ + "@io_bazel_rules_go//go/platform:darwin": ["ios_good.go"], + "@io_bazel_rules_go//go/platform:ios": ["ios_good.go"], + "//conditions:default": ["ios_bad.go"], + }), + importpath = "github.com/bazelbuild/rules_go/tests/core/cross/ios_lib", +) + +-- ios_good.go -- +package ios_lib + +-- ios_bad.go -- +donotbuild +`, + }) +} + +func Test(t *testing.T) { + if err := bazel_testing.RunBazel("build", "--platforms=@io_bazel_rules_go//go/toolchain:ios_amd64", ":ios_lib"); err != nil { + t.Fatal(err) + } +} diff --git a/tests/core/cross/lib_darwin.go b/tests/core/cross/lib_darwin.go new file mode 100644 index 00000000..dfdc059f --- /dev/null +++ b/tests/core/cross/lib_darwin.go @@ -0,0 +1,3 @@ +package platform_lib + +const Platform = "darwin" diff --git a/tests/core/cross/lib_linux.go b/tests/core/cross/lib_linux.go new file mode 100644 index 00000000..0759c9b5 --- /dev/null +++ b/tests/core/cross/lib_linux.go @@ -0,0 +1,3 @@ +package platform_lib + +const Platform = "linux" diff --git a/tests/core/cross/lib_windows.go b/tests/core/cross/lib_windows.go new file mode 100644 index 00000000..6ed0f7ab --- /dev/null +++ b/tests/core/cross/lib_windows.go @@ -0,0 +1,3 @@ +package platform_lib + +const Platform = "windows" diff --git a/tests/core/cross/main.go b/tests/core/cross/main.go new file mode 100644 index 00000000..0d0c6794 --- /dev/null +++ b/tests/core/cross/main.go @@ -0,0 +1,26 @@ +/* Copyright 2016 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "fmt" + + "github.com/bazelbuild/rules_go/tests/core/cross/platform_lib" +) + +func main() { + fmt.Println(platform_lib.Platform) +} diff --git a/tests/core/cross/non_executable_test.go b/tests/core/cross/non_executable_test.go new file mode 100644 index 00000000..ed9521c3 --- /dev/null +++ b/tests/core/cross/non_executable_test.go @@ -0,0 +1,106 @@ +// Copyright 2022 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package non_executable_test + +import ( + "regexp" + "testing" + + "github.com/bazelbuild/rules_go/go/tools/bazel_testing" +) + +var errorRegexp = regexp.MustCompile(`cannot run go_cross target "host_archive": underlying target "@{0,2}//src:archive" is not executable`); + +func TestMain(m *testing.M) { + bazel_testing.TestMain(m, bazel_testing.Args{ + Main: ` +-- src/BUILD.bazel -- +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_cross_binary") +load(":rules.bzl", "no_runfiles_check") + +go_binary( + name = "archive", + srcs = ["archive.go"], + cgo = True, + linkmode = "c-archive", +) + +# We make a new platform here so that we can exercise the go_cross_binary rule. +# However, the test needs to run on all hosts, so the platform needs +# to inherit from the host platform. +platform( + name = "host_cgo", + parents = ["@local_config_platform//:host"], + constraint_values = [ + "@io_bazel_rules_go//go/toolchain:cgo_on", + ], +) + +go_cross_binary( + name = "host_archive", + target = ":archive", + platform = ":host_cgo", +) + +cc_binary( + name = "main", + srcs = ["main.c"], + deps = [":host_archive"], +) + +no_runfiles_check( + name = "no_runfiles", + target = ":main", +) +-- src/archive.go -- +package main + +import "C" + +func main() {} +-- src/main.c -- +int main() {} +-- src/rules.bzl -- +def _no_runfiles_check_impl(ctx): + runfiles = ctx.attr.target[DefaultInfo].default_runfiles.files.to_list() + for runfile in runfiles: + if runfile.short_path not in ["src/main", "src/main.exe"]: + fail("Unexpected runfile: %s" % runfile.short_path) + +no_runfiles_check = rule( + implementation = _no_runfiles_check_impl, + attrs = { + "target": attr.label(), + } +) +`, + }) +} + +func TestNonExecutableGoBinaryCantBeRun(t *testing.T) { + if err := bazel_testing.RunBazel("build", "//src:host_archive"); err != nil { + t.Fatal(err) + } + err := bazel_testing.RunBazel("run", "//src:host_archive") + if err == nil || !errorRegexp.MatchString(err.Error()) { + t.Errorf("Expected bazel run to fail due to //src:host_archive not being executable") + } +} + +func TestNonExecutableGoBinaryNotInRunfiles(t *testing.T) { + if err := bazel_testing.RunBazel("build", "//src:no_runfiles"); err != nil { + t.Fatal(err) + } +} diff --git a/tests/core/cross/proto_test.go b/tests/core/cross/proto_test.go new file mode 100644 index 00000000..cd9e6c60 --- /dev/null +++ b/tests/core/cross/proto_test.go @@ -0,0 +1,126 @@ +// Copyright 2019 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package proto_test + +import ( + "testing" + + "github.com/bazelbuild/rules_go/go/tools/bazel_testing" +) + +var testArgs = bazel_testing.Args{ + WorkspaceSuffix: ` +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +http_archive( + name = "com_google_protobuf", + sha256 = "75be42bd736f4df6d702a0e4e4d30de9ee40eac024c4b845d17ae4cc831fe4ae", + strip_prefix = "protobuf-21.7", + # latest available in BCR, as of 2022-09-30 + urls = [ + "https://github.com/protocolbuffers/protobuf/archive/v21.7.tar.gz", + "https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v21.7.tar.gz", + ], +) + +load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps") + +protobuf_deps() + +http_archive( + name = "rules_proto", + sha256 = "4d421d51f9ecfe9bf96ab23b55c6f2b809cbaf0eea24952683e397decfbd0dd0", + strip_prefix = "rules_proto-f6b8d89b90a7956f6782a4a3609b2f0eee3ce965", + # master, as of 2020-01-06 + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/f6b8d89b90a7956f6782a4a3609b2f0eee3ce965.tar.gz", + "https://github.com/bazelbuild/rules_proto/archive/f6b8d89b90a7956f6782a4a3609b2f0eee3ce965.tar.gz", + ], +) +`, + Main: ` +-- BUILD.bazel -- +load("@rules_proto//proto:defs.bzl", "proto_library") +load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library") +load("@io_bazel_rules_go//go:def.bzl", "go_binary") + +proto_library( + name = "cross_proto", + srcs = ["cross.proto"], +) + +go_proto_library( + name = "cross_go_proto", + importpath = "github.com/bazelbuild/rules_go/tests/core/cross", + protos = [":cross_proto"], +) + +go_binary( + name = "use_bin", + srcs = ["use.go"], + deps = [":cross_go_proto"], + goos = "linux", + goarch = "386", +) + +go_binary( + name = "use_shared", + srcs = ["use.go"], + deps = [":cross_go_proto"], + linkmode = "c-shared", +) + +-- cross.proto -- +syntax = "proto3"; + +package cross; + +option go_package = "github.com/bazelbuild/rules_go/tests/core/cross"; + +message Foo { + int64 x = 1; +} + +-- use.go -- +package main + +import _ "github.com/bazelbuild/rules_go/tests/core/cross" + +func main() {} +`, +} + +func TestMain(m *testing.M) { + bazel_testing.TestMain(m, testArgs) +} + +func TestCmdLine(t *testing.T) { + args := []string{ + "build", + "--platforms=@io_bazel_rules_go//go/toolchain:linux_386", + ":cross_go_proto", + } + if err := bazel_testing.RunBazel(args...); err != nil { + t.Fatal(err) + } +} + +func TestTargets(t *testing.T) { + for _, target := range []string{"//:use_bin", "//:use_shared"} { + if err := bazel_testing.RunBazel("build", target); err != nil { + t.Errorf("building target %s: %v", target, err) + } + } +} diff --git a/tests/core/cross/sdk_version_test.go b/tests/core/cross/sdk_version_test.go new file mode 100644 index 00000000..9cb7ad51 --- /dev/null +++ b/tests/core/cross/sdk_version_test.go @@ -0,0 +1,135 @@ +// Copyright 2022 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package go_download_sdk_test + +import ( + "fmt" + "strings" + "testing" + "text/template" + + "github.com/bazelbuild/rules_go/go/tools/bazel_testing" +) + +type testcase struct { + Name, SDKVersion, expectedVersion string +} + +var testCases = []testcase{ + { + Name: "major_version", + SDKVersion: "1", + expectedVersion: "go1.16", + }, + { + Name: "minor_version", + SDKVersion: "1.16", + expectedVersion: "go1.16", + }, + { + Name: "patch_version", + SDKVersion: "1.16.0", + expectedVersion: "go1.16", + }, + { + Name: "1_17_minor_version", + SDKVersion: "1.17", + expectedVersion: "go1.17", + }, + { + Name: "1_17_patch_version", + SDKVersion: "1.17.1", + expectedVersion: "go1.17.1", + }, +} + +func TestMain(m *testing.M) { + mainFilesTmpl := template.Must(template.New("").Parse(` +-- WORKSPACE -- +local_repository( + name = "io_bazel_rules_go", + path = "../io_bazel_rules_go", +) + +load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk", "go_rules_dependencies", "go_register_toolchains") + +go_rules_dependencies() + +go_download_sdk( + name = "go_sdk", + version = "1.16", +) +go_download_sdk( + name = "go_sdk_1_17", + version = "1.17", +) +go_download_sdk( + name = "go_sdk_1_17_1", + version = "1.17.1", +) +go_register_toolchains() +-- main.go -- +package main + +import ( + "fmt" + "runtime" +) + +func main() { + fmt.Print(runtime.Version()) +} +-- BUILD.bazel -- +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_cross_binary") + +go_binary( + name = "print_version", + srcs = ["main.go"], +) +{{range .TestCases}} +go_cross_binary( + name = "{{.Name}}", + target = ":print_version", + sdk_version = "{{.SDKVersion}}", +) +{{end}} +`)) + tmplValues := struct{ + TestCases []testcase + }{ + TestCases: testCases, + } + mainFilesBuilder := &strings.Builder{} + if err := mainFilesTmpl.Execute(mainFilesBuilder, tmplValues); err != nil { + panic(err) + } + + bazel_testing.TestMain(m, bazel_testing.Args{Main: mainFilesBuilder.String()}) +} + +func Test(t *testing.T) { + for _, test := range testCases { + t.Run(test.Name, func(t *testing.T) { + output, err := bazel_testing.BazelOutput("run", fmt.Sprintf("//:%s", test.Name)) + if err != nil { + t.Fatal(err) + } + actualVersion := string(output) + if actualVersion != test.expectedVersion { + t.Fatal("actual", actualVersion, "vs expected", test.expectedVersion) + } + }) + } +} |