forked from constabulary/gb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
213 lines (188 loc) · 5.46 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cmd
import (
"fmt"
"go/build"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
"github.com/constabulary/gb"
)
// Test returns a Target representing the result of compiling the
// package pkg, and its dependencies, and linking it with the
// test runner.
func Test(flags []string, pkgs ...*gb.Package) error {
test, err := TestPackages(flags, pkgs...)
if err != nil {
return err
}
return gb.Execute(test)
}
// TestPackages produces a graph of Actions that when executed build
// and test the supplied packages.
func TestPackages(flags []string, pkgs ...*gb.Package) (*gb.Action, error) {
if len(pkgs) < 1 {
return nil, fmt.Errorf("no test packages provided")
}
targets := make(map[string]*gb.Action) // maps package import paths to their test run action
names := func(pkgs []*gb.Package) []string {
var names []string
for _, pkg := range pkgs {
names = append(names, pkg.ImportPath)
}
return names
}
// create top level test action to root all test actions
t0 := time.Now()
test := gb.Action{
Name: fmt.Sprintf("test: %s", strings.Join(names(pkgs), ",")),
Task: gb.TaskFn(func() error {
gb.Debugf("test duration: %v %v", time.Since(t0), pkgs[0].Statistics.String())
return nil
}),
}
for _, pkg := range pkgs {
a, err := TestPackage(targets, pkg, flags)
if err != nil {
return nil, err
}
if a == nil {
// nothing to do ?? not even a test action ?
continue
}
test.Deps = append(test.Deps, a)
}
return &test, nil
}
// TestPackage returns an Action representing the steps required to build
// and test this Package.
func TestPackage(targets map[string]*gb.Action, pkg *gb.Package, flags []string) (*gb.Action, error) {
var gofiles []string
gofiles = append(gofiles, pkg.GoFiles...)
gofiles = append(gofiles, pkg.TestGoFiles...)
var cgofiles []string
cgofiles = append(cgofiles, pkg.CgoFiles...)
var imports []string
imports = append(imports, pkg.Package.Imports...)
imports = append(imports, pkg.Package.TestImports...)
name := pkg.Name
if name == "main" {
// rename the main package to its package name for testing.
name = filepath.Base(filepath.FromSlash(pkg.ImportPath))
}
// internal tests
testpkg := gb.NewPackage(pkg.Context, &build.Package{
Name: name,
ImportPath: pkg.ImportPath,
Dir: pkg.Dir,
SrcRoot: pkg.SrcRoot,
GoFiles: gofiles,
CFiles: pkg.CFiles,
CgoFiles: cgofiles,
TestGoFiles: pkg.TestGoFiles, // passed directly to buildTestMain
XTestGoFiles: pkg.XTestGoFiles, // passed directly to buildTestMain
CgoCFLAGS: pkg.CgoCFLAGS,
CgoCPPFLAGS: pkg.CgoCPPFLAGS,
CgoCXXFLAGS: pkg.CgoCXXFLAGS,
CgoLDFLAGS: pkg.CgoLDFLAGS,
CgoPkgConfig: pkg.CgoPkgConfig,
Imports: imports,
})
testpkg.Scope = "test"
testpkg.Stale = true
// build dependencies
deps, err := gb.BuildDependencies(targets, testpkg)
if err != nil {
return nil, err
}
// only build the internal test if there is Go source or
// internal test files.
var testobj *gb.Action
if len(testpkg.GoFiles)+len(testpkg.CgoFiles)+len(testpkg.TestGoFiles) > 0 {
var err error
testobj, err = gb.Compile(testpkg, deps...)
if err != nil {
return nil, err
}
}
// external tests
if len(pkg.XTestGoFiles) > 0 {
xtestpkg := gb.NewPackage(pkg.Context, &build.Package{
Name: name,
ImportPath: pkg.ImportPath + "_test",
Dir: pkg.Dir,
GoFiles: pkg.XTestGoFiles,
Imports: pkg.XTestImports,
})
// build external test dependencies
deps, err := gb.BuildDependencies(targets, xtestpkg)
if err != nil {
return nil, err
}
xtestpkg.Scope = "test"
xtestpkg.Stale = true
xtestpkg.ExtraIncludes = filepath.Join(pkg.Workdir(), filepath.FromSlash(pkg.ImportPath), "_test")
// if there is an internal test object, add it as a dependency.
if testobj != nil {
deps = append(deps, testobj)
}
testobj, err = gb.Compile(xtestpkg, deps...)
if err != nil {
return nil, err
}
}
testmainpkg, err := buildTestMain(testpkg)
if err != nil {
return nil, err
}
testmain, err := gb.Compile(testmainpkg, testobj)
if err != nil {
return nil, err
}
cmd := exec.Command(testmainpkg.Binfile()+".test", flags...)
cmd.Dir = pkg.Dir // tests run in the original source directory
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
gb.Debugf("scheduling run of %v", cmd.Args)
return &gb.Action{
Name: fmt.Sprintf("run: %s", cmd.Args),
Deps: []*gb.Action{testmain},
Task: gb.TaskFn(func() error {
return cmd.Run()
}),
}, nil
}
func buildTestMain(pkg *gb.Package) (*gb.Package, error) {
if pkg.Scope != "test" {
return nil, fmt.Errorf("package %q is not test scoped", pkg.Name)
}
dir := pkg.Objdir()
if err := mkdir(dir); err != nil {
return nil, fmt.Errorf("buildTestmain: %v", err)
}
tests, err := loadTestFuncs(pkg.Package)
if err != nil {
return nil, err
}
if len(pkg.Package.XTestGoFiles) > 0 {
// if there are external tests ensure that we import the
// test package into the final binary for side effects.
tests.ImportXtest = true
}
if err := writeTestmain(filepath.Join(dir, "_testmain.go"), tests); err != nil {
return nil, err
}
testmain := gb.NewPackage(pkg.Context, &build.Package{
Name: pkg.Name,
ImportPath: path.Join(pkg.ImportPath, "testmain"),
Dir: dir,
SrcRoot: pkg.SrcRoot,
GoFiles: []string{"_testmain.go"},
Imports: pkg.Package.Imports,
})
testmain.Scope = "test"
testmain.ExtraIncludes = filepath.Join(pkg.Workdir(), filepath.FromSlash(pkg.ImportPath), "_test")
return testmain, nil
}