@@ -37,6 +37,7 @@ import (
37
37
"github.com/spf13/cobra"
38
38
"github.com/spf13/pflag"
39
39
"golang.org/x/crypto/ssh/terminal"
40
+ "golang.org/x/tools/go/buildutil"
40
41
)
41
42
42
43
var currentDirectory string
@@ -331,17 +332,17 @@ func main() {
331
332
}
332
333
s := gbuild .NewSession (options )
333
334
334
- tests := & testFuncs {Package : pkg .Package }
335
+ tests := & testFuncs {BuildContext : s . BuildContext (), Package : pkg .Package }
335
336
collectTests := func (testPkg * gbuild.PackageData , testPkgName string , needVar * bool ) error {
336
337
if testPkgName == "_test" {
337
338
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 {
339
340
return err
340
341
}
341
342
}
342
343
} else {
343
344
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 {
345
346
return err
346
347
}
347
348
}
@@ -453,7 +454,7 @@ func main() {
453
454
}
454
455
status := "ok "
455
456
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 {
457
458
if _ , ok := err .(* exec.ExitError ); ! ok {
458
459
return err
459
460
}
@@ -740,6 +741,8 @@ func sprintError(err error) string {
740
741
}
741
742
}
742
743
744
+ // runNode runs script with args using Node.js in directory dir.
745
+ // If dir is empty string, current directory is used.
743
746
func runNode (script string , args []string , dir string , quiet bool ) error {
744
747
var allArgs []string
745
748
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 {
790
793
return err
791
794
}
792
795
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
+
793
807
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
803
818
}
804
819
805
820
type testFunc struct {
@@ -811,8 +826,8 @@ type testFunc struct {
811
826
812
827
var testFileSet = token .NewFileSet ()
813
828
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 )
816
831
if err != nil {
817
832
return err
818
833
}
0 commit comments