From 660cfdff3f2ac771c6f0f18834921cfc043b2f3a Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Wed, 19 Feb 2020 15:41:22 -0800 Subject: Handle situations where go-marshal generates an empty test file. This can happen due to conditional compilation, where a subset of the source files contain no marshallable types. go-marshal is still required to write an output file in these cases, since bazel defines the output package before calling go-marshal. PiperOrigin-RevId: 296074321 --- tools/go_marshal/gomarshal/generator.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'tools/go_marshal/gomarshal') diff --git a/tools/go_marshal/gomarshal/generator.go b/tools/go_marshal/gomarshal/generator.go index d3c2f72f5..0fa868415 100644 --- a/tools/go_marshal/gomarshal/generator.go +++ b/tools/go_marshal/gomarshal/generator.go @@ -380,6 +380,26 @@ func (g *Generator) writeTests(ts []*testGenerator) error { } // Write test functions. + + // If we didn't generate any Marshallable implementations, we can't just + // emit an empty test file, since that causes the build to fail with "no + // tests/benchmarks/examples found". Unfortunately we can't signal bazel to + // omit the entire package since the outputs are already defined before + // go-marshal is called. If we'd otherwise emit an empty test suite, emit an + // empty example instead. + if len(ts) == 0 { + b.reset() + b.emit("func ExampleEmptyTestSuite() {\n") + b.inIndent(func() { + b.emit("// This example is intentionally empty to ensure this file contains at least\n") + b.emit("// one testable entity. go-marshal is forced to emit a test file if a package\n") + b.emit("// is marked marshallable, but emitting a test file with no entities results\n") + b.emit("// in a build failure.\n") + }) + b.emit("}\n") + return b.write(g.outputTest) + } + for _, t := range ts { if err := t.write(g.outputTest); err != nil { return err -- cgit v1.2.3