1
0
mirror of https://github.com/golang/go synced 2024-11-26 16:16:57 -07:00

cmd/go/internal/load/test: parse overlay files for test functions

The existing implementation implicitly reads from the filesystem
instead of using the overlay file data (due to src == nil), so
pass in the overlaid source if we have an overlay for this file.

Fixes #44946

Change-Id: I61ce09d10c5edac1b47332583efdcd3c1241f58a
Reviewed-on: https://go-review.googlesource.com/c/go/+/305071
Reviewed-by: Jay Conrod <jayconrod@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Trust: Jay Conrod <jayconrod@google.com>
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Kevin Herro 2021-03-26 08:58:11 -07:00 committed by Jay Conrod
parent ca3aefc4a9
commit 46fa8afca6
2 changed files with 32 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import (
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"cmd/go/internal/fsys"
"cmd/go/internal/str" "cmd/go/internal/str"
"cmd/go/internal/trace" "cmd/go/internal/trace"
) )
@ -578,7 +579,13 @@ type testFunc struct {
var testFileSet = token.NewFileSet() var testFileSet = token.NewFileSet()
func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error { func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
f, err := parser.ParseFile(testFileSet, filename, nil, parser.ParseComments) // Pass in the overlaid source if we have an overlay for this file.
src, err := fsys.Open(filename)
if err != nil {
return err
}
defer src.Close()
f, err := parser.ParseFile(testFileSet, filename, src, parser.ParseComments)
if err != nil { if err != nil {
return err return err
} }

View File

@ -0,0 +1,24 @@
[short] skip
cd $WORK/gopath/src/foo
go test -list=. -overlay=overlay.json .
stdout 'TestBar'
-- go.mod --
module test.pkg
-- foo/foo_test.go --
package foo
import "testing"
func TestFoo(t *testing.T) { }
-- tmp/bar_test.go --
package foo
import "testing"
func TestBar(t *testing.T) {
t.Fatal("dummy failure")
}
-- foo/overlay.json --
{"Replace": {"foo_test.go": "../tmp/bar_test.go"}}