-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.go
214 lines (173 loc) · 4.22 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
214
package test
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"golang.org/x/sync/errgroup"
"github.com/urfave/cli"
"jrubin.io/zb/cmd"
"jrubin.io/zb/lib/project"
"jrubin.io/zb/lib/zbcontext"
"jrubin.io/zb/lib/zbtest"
)
// Cmd is the test command
var Cmd cmd.Constructor = &cc{}
// NOTE: inspired by, and much of the code from https://github.com/rsc/gt
type cc struct {
zbtest.ZBTest
List bool
}
func (co *cc) New(*cli.App) cli.Command {
return cli.Command{
Name: "test",
Usage: "test all of the packages in each of the projects and cache the results",
ArgsUsage: "[build/test flags] [packages]",
Action: func(c *cli.Context) error {
ctx := cmd.Context(c)
return co.run(ctx, c.App.Writer, c.Args()...)
},
Flags: append(co.TestFlags(), []cli.Flag{
cli.BoolFlag{
Name: "f",
Destination: &co.Force,
Usage: `
treat all test results as uncached, as does the use of any 'go
test' flag other than -short and -v`,
},
cli.BoolFlag{
Name: "l",
Destination: &co.List,
Usage: "list the uncached tests it would run",
},
}...),
}
}
func (co *cc) run(ctx zbcontext.Context, w io.Writer, args ...string) error {
ctx = co.TestSetup(ctx)
var pkgs, toRun project.Packages
var err error
if ctx.Package {
pkgs, toRun, err = co.runPackages(ctx, w, args...)
} else {
pkgs, toRun, err = co.runProjects(ctx, w, args...)
}
if err != nil {
return err
}
if co.List {
for _, pkg := range toRun {
fmt.Fprintf(w, "%s\n", pkg.ImportPath)
}
return nil
}
return co.runTest(ctx, w, pkgs, toRun)
}
func (co *cc) runPackages(ctx zbcontext.Context, w io.Writer, args ...string) (pkgs, toRun project.Packages, err error) {
pkgs, err = project.ListPackages(ctx, args...)
if err != nil {
return
}
return co.buildPackagesLists(ctx, pkgs)
}
func (co *cc) runProjects(ctx zbcontext.Context, w io.Writer, args ...string) (pkgs, toRun project.Packages, err error) {
var projects project.List
projects, err = project.Projects(ctx, args...)
if err != nil {
return
}
return co.buildProjectsLists(ctx, projects)
}
func (co *cc) buildPackagesLists(ctx zbcontext.Context, in project.Packages) (pkgs, toRun project.Packages, err error) {
for _, pkg := range in {
if pkg.IsVendored {
continue
}
pkgs.Insert(pkg)
var foundResult bool
if foundResult, err = co.HaveResult(ctx, pkg); err != nil {
return
}
if !foundResult {
toRun.Insert(pkg)
}
}
return
}
func (co *cc) buildProjectsLists(ctx zbcontext.Context, projects project.List) (pkgs, toRun project.Packages, err error) {
for _, proj := range projects {
var p, r project.Packages
p, r, err = co.buildPackagesLists(ctx, proj.Packages)
if err != nil {
return
}
pkgs = pkgs.Append(p)
toRun = toRun.Append(r)
}
return
}
func (co *cc) runTest(ctx zbcontext.Context, w io.Writer, pkgs, toRun project.Packages) error {
var ecmd *exec.Cmd
pr, pw := io.Pipe()
if len(toRun) > 0 {
if err := os.MkdirAll(ctx.CacheDir, 0700); err != nil {
return err
}
args := []string{"test"}
args = append(args, co.TestArgs(nil, nil)...)
for _, pkg := range toRun {
args = append(args, pkg.ImportPath)
}
ctx.Logger.Debug(zbcontext.QuoteCommand("→ go", args))
ecmd = exec.Command("go", args...) // nosec
ecmd.Stdout = pw
ecmd.Stderr = pw
if err := ecmd.Start(); err != nil {
return err
}
}
code := zbcontext.ExitOK
var group errgroup.Group
group.Go(func() error {
defer func() { _ = pw.Close() }() // nosec
if ecmd == nil {
return nil
}
ecode, err := zbcontext.ExitCode(ecmd.Wait())
if err != nil {
return err
}
if code == zbcontext.ExitOK {
code = ecode
}
return nil
})
r := bufio.NewReader(io.TeeReader(pr, w))
for _, pkg := range pkgs {
if len(toRun) > 0 && toRun[0] == pkg {
if err := co.ReadResult(ctx, r, pkg); err != nil {
return err
}
toRun = toRun[1:]
} else {
passed, err := co.ShowResult(ctx, w, pkg)
if err != nil {
return err
}
if code == zbcontext.ExitOK && !passed {
code = zbcontext.ExitFailed
}
}
}
if _, err := io.Copy(w, r); err != nil {
return err
}
if err := group.Wait(); err != nil {
return err
}
if code != zbcontext.ExitOK {
return cli.NewExitError("", code)
}
return nil
}