/
main.go
223 lines (205 loc) · 5.17 KB
/
main.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
package main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/fatih/color"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"gotest.tools/gotestsum/testjson"
)
func main() {
name := os.Args[0]
flags, opts := setupFlags(name)
switch err := flags.Parse(os.Args[1:]); {
case err == pflag.ErrHelp:
os.Exit(0)
case err != nil:
log.Error(err.Error())
flags.Usage()
os.Exit(1)
}
opts.args = flags.Args()
setupLogging(opts)
switch err := run(opts).(type) {
case nil:
case *exec.ExitError:
// go test should already report the error to stderr so just exit with
// the same status code
os.Exit(ExitCodeWithDefault(err))
default:
fmt.Fprintln(os.Stderr, name+": Error: "+err.Error())
os.Exit(3)
}
}
func setupFlags(name string) (*pflag.FlagSet, *options) {
opts := &options{}
flags := pflag.NewFlagSet(name, pflag.ContinueOnError)
flags.SetInterspersed(false)
flags.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage:
%s [flags] [--] [go test flags]
Flags:
`, name)
flags.PrintDefaults()
fmt.Fprint(os.Stderr, `
Formats:
dots print a character for each test
short print a line for each package
short-verbose print a line for each test and package
standard-quiet default go test format
standard-verbose default go test -v format
`)
}
flags.BoolVar(&opts.debug, "debug", false, "enabled debug")
flags.StringVarP(&opts.format, "format", "f",
lookEnvWithDefault("GOTESTSUM_FORMAT", "short"),
"print format of test input")
flags.BoolVar(&opts.rawCommand, "raw-command", false,
"don't prepend 'go test -json' to the 'go test' command")
flags.StringVar(&opts.jsonFile, "jsonfile",
lookEnvWithDefault("GOTESTSUM_JSONFILE", ""),
"write all TestEvents to file")
flags.StringVar(&opts.junitFile, "junitfile",
lookEnvWithDefault("GOTESTSUM_JUNITFILE", ""),
"write a JUnit XML file")
flags.BoolVar(&opts.noColor, "no-color", false, "disable color output")
flags.StringSliceVar(&opts.noSummary, "no-summary", nil,
"do not print summary of: failed, skipped, errors")
return flags, opts
}
func lookEnvWithDefault(key, defValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defValue
}
type options struct {
args []string
format string
debug bool
rawCommand bool
jsonFile string
junitFile string
noColor bool
noSummary []string
}
func setupLogging(opts *options) {
if opts.debug {
log.SetLevel(log.DebugLevel)
}
if opts.noColor {
color.NoColor = true
}
}
// TODO: add flag --max-failures
func run(opts *options) error {
ctx := context.Background()
goTestProc, err := startGoTest(ctx, goTestCmdArgs(opts))
if err != nil {
return errors.Wrapf(err, "failed to run %s %s",
goTestProc.cmd.Path,
strings.Join(goTestProc.cmd.Args, " "))
}
defer goTestProc.cancel()
out := os.Stdout
handler, err := newEventHandler(opts, out, os.Stderr)
if err != nil {
return err
}
defer handler.Close() // nolint: errcheck
exec, err := testjson.ScanTestOutput(testjson.ScanConfig{
Stdout: goTestProc.stdout,
Stderr: goTestProc.stderr,
Handler: handler,
})
if err != nil {
return err
}
if err := summarizer(opts)(out, exec); err != nil {
return err
}
if err := writeJUnitFile(opts.junitFile, exec); err != nil {
return err
}
return goTestProc.cmd.Wait()
}
func goTestCmdArgs(opts *options) []string {
args := opts.args
defaultArgs := []string{"go", "test"}
switch {
case opts.rawCommand:
return args
case len(args) == 0:
return append(defaultArgs, "-json", pathFromEnv("./..."))
case !hasJSONArg(args):
defaultArgs = append(defaultArgs, "-json")
}
if testPath := pathFromEnv(""); testPath != "" {
args = append(args, testPath)
}
return append(defaultArgs, args...)
}
func pathFromEnv(defaultPath string) string {
return lookEnvWithDefault("TEST_DIRECTORY", defaultPath)
}
func hasJSONArg(args []string) bool {
for _, arg := range args {
if arg == "-json" || arg == "--json" {
return true
}
}
return false
}
type proc struct {
cmd *exec.Cmd
stdout io.Reader
stderr io.Reader
cancel func()
}
func startGoTest(ctx context.Context, args []string) (proc, error) {
if len(args) == 0 {
return proc{}, errors.New("missing command to run")
}
ctx, cancel := context.WithCancel(ctx)
p := proc{
cmd: exec.CommandContext(ctx, args[0], args[1:]...),
cancel: cancel,
}
log.Debugf("exec: %s", p.cmd.Args)
var err error
p.stdout, err = p.cmd.StdoutPipe()
if err != nil {
return p, err
}
p.stderr, err = p.cmd.StderrPipe()
if err != nil {
return p, err
}
err = p.cmd.Start()
if err == nil {
log.Debugf("go test pid: %d", p.cmd.Process.Pid)
}
return p, err
}
func summarizer(opts *options) func(io.Writer, *testjson.Execution) error {
summary := testjson.SummarizeAll
// TODO: do this in a pflag.Value to validate the string
for _, item := range opts.noSummary {
switch item {
case "failed":
summary -= testjson.SummarizeFailed
case "skipped":
summary -= testjson.SummarizeSkipped
case "errors":
summary -= testjson.SummarizeErrors
}
}
return func(out io.Writer, exec *testjson.Execution) error {
return testjson.PrintSummary(out, exec, summary)
}
}