-
Notifications
You must be signed in to change notification settings - Fork 2
/
fixtures.go
106 lines (100 loc) · 2.34 KB
/
fixtures.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
package fixtures
import (
"bytes"
"errors"
"io"
"os"
"path/filepath"
"runtime"
)
var Strings = []string{
"1ZQheycERGjpTeXgJrzpjmxDxaixVJVcstgKyiwUshRx7AwZAsHtRwGVFgDtQlyVRiMMw618Hr4kbty66v1NN7acAnApEBAz9BfwfQ5kz87aGcaPSeck3F8obdyRPHmA",
"xQr3HS4TmD87DPLMU17gUicZ",
"lWBsiwTmlpA0H5k2nZjz64UM",
"test",
"{\"message\": \"weird, but could happen\"}",
"\n % cat /dev/urandom | strings -n 16\n3 ,V\";u=)gg H(>{d\nhV9+pprKG >|=)IyO\nPvico@rXJ{L/g&g\n b83x)))n5GU+oI*a)\n!__dL`;5IX]/)ro1Pa4\nh;{a3U8\\fRYI}07Ivr]\nCy%JJY]kBMl!)tm \nWo:Y @1|5<FPFF t }\n",
"🤔🤔🤔",
" yikes, leading and trailing spaces!! ",
}
func TestDataDir() (string, error) {
// get path to this source file
_, here, _, ok := runtime.Caller(0)
if !ok {
return "", errors.New("Unable to find test dir: runtime.Caller failed")
}
root, err := filepath.Abs(here)
if err != nil {
return "", err
}
for i := 0; i < 3; i++ {
root = filepath.Dir(root)
}
return filepath.Join(root, "testdata"), nil
}
func cp(src, dst string) error {
// open source
srcHandle, err := os.Open(src)
if err != nil {
return err
}
defer srcHandle.Close()
// open destination
dstHandle, err := os.Create(dst)
if err != nil {
return err
}
defer dstHandle.Close()
// copy
_, err = io.Copy(dstHandle, srcHandle)
if err != nil {
return err
}
return dstHandle.Sync()
}
func compareFiles(paths ...string) (bool, error) {
var err error
n := len(paths)
files := make([]*os.File, n)
for i, path := range paths {
files[i], err = os.Open(path)
defer files[i].Close()
if err != nil {
return false, err
}
}
errs := make([]error, n)
bufs := make([][]byte, n)
for i := range bufs {
bufs[i] = make([]byte, 1024)
}
for {
// read a chunk from all files
for i, file := range files {
_, errs[i] = file.Read(bufs[i])
if errs[i] != nil && errs[i] != io.EOF {
return false, err
}
}
// if some but not all files reached EOF, the files differ
nEofs := 0
for _, err := range errs {
if err == io.EOF {
nEofs += 1
}
}
if !(nEofs == 0 || nEofs == n) {
return false, nil
}
// if any buffers differ from the first, the files differ
for _, buf := range bufs[1:] {
if !bytes.Equal(buf, bufs[0]) {
return false, nil
}
}
// if at this point we have an EOF, we have finished comparing and found no differences
if nEofs == n {
return true, nil
}
}
}