-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassertions_file.go
209 lines (194 loc) · 4.71 KB
/
assertions_file.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
package qac
import (
"crypto/sha1"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/enr/go-files/files"
)
const (
binaryDetectionBytes = 8000 // Same as git
)
func (a *FileAssertion) verify(context planContext) AssertionResult {
result := AssertionResult{
description: fmt.Sprintf(`file %s`, a.Path),
}
fp := a.Path
if a.Extension.isSet() {
fp = fmt.Sprintf(`%s%s`, a.Path, a.Extension.get())
}
actualPath, err := resolvePath(fp, context)
if err != nil {
result.addError(err)
return result
}
fileExists := files.Exists(actualPath)
shouldExist := a.Exists
if shouldExist != fileExists {
err := fmt.Errorf(`file %s exist expected %t but got %t`, actualPath, shouldExist, fileExists)
result.addError(err)
return result
}
if !shouldExist {
return result
}
if a.EqualsTo != "" {
other, err := resolvePath(a.EqualsTo, context)
if err != nil {
result.addError(err)
// block
return result
}
if !files.Exists(other) {
result.addErrorf(`file not found %s`, other)
return result
}
errs := []error{}
if isBinary(actualPath) {
errs = verifyFilesEqualHash(actualPath, other)
} else {
errs = verifyFilesEqualText(actualPath, other)
}
result.addErrors(errs)
}
if a.TextEqualsTo != "" {
exp, err := resolvePath(a.TextEqualsTo, context)
if err != nil {
result.addError(err)
// block
return result
}
if !files.Exists(exp) {
result.addErrorf(`file not found %s`, exp)
return result
}
result.addErrors(verifyFilesEqualText(actualPath, exp))
}
if len(a.ContainsAll) > 0 {
content, err := ioutil.ReadFile(actualPath)
if err != nil {
result.addError(err)
return result
}
cf := string(content)
for _, t := range a.ContainsAll {
if !strings.Contains(cf, t) {
result.addError(fmt.Errorf("%s file\n%s\ndoes not contain:\n%s", actualPath, cf, t))
}
}
}
if len(a.ContainsAny) > 0 {
content, err := ioutil.ReadFile(actualPath)
if err != nil {
result.addError(err)
return result
}
cf := string(content)
// fail := true
// for _, t := range a.ContainsAny {
// if strings.Contains(cf, t) {
// fail = false
// break
// }
// }
if a.failContainsAny(cf) {
result.addError(fmt.Errorf("%s file\n%s\ndoes not contain any of :\n%q", actualPath, cf, a.ContainsAny))
}
}
return result
}
func (a *FileAssertion) failContainsAny(cf string) bool {
fail := true
for _, t := range a.ContainsAny {
if strings.Contains(cf, t) {
fail = false
break
}
}
return fail
}
func verifyFilesEqual(actualPath string, other string) []error {
if isBinary(actualPath) {
return verifyFilesEqualHash(actualPath, other)
}
return verifyFilesEqualText(actualPath, other)
}
func verifyFilesEqualHash(actualPath string, other string) []error {
errs := []error{}
hash1, _ := hash(actualPath)
hash2, _ := hash(other)
if hash1 != hash2 {
errs = append(errs, fmt.Errorf("File %s [%s] differs from\n%s [%s]", actualPath, hash1, other, hash2))
}
return errs
}
func verifyFilesEqualText(actualPath string, exp string) []error {
errs := []error{}
filelines := []string{}
files.EachLine(actualPath, func(line string) error {
filelines = append(filelines, line)
return nil
})
expectedlines := []string{}
files.EachLine(exp, func(line string) error {
expectedlines = append(expectedlines, line)
return nil
})
if len(filelines) != len(expectedlines) {
errs = append(errs, fmt.Errorf("EachLine(%s), expected %d lines but got %d", actualPath, len(expectedlines), len(filelines)))
}
if len(filelines) == 0 || len(expectedlines) == 0 {
// probably a missing/unexpected file
return errs
}
for index, actual := range filelines {
if len(expectedlines) <= index {
errs = append(errs, fmt.Errorf(`unexpected line %d in file %s`, (index+1), actual))
continue
}
expected := expectedlines[index]
if actual != expected {
errs = append(errs, fmt.Errorf(`line %d expected %q but got %q`, (index+1), expected, actual))
}
}
return errs
}
func hash(fullpath string) (string, error) {
fh, err := os.Open(fullpath)
defer fh.Close()
if err != nil {
return "", err
}
h := sha1.New()
io.Copy(h, fh)
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
func isBinary(path string) bool {
file, _ := os.Open(path)
defer file.Close()
return isBinaryFile(file)
}
// isBinary guesses whether a file is binary by reading the first X bytes and seeing if there are any nulls.
// Assumes the file starts seeked the beginning.
func isBinaryFile(file *os.File) bool {
defer file.Seek(0, 0)
buf := make([]byte, binaryDetectionBytes)
for {
n, err := file.Read(buf)
if err != nil && err != io.EOF {
return false
}
if n == 0 {
break
}
for i := 0; i < n; i++ {
if buf[i] == 0x00 {
return true
}
}
buf = buf[n:]
}
return false
}