-
Notifications
You must be signed in to change notification settings - Fork 402
/
result.go
224 lines (196 loc) · 5.78 KB
/
result.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package ultest
import (
"sort"
"strings"
"testing"
"github.com/stretchr/testify/require"
"storj.io/storj/cmd/uplink/ulloc"
)
// Result captures all the output of running a command for inspection.
type Result struct {
Stdout string
Stderr string
Ok bool
Err error
Files []File
Pending []File
}
// RequireSuccess fails if the Result did not observe a successful execution.
func (r Result) RequireSuccess(t *testing.T) {
if !r.Ok {
errs := parseErrors(r.Stdout)
require.FailNow(t, "test did not run successfully:",
"%s", strings.Join(errs, "\n"))
}
require.NoError(t, r.Err)
}
// RequireFailure fails if the Result did not observe a failed execution.
func (r Result) RequireFailure(t *testing.T) Result {
require.False(t, r.Ok && r.Err == nil, "command ran with no error")
return r
}
// RequireStdout requires that the execution wrote to stdout the provided string.
// Blank lines are ignored and all lines are space trimmed for the comparison.
func (r Result) RequireStdout(t *testing.T, stdout string) Result {
require.Equal(t, trimNewlineSpaces(stdout), trimNewlineSpaces(r.Stdout))
return r
}
// RequireStdoutGlob requires that the execution wrote to stdout the provided string
// where the * and ? characters are interpreted like shell glob patterns, except
// they do not match newlines.
// Blank lines are ignored and all lines are space trimmed for the comparison.
func (r Result) RequireStdoutGlob(t *testing.T, stdoutPattern string) Result {
pattern := trimNewlineSpaces(stdoutPattern)
expected := trimNewlineSpaces(r.Stdout)
if !globMatch(pattern, expected) {
require.Equal(t, pattern, expected)
}
return r
}
// RequireStderr requires that the execution wrote to stderr the provided string.
// Blank lines are ignored and all lines are space trimmed for the comparison.
func (r Result) RequireStderr(t *testing.T, stderr string) Result {
require.Equal(t, trimNewlineSpaces(stderr), trimNewlineSpaces(r.Stderr))
return r
}
// RequireFiles requires that the set of files provided are all of the files that
// existed at the end of the execution. It assumes any passed in files with no
// contents contain the filename as the contents instead.
func (r Result) RequireFiles(t *testing.T, files ...File) Result {
require.Equal(t, canonicalizeFiles(files), r.Files)
return r
}
// RequirePending requires that the set of files provided are all of the files that
// existed as pending at the end of the execution.
func (r Result) RequirePending(t *testing.T, files ...File) Result {
require.Equal(t, canonicalizePendingFiles(files), r.Pending)
return r
}
// RequireLocalFiles requires that the set of files provided are all of the
// local files that existed at the end of the execution. It assumes any passed
// in files with no contents contain the filename as the contents instead.
func (r Result) RequireLocalFiles(t *testing.T, files ...File) Result {
require.Equal(t, canonicalizeFiles(files), filterFiles(r.Files, fileIsLocal))
return r
}
// RequireRemoteFiles requires that the set of files provided are all of the
// remote files that existed at the end of the execution. It assumes any passed
// in files with no contents contain the filename as the contents instead.
func (r Result) RequireRemoteFiles(t *testing.T, files ...File) Result {
require.Equal(t, canonicalizeFiles(files), filterFiles(r.Files, fileIsRemote))
return r
}
func filterFiles(files []File, match func(File) bool) (out []File) {
for _, file := range files {
if match(file) {
out = append(out, file)
}
}
return out
}
func canonicalizeFiles(files []File) (out []File) {
out = append(out, files...)
sort.Slice(out, func(i, j int) bool { return out[i].less(out[j]) })
for i := range out {
if out[i].Contents == "" {
out[i].Contents = out[i].Loc
}
}
return out
}
func canonicalizePendingFiles(files []File) (out []File) {
out = append(out, files...)
sort.Slice(out, func(i, j int) bool { return out[i].less(out[j]) })
return out
}
func fileIsLocal(file File) bool {
loc, _ := ulloc.Parse(file.Loc)
return loc.Local()
}
func fileIsRemote(file File) bool {
loc, _ := ulloc.Parse(file.Loc)
return loc.Remote()
}
func parseErrors(s string) []string {
lines := strings.Split(s, "\n")
start := 0
for i, line := range lines {
if line == "Errors:" {
start = i + 1
} else if len(line) > 0 && line[0] != ' ' {
return lines[start:i]
}
}
return nil
}
func trimNewlineSpaces(s string) string {
lines := strings.Split(s, "\n")
j := 0
for _, line := range lines {
if trimmed := strings.TrimSpace(line); len(trimmed) > 0 {
lines[j] = trimmed
j++
}
}
return strings.Join(lines[:j], "\n")
}
// globMatch matches each line in pattern with the lines in against.
func globMatch(pattern, against string) bool {
plines, alines := strings.Split(pattern, "\n"), strings.Split(against, "\n")
if len(plines) != len(alines) {
return false
}
for i, pline := range plines {
if !globMatchLine(pline, alines[i]) {
return false
}
}
return true
}
func globMatchLine(pattern, line string) bool {
px, lx := 0, 0
npx, nlx := 0, 0
for px < len(pattern) || lx < len(line) {
if px < len(pattern) {
switch c := pattern[px]; c {
default:
if lx < len(line) && line[lx] == c {
lx++
px++
continue
}
case '?':
if lx < len(line) {
lx++
px++
continue
}
case '*':
npx = px
nlx = lx + 1
px++
continue
}
}
if 0 < nlx && nlx <= len(line) {
px = npx
lx = nlx
continue
}
return false
}
return true
}
// File represents a file existing either locally or remotely.
type File struct {
Loc string
Contents string
Metadata map[string]string
}
func (f File) less(g File) bool {
fl, _ := ulloc.Parse(f.Loc)
gl, _ := ulloc.Parse(g.Loc)
return fl.Less(gl)
}