forked from anchore/quill
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassertions.go
177 lines (155 loc) · 4.67 KB
/
assertions.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
package trait
import (
"encoding/json"
"os"
"regexp"
"strings"
"testing"
"github.com/acarl005/stripansi"
"github.com/stretchr/testify/require"
)
type Assertion func(tb testing.TB, stdout, stderr string, err error)
func AssertFileOutput(tb testing.TB, path string, assertions ...Assertion) Assertion {
tb.Helper()
return func(tb testing.TB, _, stderr string, e error) {
content, err := os.ReadFile(path)
require.NoError(tb, err)
contentStr := string(content)
for _, assertion := range assertions {
// treat the file content as stdout
assertion(tb, contentStr, stderr, e)
}
}
}
func AssertJSONReport(tb testing.TB, stdout, _ string, _ error) {
tb.Helper()
var data interface{}
if err := json.Unmarshal([]byte(stdout), &data); err != nil {
tb.Errorf("expected to find a JSON report, but was unmarshalable: %+v", err)
}
}
func AssertTableReport(tb testing.TB, stdout, _ string, _ error) {
tb.Helper()
for _, c := range []string{"┌", "└", "┐", "┘"} {
if !strings.Contains(stdout, c) {
tb.Errorf("expected to find a table report, but did not")
}
}
}
func AssertStdoutRowContains(strs ...string) Assertion {
return func(tb testing.TB, stdout, _ string, _ error) {
tb.Helper()
nextLine:
for _, line := range strings.Split(stdout, "\n") {
for _, s := range strs {
if !strings.Contains(line, s) {
continue nextLine
}
}
// we've found all target strings, bail!
return
}
tb.Errorf("could not find a single row with all elements: %s", strs)
}
}
func AssertStderrRowContains(strs ...string) Assertion {
return func(tb testing.TB, _, stderr string, _ error) {
tb.Helper()
nextLine:
for _, line := range strings.Split(stderr, "\n") {
for _, s := range strs {
if !strings.Contains(line, s) {
continue nextLine
}
}
// we've found all target strings, bail!
return
}
tb.Errorf("could not find a single row with all elements: %s", strs)
}
}
func AssertLoggingLevel(level string) Assertion {
// match examples:
// "[0000] INFO"
// "[0012] DEBUG"
logPattern := regexp.MustCompile(`(?m)^\[\d\d\d\d\]\s+` + strings.ToUpper(level))
return func(tb testing.TB, _, stderr string, _ error) {
tb.Helper()
if !logPattern.MatchString(stripansi.Strip(stderr)) {
tb.Errorf("output did not indicate the %q logging level", level)
}
}
}
func AssertNotInOutput(data string) Assertion {
return func(tb testing.TB, stdout, stderr string, _ error) {
tb.Helper()
if strings.Contains(stripansi.Strip(stderr), data) {
tb.Errorf("data=%q was found in stderr, but should not have been there", data)
}
if strings.Contains(stripansi.Strip(stdout), data) {
tb.Errorf("data=%q was found in stdout, but should not have been there", data)
}
}
}
func AssertInOutput(data string) Assertion {
return func(tb testing.TB, stdout, stderr string, _ error) {
tb.Helper()
if !strings.Contains(stripansi.Strip(stderr), data) && !strings.Contains(stripansi.Strip(stdout), data) {
tb.Errorf("data=%q was NOT found in any output, but should have been there", data)
}
}
}
func AssertRegexInOutput(re *regexp.Regexp) Assertion {
return func(tb testing.TB, stdout, stderr string, _ error) {
tb.Helper()
stderrBy := []byte(stripansi.Strip(stderr))
stdoutBy := []byte(stripansi.Strip(stdout))
if !re.Match(stderrBy) && !re.Match(stdoutBy) {
tb.Errorf("regexp=%q was NOT found in any output, but should have been there", re.String())
}
}
}
func AssertInStdout(data string) Assertion {
return func(tb testing.TB, stdout, _ string, _ error) {
tb.Helper()
if !strings.Contains(stripansi.Strip(stdout), data) {
tb.Errorf("data=%q was NOT found in stdout, but should have been there", data)
}
}
}
func AssertInStderr(data string) Assertion {
return func(tb testing.TB, _, stderr string, _ error) {
tb.Helper()
if !strings.Contains(stripansi.Strip(stderr), data) {
tb.Errorf("data=%q was NOT found in stderr, but should have been there", data)
}
}
}
func AssertStdoutLengthGreaterThan(length uint) Assertion {
return func(tb testing.TB, stdout, _ string, _ error) {
tb.Helper()
if uint(len(stdout)) < length {
tb.Errorf("not enough output (expected at least %d, got %d)", length, len(stdout))
}
}
}
func AssertFailingReturnCode(tb testing.TB, _, _ string, e error) {
tb.Helper()
if e == nil {
tb.Error("expected a failure but got none")
}
}
func AssertSuccessfulReturnCode(tb testing.TB, _, _ string, e error) {
tb.Helper()
if e != nil {
tb.Errorf("expected no failure but got err=%+v", e)
}
}
func AssertFileExists(file string) Assertion {
return func(tb testing.TB, _, _ string, _ error) {
tb.Helper()
if _, err := os.Stat(file); err != nil {
tb.Errorf("expected file to exist %s", file)
}
}
}