-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathmain.go
More file actions
174 lines (143 loc) · 3.64 KB
/
main.go
File metadata and controls
174 lines (143 loc) · 3.64 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"iter"
"log/slog"
"os"
"runtime/debug"
"sync"
"time"
"github.com/pkg/errors"
)
type config struct {
slowThreshold time.Duration
modName string
verbose bool
stream bool
logDir string
}
func main() {
tmp, err := os.MkdirTemp("", "test2json2gha-")
if err != nil {
panic(err)
}
var cfg config
flag.DurationVar(&cfg.slowThreshold, "slow", 500*time.Millisecond, "Threshold to mark test as slow")
flag.BoolVar(&cfg.verbose, "verbose", false, "Enable verbose output")
flag.BoolVar(&cfg.stream, "stream", false, "Enable streaming output")
flag.StringVar(&cfg.logDir, "logdir", "", "Directory to store all test logs")
flag.Parse()
logger := slog.New(slog.NewTextHandler(os.Stderr, nil))
slog.SetDefault(logger)
info, _ := debug.ReadBuildInfo()
if info != nil {
cfg.modName = info.Main.Path
}
// Set TMPDIR so that [os.CreateTemp] can use an empty string as the dir
// and wind up in our dir.
os.Setenv("TMPDIR", tmp)
cleanup := func() { os.RemoveAll(tmp) } //nolint:errcheck
anyFail, err := do(os.Stdin, os.Stdout, cfg)
cleanup()
if err != nil {
fmt.Fprintf(os.Stderr, "%+v", err)
os.Exit(1)
}
if anyFail {
// In case pipefail is not enabled, make sure we exit non-zero
os.Exit(2)
}
}
func do(in io.Reader, out io.Writer, cfg config) (bool, error) {
dec := json.NewDecoder(in)
results := &resultsHandler{}
defer results.Cleanup()
defer func() {
var wg waitGroup
results.markUnfinishedAsTimeout()
signalTimeout(results.Results())
wg.Go(func() {
var rf ResultsFormatter
rf = &consoleFormatter{modName: cfg.modName, verbose: cfg.verbose}
if err := rf.FormatResults(results.Results(), out); err != nil {
slog.Error("Error writing annotations", "error", err)
}
rf = &errorAnnotationFormatter{}
if err := rf.FormatResults(results.Results(), out); err != nil {
slog.Error("Error writing error annotations", "error", err)
}
})
wg.Go(func() {
summary := getSummaryFile()
formatter := &summaryFormatter{slowThreshold: cfg.slowThreshold}
if err := formatter.FormatResults(results.Results(), getSummaryFile()); err != nil {
slog.Error("Error writing summary", "error", err)
}
summary.Close()
})
wg.Wait()
if cfg.logDir != "" {
results.WriteLogs(cfg.logDir)
}
}()
var anyFailed checkFailed
handlers := []EventHandler{
results,
&anyFailed,
}
if cfg.stream {
handlers = append(handlers, &outputStreamer{out: out})
}
te := &TestEvent{}
for {
*te = TestEvent{} // Reset the event struct to avoid reusing old data
err := dec.Decode(te)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return false, errors.WithStack(err)
}
for _, h := range handlers {
if err := h.HandleEvent(te); err != nil {
slog.Error("Error handling event", "error", err)
}
}
}
return bool(anyFailed), nil
}
// signalTimeout writes test_timeout=true to GITHUB_OUTPUT if any test timed out.
// This allows subsequent CI steps to detect that a timeout occurred.
func signalTimeout(results iter.Seq[*TestResult]) {
ghOutput := os.Getenv("GITHUB_OUTPUT")
if ghOutput == "" {
return
}
for r := range results {
if r.timeout {
f, err := os.OpenFile(ghOutput, os.O_WRONLY|os.O_APPEND, 0)
if err != nil {
slog.Error("Error opening GITHUB_OUTPUT", "error", err)
return
}
if _, err := fmt.Fprintln(f, "test_timeout=true"); err != nil {
slog.Error("Error writing timeout status to GITHUB_OUTPUT", "error", err)
}
f.Close()
return
}
}
}
type waitGroup struct {
sync.WaitGroup
}
func (wg *waitGroup) Go(f func()) {
wg.Add(1)
go func() {
f()
wg.Done()
}()
}