-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
194 lines (160 loc) · 3.86 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
package main
import (
"bufio"
"bytes"
"context"
_ "embed"
"errors"
"flag"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
"os/signal"
"syscall"
"time"
"github.com/lmittmann/tint"
"github.com/muesli/cancelreader"
)
//go:embed node_modules/plotly.js-dist/plotly.js
var plotly string
var debug bool
var dontPassOutput bool
var testDurationCutoff string
var testDurationCutoffDuration time.Duration
var printHTML bool
var keepRunning bool
var fromFile string
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
flag.BoolVar(&debug, "debug", false, "enable debug mode")
flag.BoolVar(&dontPassOutput, "dont-pass-output", false, "don't print output received to stdin")
flag.BoolVar(&keepRunning, "keep-running", false, "keep browser running after page was opened")
flag.BoolVar(&printHTML, "print-html", false, "print html to stdout instead of opening browser")
flag.StringVar(&fromFile, "from-file", "", "read input from file instead of stdin")
flag.StringVar(
&testDurationCutoff,
"duration-cutoff",
"100µs",
"threshold for test duration cutoff, under which tests are not shown in the chart",
)
flag.Parse()
logLevel := slog.LevelInfo
if debug {
logLevel = slog.LevelDebug
}
var err error
testDurationCutoffDuration, err = time.ParseDuration(testDurationCutoff)
if err != nil {
panic(err)
}
slog.SetDefault(slog.New(
tint.NewHandler(os.Stderr, &tint.Options{
Level: logLevel,
TimeFormat: time.Kitchen,
}),
))
r, cleanup, exitCode, done := newReader(ctx)
if !done {
return
}
defer cleanup()
scanner := bufio.NewScanner(r)
result := Parse(scanner)
if checkClosing(ctx) {
return
}
if printHTML {
charts := generateCharts(result)
html, err := render(result, charts, false)
if err != nil {
slog.Error("Error rendering html", "err", err)
return
}
_, _ = os.Stdout.Write([]byte(html))
} else {
serveHTML(ctx, result)
}
if exitCode != 0 {
os.Exit(exitCode)
}
if result.Failed {
os.Exit(1)
}
}
func newReader(ctx context.Context) (io.Reader, func(), int, bool) {
fi, err := os.Stdin.Stat()
if err != nil {
slog.Error("Error getting stdin stat", "err", err)
return nil, nil, 0, false
}
isPipe := (fi.Mode() & os.ModeCharDevice) == 0
readFromFile := fromFile != ""
if isPipe && readFromFile {
slog.Error("Can't read from file and stdin at the same time")
return nil, nil, 0, false
}
if readFromFile {
f, err := os.Open(fromFile)
if err != nil {
slog.Error("Error opening file", "err", err)
return nil, nil, 0, false
}
return f, func() {
_ = f.Close()
}, 0, true
}
if isPipe {
sr, err := cancelreader.NewReader(os.Stdin)
if err != nil {
slog.Error("Error creating cancel reader", "err", err)
return nil, nil, 0, false
}
go func() {
<-ctx.Done()
sr.Cancel()
}()
return sr, func() {}, 0, true
}
r := bytes.NewBuffer([]byte{})
command := append([]string{"go", "test", "-json"}, flag.Args()...)
slog.Info("Running go test", "command", command)
cmd := exec.Command(command[0], command[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = io.MultiWriter(r, os.Stdout)
cmd.Stderr = os.Stderr
var exitCode int
err = cmd.Run()
var exitErr *exec.ExitError
if err != nil {
if errors.As(err, &exitErr) {
// this is expected - tests failed
slog.Info("Error running go test", "err", err)
exitCode = exitErr.ExitCode()
} else {
slog.Error("Error running go test", "err", err)
return nil, nil, 0, false
}
}
go func() {
<-ctx.Done()
_ = cmd.Process.Kill()
}()
return r, func() {
_, _ = cmd.Process.Wait()
}, exitCode, true
}
func checkClosing(ctx context.Context) bool {
select {
case <-ctx.Done():
fmt.Println(
`Process closed without input: you should pipe the output of your test command into this program.
For example: go test -json ./... | vgt`,
)
return true
default:
return false
}
}