-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.go
352 lines (304 loc) · 7.69 KB
/
utils.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package got
import (
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/fs"
"math/big"
"os"
"path/filepath"
"reflect"
"sync"
"text/template"
"time"
)
// Context helper
type Context struct {
context.Context
Cancel func()
}
// Utils for commonly used methods
type Utils struct {
Testable
}
// Fatal is the same as [testing.common.Fatal]
func (ut Utils) Fatal(args ...interface{}) {
ut.Helper()
ut.Log(args...)
ut.FailNow()
}
// Fatalf is the same as [testing.common.Fatalf]
func (ut Utils) Fatalf(format string, args ...interface{}) {
ut.Helper()
ut.Logf(format, args...)
ut.FailNow()
}
// Log is the same as [testing.common.Log]
func (ut Utils) Log(args ...interface{}) {
ut.Helper()
ut.Logf("%s", fmt.Sprintln(args...))
}
// Error is the same as [testing.common.Error]
func (ut Utils) Error(args ...interface{}) {
ut.Helper()
ut.Log(args...)
ut.Fail()
}
// Errorf is the same as [testing.common.Errorf]
func (ut Utils) Errorf(format string, args ...interface{}) {
ut.Helper()
ut.Logf(format, args...)
ut.Fail()
}
// Skipf is the same as [testing.common.Skipf]
func (ut Utils) Skipf(format string, args ...interface{}) {
ut.Helper()
ut.Logf(format, args...)
ut.SkipNow()
}
// Skip is the same as [testing.common.Skip]
func (ut Utils) Skip(args ...interface{}) {
ut.Helper()
ut.Log(args...)
ut.SkipNow()
}
// Go runs f in a goroutine and wait for it to finish before the test ends.
func (ut Utils) Go(f func()) {
wait := make(chan struct{})
ut.Cleanup(func() { <-wait })
go func() {
f()
wait <- struct{}{}
}()
}
// Run f as a sub-test
func (ut Utils) Run(name string, f func(t G)) bool {
runVal := reflect.ValueOf(ut.Testable).MethodByName("Run")
return runVal.Call([]reflect.Value{
reflect.ValueOf(name),
reflect.MakeFunc(runVal.Type().In(1), func(args []reflect.Value) []reflect.Value {
f(New(args[0].Interface().(Testable)))
return nil
}),
})[0].Interface().(bool)
}
// Parallel is the same as [testing.T.Parallel]
func (ut Utils) Parallel() Utils {
reflect.ValueOf(ut.Testable).MethodByName("Parallel").Call(nil)
return ut
}
// DoAfter d duration if the test is still running
func (ut Utils) DoAfter(d time.Duration, do func()) (cancel func()) {
ctx := ut.Context()
go func() {
ut.Helper()
tmr := time.NewTimer(d)
defer tmr.Stop()
select {
case <-ctx.Done():
case <-tmr.C:
do()
}
}()
return ctx.Cancel
}
// PanicAfter d duration if the test is still running
func (ut Utils) PanicAfter(d time.Duration) (cancel func()) {
return ut.DoAfter(d, func() {
ut.Helper()
panicWithTrace(fmt.Sprintf("%s timeout after %v", ut.Name(), d))
})
}
// Context that will be canceled after the test
func (ut Utils) Context() Context {
ctx, cancel := context.WithCancel(context.Background())
ut.Cleanup(cancel)
return Context{ctx, cancel}
}
// Timeout context that will be canceled after the test
func (ut Utils) Timeout(d time.Duration) Context {
ctx, cancel := context.WithTimeout(context.Background(), d)
ut.Cleanup(cancel)
return Context{ctx, cancel}
}
// RandStr generates a random string with the specified length
func (ut Utils) RandStr(l int) string {
ut.Helper()
b := ut.RandBytes((l + 1) / 2)
return hex.EncodeToString(b)[:l]
}
// RandInt generates a random integer within [min, max)
func (ut Utils) RandInt(min, max int) int {
ut.Helper()
n, err := rand.Int(rand.Reader, big.NewInt(int64(max-min)))
ut.err(err)
return int(n.Int64()) + min
}
// RandBytes generates a random byte array with the specified length
func (ut Utils) RandBytes(l int) []byte {
ut.Helper()
b := make([]byte, l)
_, err := rand.Read(b)
ut.err(err)
return b
}
// Render template. It will use [Utils.Read] to read the value as the template string.
func (ut Utils) Render(value interface{}, data interface{}) *bytes.Buffer {
ut.Helper()
out := bytes.NewBuffer(nil)
t := template.New("")
t, err := t.Parse(ut.Read(value).String())
ut.err(err)
ut.err(t.Execute(out, data))
return out
}
// WriteFile at path with content, it uses [Utils.Open] to open the file.
func (ut Utils) WriteFile(path string, content interface{}) {
f := ut.Open(true, path)
defer func() { ut.err(f.Close()) }()
ut.Write(content)(f)
}
// PathExists checks if path exists
func (ut Utils) PathExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// Chdir is like [os.Chdir] but will restore the dir after test.
func (ut Utils) Chdir(dir string) {
ut.Helper()
cwd, err := os.Getwd()
ut.err(err)
ut.err(os.Chdir(dir))
ut.Cleanup(func() { _ = os.Chdir(cwd) })
}
// Setenv is like [os.Setenv] but will restore the env after test.
func (ut Utils) Setenv(key, value string) {
ut.Helper()
old := os.Getenv(key)
ut.err(os.Setenv(key, value))
ut.Cleanup(func() { _ = os.Setenv(key, old) })
}
// MkdirAll is like [os.MkdirAll] but will remove the dir after test and fail the test if error.
// The default perm is 0755.
func (ut Utils) MkdirAll(perm fs.FileMode, path string) {
if perm == 0 {
perm = 0755
}
dir := filepath.Dir(path)
if !ut.PathExists(dir) {
ut.MkdirAll(perm, dir)
}
if ut.PathExists(path) {
return
}
ut.err(os.Mkdir(path, perm))
ut.Cleanup(func() { _ = os.RemoveAll(path) })
}
// Open a file. Override it if create is true. Directories will be auto-created.
// If the directory and file doesn't exist, it will be removed after the test.
func (ut Utils) Open(create bool, path string) (f *os.File) {
ut.Helper()
var err error
if create {
ut.MkdirAll(0, filepath.Dir(path))
f, err = os.Create(path)
if err == nil {
ut.Cleanup(func() { _ = os.Remove(path) })
}
} else {
f, err = os.Open(path)
}
ut.err(err)
return f
}
// Read all from value. If the value is string and it's a file path,
// the file content will be read, or the string will be returned.
// If the value is [io.Reader], the reader will be read. If the value is []byte, the value will be returned.
// Others will be converted to string and returned.
func (ut Utils) Read(value interface{}) *bytes.Buffer {
ut.Helper()
var r io.Reader
switch v := value.(type) {
case string:
if !ut.PathExists(v) {
return bytes.NewBufferString(v)
}
f := ut.Open(false, v)
defer func() { ut.err(f.Close()) }()
r = f
case io.Reader:
r = v
case []byte:
return bytes.NewBuffer(v)
default:
return bytes.NewBufferString(fmt.Sprintf("%v", v))
}
b := bytes.NewBuffer(nil)
_, err := io.Copy(b, r)
ut.err(err)
return b
}
// JSON from string, []byte, or io.Reader
func (ut Utils) JSON(src interface{}) (v interface{}) {
ut.Helper()
var b []byte
switch obj := src.(type) {
case []byte:
b = obj
case string:
b = []byte(obj)
case io.Reader:
var err error
b, err = io.ReadAll(obj)
ut.err(err)
}
ut.err(json.Unmarshal(b, &v))
return
}
// ToJSON convert obj to JSON bytes
func (ut Utils) ToJSON(obj interface{}) *bytes.Buffer {
ut.Helper()
b, err := json.MarshalIndent(obj, "", " ")
ut.err(err)
return bytes.NewBuffer(b)
}
// ToJSONString convert obj to JSON string
func (ut Utils) ToJSONString(obj interface{}) string {
ut.Helper()
return ut.ToJSON(obj).String()
}
// Write obj to the writer. Encode obj to []byte and cache it for writer.
// If obj is not []byte, string, or [io.Reader], it will be encoded as JSON.
func (ut Utils) Write(obj interface{}) (writer func(io.Writer)) {
lock := sync.Mutex{}
var cache []byte
return func(w io.Writer) {
lock.Lock()
defer lock.Unlock()
ut.Helper()
if cache != nil {
_, err := w.Write(cache)
ut.err(err)
return
}
buf := bytes.NewBuffer(nil)
w = io.MultiWriter(buf, w)
var err error
switch v := obj.(type) {
case []byte:
_, err = w.Write(v)
case string:
_, err = w.Write([]byte(v))
case io.Reader:
_, err = io.Copy(w, v)
default:
err = json.NewEncoder(w).Encode(v)
}
ut.err(err)
cache = buf.Bytes()
}
}