forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
89 lines (77 loc) · 2.34 KB
/
Copy pathmain_test.go
File metadata and controls
89 lines (77 loc) · 2.34 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
package main
import (
"bytes"
"flag"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/require"
)
// To update the golden files:
// make update-golden-files
var updateGoldenFiles = flag.Bool("update", false, "update .golden files")
func TestOutputMatchesGoldenFile(t *testing.T) {
t.Parallel()
for _, name := range []string{
// Sample created via:
// gotestsum --jsonfile ./scripts/ci-report/testdata/gotests.json.sample -- \
// ./agent ./cli ./cli/cliui \
// -count=1 \
// -timeout=5m \
// -parallel=24 \
// -run='^(TestServer|TestAgent_Session|TestGitAuth$|TestPrompt$)'
filepath.Join("testdata", "gotests.json.sample"),
// Sample created via:
// gotestsum --jsonfile ./scripts/ci-report/testdata/gotests-timeout.json.sample -- \
// ./agent -run='^TestAgent_Session' -count=1 -timeout=5m -parallel=24 -timeout=2s
filepath.Join("testdata", "gotests-timeout.json.sample"),
// https://github.com/golang/go/issues/57305
filepath.Join("testdata", "gotests-go-issue-57305.json.sample"),
filepath.Join("testdata", "gotests-go-issue-57305-parallel.json.sample"),
} {
name := name
t.Run(name, func(t *testing.T) {
t.Parallel()
goTests, err := parseGoTestJSON(name)
if err != nil {
t.Fatalf("error parsing gotestsum report: %v", err)
}
rep, err := parseCIReport(goTests)
if err != nil {
t.Fatalf("error parsing ci report: %v", err)
}
var b bytes.Buffer
err = printCIReport(&b, rep)
if err != nil {
t.Fatalf("error printing report: %v", err)
}
goldenFile := filepath.Join("testdata", "ci-report_"+filepath.Base(name)+".golden")
got := b.Bytes()
if updateGoldenFile(t, goldenFile, got) {
return
}
want := readGoldenFile(t, goldenFile)
if runtime.GOOS == "windows" {
want = bytes.ReplaceAll(want, []byte("\r\n"), []byte("\n"))
got = bytes.ReplaceAll(got, []byte("\r\n"), []byte("\n"))
}
require.Equal(t, string(want), string(got))
})
}
}
func readGoldenFile(t *testing.T, name string) []byte {
t.Helper()
b, err := os.ReadFile(name)
require.NoError(t, err, "error reading golden file")
return b
}
func updateGoldenFile(t *testing.T, name string, content []byte) bool {
t.Helper()
if *updateGoldenFiles {
err := os.WriteFile(name, content, 0o600)
require.NoError(t, err, "error updating golden file")
return true
}
return false
}