Skip to content

Commit 046d5bc

Browse files
fmeumtyler-french
authored andcommitted
Run nogo on internal and external tests libs, not testmain (#4082)
**What type of PR is this?** Bug fix **What does this PR do? Why is it needed?** nogo should run on both the internal and external libraries compiled for a `go_test`, but didn't. It also shouldn't run on the generated `testmain.go` file, but did.
1 parent c93053c commit 046d5bc

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

go/private/rules/test.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ def _go_test_impl(ctx):
6060

6161
go = go_context(ctx)
6262

63+
validation_outputs = []
64+
6365
# Compile the library to test with internal white box tests
6466
internal_library = go.new_library(go, testfilter = "exclude")
6567
internal_source = go.library_to_source(go, ctx.attr, internal_library, ctx.coverage_instrumented())
6668
internal_archive = go.archive(go, internal_source)
69+
if internal_archive.data._validation_output:
70+
validation_outputs.append(internal_archive.data._validation_output)
6771
go_srcs = split_srcs(internal_source.srcs).go
6872

6973
# Compile the library with the external black box tests
@@ -82,6 +86,8 @@ def _go_test_impl(ctx):
8286
), external_library, ctx.coverage_instrumented())
8387
external_source, internal_archive = _recompile_external_deps(go, external_source, internal_archive, [t.label for t in ctx.attr.embed])
8488
external_archive = go.archive(go, external_source, is_external_pkg = True)
89+
if external_archive.data._validation_output:
90+
validation_outputs.append(external_archive.data._validation_output)
8591

8692
# now generate the main function
8793
repo_relative_rundir = ctx.attr.rundir or ctx.label.package or "."
@@ -165,7 +171,6 @@ def _go_test_impl(ctx):
165171
version_file = ctx.version_file,
166172
info_file = ctx.info_file,
167173
)
168-
validation_output = test_archive.data._validation_output
169174

170175
env = {}
171176
for k, v in ctx.attr.env.items():
@@ -187,7 +192,7 @@ def _go_test_impl(ctx):
187192
),
188193
OutputGroupInfo(
189194
compilation_outputs = [internal_archive.data.file],
190-
_validation = [validation_output] if validation_output else [],
195+
_validation = validation_outputs,
191196
),
192197
coverage_common.instrumented_files_info(
193198
ctx,

tests/core/nogo/tests/tests_test.go

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package importpath_test
1616

1717
import (
18+
"strings"
1819
"testing"
1920

2021
"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
@@ -45,6 +46,18 @@ go_test(
4546
srcs = ["super_simple_test.go"],
4647
)
4748
49+
go_test(
50+
name = "diagnostic_external_test",
51+
size = "small",
52+
srcs = ["diagnostic_external_test.go"],
53+
)
54+
55+
go_test(
56+
name = "diagnostic_internal_test",
57+
size = "small",
58+
srcs = ["diagnostic_internal_test.go"],
59+
)
60+
4861
nogo(
4962
name = "nogo",
5063
vet = True,
@@ -75,21 +88,63 @@ import (
7588
7689
func TestFoo(t *testing.T) {
7790
}
91+
-- diagnostic_external_test.go --
92+
package diagnostic_test
93+
94+
import (
95+
"testing"
96+
)
97+
98+
func TestFoo(t *testing.T) {
99+
if TestFoo == nil {
100+
t.Fatal("TestFoo is nil")
101+
}
102+
}
103+
-- diagnostic_internal_test.go --
104+
package diagnostic
105+
106+
import (
107+
"testing"
108+
)
109+
110+
func TestFoo(t *testing.T) {
111+
if TestFoo == nil {
112+
t.Fatal("TestFoo is nil")
113+
}
114+
}
78115
`,
79116
Nogo: `@//:nogo`,
80117
})
81118
}
82119

83120
func TestExternalTestWithFullImportpath(t *testing.T) {
84-
if out, err := bazel_testing.BazelOutput("test", "//:all"); err != nil {
121+
if out, err := bazel_testing.BazelOutput("test", "//:simple_test"); err != nil {
85122
println(string(out))
86123
t.Fatal(err)
87124
}
88125
}
89126

90127
func TestEmptyExternalTest(t *testing.T) {
91-
if out, err := bazel_testing.BazelOutput("test", "//:all"); err != nil {
128+
if out, err := bazel_testing.BazelOutput("test", "//:super_simple_test"); err != nil {
92129
println(string(out))
93130
t.Fatal(err)
94131
}
95132
}
133+
134+
func TestDiagnosticInExternalTest(t *testing.T) {
135+
if _, err := bazel_testing.BazelOutput("test", "//:diagnostic_external_test"); err == nil {
136+
t.Fatal("unexpected success")
137+
} else if !strings.Contains(err.Error(), "diagnostic_external_test.go:8:8: comparison of function TestFoo == nil is always false (nilfunc)") {
138+
println(err.Error())
139+
t.Fatal("unexpected output")
140+
}
141+
}
142+
143+
func TestDiagnosticInInternalTest(t *testing.T) {
144+
if _, err := bazel_testing.BazelOutput("test", "//:diagnostic_internal_test"); err == nil {
145+
t.Fatal("unexpected success")
146+
} else if !strings.Contains(err.Error(), "diagnostic_internal_test.go:8:8: comparison of function TestFoo == nil is always false (nilfunc)") {
147+
println(err.Error())
148+
t.Fatal("unexpected output")
149+
}
150+
}

0 commit comments

Comments
 (0)