-
Notifications
You must be signed in to change notification settings - Fork 83
/
assert.go
223 lines (169 loc) · 4.69 KB
/
assert.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
package test
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
"testing"
"github.com/google/go-cmp/cmp"
)
// Assert is modified version of https://github.com/benbjohnson/testing.
// Assert fails the test if the condition is false.
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
tb.Helper()
if !condition {
_, file, line, _ := runtime.Caller(1)
tb.Fatalf("%s:%d: "+msg+"\n", append([]interface{}{filepath.Base(file), line}, v...)...)
}
}
// Ok fails the test if an err is not nil.
func Ok(tb testing.TB, err error) {
tb.Helper()
if err != nil {
_, file, line, _ := runtime.Caller(1)
tb.Fatalf("%s:%d: unexpected error: %s\n", filepath.Base(file), line, err.Error())
}
}
// NotOk fails the test if an err is nil.
func NotOk(tb testing.TB, err error) {
tb.Helper()
if err == nil {
_, file, line, _ := runtime.Caller(1)
tb.Fatalf("%s:%d: expected error, got nothing\n", filepath.Base(file), line)
}
}
// Expected fails if the errors does not match.
func Expected(tb testing.TB, got, want error) {
tb.Helper()
NotOk(tb, got)
if errors.Is(got, want) {
return
}
_, file, line, _ := runtime.Caller(1)
tb.Fatalf("%s:%d: got unexpected error: %v\n", filepath.Base(file), line, got.Error())
}
// Exists fails if the file or directory in the given path does not exist.
func Exists(tb testing.TB, path string) {
tb.Helper()
_, err := os.Lstat(path)
if err != nil {
_, file, line, _ := runtime.Caller(1)
if os.IsNotExist(err) {
tb.Fatalf("%s:%d: should exists: %s\n", filepath.Base(file), line, err.Error())
}
}
}
// Equals fails the test if want is not equal to got.
func Equals(tb testing.TB, want, got interface{}, v ...interface{}) {
tb.Helper()
if diff := cmp.Diff(want, got); diff != "" {
_, file, line, _ := runtime.Caller(1)
var msg string
if len(v) > 0 {
msg = fmt.Sprintf(v[0].(string), v[1:]...)
}
tb.Fatalf("%s:%d:"+msg+"\n\n\t (-want +got):\n%s", filepath.Base(file), line, diff)
}
}
// nolint:funlen // EqualDirs fails if the contents of given directories are not the same.
func EqualDirs(tb testing.TB, dst string, src string, srcs []string) {
tb.Helper()
srcList := []string{}
for _, s := range srcs {
if isDir(s) {
paths, err := expand(s)
if err != nil {
tb.Fatalf("expand %s: %v\n", s, err)
}
srcList = append(srcList, paths...)
continue
}
srcList = append(srcList, s)
}
dstList, err := expand(dst)
if err != nil {
tb.Fatalf("expand %s: %v\n", dst, err)
}
sort.Strings(srcList)
sort.Strings(dstList)
relSrcList, err := relative(src, srcList)
if err != nil {
tb.Fatalf("relative %s: %v\n", src, err)
}
relDstList, err := relative(dst, dstList)
if err != nil {
tb.Fatalf("relative %s: %v\n", dst, err)
}
Equals(tb, relSrcList, relDstList)
_, file, line, _ := runtime.Caller(1)
for i := 0; i < len(srcList); i++ {
src := srcList[i]
dst := dstList[i]
if isSymlink(src) && isSymlink(dst) {
src, err = os.Readlink(src)
if err != nil {
tb.Fatalf("%s:%d: unexpected error, src path, link <%s>: %s\n",
filepath.Base(file), line, src, err.Error())
}
dst, err = os.Readlink(dst)
if err != nil {
tb.Fatalf("%s:%d: unexpected error, dst path, link <%s>: %s\n",
filepath.Base(file), line, dst, err.Error())
}
}
wContent, err := ioutil.ReadFile(src)
if err != nil {
tb.Fatalf("%s:%d: unexpected error, src path <%s>: %s\n", filepath.Base(file), line, srcList[i], err.Error())
}
gContent, err := ioutil.ReadFile(dst)
if err != nil {
tb.Fatalf("%s:%d: unexpected error, dst path <%s>: %s\n",
filepath.Base(file), line, dstList[i], err.Error())
}
Equals(tb, wContent, gContent)
}
}
// Helpers
func isDir(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return false
}
return fi.IsDir()
}
func isSymlink(path string) bool {
fi, err := os.Lstat(path)
return err == nil && fi.Mode()&os.ModeSymlink != 0
}
func expand(src string) ([]string, error) {
paths := []string{}
if err := filepath.Walk(src, func(path string, fi os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("walk %q: %v", path, err)
}
if fi.IsDir() {
return nil
}
paths = append(paths, path)
return nil
}); err != nil {
return nil, fmt.Errorf("walking the path %q: %v", src, err)
}
return paths, nil
}
func relative(top string, paths []string) ([]string, error) {
result := make([]string, len(paths))
for _, p := range paths {
name := filepath.Base(p)
rel, err := filepath.Rel(top, filepath.Dir(p))
if err != nil {
return []string{}, fmt.Errorf("relative path %q: %q %v", p, rel, err)
}
name = filepath.Join(filepath.ToSlash(rel), name)
result = append(result, name)
}
return result, nil
}