aboutsummaryrefslogtreecommitdiff
path: root/tests/core/go_test
diff options
context:
space:
mode:
Diffstat (limited to 'tests/core/go_test')
-rw-r--r--tests/core/go_test/BUILD.bazel261
-rw-r--r--tests/core/go_test/README.rst126
-rw-r--r--tests/core/go_test/binary_env_test.go55
-rw-r--r--tests/core/go_test/data_test.go45
-rw-r--r--tests/core/go_test/data_test_dep.go15
-rw-r--r--tests/core/go_test/data_test_embed.go15
-rw-r--r--tests/core/go_test/env_inherit_test.go61
-rw-r--r--tests/core/go_test/env_test.go39
-rw-r--r--tests/core/go_test/example_test.go32
-rw-r--r--tests/core/go_test/external_importmap_dep.go11
-rw-r--r--tests/core/go_test/external_importmap_lib.go5
-rw-r--r--tests/core/go_test/external_importmap_test.go10
-rw-r--r--tests/core/go_test/external_test.go29
-rw-r--r--tests/core/go_test/flag_test.go30
-rw-r--r--tests/core/go_test/fuzz_test.go29
-rw-r--r--tests/core/go_test/indirect_import_dep.go7
-rw-r--r--tests/core/go_test/indirect_import_i_test.go7
-rw-r--r--tests/core/go_test/indirect_import_lib.go3
-rw-r--r--tests/core/go_test/indirect_import_x_test.go15
-rw-r--r--tests/core/go_test/internal_test.go28
-rw-r--r--tests/core/go_test/lib.go20
-rw-r--r--tests/core/go_test/only_testmain_test.go10
-rw-r--r--tests/core/go_test/pwd_test.go16
-rw-r--r--tests/core/go_test/same_package_test.go7
-rw-r--r--tests/core/go_test/sharding_test.go13
-rw-r--r--tests/core/go_test/tags_bad_test.go9
-rw-r--r--tests/core/go_test/tags_good_test.go7
-rw-r--r--tests/core/go_test/test_fail_fast_test.go75
-rw-r--r--tests/core/go_test/test_filter_test.go54
-rw-r--r--tests/core/go_test/testmain_import_indirect_test.go7
-rw-r--r--tests/core/go_test/testmain_import_main_test.go11
-rw-r--r--tests/core/go_test/testmain_without_exit_test.go59
-rw-r--r--tests/core/go_test/wrapped_test.go19
-rw-r--r--tests/core/go_test/wrapper_test.go46
-rw-r--r--tests/core/go_test/x0
-rw-r--r--tests/core/go_test/x_defs/BUILD.bazel73
-rw-r--r--tests/core/go_test/x_defs/bar.go10
-rw-r--r--tests/core/go_test/x_defs/baz.go6
-rw-r--r--tests/core/go_test/x_defs/foo.go1
-rw-r--r--tests/core/go_test/x_defs/foo_test.go28
-rw-r--r--tests/core/go_test/x_defs/qux.go3
-rw-r--r--tests/core/go_test/x_defs/x_defs_lib.go3
-rw-r--r--tests/core/go_test/x_defs/x_defs_test.go34
-rw-r--r--tests/core/go_test/xmlreport_test.go163
-rw-r--r--tests/core/go_test/y0
-rw-r--r--tests/core/go_test/z0
46 files changed, 1497 insertions, 0 deletions
diff --git a/tests/core/go_test/BUILD.bazel b/tests/core/go_test/BUILD.bazel
new file mode 100644
index 00000000..b77c851b
--- /dev/null
+++ b/tests/core/go_test/BUILD.bazel
@@ -0,0 +1,261 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
+load("@io_bazel_rules_go//go/tools/bazel_testing:def.bzl", "go_bazel_test")
+
+test_suite(
+ name = "go_test",
+)
+
+go_library(
+ name = "lib",
+ srcs = ["lib.go"],
+ cgo = True,
+ importpath = "lib",
+)
+
+go_test(
+ name = "internal_test",
+ size = "small",
+ srcs = ["internal_test.go"],
+ embed = [":lib"],
+ x_defs = {
+ "Got": "Internal",
+ "Expect": "Internal",
+ },
+)
+
+go_test(
+ name = "external_test",
+ size = "small",
+ srcs = ["external_test.go"],
+ embed = [":lib"],
+ x_defs = {
+ "Got": "External",
+ "Expect": "External",
+ },
+)
+
+go_test(
+ name = "combined_test",
+ size = "small",
+ srcs = [
+ "external_test.go",
+ "internal_test.go",
+ ],
+ embed = [":lib"],
+ x_defs = {
+ "Got": "Combined",
+ "Expect": "Combined",
+ },
+)
+
+go_test(
+ name = "flag_test",
+ size = "small",
+ srcs = [
+ "flag_test.go",
+ ],
+ args = [
+ "-aflag",
+ "somevalue",
+ ],
+)
+
+go_test(
+ name = "example_test",
+ size = "small",
+ srcs = ["example_test.go"],
+ embed = [":lib"],
+ x_defs = {
+ "Got": "Example",
+ "Expected": "Example",
+ },
+)
+
+go_test(
+ name = "only_testmain_test",
+ size = "small",
+ srcs = ["only_testmain_test.go"],
+)
+
+go_test(
+ name = "external_importmap_test",
+ size = "small",
+ srcs = ["external_importmap_test.go"],
+ embed = [":external_importmap_lib"],
+ deps = [":external_importmap_dep"],
+)
+
+go_library(
+ name = "external_importmap_lib",
+ srcs = ["external_importmap_lib.go"],
+ importmap = "x/github.com/bazelbuild/rules_go/tests/core/go_test/external_importmap",
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/external_importmap",
+)
+
+go_library(
+ name = "external_importmap_dep",
+ srcs = ["external_importmap_dep.go"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/external_importmap_dep",
+ deps = [":external_importmap_lib"],
+)
+
+go_test(
+ name = "pwd_test",
+ size = "small",
+ srcs = ["pwd_test.go"],
+)
+
+go_test(
+ name = "data_test",
+ size = "small",
+ embed = [":data_test_lib"],
+)
+
+go_library(
+ name = "data_test_lib",
+ srcs = [":data_test_srcs"],
+ data = ["x"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/data_test",
+ deps = [":data_test_dep"],
+)
+
+filegroup(
+ name = "data_test_srcs",
+ srcs = ["data_test.go"],
+)
+
+go_library(
+ name = "data_test_dep",
+ srcs = ["data_test_dep.go"],
+ data = ["y"],
+ embed = [":data_test_embed"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/data_test_dep",
+)
+
+go_library(
+ name = "data_test_embed",
+ srcs = ["data_test_embed.go"],
+ data = ["z"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/data_test_dep",
+)
+
+go_bazel_test(
+ name = "test_fail_fast_test",
+ srcs = ["test_fail_fast_test.go"],
+)
+
+go_bazel_test(
+ name = "test_filter_test",
+ srcs = ["test_filter_test.go"],
+)
+
+go_bazel_test(
+ name = "xmlreport_test",
+ srcs = ["xmlreport_test.go"],
+)
+
+go_test(
+ name = "testmain_import_test",
+ srcs = [
+ "testmain_import_indirect_test.go",
+ "testmain_import_main_test.go",
+ ],
+ importpath = "example.com/imports/test_main",
+)
+
+go_test(
+ name = "tags_test",
+ srcs = [
+ "tags_bad_test.go",
+ "tags_good_test.go",
+ ],
+ gotags = ["good"],
+)
+
+go_test(
+ name = "indirect_import_test",
+ srcs = [
+ "indirect_import_i_test.go",
+ "indirect_import_x_test.go",
+ ],
+ embed = [":indirect_import_lib"],
+ deps = [":indirect_import_dep"],
+)
+
+go_library(
+ name = "indirect_import_lib",
+ srcs = ["indirect_import_lib.go"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/indirect_import",
+)
+
+go_library(
+ name = "indirect_import_dep",
+ srcs = ["indirect_import_dep.go"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/indirect_import_dep",
+ deps = [":indirect_import_lib"],
+)
+
+[
+ go_test(
+ name = "same_package_{}_test".format(i),
+ srcs = ["same_package_test.go"],
+ )
+ for i in range(1, 80)
+]
+
+test_suite(
+ name = "same_package_test",
+ tests = ["same_package_{}_test".format(i) for i in range(1, 80)],
+)
+
+go_bazel_test(
+ name = "testmain_without_exit_test",
+ srcs = ["testmain_without_exit_test.go"],
+)
+
+go_test(
+ name = "wrapper_test",
+ srcs = ["wrapper_test.go"],
+ args = ["$(location :wrapped_test)"],
+ data = [":wrapped_test"],
+ rundir = ".",
+)
+
+go_test(
+ name = "wrapped_test",
+ srcs = ["wrapped_test.go"],
+ tags = ["manual"],
+)
+
+go_test(
+ name = "fuzz_test",
+ srcs = ["fuzz_test.go"],
+)
+
+go_test(
+ name = "env_test",
+ srcs = ["env_test.go"],
+ data = ["@go_sdk//:lib/time/zoneinfo.zip"],
+ env = {
+ "ZONEINFO": "$(execpath @go_sdk//:lib/time/zoneinfo.zip)",
+ },
+ deps = [
+ "@io_bazel_rules_go//go/tools/bazel",
+ ],
+)
+
+go_test(
+ name = "sharding_test",
+ srcs = ["sharding_test.go"],
+ shard_count = 2,
+)
+
+go_bazel_test(
+ name = "env_inherit_test",
+ srcs = ["env_inherit_test.go"],
+)
+
+go_bazel_test(
+ name = "binary_env_test",
+ srcs = ["binary_env_test.go"],
+)
diff --git a/tests/core/go_test/README.rst b/tests/core/go_test/README.rst
new file mode 100644
index 00000000..176cf196
--- /dev/null
+++ b/tests/core/go_test/README.rst
@@ -0,0 +1,126 @@
+Basic go_test functionality
+===========================
+
+.. _go_test: /docs/go/core/rules.md#_go_test
+.. _#1877: https://github.com/bazelbuild/rules_go/issues/1877
+.. _#34129: https:////github.com/golang/go/issues/34129
+.. _#2749: https://github.com/bazelbuild/rules_go/issues/2749
+
+Tests to ensure that basic features of `go_test`_ are working as expected.
+
+.. contents::
+
+internal_test
+-------------
+
+Test that a `go_test`_ rule that adds white box tests to an embedded package works.
+This builds a library with `lib.go <lib.go>`_ and then a package with an
+`internal test <internal_test.go>`_ that contains the test case.
+It uses x_def stamped values to verify the library names are correct.
+
+external_test
+-------------
+
+Test that a `go_test`_ rule that adds black box tests for a dependant package works.
+This builds a library with `lib.go <lib.go>`_ and then a package with an
+`external test <external_test.go>`_ that contains the test case.
+It uses x_def stamped values to verify the library names are correct.
+
+combined_test
+-------------
+Test that a `go_test`_ rule that adds both white and black box tests for a
+package works.
+This builds a library with `lib.go <lib.go>`_ and then a one merged with the
+`internal test <internal_test.go>`_, and then another one that depends on it
+with the `external test <external_test.go>`_.
+It uses x_def stamped values to verify that all library names are correct.
+Verifies #413
+
+flag_test
+---------
+Test that a `go_test`_ rule that adds flags, even in the main package, can read
+the flag.
+This does not even build a library, it's a test in the main package with no
+dependancies that checks it can declare and then read a flag.
+Verifies #838
+
+only_testmain_test
+------------------
+Test that an `go_test`_ that contains a ``TestMain`` function but no tests
+still builds and passes.
+
+external_importmap_test
+----------------------
+
+Test that an external test package in `go_test`_ is compiled with the correct
+``importmap`` for the library under test. This is verified by defining an
+interface in the library under test and implementing it in a separate
+dependency.
+
+Verifies #1538.
+
+pwd_test
+--------
+
+Checks that the ``PWD`` environment variable is set to the current directory
+in the generated test main before running a test. This matches functionality
+in ``go test``.
+
+Verifies #1561.
+
+data_test
+---------
+
+Checks that data dependencies, including those inherited from ``deps`` and
+``embed``, are visible to tests at run-time. Source files should not be
+visible at run-time.
+
+test_fail_fast_test
+----------------
+
+Checks that ``--test_runner_fail_fast`` actually enables stopping test execution after
+the first failure.
+
+Verifies #3055.
+
+test_filter_test
+----------------
+
+Checks that ``--test_filter`` actually filters out test cases.
+
+testmain_import_test
+----------------
+
+Check if all packages in all source files are imported to test main, to ensure
+a consistent test behaviour. This ensures a consistent behaviour when thinking
+about global indirect depencencies.
+
+tags_test
+---------
+
+Checks that setting ``gotags`` affects source filtering. The test will fail
+unless a specific tag is set.
+
+indirect_import_test
+--------------------
+
+Checks that an external test can import another package that imports the library
+under test. The other package should be compiled against the internal test
+package, not the library under test. Verifies `#1877`_.
+
+testmain_without_exit
+---------------------
+
+Checks that TestMain without calling os.Exit directly works.
+Verifies `#34129`_ from Go 1.15.
+
+wrapper_test
+------------
+
+Checks that a ``go_test`` can be executed by another test in a subdirectory.
+Verifies `#2749`_.
+
+fuzz_test
+---------
+
+Checks that a ``go_test`` with a fuzz target builds correctly.
diff --git a/tests/core/go_test/binary_env_test.go b/tests/core/go_test/binary_env_test.go
new file mode 100644
index 00000000..25089034
--- /dev/null
+++ b/tests/core/go_test/binary_env_test.go
@@ -0,0 +1,55 @@
+// Copyright 2023 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 binary_env_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: `
+-- src/BUILD.bazel --
+load("@io_bazel_rules_go//go:def.bzl", "go_binary")
+go_binary(
+ name = "main",
+ srcs = ["env.go"],
+ env = {"FOO": "bar"},
+)
+-- src/env.go --
+package main
+
+import (
+ "log"
+ "os"
+)
+
+func main() {
+ v := os.Getenv("FOO")
+ if v != "bar" {
+ log.Fatalf("FOO was not equal to bar")
+ }
+}
+`,
+ })
+}
+
+func TestBinaryEnv(t *testing.T) {
+ if err := bazel_testing.RunBazel("run", "//src:main"); err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/tests/core/go_test/data_test.go b/tests/core/go_test/data_test.go
new file mode 100644
index 00000000..fd8a3930
--- /dev/null
+++ b/tests/core/go_test/data_test.go
@@ -0,0 +1,45 @@
+// Copyright 2018 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 data_test
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func TestVisibleRunfiles(t *testing.T) {
+ var got []string
+ err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+ if base := filepath.Base(path); info.IsDir() || base == "data_test" || base == "data_test.exe" {
+ return nil
+ }
+ got = append(got, filepath.ToSlash(path))
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ gotStr := strings.Join(got, "\n")
+ wantStr := "x\ny\nz"
+ if gotStr != wantStr {
+ t.Errorf("got:\n%s\nwant:\n%s\n", gotStr, wantStr)
+ }
+}
diff --git a/tests/core/go_test/data_test_dep.go b/tests/core/go_test/data_test_dep.go
new file mode 100644
index 00000000..d8eb2672
--- /dev/null
+++ b/tests/core/go_test/data_test_dep.go
@@ -0,0 +1,15 @@
+// Copyright 2018 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 data_test_dep
diff --git a/tests/core/go_test/data_test_embed.go b/tests/core/go_test/data_test_embed.go
new file mode 100644
index 00000000..d8eb2672
--- /dev/null
+++ b/tests/core/go_test/data_test_embed.go
@@ -0,0 +1,15 @@
+// Copyright 2018 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 data_test_dep
diff --git a/tests/core/go_test/env_inherit_test.go b/tests/core/go_test/env_inherit_test.go
new file mode 100644
index 00000000..b425abe2
--- /dev/null
+++ b/tests/core/go_test/env_inherit_test.go
@@ -0,0 +1,61 @@
+// Copyright 2021 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 env_inherit_test
+
+import (
+ "os"
+ "testing"
+
+ "github.com/bazelbuild/rules_go/go/tools/bazel_testing"
+)
+
+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_test")
+go_test(
+ name = "main",
+ srcs = ["env_inherit.go"],
+ env_inherit = ["INHERITEDVAR"],
+)
+-- src/env_inherit.go --
+package env_inherit_test
+
+import (
+ "os"
+ "testing"
+)
+
+func TestInherit(t *testing.T) {
+ v := os.Getenv("INHERITEDVAR")
+ if v != "b" {
+ t.Fatalf("INHERITEDVAR was not equal to b")
+ }
+}
+`,
+
+ SetUp: func() error {
+ os.Setenv("INHERITEDVAR", "b")
+ return nil
+ },
+ })
+}
+
+func TestInheritedEnvVar(t *testing.T) {
+ if err := bazel_testing.RunBazel("test", "//src:main"); err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/tests/core/go_test/env_test.go b/tests/core/go_test/env_test.go
new file mode 100644
index 00000000..89dae3ca
--- /dev/null
+++ b/tests/core/go_test/env_test.go
@@ -0,0 +1,39 @@
+// Copyright 2021 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 env_test
+
+import (
+ "os"
+ "testing"
+
+ "github.com/bazelbuild/rules_go/go/tools/bazel"
+)
+
+func TestEnv(t *testing.T) {
+ v := os.Getenv("ZONEINFO")
+ if v == "" {
+ t.Fatalf("ZONEINFO env var was empty")
+ }
+
+ path, err := bazel.Runfile(v)
+ if err != nil {
+ t.Fatalf("Could not find runfile %v: %v", v, err)
+ }
+
+ if _, err := os.Stat(path); err != nil {
+ t.Fatalf("Could not find file at env var $ZONEINFO (value: %v) at path %v: %v", v, path, err)
+ }
+}
+
diff --git a/tests/core/go_test/example_test.go b/tests/core/go_test/example_test.go
new file mode 100644
index 00000000..6314e69c
--- /dev/null
+++ b/tests/core/go_test/example_test.go
@@ -0,0 +1,32 @@
+package lib
+
+import "fmt"
+
+var Expected = "Expected"
+
+func ExampleHelloWorld() {
+ fmt.Println("Hello Example!")
+ fmt.Println("expected: " + Expected)
+ fmt.Println("got: " + Got)
+ // Output:
+ // Hello Example!
+ // expected: Example
+ // got: Example
+}
+
+func ExampleDontTestMe() {
+ panic("Dont Test Me!")
+}
+
+func ExampleTestEmptyOutput() {
+ if false {
+ fmt.Println("Say something!")
+ }
+ // Output:
+}
+
+func ExampleTestQuoting() {
+ fmt.Printf(`"quotes are handled"`)
+ // Output:
+ // "quotes are handled"
+}
diff --git a/tests/core/go_test/external_importmap_dep.go b/tests/core/go_test/external_importmap_dep.go
new file mode 100644
index 00000000..c9990774
--- /dev/null
+++ b/tests/core/go_test/external_importmap_dep.go
@@ -0,0 +1,11 @@
+package external_importmap_dep
+
+import (
+ "github.com/bazelbuild/rules_go/tests/core/go_test/external_importmap"
+)
+
+type Impl struct{}
+
+func (_ *Impl) DeepCopyObject() external_importmap.Object {
+ return nil
+}
diff --git a/tests/core/go_test/external_importmap_lib.go b/tests/core/go_test/external_importmap_lib.go
new file mode 100644
index 00000000..e9800a1c
--- /dev/null
+++ b/tests/core/go_test/external_importmap_lib.go
@@ -0,0 +1,5 @@
+package external_importmap
+
+type Object interface {
+ DeepCopyObject() Object
+}
diff --git a/tests/core/go_test/external_importmap_test.go b/tests/core/go_test/external_importmap_test.go
new file mode 100644
index 00000000..fbcc55ef
--- /dev/null
+++ b/tests/core/go_test/external_importmap_test.go
@@ -0,0 +1,10 @@
+package external_importmap_test
+
+import (
+ "github.com/bazelbuild/rules_go/tests/core/go_test/external_importmap"
+ "github.com/bazelbuild/rules_go/tests/core/go_test/external_importmap_dep"
+)
+
+var _ external_importmap.Object = &external_importmap_dep.Impl{}
+
+// Test passes if it compiles.
diff --git a/tests/core/go_test/external_test.go b/tests/core/go_test/external_test.go
new file mode 100644
index 00000000..957cce9e
--- /dev/null
+++ b/tests/core/go_test/external_test.go
@@ -0,0 +1,29 @@
+/* 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 lib_test
+
+import (
+ "lib"
+ "testing"
+)
+
+var Expect = "Expect"
+
+func TestExternal(t *testing.T) {
+ if lib.Got != Expect {
+ t.Errorf("Got %q expected %q", lib.Got, Expect)
+ }
+}
diff --git a/tests/core/go_test/flag_test.go b/tests/core/go_test/flag_test.go
new file mode 100644
index 00000000..ec7c3576
--- /dev/null
+++ b/tests/core/go_test/flag_test.go
@@ -0,0 +1,30 @@
+/* Copyright 2018 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 (
+ "flag"
+ "testing"
+)
+
+const flagDefault = "flag default"
+
+var aflag = flag.String("aflag", flagDefault, "A test flag")
+
+func TestAFlag(t *testing.T) {
+ if *aflag == flagDefault {
+ t.Error("aflag is still the default")
+ }
+}
diff --git a/tests/core/go_test/fuzz_test.go b/tests/core/go_test/fuzz_test.go
new file mode 100644
index 00000000..48c88194
--- /dev/null
+++ b/tests/core/go_test/fuzz_test.go
@@ -0,0 +1,29 @@
+// 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.
+
+//go:build go1.18
+// +build go1.18
+
+package fuzz_test
+
+import "testing"
+
+func Fuzz(f *testing.F) {
+ f.Add("seed")
+ f.Fuzz(func(t *testing.T, s string) {
+ if s != "seed" {
+ t.Fail()
+ }
+ })
+}
diff --git a/tests/core/go_test/indirect_import_dep.go b/tests/core/go_test/indirect_import_dep.go
new file mode 100644
index 00000000..c47d28b5
--- /dev/null
+++ b/tests/core/go_test/indirect_import_dep.go
@@ -0,0 +1,7 @@
+package indirect_import_dep
+
+import "github.com/bazelbuild/rules_go/tests/core/go_test/indirect_import"
+
+func GetX() string {
+ return indirect_import.X
+}
diff --git a/tests/core/go_test/indirect_import_i_test.go b/tests/core/go_test/indirect_import_i_test.go
new file mode 100644
index 00000000..e6814143
--- /dev/null
+++ b/tests/core/go_test/indirect_import_i_test.go
@@ -0,0 +1,7 @@
+package indirect_import
+
+import "testing"
+
+func TestMain(m *testing.M) {
+ X = "set by TestMain"
+}
diff --git a/tests/core/go_test/indirect_import_lib.go b/tests/core/go_test/indirect_import_lib.go
new file mode 100644
index 00000000..915c0dfe
--- /dev/null
+++ b/tests/core/go_test/indirect_import_lib.go
@@ -0,0 +1,3 @@
+package indirect_import
+
+var X string = "not set"
diff --git a/tests/core/go_test/indirect_import_x_test.go b/tests/core/go_test/indirect_import_x_test.go
new file mode 100644
index 00000000..008063ce
--- /dev/null
+++ b/tests/core/go_test/indirect_import_x_test.go
@@ -0,0 +1,15 @@
+package indirect_import_test
+
+import (
+ "testing"
+
+ "github.com/bazelbuild/rules_go/tests/core/go_test/indirect_import_dep"
+)
+
+func Test(t *testing.T) {
+ got := indirect_import_dep.GetX()
+ want := "set by TestMain"
+ if got != want {
+ t.Errorf("got %q; want %q", got, want)
+ }
+}
diff --git a/tests/core/go_test/internal_test.go b/tests/core/go_test/internal_test.go
new file mode 100644
index 00000000..fdb112e6
--- /dev/null
+++ b/tests/core/go_test/internal_test.go
@@ -0,0 +1,28 @@
+/* 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 lib
+
+import (
+ "testing"
+)
+
+var Expect = "Expect"
+
+func TestInternal(t *testing.T) {
+ if Got != Expect {
+ t.Errorf("Got %q expected %q", Got, Expect)
+ }
+}
diff --git a/tests/core/go_test/lib.go b/tests/core/go_test/lib.go
new file mode 100644
index 00000000..f34c854f
--- /dev/null
+++ b/tests/core/go_test/lib.go
@@ -0,0 +1,20 @@
+/* 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 lib
+
+import "C"
+
+var Got = "Got"
diff --git a/tests/core/go_test/only_testmain_test.go b/tests/core/go_test/only_testmain_test.go
new file mode 100644
index 00000000..474a3d6c
--- /dev/null
+++ b/tests/core/go_test/only_testmain_test.go
@@ -0,0 +1,10 @@
+package only_testmain
+
+import (
+ "os"
+ "testing"
+)
+
+func TestMain(m *testing.M) {
+ os.Exit(m.Run())
+}
diff --git a/tests/core/go_test/pwd_test.go b/tests/core/go_test/pwd_test.go
new file mode 100644
index 00000000..9de4abf0
--- /dev/null
+++ b/tests/core/go_test/pwd_test.go
@@ -0,0 +1,16 @@
+package pwd
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+)
+
+func TestPwd(t *testing.T) {
+ pwd := os.Getenv("PWD")
+ suffix := filepath.FromSlash("tests/core/go_test")
+ if !strings.HasSuffix(pwd, filepath.FromSlash(suffix)) {
+ t.Errorf("PWD not set. got %q; want something ending with %q", pwd, suffix)
+ }
+}
diff --git a/tests/core/go_test/same_package_test.go b/tests/core/go_test/same_package_test.go
new file mode 100644
index 00000000..cf991492
--- /dev/null
+++ b/tests/core/go_test/same_package_test.go
@@ -0,0 +1,7 @@
+package same_package
+
+import (
+ "testing"
+)
+
+func OkTest(t *testing.T) {}
diff --git a/tests/core/go_test/sharding_test.go b/tests/core/go_test/sharding_test.go
new file mode 100644
index 00000000..a83bb85c
--- /dev/null
+++ b/tests/core/go_test/sharding_test.go
@@ -0,0 +1,13 @@
+package sharding_test
+
+import (
+ "log"
+ "os"
+ "testing"
+)
+
+func TestShardStatusFile(t *testing.T) {
+ if _, err := os.Stat(os.Getenv("TEST_SHARD_STATUS_FILE")); err != nil {
+ log.Fatalf("Expected Go test runner to create TEST_SHARD_STATUS_FILE: %v", err)
+ }
+}
diff --git a/tests/core/go_test/tags_bad_test.go b/tests/core/go_test/tags_bad_test.go
new file mode 100644
index 00000000..76d32cd5
--- /dev/null
+++ b/tests/core/go_test/tags_bad_test.go
@@ -0,0 +1,9 @@
+// +build !good
+
+package tags_test
+
+import "testing"
+
+func Test(t *testing.T) {
+ t.Fail()
+}
diff --git a/tests/core/go_test/tags_good_test.go b/tests/core/go_test/tags_good_test.go
new file mode 100644
index 00000000..60071805
--- /dev/null
+++ b/tests/core/go_test/tags_good_test.go
@@ -0,0 +1,7 @@
+// +build good
+
+package tags_test
+
+import "testing"
+
+func Test(t *testing.T) {}
diff --git a/tests/core/go_test/test_fail_fast_test.go b/tests/core/go_test/test_fail_fast_test.go
new file mode 100644
index 00000000..b949b489
--- /dev/null
+++ b/tests/core/go_test/test_fail_fast_test.go
@@ -0,0 +1,75 @@
+// 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 test_fail_fast_test
+
+import (
+ "bytes"
+ "io/ioutil"
+ "path/filepath"
+ "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 = "fail_fast_test",
+ srcs = ["fail_fast_test.go"],
+)
+
+-- fail_fast_test.go --
+package test_fail_fast
+
+import "testing"
+
+func TestShouldFail(t *testing.T) {
+ t.Fail()
+}
+
+func TestShouldNotRun(t *testing.T) {
+ t.Fail()
+}
+`,
+ })
+}
+
+func Test(t *testing.T) {
+ if err := bazel_testing.RunBazel("test", "//:fail_fast_test", "--test_runner_fail_fast"); err == nil {
+ t.Fatal("got success; want failure")
+ } else if bErr, ok := err.(*bazel_testing.StderrExitError); !ok {
+ t.Fatalf("got %v; want StderrExitError", err)
+ } else if code := bErr.Err.ExitCode(); code != 3 {
+ t.Fatalf("got code %d; want code 3", code)
+ }
+
+ logPath := filepath.FromSlash("bazel-testlogs/fail_fast_test/test.log")
+ logData, err := ioutil.ReadFile(logPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !bytes.Contains(logData, []byte("TestShouldFail")) {
+ t.Fatalf("test log does not contain 'TestShouldFail': %q", logData)
+ }
+
+ if bytes.Contains(logData, []byte("TestShouldNotRun")) {
+ t.Fatalf("test log contains 'TestShouldNotRun' but should not: %q", logData)
+ }
+}
diff --git a/tests/core/go_test/test_filter_test.go b/tests/core/go_test/test_filter_test.go
new file mode 100644
index 00000000..49b1ce73
--- /dev/null
+++ b/tests/core/go_test/test_filter_test.go
@@ -0,0 +1,54 @@
+// 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 test_filter_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_test")
+
+go_test(
+ name = "filter_test",
+ srcs = ["filter_test.go"],
+)
+
+-- filter_test.go --
+package test_filter
+
+import "testing"
+
+func TestShouldPass(t *testing.T) {
+}
+
+func TestShouldFail(t *testing.T) {
+ t.Fail()
+}
+
+`,
+ })
+}
+
+func Test(t *testing.T) {
+ if err := bazel_testing.RunBazel("test", "//:filter_test", "--test_filter=Pass"); err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/tests/core/go_test/testmain_import_indirect_test.go b/tests/core/go_test/testmain_import_indirect_test.go
new file mode 100644
index 00000000..bcb6fac8
--- /dev/null
+++ b/tests/core/go_test/testmain_import_indirect_test.go
@@ -0,0 +1,7 @@
+package test_main_test
+
+import "example.com/imports/test_main"
+
+func init() {
+ test_main.Updated = true
+}
diff --git a/tests/core/go_test/testmain_import_main_test.go b/tests/core/go_test/testmain_import_main_test.go
new file mode 100644
index 00000000..b723bf06
--- /dev/null
+++ b/tests/core/go_test/testmain_import_main_test.go
@@ -0,0 +1,11 @@
+package test_main
+
+import "testing"
+
+var Updated bool
+
+func TestShouldPass(t *testing.T) {
+ if !Updated {
+ t.Fail()
+ }
+}
diff --git a/tests/core/go_test/testmain_without_exit_test.go b/tests/core/go_test/testmain_without_exit_test.go
new file mode 100644
index 00000000..6944747d
--- /dev/null
+++ b/tests/core/go_test/testmain_without_exit_test.go
@@ -0,0 +1,59 @@
+// 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.
+
+package testmain_without_exit
+
+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_test")
+
+go_test(
+ name = "main_without_exit_test",
+ srcs = ["main_without_exit_test.go"],
+)
+
+-- main_without_exit_test.go --
+package test_main_without_exit
+
+import "testing"
+
+func TestMain(m *testing.M) {
+ m.Run()
+}
+
+func TestShouldFail(t *testing.T) {
+ t.Fail()
+}
+`,
+ })
+}
+
+func Test(t *testing.T) {
+ err := bazel_testing.RunBazel("test", "//:main_without_exit_test")
+ if err == nil {
+ t.Fatal("expected bazel test to have failed")
+ }
+
+ if xerr, ok := err.(*bazel_testing.StderrExitError); !ok || xerr.Err.ExitCode() != 3 {
+ t.Fatalf("expected bazel tests to fail with exit code 3 (TESTS_FAILED), got: %s", err)
+ }
+}
diff --git a/tests/core/go_test/wrapped_test.go b/tests/core/go_test/wrapped_test.go
new file mode 100644
index 00000000..ce8c1933
--- /dev/null
+++ b/tests/core/go_test/wrapped_test.go
@@ -0,0 +1,19 @@
+// 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.
+
+package wrapped_test
+
+import "testing"
+
+func Test(t *testing.T) {}
diff --git a/tests/core/go_test/wrapper_test.go b/tests/core/go_test/wrapper_test.go
new file mode 100644
index 00000000..c889ccac
--- /dev/null
+++ b/tests/core/go_test/wrapper_test.go
@@ -0,0 +1,46 @@
+// 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.
+
+package wrapper_test
+
+import (
+ "io/ioutil"
+ "os"
+ "os/exec"
+ "testing"
+)
+
+func Test(t *testing.T) {
+ if _, ok := os.LookupEnv("RUNFILES_MANIFEST_FILE"); ok {
+ t.Skipf("test only applicable with a runfiles directory")
+ }
+
+ tmpLog, err := ioutil.TempFile("", "tmp.xml")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer func() {
+ tmpLog.Close()
+ os.Remove(tmpLog.Name())
+ }()
+
+ arg := os.Args[1]
+ cmd := exec.Command(arg)
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ cmd.Env = append(os.Environ(), "GO_TEST_WRAP=1", "XML_OUTPUT_FILE="+tmpLog.Name())
+ if err := cmd.Run(); err != nil {
+ t.Fatalf("running wrapped_test: %v", err)
+ }
+}
diff --git a/tests/core/go_test/x b/tests/core/go_test/x
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/core/go_test/x
diff --git a/tests/core/go_test/x_defs/BUILD.bazel b/tests/core/go_test/x_defs/BUILD.bazel
new file mode 100644
index 00000000..7edc4885
--- /dev/null
+++ b/tests/core/go_test/x_defs/BUILD.bazel
@@ -0,0 +1,73 @@
+load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
+
+test_suite(
+ name = "x_defs",
+)
+
+go_test(
+ name = "foo_test",
+ srcs = ["foo_test.go"],
+ embed = [":foo"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/foo",
+ pure = "on",
+ deps = [":bar"],
+)
+
+go_library(
+ name = "bar",
+ srcs = ["bar.go"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/bar",
+ visibility = ["//visibility:public"],
+ x_defs = {"Bar": "Bar"},
+ deps = [
+ ":baz",
+ ":foo",
+ ],
+)
+
+go_library(
+ name = "baz",
+ srcs = ["baz.go"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/baz",
+ visibility = ["//visibility:public"],
+ x_defs = {"Baz": "Baz"},
+ deps = [":qux"],
+)
+
+go_library(
+ name = "foo",
+ srcs = ["foo.go"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/foo",
+ visibility = ["//visibility:public"],
+)
+
+go_library(
+ name = "qux",
+ srcs = ["qux.go"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/qux",
+ visibility = ["//visibility:public"],
+ x_defs = {"Qux": "Qux"},
+)
+
+go_library(
+ name = "x_defs_lib",
+ srcs = ["x_defs_lib.go"],
+ data = ["x_defs_lib.go"],
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/x_defs_lib",
+ x_defs = {
+ "LibGo": "$(rlocationpath x_defs_lib.go)",
+ },
+)
+
+go_test(
+ name = "x_defs_test",
+ srcs = ["x_defs_test.go"],
+ data = ["x_defs_test.go"],
+ x_defs = {
+ "BinGo": "$(rlocationpath x_defs_test.go)",
+ },
+ deps = [
+ ":x_defs_lib",
+ "//go/runfiles",
+ ],
+)
diff --git a/tests/core/go_test/x_defs/bar.go b/tests/core/go_test/x_defs/bar.go
new file mode 100644
index 00000000..1d0081a4
--- /dev/null
+++ b/tests/core/go_test/x_defs/bar.go
@@ -0,0 +1,10 @@
+package bar
+
+import (
+ "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/baz"
+ _ "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/foo"
+)
+
+var Bar string
+var Qux = baz.Qux
+var Baz = baz.Baz
diff --git a/tests/core/go_test/x_defs/baz.go b/tests/core/go_test/x_defs/baz.go
new file mode 100644
index 00000000..9d4334f7
--- /dev/null
+++ b/tests/core/go_test/x_defs/baz.go
@@ -0,0 +1,6 @@
+package baz
+
+import "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/qux"
+
+var Baz string
+var Qux = qux.Qux
diff --git a/tests/core/go_test/x_defs/foo.go b/tests/core/go_test/x_defs/foo.go
new file mode 100644
index 00000000..f52652b1
--- /dev/null
+++ b/tests/core/go_test/x_defs/foo.go
@@ -0,0 +1 @@
+package foo
diff --git a/tests/core/go_test/x_defs/foo_test.go b/tests/core/go_test/x_defs/foo_test.go
new file mode 100644
index 00000000..dc940509
--- /dev/null
+++ b/tests/core/go_test/x_defs/foo_test.go
@@ -0,0 +1,28 @@
+package foo_test
+
+import (
+ "testing"
+
+ "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/bar"
+)
+
+func TestBar(t *testing.T) {
+ Equal(t, "Bar", bar.Bar)
+}
+
+func TestBaz(t *testing.T) {
+ Equal(t, "Baz", bar.Baz)
+}
+
+func TestQux(t *testing.T) {
+ Equal(t, "Qux", bar.Qux)
+}
+
+func Equal(t *testing.T, expected string, actual string) bool {
+ if expected != actual {
+ t.Errorf("Not equal: \n"+
+ "expected: %s\n"+
+ "actual : %s", expected, actual)
+ }
+ return true
+}
diff --git a/tests/core/go_test/x_defs/qux.go b/tests/core/go_test/x_defs/qux.go
new file mode 100644
index 00000000..4983170c
--- /dev/null
+++ b/tests/core/go_test/x_defs/qux.go
@@ -0,0 +1,3 @@
+package qux
+
+var Qux string
diff --git a/tests/core/go_test/x_defs/x_defs_lib.go b/tests/core/go_test/x_defs/x_defs_lib.go
new file mode 100644
index 00000000..e7794b33
--- /dev/null
+++ b/tests/core/go_test/x_defs/x_defs_lib.go
@@ -0,0 +1,3 @@
+package x_defs_lib
+
+var LibGo = "not set"
diff --git a/tests/core/go_test/x_defs/x_defs_test.go b/tests/core/go_test/x_defs/x_defs_test.go
new file mode 100644
index 00000000..0832c247
--- /dev/null
+++ b/tests/core/go_test/x_defs/x_defs_test.go
@@ -0,0 +1,34 @@
+package x_defs_lib_test
+
+import (
+ "os"
+ "testing"
+
+ "github.com/bazelbuild/rules_go/go/runfiles"
+
+ "github.com/bazelbuild/rules_go/tests/core/go_test/x_defs/x_defs_lib"
+)
+
+var BinGo = "not set"
+
+func TestLibGoPath(t *testing.T) {
+ libGoPath, err := runfiles.Rlocation(x_defs_lib.LibGo)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = os.Stat(libGoPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+}
+
+func TestBinGoPath(t *testing.T) {
+ binGoPath, err := runfiles.Rlocation(BinGo)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = os.Stat(binGoPath)
+ if err != nil {
+ t.Fatal(err)
+ }
+}
diff --git a/tests/core/go_test/xmlreport_test.go b/tests/core/go_test/xmlreport_test.go
new file mode 100644
index 00000000..09198356
--- /dev/null
+++ b/tests/core/go_test/xmlreport_test.go
@@ -0,0 +1,163 @@
+// 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.
+
+package test_filter_test
+
+import (
+ "encoding/xml"
+ "io/ioutil"
+ "path/filepath"
+ "reflect"
+ "strings"
+ "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 = "xml_test",
+ importpath = "github.com/bazelbuild/rules_go/tests/core/go_test/xml_test",
+ srcs = ["xml_test.go"],
+)
+
+-- xml_test.go --
+package test
+
+import (
+ "math/rand"
+ "testing"
+ "time"
+)
+
+func TestPass(t *testing.T) {
+ t.Parallel()
+}
+
+func TestPassLog(t *testing.T) {
+ t.Parallel()
+ t.Log("pass")
+}
+
+func TestFail(t *testing.T) {
+ t.Error("Not working")
+}
+
+func TestSubtests(t *testing.T) {
+ for i, subtest := range []string{"subtest a", "testB", "another subtest"} {
+ t.Run(subtest, func(t *testing.T) {
+ t.Logf("from subtest %s", subtest)
+ time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
+ t.Logf("from subtest %s", subtest)
+ if i%3 == 0 {
+ t.Skip("skipping this test")
+ }
+ if i%2 == 0 {
+ t.Fail()
+ }
+ })
+ }
+}
+`,
+ })
+}
+
+// test execution time attributes will vary per testrun, so we must parse the
+// xml to inspect a subset of testresults
+type xmlTestSuite struct {
+ XMLName xml.Name `xml:"testsuite"`
+ Errors int `xml:"errors,attr"`
+ Failures int `xml:"failures,attr"`
+ Skipped int `xml:"skipped,attr"`
+ Tests int `xml:"tests,attr"`
+ Name string `xml:"name,attr"`
+}
+type xmlTestSuites struct {
+ XMLName xml.Name `xml:"testsuites"`
+ Suites []xmlTestSuite `xml:"testsuite"`
+}
+
+func Test(t *testing.T) {
+ tests := []struct {
+ name string
+ args []string
+ expected xmlTestSuites
+ }{
+ {
+ name: "default",
+ args: []string{"test", "//:xml_test"},
+ expected: xmlTestSuites{
+ XMLName: xml.Name{Local: "testsuites"},
+ Suites: []xmlTestSuite{{
+ XMLName: xml.Name{Local: "testsuite"},
+ Name: "github.com/bazelbuild/rules_go/tests/core/go_test/xml_test",
+ Errors: 0,
+ Failures: 3,
+ Tests: 3,
+ }},
+ },
+ },
+ {
+ name: "verbose",
+ args: []string{"test", "--test_env=GO_TEST_WRAP_TESTV=1", "//:xml_test"},
+ expected: xmlTestSuites{
+ XMLName: xml.Name{Local: "testsuites"},
+ Suites: []xmlTestSuite{{
+ XMLName: xml.Name{Local: "testsuite"},
+ Name: "github.com/bazelbuild/rules_go/tests/core/go_test/xml_test",
+ Errors: 0,
+ Failures: 3,
+ Skipped: 1,
+ Tests: 7,
+ }},
+ },
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ err := bazel_testing.RunBazel(tt.args...)
+ if err == nil {
+ t.Fatal("expected bazel test to have failed")
+ }
+ if xerr, ok := err.(*bazel_testing.StderrExitError); !ok || xerr.Err.ExitCode() != 3 {
+ t.Fatalf("expected bazel tests to fail with exit code 3 (TESTS_FAILED), got: %s", err)
+ }
+
+ p, err := bazel_testing.BazelOutput("info", "bazel-testlogs")
+ if err != nil {
+ t.Fatal("could not find testlog root: %s", err)
+ }
+ path := filepath.Join(strings.TrimSpace(string(p)), "xml_test/test.xml")
+ b, err := ioutil.ReadFile(path)
+ if err != nil {
+ t.Fatalf("could not read generated xml file: %s", err)
+ }
+
+ var suites xmlTestSuites
+ if err := xml.Unmarshal(b, &suites); err != nil {
+ t.Fatalf("could not unmarshall generated xml: %s", err)
+ }
+
+ if !reflect.DeepEqual(suites, tt.expected) {
+ t.Fatalf("expected %#v, got: %#v", tt.expected, suites)
+ }
+ })
+ }
+}
diff --git a/tests/core/go_test/y b/tests/core/go_test/y
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/core/go_test/y
diff --git a/tests/core/go_test/z b/tests/core/go_test/z
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/core/go_test/z