-
Notifications
You must be signed in to change notification settings - Fork 9
/
run.go
235 lines (219 loc) · 4.99 KB
/
run.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package gotesplit
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"math"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/jstemmer/go-junit-report/formatter"
"github.com/jstemmer/go-junit-report/parser"
"golang.org/x/sync/errgroup"
)
func run(ctx context.Context, total, idx uint, junitDir string, argv []string, outStream io.Writer, errStream io.Writer) error {
if idx >= total {
return fmt.Errorf("`index` should be the range from 0 to `total`-1, but: %d (total:%d)", idx, total)
}
l := len(argv)
var (
pkgs []string
testOpts []string
)
for i := 0; i < l; i++ {
pkg := argv[i]
if pkg == "--" {
testOpts = argv[i+1:]
break
}
pkgs = append(pkgs, pkg)
}
if junitDir != "" {
verbose := false
for _, opt := range testOpts {
if strings.HasSuffix(opt, "-v") {
verbose = true
}
if strings.HasSuffix(opt, "-json") {
return fmt.Errorf("-json output and -junitDir cannot be specified at the same time")
}
}
if !verbose {
testOpts = append([]string{"-v"}, testOpts...)
}
}
testLists, err := getTestListsFromPkgs(pkgs)
if err != nil {
return err
}
testListMap := make(map[string]testList, len(testLists))
for _, tl := range testLists {
testListMap[tl.pkg] = tl
}
const delim = ":::"
var testListStrs []string
for _, v := range testLists {
for _, t := range v.list {
testListStrs = append(testListStrs, v.pkg+delim+t)
}
}
testNum := uint(len(testListStrs))
minMemberPerGroup := testNum / total
mod := testNum % total
getOffset := func(i uint) uint {
return minMemberPerGroup*i + uint(math.Min(float64(i), float64(mod)))
}
from := getOffset(idx)
to := getOffset(idx + 1)
var (
targetTests []testList
allPkgs []string
currList testList
)
addList := func(l testList) {
tl := testListMap[l.pkg]
if len(tl.list) == len(l.list) {
allPkgs = append(allPkgs, l.pkg)
} else {
targetTests = append(targetTests, l)
}
}
for _, v := range testListStrs[from:to] {
stuff := strings.Split(v, delim)
pkg := stuff[0]
test := stuff[1]
if pkg != currList.pkg {
if currList.pkg != "" {
addList(currList)
}
currList = testList{pkg: pkg}
}
currList.list = append(currList.list, test)
}
if len(currList.list) > 0 {
addList(currList)
}
if junitDir != "" {
if err := os.MkdirAll(junitDir, 0755); err != nil {
return err
}
}
var testArgsList [][]string
if len(allPkgs) > 0 {
args := append([]string{"test"}, testOpts...)
args = append(args, allPkgs...)
testArgsList = append(testArgsList, args)
}
for _, tl := range targetTests {
args := append([]string{"test"}, testOpts...)
run := "^(?:" + strings.Join(tl.list, "|") + ")$"
args = append(args, "-run", run, tl.pkg)
testArgsList = append(testArgsList, args)
}
for i, args := range testArgsList {
report := goTest(args, outStream, errStream, junitDir)
if err2 := report.err; err2 != nil {
err = err2
}
if report.report != nil {
if report.report.err != nil {
log.Printf("failed to collect test report: %s\n", err)
} else {
fpath := filepath.Join(junitDir, fmt.Sprintf("junit-%d-%d.xml", idx, i))
f, err := os.Create(fpath)
if err != nil {
log.Printf("failed to open file to store test report: %s\n", err)
} else {
defer f.Close()
if err := formatter.JUnitReportXML(report.report.report, false, "", f); err != nil {
log.Printf("failed to store test report: %s\n", err)
}
}
}
}
}
return err
}
type junitReport struct {
report *parser.Report
err error
}
type testReport struct {
err error
report *junitReport
}
func goTest(args []string, stdout, stderr io.Writer, junitDir string) *testReport {
cmd := exec.Command("go", args...)
log.Printf("running following go test:\n%s", cmd.String())
var (
outCloser io.Closer
errCloser io.Closer
outReader io.Reader
errReader io.Reader
outBuf = bytes.NewBuffer(nil)
eg = &errgroup.Group{}
)
if junitDir == "" {
cmd.Stdout = stdout
cmd.Stderr = stderr
} else {
outPipe, err := cmd.StdoutPipe()
if err != nil {
return &testReport{
err: err,
}
}
defer outPipe.Close()
outCloser = outPipe
outReader = io.TeeReader(outPipe, outBuf)
errPipe, err := cmd.StderrPipe()
if err != nil {
return &testReport{
err: err,
}
}
defer errPipe.Close()
errCloser = errPipe
errReader = io.TeeReader(errPipe, outBuf)
}
if err := cmd.Start(); err != nil {
return &testReport{
err: err,
}
}
if junitDir != "" {
eg.Go(func() error {
defer outCloser.Close()
_, err := io.Copy(stdout, outReader)
if err != nil && errors.Is(err, os.ErrClosed) {
err = nil
}
return err
})
eg.Go(func() error {
defer errCloser.Close()
_, err := io.Copy(stderr, errReader)
if err != nil && errors.Is(err, os.ErrClosed) {
err = nil
}
return err
})
}
eg.Go(cmd.Wait)
err := eg.Wait()
ret := &testReport{
err: err,
}
if junitDir != "" {
report, err := parser.Parse(outBuf, "")
ret.report = &junitReport{
report: report,
err: err,
}
}
return ret
}