Skip to content

Commit

Permalink
Restore support for testing js package.
Browse files Browse the repository at this point in the history
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 20, 2018
1 parent b90dbcb commit b24e356
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
8 changes: 7 additions & 1 deletion build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down
45 changes: 30 additions & 15 deletions tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit b24e356

Please sign in to comment.