Skip to content

Commit 3a58556

Browse files
merged test generation into build
1 parent e352107 commit 3a58556

File tree

3 files changed

+87
-84
lines changed

3 files changed

+87
-84
lines changed

build/build.go

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/fsnotify/fsnotify"
2828
"github.com/gopherjs/gopherjs/compiler"
2929
"github.com/gopherjs/gopherjs/compiler/astutil"
30+
"github.com/gopherjs/gopherjs/internal/testmain"
3031
log "github.com/sirupsen/logrus"
3132

3233
"github.com/neelance/sourcemap"
@@ -686,14 +687,6 @@ func (p PackageData) FileModTime() time.Time {
686687
return newest
687688
}
688689

689-
// InternalBuildContext returns the build context that produced the package.
690-
//
691-
// WARNING: This function is a part of internal API and will be removed in
692-
// future.
693-
func (p *PackageData) InternalBuildContext() *build.Context {
694-
return p.bctx
695-
}
696-
697690
// TestPackage returns a variant of the package with "internal" tests.
698691
func (p *PackageData) TestPackage() *PackageData {
699692
return &PackageData{
@@ -913,11 +906,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, cwd string) erro
913906
})
914907
}
915908

916-
parsed, err := s.BuildPackage(pkg)
917-
if err != nil {
918-
return err
919-
}
920-
archive, err := s.CompilePackage(parsed)
909+
archive, err := s.BuildProject(pkg)
921910
if err != nil {
922911
return err
923912
}
@@ -927,7 +916,73 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, cwd string) erro
927916
return s.WriteCommandPackage(archive, pkgObj)
928917
}
929918

930-
// buildImportPathWithSrcDir builds the package specified by the import path.
919+
func (s *Session) BuildProject(pkg *PackageData) (*compiler.Archive, error) {
920+
var parsed *ParsedPackage
921+
var err error
922+
if pkg.IsTest {
923+
parsed, err = s.buildTestProject(pkg)
924+
} else {
925+
parsed, err = s.buildNonTestProject(pkg)
926+
}
927+
if err != nil {
928+
return nil, err
929+
}
930+
931+
// TODO(grantnelson-wf): At this point we should have all the parsed packages we need to
932+
// compile the whole project. We can perform analysis on the whole project
933+
// at this point to get the propagate flatten, blocking, etc. information
934+
// and check types to get the type info with all the instances for all
935+
// generics in the whole project.
936+
937+
return s.compilePackages(parsed)
938+
}
939+
940+
func (s *Session) buildTestProject(pkg *PackageData) (*ParsedPackage, error) {
941+
tpkg := pkg.TestPackage()
942+
tpkg.Imports = append(tpkg.Imports,
943+
"github.com/gopherjs/gopherjs/js",
944+
"github.com/gopherjs/gopherjs/nosync",
945+
"testing/internal/testdeps",
946+
"runtime",
947+
)
948+
_, err := s.buildPackages(tpkg)
949+
if err != nil {
950+
return nil, err
951+
}
952+
_, err = s.buildPackages(pkg.XTestPackage())
953+
if err != nil {
954+
return nil, err
955+
}
956+
957+
fset := token.NewFileSet()
958+
tests := testmain.TestMain{Package: pkg.Package, Context: pkg.bctx}
959+
tests.Scan(fset)
960+
mainPkg, mainFile, err := tests.Synthesize(fset)
961+
if err != nil {
962+
return nil, fmt.Errorf("failed to generate testmain package for %s: %w", pkg.ImportPath, err)
963+
}
964+
965+
parsed := &ParsedPackage{
966+
ImportPath: mainPkg.ImportPath,
967+
Dir: mainPkg.Dir,
968+
GoFiles: []*ast.File{mainFile},
969+
FileSet: fset,
970+
}
971+
972+
return parsed, nil
973+
}
974+
975+
func (s *Session) buildNonTestProject(pkg *PackageData) (*ParsedPackage, error) {
976+
// Add some packages required by GopherJS as imports.
977+
pkg.Imports = append(pkg.Imports,
978+
"github.com/gopherjs/gopherjs/js",
979+
"github.com/gopherjs/gopherjs/nosync",
980+
"runtime",
981+
)
982+
return s.buildPackages(pkg)
983+
}
984+
985+
// buildImportPathWithSrcDir builds the parsed package specified by the import path.
931986
//
932987
// Relative import paths are interpreted relative to the passed srcDir. If
933988
// srcDir is empty, current working directory is assumed.
@@ -940,7 +995,7 @@ func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*Packag
940995
return nil, nil, err
941996
}
942997

943-
parsed, err := s.recursivelyBuildPackages(pkg)
998+
parsed, err := s.buildPackages(pkg)
944999
if err != nil {
9451000
return nil, nil, err
9461001
}
@@ -974,23 +1029,7 @@ var getExeModTime = func() func() time.Time {
9741029
}
9751030
}()
9761031

977-
func (s *Session) BuildPackage(pkg *PackageData) (*ParsedPackage, error) {
978-
// If testing add in the required test imports into the package.
979-
if pkg.IsTest {
980-
pkg = pkg.TestPackage()
981-
// Add some packages required by GopherJS for tests as imports.
982-
pkg.Imports = append(pkg.Imports,
983-
"testing/internal/testdeps",
984-
)
985-
}
986-
// Add some packages required by GopherJS as imports.
987-
pkg.Imports = append(pkg.Imports,
988-
"github.com/gopherjs/gopherjs/js",
989-
"github.com/gopherjs/gopherjs/nosync")
990-
return s.recursivelyBuildPackages(pkg)
991-
}
992-
993-
func (s *Session) recursivelyBuildPackages(pkg *PackageData) (*ParsedPackage, error) {
1032+
func (s *Session) buildPackages(pkg *PackageData) (*ParsedPackage, error) {
9941033
if parsed, ok := s.ParsedPackages[pkg.ImportPath]; ok {
9951034
return parsed, nil
9961035
}
@@ -1042,7 +1081,7 @@ func (s *Session) recursivelyBuildPackages(pkg *PackageData) (*ParsedPackage, er
10421081
return parsed, nil
10431082
}
10441083

1045-
func (s *Session) CompilePackage(pkg *ParsedPackage) (*compiler.Archive, error) {
1084+
func (s *Session) compilePackages(pkg *ParsedPackage) (*compiler.Archive, error) {
10461085
if archive, ok := s.UpToDateArchives[pkg.ImportPath]; ok {
10471086
return archive, nil
10481087
}
@@ -1082,7 +1121,7 @@ func (s *Session) ImportResolverFor(srcDir string) func(string) (*compiler.Archi
10821121

10831122
// The archive hasn't been compiled yet so compile it with the parsed package.
10841123
if parsed, ok := s.ParsedPackages[path]; ok {
1085-
return s.CompilePackage(parsed)
1124+
return s.compilePackages(parsed)
10861125
}
10871126

10881127
return nil, fmt.Errorf(`parsed package for %q not found`, path)

internal/testmain/testmain.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"go/ast"
8-
gobuild "go/build"
8+
"go/build"
99
"go/doc"
1010
"go/parser"
1111
"go/token"
@@ -16,7 +16,6 @@ import (
1616
"unicode"
1717
"unicode/utf8"
1818

19-
"github.com/gopherjs/gopherjs/build"
2019
"golang.org/x/tools/go/buildutil"
2120
)
2221

@@ -66,7 +65,8 @@ func (ef ExampleFunc) Executable() bool {
6665

6766
// TestMain is a helper type responsible for generation of the test main package.
6867
type TestMain struct {
69-
Package *build.PackageData
68+
Package *build.Package
69+
Context *build.Context
7070
Tests []TestFunc
7171
Benchmarks []TestFunc
7272
Fuzz []TestFunc
@@ -88,7 +88,7 @@ func (tm *TestMain) Scan(fset *token.FileSet) error {
8888
func (tm *TestMain) scanPkg(fset *token.FileSet, files []string, loc FuncLocation) error {
8989
for _, name := range files {
9090
srcPath := path.Join(tm.Package.Dir, name)
91-
f, err := buildutil.OpenFile(tm.Package.InternalBuildContext(), srcPath)
91+
f, err := buildutil.OpenFile(tm.Context, srcPath)
9292
if err != nil {
9393
return fmt.Errorf("failed to open source file %q: %w", srcPath, err)
9494
}
@@ -158,7 +158,7 @@ func (tm *TestMain) scanFile(f *ast.File, loc FuncLocation) error {
158158
}
159159

160160
// Synthesize main package for the tests.
161-
func (tm *TestMain) Synthesize(fset *token.FileSet) (*build.PackageData, *ast.File, error) {
161+
func (tm *TestMain) Synthesize(fset *token.FileSet) (*build.Package, *ast.File, error) {
162162
buf := &bytes.Buffer{}
163163
if err := testmainTmpl.Execute(buf, tm); err != nil {
164164
return nil, nil, fmt.Errorf("failed to generate testmain source for package %s: %w", tm.Package.ImportPath, err)
@@ -167,12 +167,10 @@ func (tm *TestMain) Synthesize(fset *token.FileSet) (*build.PackageData, *ast.Fi
167167
if err != nil {
168168
return nil, nil, fmt.Errorf("failed to parse testmain source for package %s: %w", tm.Package.ImportPath, err)
169169
}
170-
pkg := &build.PackageData{
171-
Package: &gobuild.Package{
172-
ImportPath: tm.Package.ImportPath + ".testmain",
173-
Name: "main",
174-
GoFiles: []string{"_testmain.go"},
175-
},
170+
pkg := &build.Package{
171+
ImportPath: tm.Package.ImportPath + ".testmain",
172+
Name: "main",
173+
GoFiles: []string{"_testmain.go"},
176174
}
177175
return pkg, src, nil
178176
}

tool.go

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"go/ast"
87
"go/build"
98
"go/scanner"
10-
"go/token"
119
"go/types"
1210
"io"
1311
"net"
@@ -29,7 +27,6 @@ import (
2927
"github.com/gopherjs/gopherjs/build/cache"
3028
"github.com/gopherjs/gopherjs/compiler"
3129
"github.com/gopherjs/gopherjs/internal/sysutil"
32-
"github.com/gopherjs/gopherjs/internal/testmain"
3330
"github.com/neelance/sourcemap"
3431
log "github.com/sirupsen/logrus"
3532
"github.com/spf13/cobra"
@@ -147,11 +144,7 @@ func main() {
147144
if err != nil {
148145
return err
149146
}
150-
parsed, err := s.BuildPackage(pkg)
151-
if err != nil {
152-
return err
153-
}
154-
archive, err := s.CompilePackage(parsed)
147+
archive, err := s.BuildProject(pkg)
155148
if err != nil {
156149
return err
157150
}
@@ -218,11 +211,7 @@ func main() {
218211
if err != nil {
219212
return err
220213
}
221-
parsed, err := s.BuildPackage(pkg)
222-
if err != nil {
223-
return err
224-
}
225-
archive, err := s.CompilePackage(parsed)
214+
archive, err := s.BuildProject(pkg)
226215
if err != nil {
227216
return err
228217
}
@@ -378,27 +367,8 @@ func main() {
378367
return err
379368
}
380369

381-
_, err = s.BuildPackage(pkg.TestPackage())
382-
if err != nil {
383-
return err
384-
}
385-
_, err = s.BuildPackage(pkg.XTestPackage())
386-
if err != nil {
387-
return err
388-
}
389-
390-
fset := token.NewFileSet()
391-
tests := testmain.TestMain{Package: pkg}
392-
tests.Scan(fset)
393-
mainPkg, mainFile, err := tests.Synthesize(fset)
394-
if err != nil {
395-
return fmt.Errorf("failed to generate testmain package for %s: %w", pkg.ImportPath, err)
396-
}
397-
importContext := &compiler.ImportContext{
398-
Packages: s.Types,
399-
Import: s.ImportResolverFor(mainPkg.Dir),
400-
}
401-
mainPkgArchive, err := compiler.Compile(mainPkg.ImportPath, []*ast.File{mainFile}, fset, importContext, options.Minify)
370+
pkg.IsTest = true
371+
mainPkgArchive, err := s.BuildProject(pkg)
402372
if err != nil {
403373
return fmt.Errorf("failed to compile testmain package for %s: %w", pkg.ImportPath, err)
404374
}
@@ -671,11 +641,7 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
671641
buf := new(bytes.Buffer)
672642
browserErrors := new(bytes.Buffer)
673643
err := func() error {
674-
parsed, err := s.BuildPackage(pkg)
675-
if err != nil {
676-
return err
677-
}
678-
archive, err := s.CompilePackage(parsed)
644+
archive, err := s.BuildProject(pkg)
679645
if err != nil {
680646
return err
681647
}

0 commit comments

Comments
 (0)