-
Notifications
You must be signed in to change notification settings - Fork 26
/
overalls.go
414 lines (334 loc) · 9.17 KB
/
overalls.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"go/build"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/yookoala/realpath"
)
const (
helpString = `
usage: overalls -project=[path] -covermode[mode] OPTIONS
overalls recursively traverses your projects directory structure
running 'go test -covermode=count -coverprofile=profile.coverprofile'
in each directory with go test files, concatenates them into one
coverprofile in your root directory named 'overalls.coverprofile'
OPTIONS
-project
Your project path as an absolute path or relative to the '$GOPATH/src' directory
example: -project=github.com/go-playground/overalls
-covermode
Mode to run when testing files.
default:count
OPTIONAL
-go-binary
An alternative 'go' binary to run the tests, for example to use 'richgo' for
more human-friendly output.
example: -go-binary=richgo
default: 'go'
-ignore
A comma separated list of directory names to ignore, relative to project path.
example: -ignore=[.git,.hiddentdir...]
default: '.git,vendor'
-debug
A flag indicating whether to print debug messages.
example: -debug
default:false
-concurrency
Limit the number of packages being processed at one time.
The minimum value must be 2 or more when set.
example: -concurrency=5
default: unlimited
-outfile
In addition to stdout, write all output to this file.
`
)
const (
defaultIgnores = ".git,vendor"
outFilename = "overalls.coverprofile"
pkgFilename = "profile.coverprofile"
)
var (
modeRegex = regexp.MustCompile("mode: [a-z]+\n")
srcPath string
projectPath string
goBinary string
ignoreFlag string
projectFlag string
coverFlag string
outPath string
helpFlag bool
debugFlag bool
concurrencyFlag int
isLimited bool
emptyStruct struct{}
ignores = map[string]struct{}{}
flagArgs []string
outputFunc func(...interface{})
)
func help() {
fmt.Printf(helpString)
}
func init() {
flag.StringVar(&goBinary, "go-binary", "go", "Use an alternative test runner such as 'richgo'")
flag.StringVar(&projectFlag, "project", "", "-project [path]: as an absolute path or relative to the '$GOPATH/src' directory")
flag.StringVar(&coverFlag, "covermode", "count", "Mode to run when testing files")
flag.StringVar(&ignoreFlag, "ignore", defaultIgnores, "-ignore [dir1,dir2...]: comma separated list of directory names to ignore")
flag.StringVar(&outPath, "outfile", "", "write output to this file as well as stdout")
flag.IntVar(&concurrencyFlag, "concurrency", -1, "-concurrency [int]: number of packages to process concurrently, The minimum value must be 2 or more when set.")
flag.BoolVar(&debugFlag, "debug", false, "-debug [true|false]")
flag.BoolVar(&helpFlag, "help", false, "-help")
}
func parseFlags(logger *log.Logger) {
flag.Parse()
if helpFlag {
help()
os.Exit(0)
}
if debugFlag {
fmt.Println("GOPATH:", os.Getenv("GOPATH"))
}
fmt.Println("|", projectFlag)
projectFlag = filepath.Clean(projectFlag)
if debugFlag {
fmt.Println("Project Path:", projectFlag)
}
if len(projectFlag) == 0 || projectFlag == "." {
fmt.Printf("\n**invalid project path '%s'\n", projectFlag)
help()
os.Exit(1)
}
if filepath.IsAbs(projectFlag) {
srcPath = filepath.Dir(projectFlag)
projectFlag = filepath.Base(projectFlag)
} else {
pkg, err := build.Default.Import(projectFlag, "", build.FindOnly)
if err != nil {
fmt.Printf("\n**could not find project path '%s' in GOPATH '%s'\n", projectFlag, os.Getenv("GOPATH"))
os.Exit(1)
}
srcPath = pkg.SrcRoot
}
flagArgs = flag.Args()
switch coverFlag {
case "set", "atomic":
case "count":
for _, flg := range flagArgs {
if flg == "-race" {
logger.Println("\n*****\n** WARNING: some common patterns in parallel code can trigger race conditions when using coverprofile=count and the -race flag; in which case coverprofile=atomic should be used.\n*****")
break
}
}
default:
fmt.Printf("\n**invalid covermode '%s'\n", coverFlag)
os.Exit(1)
}
arr := strings.Split(ignoreFlag, ",")
for _, v := range arr {
ignores[v] = emptyStruct
}
isLimited = concurrencyFlag != -1
if isLimited && concurrencyFlag < 1 {
fmt.Printf("\n**invalid concurrency value '%d', value must be at least 1\n", concurrencyFlag)
os.Exit(1)
}
outputFunc = createLogfunc(logger)
}
func createLogfunc(logger *log.Logger) func(...interface{}) {
if outPath == "" {
return logger.Print
}
fd, err := os.Create(outPath)
if err != nil {
logger.Printf("unable to open %s for writing", outPath)
return logger.Print
}
return func(args ...interface{}) {
logger.Print(args...)
fmt.Fprintln(fd, args...)
}
}
func main() {
logger := log.New(os.Stdout, "", log.LstdFlags)
runMain(logger)
}
func runMain(logger *log.Logger) {
parseFlags(logger)
var err error
var wd string
projectPath = filepath.Join(srcPath, projectFlag)
if err = os.Chdir(projectPath); err != nil {
logger.Printf("\n**invalid project path '%s'\n%s\n", projectFlag, err)
help()
os.Exit(1)
}
if debugFlag {
wd, err = os.Getwd()
if err != nil {
fmt.Println(err)
}
logger.Println("Working DIR:", wd)
}
testFiles(logger)
}
func scanOutput(r io.ReadCloser, fn func(...interface{})) {
defer r.Close()
bs := bufio.NewScanner(r)
for bs.Scan() {
fn(bs.Text())
}
if err := bs.Err(); err != nil {
fn(fmt.Sprintf("Scan error: %v", err.Error()))
}
}
func processDIR(logger *log.Logger, wg *sync.WaitGroup, fullPath, relPath string, out chan<- []byte, semaphore chan struct{}) {
defer wg.Done()
if isLimited {
semaphore <- struct{}{}
}
// 1 for "test", 4 for covermode, coverprofile, outputdir, relpath
args := make([]string, 1, 1+len(flagArgs)+4)
args[0] = "test"
// To split '-- <go test arguments> -args <program arguments>'
for i, arg := range flagArgs {
if arg == "-args" {
args = append(args, flagArgs[:i]...)
flagArgs = flagArgs[i:]
break
}
}
args = append(args, "-covermode="+coverFlag, "-coverprofile="+pkgFilename, "-outputdir="+fullPath+"/", relPath)
args = append(args, flagArgs...)
fmt.Printf("Test args: %+v\n", args)
cmd := exec.Command(goBinary, args...)
if debugFlag {
logger.Println("Processing:", strings.Join(cmd.Args, " "))
}
stdout, err := cmd.StdoutPipe()
if err != nil {
logger.Fatal("Unable to get process stdout")
}
stderr, err := cmd.StderrPipe()
if err != nil {
logger.Fatal("Unable to get process stderr")
}
if err := cmd.Start(); err != nil {
logger.Fatal("ERROR:", err)
}
scanOutput(stdout, outputFunc)
scanOutput(stderr, outputFunc)
if err := cmd.Wait(); err != nil {
logger.Fatal("ERROR:", err)
}
b, err := ioutil.ReadFile(relPath + "/profile.coverprofile")
if err != nil {
logger.Fatal("ERROR:", err)
}
out <- b
if isLimited {
<-semaphore
}
}
// walk is like filepath.Walk, but it follows symlinks and only calls walkFunc on directories.
func walkDirectories(path string, walkFunc func(path string, info os.FileInfo) error) error {
seen := make(map[string]bool)
var walkHelper func(path string) error
walkHelper = func(path string) error {
qualifiedPath, err := realpath.Realpath(path)
if err != nil {
return err
}
// Prevent circular links.
if seen[qualifiedPath] {
return nil
}
seen[qualifiedPath] = true
// Skip anything that isn't a directory.
file, err := os.Stat(path)
if err != nil {
return err
}
if !file.IsDir() {
return nil
}
err = walkFunc(path, file)
if err != nil {
if err == filepath.SkipDir {
return nil
}
return err
}
files, err := ioutil.ReadDir(path)
if err != nil {
return err
}
for _, file := range files {
err = walkHelper(filepath.Join(path, file.Name()))
if err != nil {
return err
}
}
return nil
}
return walkHelper(path)
}
func testFiles(logger *log.Logger) {
var semaphore chan struct{}
if isLimited {
semaphore = make(chan struct{}, concurrencyFlag)
}
out := make(chan []byte)
wg := &sync.WaitGroup{}
walker := func(path string, info os.FileInfo) error {
rel, err := filepath.Rel(projectPath, path)
if err != nil {
logger.Fatalf("Could not make path '%s' relative to project path '%s'", path, projectPath)
}
if _, ignore := ignores[rel]; ignore {
return filepath.SkipDir
}
rel = "./" + rel
if files, err := filepath.Glob(rel + "/*_test.go"); len(files) == 0 || err != nil {
if err != nil {
logger.Fatal("Error checking for test files")
}
if debugFlag {
logger.Println("No Go Test files in DIR:", rel, "skipping")
}
return nil
}
wg.Add(1)
go processDIR(logger, wg, path, rel, out, semaphore)
return nil
}
if err := walkDirectories(projectPath, walker); err != nil {
logger.Fatalf("\n**could not walk project path '%s'\n%s\n", projectPath, err)
}
go func() {
wg.Wait()
close(out)
if isLimited {
close(semaphore)
}
}()
buff := bytes.NewBufferString("")
for cover := range out {
buff.Write(cover)
}
final := buff.String()
final = modeRegex.ReplaceAllString(final, "")
final = "mode: " + coverFlag + "\n" + final
if err := ioutil.WriteFile(outFilename, []byte(final), 0644); err != nil {
logger.Fatal("ERROR Writing \""+outFilename+"\"", err)
}
}