forked from gotestyourself/gotestsum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_e2e_test.go
130 lines (120 loc) · 2.87 KB
/
main_e2e_test.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
package main
import (
"bytes"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"gotest.tools/gotestsum/internal/text"
"gotest.tools/v3/assert"
"gotest.tools/v3/env"
"gotest.tools/v3/fs"
"gotest.tools/v3/golden"
)
func TestE2E_RerunFails(t *testing.T) {
type testCase struct {
name string
args []string
expectedErr string
}
fn := func(t *testing.T, tc testCase) {
tmpFile := fs.NewFile(t, t.Name()+"-seedfile", fs.WithContent("0"))
defer tmpFile.Remove()
envVars := osEnviron()
envVars["TEST_SEEDFILE"] = tmpFile.Path()
defer env.PatchAll(t, envVars)()
flags, opts := setupFlags("gotestsum")
assert.NilError(t, flags.Parse(tc.args))
opts.args = flags.Args()
bufStdout := new(bytes.Buffer)
opts.stdout = bufStdout
bufStderr := new(bytes.Buffer)
opts.stderr = bufStderr
err := run(opts)
if tc.expectedErr != "" {
assert.Error(t, err, tc.expectedErr)
} else {
assert.NilError(t, err)
}
out := text.ProcessLines(t, bufStdout,
text.OpRemoveSummaryLineElapsedTime,
text.OpRemoveTestElapsedTime,
filepath.ToSlash, // for windows
)
golden.Assert(t, out, "e2e/expected/"+expectedFilename(t.Name()))
}
var testCases = []testCase{
{
name: "reruns until success",
args: []string{
"-f=testname",
"--rerun-fails=4",
"--packages=./testdata/e2e/flaky/",
"--", "-count=1", "-tags=testdata",
},
},
{
name: "reruns continues to fail",
args: []string{
"-f=testname",
"--rerun-fails=2",
"--packages=./testdata/e2e/flaky/",
"--", "-count=1", "-tags=testdata",
},
expectedErr: "exit status 1",
},
{
name: "first run has errors, abort rerun",
args: []string{
"-f=testname",
"--rerun-fails=2",
"--packages=./testjson/internal/broken",
"--", "-count=1", "-tags=stubpkg",
},
expectedErr: "rerun aborted because previous run had errors",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if testing.Short() {
t.Skip("too slow for short run")
}
fn(t, tc)
})
}
}
// osEnviron returns os.Environ() as a map, with any GOTESTSUM_ env vars removed
// so that they do not alter the test results.
func osEnviron() map[string]string {
e := env.ToMap(os.Environ())
for k := range e {
if strings.HasPrefix(k, "GOTESTSUM_") {
delete(e, k)
}
}
return e
}
func expectedFilename(name string) string {
ver := runtime.Version()
switch {
case isPreGo114(ver):
return name + "-go1.13"
default:
return name
}
}
// go1.14.6 changed how it prints messages from tests. go1.14.{0-5} used a format
// that was different from both go1.14.6 and previous versions of Go. These tests
// no longer support that format.
func isPreGo114(ver string) bool {
prefix := "go1.1"
if !strings.HasPrefix(ver, prefix) || len(ver) < len(prefix)+1 {
return false
}
switch ver[len(prefix)] {
case '0', '1', '2', '3':
return true
}
return false
}