Permalink
Browse files

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.
  • Loading branch information...
dmitshur committed Apr 8, 2018
1 parent b90dbcb commit b24e3563d0efa33b32250411d469ba59fa5cf254
Showing with 37 additions and 16 deletions.
  1. +7 −1 build/build.go
  2. +30 −15 tool.go
View
@@ -130,6 +130,7 @@ func Import(path string, mode build.ImportMode, installSuffix string, buildTags
func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build.ImportMode, installSuffix string) (*PackageData, error) {
// bctx is passed by value, so it can be modified here.
var isVirtual bool
switch path {
case "syscall":
// 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
// These packages are already embedded via gopherjspkg.FS virtual filesystem (which can be
// safely vendored). Don't try to use vendor directory to resolve them.
mode |= build.IgnoreVendor
isVirtual = true
}
pkg, err := bctx.Import(path, srcDir, mode)
if err != nil {
@@ -192,7 +194,7 @@ func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build
return nil, err
}
return &PackageData{Package: pkg, JSFiles: jsFiles}, nil
return &PackageData{Package: pkg, JSFiles: jsFiles, IsVirtual: isVirtual}, nil
}
// excludeExecutable excludes all executable implementation .go files.
@@ -459,6 +461,7 @@ type PackageData struct {
IsTest bool // IsTest is true if the package is being built for running tests.
SrcModTime time.Time
UpToDate bool
IsVirtual bool // If true, the package does not have a corresponding physical directory on disk.
}
type Session struct {
@@ -500,6 +503,9 @@ func NewSession(options *Options) *Session {
return s
}
// BuildContext returns the session's build context.
func (s *Session) BuildContext() *build.Context { return s.bctx }
func (s *Session) InstallSuffix() string {
if s.options.Minify {
return "min"
View
45 tool.go
@@ -37,6 +37,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/tools/go/buildutil"
)
var currentDirectory string
@@ -331,17 +332,17 @@ func main() {
}
s := gbuild.NewSession(options)
tests := &testFuncs{Package: pkg.Package}
tests := &testFuncs{BuildContext: s.BuildContext(), Package: pkg.Package}
collectTests := func(testPkg *gbuild.PackageData, testPkgName string, needVar *bool) error {
if testPkgName == "_test" {
for _, file := range pkg.TestGoFiles {
if err := tests.load(filepath.Join(pkg.Package.Dir, file), testPkgName, &tests.ImportTest, &tests.NeedTest); err != nil {
if err := tests.load(pkg.Package.Dir, file, testPkgName, &tests.ImportTest, &tests.NeedTest); err != nil {
return err
}
}
} else {
for _, file := range pkg.XTestGoFiles {
if err := tests.load(filepath.Join(pkg.Package.Dir, file), "_xtest", &tests.ImportXtest, &tests.NeedXtest); err != nil {
if err := tests.load(pkg.Package.Dir, file, "_xtest", &tests.ImportXtest, &tests.NeedXtest); err != nil {
return err
}
}
@@ -453,7 +454,7 @@ func main() {
}
status := "ok "
start := time.Now()
if err := runNode(outfile.Name(), args, pkg.Dir, options.Quiet); err != nil {
if err := runNode(outfile.Name(), args, runTestDir(pkg), options.Quiet); err != nil {
if _, ok := err.(*exec.ExitError); !ok {
return err
}
@@ -740,6 +741,8 @@ func sprintError(err error) string {
}
}
// runNode runs script with args using Node.js in directory dir.
// If dir is empty string, current directory is used.
func runNode(script string, args []string, dir string, quiet bool) error {
var allArgs []string
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 {
return err
}
// runTestDir returns the directory for Node.js to use when running tests for package p.
// Empty string means current directory.
func runTestDir(p *gbuild.PackageData) string {
if p.IsVirtual {
// The package is virtual and doesn't have a physical directory. Use current directory.
return ""
}
// Run tests in the package directory.
return p.Dir
}
type testFuncs struct {
Tests []testFunc
Benchmarks []testFunc
Examples []testFunc
TestMain *testFunc
Package *build.Package
ImportTest bool
NeedTest bool
ImportXtest bool
NeedXtest bool
BuildContext *build.Context
Tests []testFunc
Benchmarks []testFunc
Examples []testFunc
TestMain *testFunc
Package *build.Package
ImportTest bool
NeedTest bool
ImportXtest bool
NeedXtest bool
}
type testFunc struct {
@@ -811,8 +826,8 @@ type testFunc struct {
var testFileSet = token.NewFileSet()
func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
f, err := parser.ParseFile(testFileSet, filename, nil, parser.ParseComments)
func (t *testFuncs) load(dir, file, pkg string, doImport, seen *bool) error {
f, err := buildutil.ParseFile(testFileSet, t.BuildContext, nil, dir, file, parser.ParseComments)
if err != nil {
return err
}

0 comments on commit b24e356

Please sign in to comment.