Skip to content

Commit b24e356

Browse files
committed
Restore support for testing js package.
The _test.go file needs to be accessed via the build context's file system interface. Take into account the fact that the js and nosync packages are now effectively virtual, and don't have a corresponding physical directory. Use current directory when running tests with Node.js, otherwise it would fail with "directory not exist" error.
1 parent b90dbcb commit b24e356

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

build/build.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func Import(path string, mode build.ImportMode, installSuffix string, buildTags
130130

131131
func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build.ImportMode, installSuffix string) (*PackageData, error) {
132132
// bctx is passed by value, so it can be modified here.
133+
var isVirtual bool
133134
switch path {
134135
case "syscall":
135136
// syscall needs to use a typical GOARCH like amd64 to pick up definitions for _Socklen, BpfInsn, IFNAMSIZ, Timeval, BpfStat, SYS_FCNTL, Flock_t, etc.
@@ -148,6 +149,7 @@ func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build
148149
// These packages are already embedded via gopherjspkg.FS virtual filesystem (which can be
149150
// safely vendored). Don't try to use vendor directory to resolve them.
150151
mode |= build.IgnoreVendor
152+
isVirtual = true
151153
}
152154
pkg, err := bctx.Import(path, srcDir, mode)
153155
if err != nil {
@@ -192,7 +194,7 @@ func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build
192194
return nil, err
193195
}
194196

195-
return &PackageData{Package: pkg, JSFiles: jsFiles}, nil
197+
return &PackageData{Package: pkg, JSFiles: jsFiles, IsVirtual: isVirtual}, nil
196198
}
197199

198200
// excludeExecutable excludes all executable implementation .go files.
@@ -459,6 +461,7 @@ type PackageData struct {
459461
IsTest bool // IsTest is true if the package is being built for running tests.
460462
SrcModTime time.Time
461463
UpToDate bool
464+
IsVirtual bool // If true, the package does not have a corresponding physical directory on disk.
462465
}
463466

464467
type Session struct {
@@ -500,6 +503,9 @@ func NewSession(options *Options) *Session {
500503
return s
501504
}
502505

506+
// BuildContext returns the session's build context.
507+
func (s *Session) BuildContext() *build.Context { return s.bctx }
508+
503509
func (s *Session) InstallSuffix() string {
504510
if s.options.Minify {
505511
return "min"

tool.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/spf13/cobra"
3838
"github.com/spf13/pflag"
3939
"golang.org/x/crypto/ssh/terminal"
40+
"golang.org/x/tools/go/buildutil"
4041
)
4142

4243
var currentDirectory string
@@ -331,17 +332,17 @@ func main() {
331332
}
332333
s := gbuild.NewSession(options)
333334

334-
tests := &testFuncs{Package: pkg.Package}
335+
tests := &testFuncs{BuildContext: s.BuildContext(), Package: pkg.Package}
335336
collectTests := func(testPkg *gbuild.PackageData, testPkgName string, needVar *bool) error {
336337
if testPkgName == "_test" {
337338
for _, file := range pkg.TestGoFiles {
338-
if err := tests.load(filepath.Join(pkg.Package.Dir, file), testPkgName, &tests.ImportTest, &tests.NeedTest); err != nil {
339+
if err := tests.load(pkg.Package.Dir, file, testPkgName, &tests.ImportTest, &tests.NeedTest); err != nil {
339340
return err
340341
}
341342
}
342343
} else {
343344
for _, file := range pkg.XTestGoFiles {
344-
if err := tests.load(filepath.Join(pkg.Package.Dir, file), "_xtest", &tests.ImportXtest, &tests.NeedXtest); err != nil {
345+
if err := tests.load(pkg.Package.Dir, file, "_xtest", &tests.ImportXtest, &tests.NeedXtest); err != nil {
345346
return err
346347
}
347348
}
@@ -453,7 +454,7 @@ func main() {
453454
}
454455
status := "ok "
455456
start := time.Now()
456-
if err := runNode(outfile.Name(), args, pkg.Dir, options.Quiet); err != nil {
457+
if err := runNode(outfile.Name(), args, runTestDir(pkg), options.Quiet); err != nil {
457458
if _, ok := err.(*exec.ExitError); !ok {
458459
return err
459460
}
@@ -740,6 +741,8 @@ func sprintError(err error) string {
740741
}
741742
}
742743

744+
// runNode runs script with args using Node.js in directory dir.
745+
// If dir is empty string, current directory is used.
743746
func runNode(script string, args []string, dir string, quiet bool) error {
744747
var allArgs []string
745748
if b, _ := strconv.ParseBool(os.Getenv("SOURCE_MAP_SUPPORT")); os.Getenv("SOURCE_MAP_SUPPORT") == "" || b {
@@ -790,16 +793,28 @@ func runNode(script string, args []string, dir string, quiet bool) error {
790793
return err
791794
}
792795

796+
// runTestDir returns the directory for Node.js to use when running tests for package p.
797+
// Empty string means current directory.
798+
func runTestDir(p *gbuild.PackageData) string {
799+
if p.IsVirtual {
800+
// The package is virtual and doesn't have a physical directory. Use current directory.
801+
return ""
802+
}
803+
// Run tests in the package directory.
804+
return p.Dir
805+
}
806+
793807
type testFuncs struct {
794-
Tests []testFunc
795-
Benchmarks []testFunc
796-
Examples []testFunc
797-
TestMain *testFunc
798-
Package *build.Package
799-
ImportTest bool
800-
NeedTest bool
801-
ImportXtest bool
802-
NeedXtest bool
808+
BuildContext *build.Context
809+
Tests []testFunc
810+
Benchmarks []testFunc
811+
Examples []testFunc
812+
TestMain *testFunc
813+
Package *build.Package
814+
ImportTest bool
815+
NeedTest bool
816+
ImportXtest bool
817+
NeedXtest bool
803818
}
804819

805820
type testFunc struct {
@@ -811,8 +826,8 @@ type testFunc struct {
811826

812827
var testFileSet = token.NewFileSet()
813828

814-
func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
815-
f, err := parser.ParseFile(testFileSet, filename, nil, parser.ParseComments)
829+
func (t *testFuncs) load(dir, file, pkg string, doImport, seen *bool) error {
830+
f, err := buildutil.ParseFile(testFileSet, t.BuildContext, nil, dir, file, parser.ParseComments)
816831
if err != nil {
817832
return err
818833
}

0 commit comments

Comments
 (0)