x/playground: support multifile code with tests Previously, multifile code with tests was failing with error saying that function main is undeclared in the main package. This change aims to restrict number of files restriction to allow tests to run. The change also fixes test output typo. To test this change, cd to sandbox and run `make runlocal`. In a second terminal, stay on the root of the project and run `make test_gvisor`. Without removing `files.Num() == 1`, you would see the following error: ``` resp.Errors = "runtime.main_mainĀ·f: function main is undeclared in the main package\n", want "" ``` Fixes golang/go#32403 Change-Id: I98c0de6bf7a80559749a12a19ae3cc19b9d48e91 Reviewed-on: https://go-review.googlesource.com/c/playground/+/462835 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Bryan Mills <bcmills@google.com>
diff --git a/sandbox.go b/sandbox.go index c081e06..91cf0ea 100644 --- a/sandbox.go +++ b/sandbox.go
@@ -392,7 +392,7 @@ br = new(buildResult) defer br.cleanup() var buildPkgArg = "." - if files.Num() == 1 && len(files.Data(progName)) > 0 { + if len(files.Data(progName)) > 0 { src := files.Data(progName) if isTestProg(src) { br.testParam = "-test.v"
diff --git a/tests.go b/tests.go index 46731a6..b0911a1 100644 --- a/tests.go +++ b/tests.go
@@ -630,7 +630,26 @@ func print() { =fmt.Println("Hello, playground") } -`, errors: `./foo.go:6:2: syntax error: unexpected =, expecting } +`, errors: `./foo.go:6:2: syntax error: unexpected =, expected } `, }, + { + name: "multi_files_tests", + prog: ` +package main +import ( + "testing" + _ "play.ground/pkg" +) + +func TestFoo(t *testing.T) { +} +-- go.mod -- +module play.ground +-- pkg/x.go -- +package pkg + +`, want: `=== RUN TestFoo +--- PASS: TestFoo (0.00s) +PASS`}, }