Skip to content

Commit 46fa8af

Browse files
Kevin HerroJay Conrod
Kevin Herro
authored and
Jay Conrod
committed
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 <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Trust: Jay Conrod <[email protected]> Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent ca3aefc commit 46fa8af

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/cmd/go/internal/load/test.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"unicode"
2222
"unicode/utf8"
2323

24+
"cmd/go/internal/fsys"
2425
"cmd/go/internal/str"
2526
"cmd/go/internal/trace"
2627
)
@@ -578,7 +579,13 @@ type testFunc struct {
578579
var testFileSet = token.NewFileSet()
579580

580581
func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
581-
f, err := parser.ParseFile(testFileSet, filename, nil, parser.ParseComments)
582+
// Pass in the overlaid source if we have an overlay for this file.
583+
src, err := fsys.Open(filename)
584+
if err != nil {
585+
return err
586+
}
587+
defer src.Close()
588+
f, err := parser.ParseFile(testFileSet, filename, src, parser.ParseComments)
582589
if err != nil {
583590
return err
584591
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[short] skip
2+
3+
cd $WORK/gopath/src/foo
4+
go test -list=. -overlay=overlay.json .
5+
stdout 'TestBar'
6+
7+
-- go.mod --
8+
module test.pkg
9+
-- foo/foo_test.go --
10+
package foo
11+
12+
import "testing"
13+
14+
func TestFoo(t *testing.T) { }
15+
-- tmp/bar_test.go --
16+
package foo
17+
18+
import "testing"
19+
20+
func TestBar(t *testing.T) {
21+
t.Fatal("dummy failure")
22+
}
23+
-- foo/overlay.json --
24+
{"Replace": {"foo_test.go": "../tmp/bar_test.go"}}

0 commit comments

Comments
 (0)