Skip to content

Commit

Permalink
Run nogo on internal and external tests libs, not testmain
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fmeum committed Sep 4, 2024
1 parent 4682a34 commit 4a64b95
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
9 changes: 7 additions & 2 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 "."
Expand Down Expand Up @@ -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():
Expand All @@ -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,
Expand Down
59 changes: 57 additions & 2 deletions tests/core/nogo/tests/tests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package importpath_test

import (
"strings"
"testing"

"github.com/bazelbuild/rules_go/go/tools/bazel_testing"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}
}

0 comments on commit 4a64b95

Please sign in to comment.