diff --git a/go/private/rules/test.bzl b/go/private/rules/test.bzl index 72e7c9034..5ee3f855c 100644 --- a/go/private/rules/test.bzl +++ b/go/private/rules/test.bzl @@ -67,10 +67,14 @@ def _go_test_impl(ctx): goarch = ctx.attr.goarch, ) + validation_outputs = [] + # Compile the library to test with internal white box tests internal_library = go.new_library(go, testfilter = "exclude") internal_source = go.library_to_source(go, ctx.attr, internal_library, ctx.coverage_instrumented()) internal_archive = go.archive(go, internal_source) + if internal_archive.data._validation_output: + validation_outputs.append(internal_archive.data._validation_output) go_srcs = [src for src in internal_source.srcs if src.extension == "go"] # Compile the library with the external black box tests @@ -89,6 +93,8 @@ def _go_test_impl(ctx): ), external_library, ctx.coverage_instrumented()) external_source, internal_archive = _recompile_external_deps(go, external_source, internal_archive, [t.label for t in ctx.attr.embed]) external_archive = go.archive(go, external_source, is_external_pkg = True) + if external_archive.data._validation_output: + validation_outputs.append(external_archive.data._validation_output) # now generate the main function repo_relative_rundir = ctx.attr.rundir or ctx.label.package or "." @@ -172,7 +178,6 @@ def _go_test_impl(ctx): version_file = ctx.version_file, info_file = ctx.info_file, ) - validation_output = test_archive.data._validation_output env = {} for k, v in ctx.attr.env.items(): @@ -194,7 +199,7 @@ def _go_test_impl(ctx): ), OutputGroupInfo( compilation_outputs = [internal_archive.data.file], - _validation = [validation_output] if validation_output else [], + _validation = validation_outputs, ), coverage_common.instrumented_files_info( ctx, diff --git a/tests/core/nogo/tests/tests_test.go b/tests/core/nogo/tests/tests_test.go index ed19dd41e..801388b02 100644 --- a/tests/core/nogo/tests/tests_test.go +++ b/tests/core/nogo/tests/tests_test.go @@ -15,6 +15,7 @@ package importpath_test import ( + "strings" "testing" "github.com/bazelbuild/rules_go/go/tools/bazel_testing" @@ -45,6 +46,18 @@ go_test( srcs = ["super_simple_test.go"], ) +go_test( + name = "diagnostic_external_test", + size = "small", + srcs = ["diagnostic_external_test.go"], +) + +go_test( + name = "diagnostic_internal_test", + size = "small", + srcs = ["diagnostic_internal_test.go"], +) + nogo( name = "nogo", vet = True, @@ -75,21 +88,63 @@ import ( func TestFoo(t *testing.T) { } +-- diagnostic_external_test.go -- +package diagnostic_test + +import ( + "testing" +) + +func TestFoo(t *testing.T) { + if TestFoo == nil { + t.Fatal("TestFoo is nil") + } +} +-- diagnostic_internal_test.go -- +package diagnostic + +import ( + "testing" +) + +func TestFoo(t *testing.T) { + if TestFoo == nil { + t.Fatal("TestFoo is nil") + } +} `, Nogo: `@//:nogo`, }) } func TestExternalTestWithFullImportpath(t *testing.T) { - if out, err := bazel_testing.BazelOutput("test", "//:all"); err != nil { + if out, err := bazel_testing.BazelOutput("test", "//:simple_test"); err != nil { println(string(out)) t.Fatal(err) } } func TestEmptyExternalTest(t *testing.T) { - if out, err := bazel_testing.BazelOutput("test", "//:all"); err != nil { + if out, err := bazel_testing.BazelOutput("test", "//:super_simple_test"); err != nil { println(string(out)) t.Fatal(err) } } + +func TestDiagnosticInExternalTest(t *testing.T) { + if _, err := bazel_testing.BazelOutput("test", "//:diagnostic_external_test"); err == nil { + t.Fatal("unexpected success") + } else if !strings.Contains(err.Error(), "diagnostic_external_test.go:8:8: comparison of function TestFoo == nil is always false (nilfunc)") { + println(err.Error()) + t.Fatal("unexpected output") + } +} + +func TestDiagnosticInInternalTest(t *testing.T) { + if _, err := bazel_testing.BazelOutput("test", "//:diagnostic_internal_test"); err == nil { + t.Fatal("unexpected success") + } else if !strings.Contains(err.Error(), "diagnostic_internal_test.go:8:8: comparison of function TestFoo == nil is always false (nilfunc)") { + println(err.Error()) + t.Fatal("unexpected output") + } +}