-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathroveralls.go
323 lines (282 loc) · 6.74 KB
/
roveralls.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
// Copyright (c) 2016 Lawrence Woodman <[email protected]>
// Licensed under an MIT licence. Please see LICENCE.md for details.
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
// This is a horrible kludge so that errors can be tested properly
var program *Program
// Usage is used by flag package if an error occurs when parsing flags
var Usage = func() {
subUsage(program.outErr)
}
func subUsage(out io.Writer) {
fmt.Fprintf(out, usageMsg())
}
func usageMsg() string {
var b bytes.Buffer
const desc = `
roveralls runs coverage tests on a package and all its sub-packages. The
coverage profile is output as a single file called 'roveralls.coverprofile'
for use by tools such as goveralls.
`
fmt.Fprintf(&b, "%s\n", desc)
fmt.Fprintf(&b, "Usage:\n")
program.flagSet.SetOutput(&b)
program.flagSet.PrintDefaults()
program.flagSet.SetOutput(program.outErr)
return b.String()
}
func usagePartialMsg() string {
var b bytes.Buffer
fmt.Fprintf(&b, "Usage:\n")
program.flagSet.SetOutput(&b)
program.flagSet.PrintDefaults()
program.flagSet.SetOutput(program.outErr)
return b.String()
}
const (
defaultIgnores = ".git,vendor"
outFilename = "roveralls.coverprofile"
)
type goTestError struct {
stderr string
stdout string
}
func (e goTestError) Error() string {
return fmt.Sprintf("error from go test: %s\noutput: %s",
e.stderr, e.stdout)
}
type walkingError struct {
dir string
err error
}
func (e walkingError) Error() string {
return fmt.Sprintf("could not walk working directory '%s': %s",
e.dir, e.err)
}
// Program contains the configuration and state of the program
type Program struct {
ignore string
cover string
help bool
short bool
verbose bool
ignores map[string]bool
cmdArgs []string
flagSet *flag.FlagSet
out io.Writer
outErr io.Writer
gopath string
}
func initProgram(
cmdArgs []string,
out io.Writer,
outErr io.Writer,
gopath string,
) {
program = &Program{out: out, outErr: outErr, cmdArgs: cmdArgs, gopath: gopath}
program.initFlagSet()
}
// Run starts the program
func (p *Program) Run() int {
if err := p.flagSet.Parse(p.cmdArgs[1:]); err != nil {
return 1
}
if isProblem := p.handleGOPATH(); isProblem {
return 1
}
if isProblem := p.handleFlags(); isProblem {
return 1
}
if p.help {
subUsage(p.out)
return 0
}
if err := p.testCoverage(); err != nil {
fmt.Fprintf(p.outErr, "\n%s\n", err)
return 1
}
return 0
}
func (p *Program) ignoreDir(relDir string) bool {
_, ignore := p.ignores[relDir]
return ignore
}
func (p *Program) initFlagSet() {
p.flagSet = flag.NewFlagSet("", flag.ContinueOnError)
p.flagSet.SetOutput(p.outErr)
p.flagSet.StringVar(
&p.cover,
"covermode",
"count",
"Mode to run when testing files: `count,set,atomic`",
)
p.flagSet.StringVar(
&p.ignore,
"ignore",
defaultIgnores,
"Comma separated list of directory names to ignore: `dir1,dir2,...`",
)
p.flagSet.BoolVar(&p.verbose, "v", false, "Verbose output")
p.flagSet.BoolVar(
&p.short,
"short",
false,
"Tell long-running tests to shorten their run time",
)
p.flagSet.BoolVar(&p.help, "help", false, "Display this help")
}
// returns true if a problem, else false
func (p *Program) handleGOPATH() bool {
gopath := filepath.Clean(p.gopath)
if p.verbose {
fmt.Fprintln(p.out, "GOPATH:", gopath)
}
if len(gopath) == 0 || gopath == "." {
fmt.Fprintf(p.outErr, "invalid GOPATH '%s'\n", gopath)
return true
}
return false
}
// returns true if a problem, else false
func (p *Program) handleFlags() bool {
validCoverModes := map[string]bool{"set": true, "count": true, "atomic": true}
if _, ok := validCoverModes[p.cover]; !ok {
fmt.Fprintf(p.outErr, "invalid covermode '%s'\n", p.cover)
subUsage(p.outErr)
return true
}
arr := strings.Split(p.ignore, ",")
p.ignores = make(map[string]bool, len(arr))
for _, v := range arr {
p.ignores[v] = true
}
return false
}
var modeRegexp = regexp.MustCompile("mode: [a-z]+\n")
func (p *Program) testCoverage() error {
var buff bytes.Buffer
wd, err := os.Getwd()
if err != nil {
return err
}
if p.verbose {
fmt.Fprintln(p.out, "Working dir:", wd)
}
walker := p.makeWalker(wd, &buff)
if err := filepath.Walk(wd, walker); err != nil {
return walkingError{
dir: wd,
err: err,
}
}
final := buff.String()
final = modeRegexp.ReplaceAllString(final, "")
final = fmt.Sprintf("mode: %s\n%s", p.cover, final)
if err := ioutil.WriteFile(outFilename, []byte(final), 0644); err != nil {
return fmt.Errorf("error writing to: %s, %s", outFilename, err)
}
return nil
}
func (p *Program) makeWalker(
wd string,
buff *bytes.Buffer,
) func(string, os.FileInfo, error) error {
return func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
return nil
}
rel, err := filepath.Rel(wd, path)
if err != nil {
return fmt.Errorf("error creating relative path")
}
if p.ignoreDir(rel) {
return filepath.SkipDir
}
files, err := filepath.Glob(filepath.Join(path, "*_test.go"))
if err != nil {
return fmt.Errorf("error checking for test files")
}
if len(files) == 0 {
if p.verbose {
fmt.Fprintf(p.out, "No Go test files in dir: %s, skipping\n", rel)
}
return nil
}
return p.processDir(wd, path, buff)
}
}
func (p *Program) processDir(wd string, path string, buff *bytes.Buffer) error {
var cmd *exec.Cmd
var cmdOut bytes.Buffer
var cmdErr bytes.Buffer
if err := os.Chdir(path); err != nil {
return err
}
defer os.Chdir(wd)
outDir, err := ioutil.TempDir("", "roveralls")
if err != nil {
return err
}
defer os.RemoveAll(outDir)
if p.verbose {
rel, err := filepath.Rel(wd, path)
if err != nil {
return fmt.Errorf("can't create relative path")
}
fmt.Fprintf(p.out, "Processing dir: %s\n", rel)
if p.short {
fmt.Fprintf(p.out,
"Processing: go test -short -covermode=%s -coverprofile=profile.coverprofile -outputdir=%s\n",
p.cover, outDir)
} else {
fmt.Fprintf(p.out,
"Processing: go test -covermode=%s -coverprofile=profile.coverprofile -outputdir=%s\n",
p.cover, outDir)
}
}
if p.short {
cmd = exec.Command("go",
"test",
"-short",
"-covermode="+p.cover,
"-coverprofile=profile.coverprofile",
"-outputdir="+outDir,
)
} else {
cmd = exec.Command("go",
"test",
"-covermode="+p.cover,
"-coverprofile=profile.coverprofile",
"-outputdir="+outDir,
)
}
cmd.Stdout = &cmdOut
cmd.Stderr = &cmdErr
if err := cmd.Run(); err != nil {
return goTestError{
stderr: cmdErr.String(),
stdout: cmdOut.String(),
}
}
b, err := ioutil.ReadFile(filepath.Join(outDir, "profile.coverprofile"))
if err != nil {
return err
}
_, err = buff.Write(b)
return err
}
func main() {
initProgram(os.Args, os.Stdout, os.Stderr, os.Getenv("GOPATH"))
os.Exit(program.Run())
}