From a0149d61d6c082d3b4e21bb514cefd96d817a828 Mon Sep 17 00:00:00 2001 From: Jay Conrod Date: Wed, 27 Nov 2019 12:51:46 -0500 Subject: [PATCH] Migrate legacy tests to //tests/core/go_download_sdk (a go_bazel_test) Updates #2285 --- .bazelci/presubmit.yml | 4 - tests/core/README.rst | 1 + tests/core/go_download_sdk/BUILD.bazel | 6 + tests/core/go_download_sdk/README.rst | 7 + .../go_download_sdk/go_download_sdk_test.go | 122 ++++++++++++++++++ tests/legacy/build_with_old_sdk/BUILD.bazel | 24 ---- tests/legacy/build_with_old_sdk/new_test.go | 9 -- tests/legacy/build_with_old_sdk/old_test.go | 8 -- tests/legacy/custom_go_toolchain/BUILD.bazel | 34 ----- .../custom_go_toolchain/version_test.go | 14 -- 10 files changed, 136 insertions(+), 93 deletions(-) create mode 100644 tests/core/go_download_sdk/BUILD.bazel create mode 100644 tests/core/go_download_sdk/README.rst create mode 100644 tests/core/go_download_sdk/go_download_sdk_test.go delete mode 100644 tests/legacy/build_with_old_sdk/BUILD.bazel delete mode 100644 tests/legacy/build_with_old_sdk/new_test.go delete mode 100644 tests/legacy/build_with_old_sdk/old_test.go delete mode 100644 tests/legacy/custom_go_toolchain/BUILD.bazel delete mode 100644 tests/legacy/custom_go_toolchain/version_test.go diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 6884544267..5426644fdf 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml @@ -178,9 +178,7 @@ platforms: - "-//tests/integration/googleapis:color_service_go_proto" - "-//tests/integration/googleapis:color_service_proto" - "-//tests/integration/googleapis:color_service_test" - - "-//tests/legacy/build_with_old_sdk:build_with_old_sdk" - "-//tests/legacy/cgo_library_root_dir:cgo_library_root_dir" - - "-//tests/legacy/custom_go_toolchain:custom_go_toolchain" - "-//tests/legacy/examples/cgo/example_command:example_command_test" - "-//tests/legacy/examples/cgo/example_command:example_command_script" - "-//tests/legacy/examples/cgo/example_command:example_command" @@ -294,9 +292,7 @@ platforms: - "-//tests/core/stdlib:buildid_test" - "-//tests/examples/executable_name:executable_name" - "-//tests/integration/googleapis:color_service_test" - - "-//tests/legacy/build_with_old_sdk:build_with_old_sdk" - "-//tests/legacy/cgo_library_root_dir:cgo_library_root_dir" - - "-//tests/legacy/custom_go_toolchain:custom_go_toolchain" - "-//tests/legacy/examples/cgo/example_command:example_command_test" - "-//tests/legacy/examples/cgo/example_command:example_command_script" - "-//tests/legacy/examples/cgo/example_command:example_command" diff --git a/tests/core/README.rst b/tests/core/README.rst index 1de62e0647..ad12274140 100644 --- a/tests/core/README.rst +++ b/tests/core/README.rst @@ -20,6 +20,7 @@ Contents * `Basic go_test functionality `_ * `.. _#2067: https://github.com/bazelbuild/rules_go/issues/2067 `_ * `Runfiles functionality `_ +* `go_download_sdk `_ * `race instrumentation `_ * `stdlib functionality `_ * `Basic go_binary functionality `_ diff --git a/tests/core/go_download_sdk/BUILD.bazel b/tests/core/go_download_sdk/BUILD.bazel new file mode 100644 index 0000000000..f294f987fd --- /dev/null +++ b/tests/core/go_download_sdk/BUILD.bazel @@ -0,0 +1,6 @@ +load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test") + +go_bazel_test( + name = "go_download_sdk_test", + srcs = ["go_download_sdk_test.go"], +) diff --git a/tests/core/go_download_sdk/README.rst b/tests/core/go_download_sdk/README.rst new file mode 100644 index 0000000000..78e9311830 --- /dev/null +++ b/tests/core/go_download_sdk/README.rst @@ -0,0 +1,7 @@ +go_download_sdk +=============== + +go_download_sdk_test +-------------------- +Verifies that ``go_downlaod_sdk`` can be used to download a specific version +or a set of archives for various platforms. diff --git a/tests/core/go_download_sdk/go_download_sdk_test.go b/tests/core/go_download_sdk/go_download_sdk_test.go new file mode 100644 index 0000000000..b8bbf99e3a --- /dev/null +++ b/tests/core/go_download_sdk/go_download_sdk_test.go @@ -0,0 +1,122 @@ +// 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 go_download_sdk_test + +import ( + "bytes" + "io/ioutil" + "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_test") + +go_test( + name = "version_test", + srcs = ["version_test.go"], +) + +-- version_test.go -- +package version_test + +import ( + "flag" + "runtime" + "testing" +) + +var want = flag.String("version", "", "") + +func Test(t *testing.T) { + if v := runtime.Version(); v != *want { + t.Errorf("got version %q; want %q", v, *want) + } +} +`, + }) +} + +func Test(t *testing.T) { + for _, test := range []struct { + desc, rule, wantVersion string + }{ + { + desc: "version", + rule: ` +load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk") + +go_download_sdk( + name = "go_sdk", + version = "1.12.13", +) + +`, + wantVersion: "go1.12.13", + }, { + desc: "custom_archives", + rule: ` +load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk") + +go_download_sdk( + name = "go_sdk", + sdks = { + "darwin_amd64": ("go1.12.13.darwin-amd64.tar.gz", "6d3de6f7d7c0e8162aaa009128839fa5afcba578dcbd6ff034a82419d82480e9"), + "linux_amd64": ("go1.12.13.linux-amd64.tar.gz", "da036454cb3353f9f507f0ceed4048feac611065e4e1818b434365eb32ac9bdc"), + "windows_amd64": ("go1.12.13.windows-amd64.zip", "c441a298e8b510d3cdabfd361885cd6762e33eaceb27cbb0eabe6757f9d1f07d"), + }, +) +`, + wantVersion: "go1.12.13", + }, + } { + t.Run(test.desc, func(t *testing.T) { + origWorkspaceData, err := ioutil.ReadFile("WORKSPACE") + if err != nil { + t.Fatal(err) + } + + i := bytes.Index(origWorkspaceData, []byte("go_rules_dependencies()")) + if i < 0 { + t.Fatalf("%s: could not find call to go_rules_dependencies()") + } + + buf := &bytes.Buffer{} + buf.Write(origWorkspaceData[:i]) + buf.WriteString(test.rule) + buf.WriteString(` +go_rules_dependencies() + +go_register_toolchains() +`) + if err := ioutil.WriteFile("WORKSPACE", buf.Bytes(), 0666); err != nil { + t.Fatal(err) + } + defer func() { + if err := ioutil.WriteFile("WORKSPACE", origWorkspaceData, 0666); err != nil { + t.Errorf("error restoring WORKSPACE: %v", err) + } + }() + + if err := bazel_testing.RunBazel("test", "//:version_test", "--test_arg=-version="+test.wantVersion); err != nil { + t.Fatal(err) + } + }) + } +} diff --git a/tests/legacy/build_with_old_sdk/BUILD.bazel b/tests/legacy/build_with_old_sdk/BUILD.bazel deleted file mode 100644 index efd3b20fdd..0000000000 --- a/tests/legacy/build_with_old_sdk/BUILD.bazel +++ /dev/null @@ -1,24 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_test") -load("@io_bazel_rules_go//tests:bazel_tests.bzl", "bazel_test") - -go_test( - name = "go_default_test", - size = "small", - srcs = [ - "new_test.go", - "old_test.go", - ], - tags = ["manual"], -) - -bazel_test( - name = "build_with_old_sdk", - command = "test", - config = "fetch", - externals = [ - "@org_golang_x_tools//go/vcs:go_default_library", - ], - go_version = "1.11.13", - tags = ["dev"], - targets = [":go_default_test"], -) diff --git a/tests/legacy/build_with_old_sdk/new_test.go b/tests/legacy/build_with_old_sdk/new_test.go deleted file mode 100644 index 2efb83fd83..0000000000 --- a/tests/legacy/build_with_old_sdk/new_test.go +++ /dev/null @@ -1,9 +0,0 @@ -// +build go1.12 - -package test_version - -import "testing" - -func TestShouldFail(t *testing.T) { - t.Fail() -} diff --git a/tests/legacy/build_with_old_sdk/old_test.go b/tests/legacy/build_with_old_sdk/old_test.go deleted file mode 100644 index fe5dec39d9..0000000000 --- a/tests/legacy/build_with_old_sdk/old_test.go +++ /dev/null @@ -1,8 +0,0 @@ -// +build !go1.12 - -package test_version - -import "testing" - -func TestShouldPass(t *testing.T) { -} diff --git a/tests/legacy/custom_go_toolchain/BUILD.bazel b/tests/legacy/custom_go_toolchain/BUILD.bazel deleted file mode 100644 index 64f1717a5a..0000000000 --- a/tests/legacy/custom_go_toolchain/BUILD.bazel +++ /dev/null @@ -1,34 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test") -load("@io_bazel_rules_go//tests:bazel_tests.bzl", "bazel_test") - -go_test( - name = "go_default_test", - size = "small", - srcs = ["version_test.go"], - tags = ["manual"], -) - -bazel_test( - name = "custom_go_toolchain", - command = "test", - config = "fetch", - go_version = "", - tags = ["dev"], - targets = [":go_default_test"], - workspace = """ - -load("@io_bazel_rules_go//go:deps.bzl", "go_download_sdk") -load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies") - -go_download_sdk( - name = "go_sdk", - sdks = { - "linux_amd64": ("go1.10.1.linux-amd64.tar.gz", "72d820dec546752e5a8303b33b009079c15c2390ce76d67cf514991646c6127b"), - "darwin_amd64": ("go1.10.1.darwin-amd64.tar.gz", "0a5bbcbbb0d150338ba346151d2864fd326873beaedf964e2057008c8a4dc557"), - }, -) - -go_rules_dependencies() - -""", -) diff --git a/tests/legacy/custom_go_toolchain/version_test.go b/tests/legacy/custom_go_toolchain/version_test.go deleted file mode 100644 index 4ef4e7bc0a..0000000000 --- a/tests/legacy/custom_go_toolchain/version_test.go +++ /dev/null @@ -1,14 +0,0 @@ -package version - -import ( - "runtime" - "testing" -) - -const expected = "go1.10.1" - -func TestVersion(t *testing.T) { - if expected != runtime.Version() { - t.Fatalf("Incorrect go version, expected %s got %s", expected, runtime.Version()) - } -}