-
Notifications
You must be signed in to change notification settings - Fork 587
/
trait_assertions_test.go
172 lines (150 loc) · 4.67 KB
/
trait_assertions_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
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
package cli
import (
"encoding/json"
"fmt"
"os"
"regexp"
"sort"
"strings"
"testing"
"github.com/acarl005/stripansi"
"github.com/stretchr/testify/require"
)
type traitAssertion func(tb testing.TB, stdout, stderr string, rc int)
func assertFileOutput(tb testing.TB, path string, assertions ...traitAssertion) traitAssertion {
tb.Helper()
return func(tb testing.TB, _, stderr string, rc int) {
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, rc)
}
}
}
func assertJsonReport(tb testing.TB, stdout, _ string, _ int) {
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, _ int) {
tb.Helper()
if !strings.Contains(stdout, "NAME") || !strings.Contains(stdout, "VERSION") || !strings.Contains(stdout, "TYPE") {
tb.Errorf("expected to find a table report, but did not")
}
}
//func assertScope(scope source.Scope) traitAssertion {
// return func(tb testing.TB, stdout, stderr string, rc int) {
// tb.Helper()
// // we can only verify source with the json report
// assertJsonReport(tb, stdout, stderr, rc)
//
// if !strings.Contains(stdout, fmt.Sprintf(`"scope": "%s"`, scope.String())) {
// tb.Errorf("JSON report did not indicate the %q scope", scope)
// }
// }
//}
func assertLoggingLevel(level string) traitAssertion {
// 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, _ int) {
tb.Helper()
if !logPattern.MatchString(stripansi.Strip(stderr)) {
tb.Errorf("output did not indicate the %q logging level", level)
}
}
}
func assertNotInOutput(data string) traitAssertion {
return func(tb testing.TB, stdout, stderr string, _ int) {
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 assertNoStderr(tb testing.TB, _, stderr string, _ int) {
tb.Helper()
if len(stderr) > 0 {
tb.Errorf("expected stderr to be empty, but wasn't")
if showOutput != nil && *showOutput {
tb.Errorf("STDERR:%s", stderr)
}
}
}
func assertInOutput(data string) traitAssertion {
return func(tb testing.TB, stdout, stderr string, _ int) {
tb.Helper()
stdout = stripansi.Strip(stdout)
stderr = stripansi.Strip(stderr)
if !strings.Contains(stdout, data) && !strings.Contains(stderr, data) {
tb.Errorf("data=%q was NOT found in any output, but should have been there", data)
if showOutput != nil && *showOutput {
tb.Errorf("STDOUT:%s\nSTDERR:%s", stdout, stderr)
}
}
}
}
func assertStdoutLengthGreaterThan(length uint) traitAssertion {
return func(tb testing.TB, stdout, _ string, _ int) {
tb.Helper()
if uint(len(stdout)) < length {
tb.Errorf("not enough output (expected at least %d, got %d)", length, len(stdout))
}
}
}
func assertPackageCount(length uint) traitAssertion {
return func(tb testing.TB, stdout, _ string, _ int) {
tb.Helper()
type NameAndVersion struct {
Name string `json:"name"`
Version string `json:"version"`
Type string `json:"type"`
}
type partial struct {
Artifacts []NameAndVersion `json:"artifacts"`
}
var data partial
if err := json.Unmarshal([]byte(stdout), &data); err != nil {
tb.Errorf("expected to find a JSON report, but was unmarshalable: %+v", err)
}
if uint(len(data.Artifacts)) != length {
tb.Errorf("expected package count of %d, but found %d", length, len(data.Artifacts))
debugArtifacts := make([]string, len(data.Artifacts))
for i, a := range data.Artifacts {
debugArtifacts[i] = fmt.Sprintf("%s@%s (%s)", a.Name, a.Version, a.Type)
}
sort.Strings(debugArtifacts)
for i, a := range debugArtifacts {
tb.Errorf("package %d: %s", i+1, a)
}
}
}
}
func assertFailingReturnCode(tb testing.TB, _, _ string, rc int) {
tb.Helper()
if rc == 0 {
tb.Errorf("expected a failure but got rc=%d", rc)
}
}
func assertSuccessfulReturnCode(tb testing.TB, _, _ string, rc int) {
tb.Helper()
if rc != 0 {
tb.Errorf("expected no failure but got rc=%d", rc)
}
}
func assertFileExists(file string) traitAssertion {
return func(tb testing.TB, _, _ string, _ int) {
tb.Helper()
if _, err := os.Stat(file); err != nil {
tb.Errorf("expected file to exist %s", file)
}
}
}