-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilecontext.go
171 lines (144 loc) · 3.31 KB
/
filecontext.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
package shared
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
)
// TestResult is results of a single test
type TestResult struct {
Code string
Success bool
Detail map[string]interface{}
}
// FileContext data about a file being tested
type FileContext struct {
FilePath string
data []byte
//text string
tests []TestResult
}
// IsDir if it is a directory
func (fc *FileContext) IsDir() bool {
fi, statErr := fc.Stat()
if statErr != nil {
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: error doing stat on %s: %s\n", fc.FilePath, statErr.Error())
}
return false
}
if fi.IsDir() {
return true
}
return false
}
// IsFile if it is a file
func (fc *FileContext) IsFile() bool {
fi, statErr := fc.Stat()
if statErr != nil {
if Debug {
fmt.Fprintf(os.Stderr, "DEBUG: error doing stat on %s: %s\n", fc.FilePath, statErr.Error())
}
return false
}
if fi.IsDir() {
return false
}
//LATER: other tests?
return true
}
// Stat os.Stat, possibly cached
func (fc *FileContext) Stat() (os.FileInfo, error) {
return os.Stat(fc.FilePath) // MAYBE: cache?
}
func (fc *FileContext) Size() int64 {
if len(fc.data) > 0 {
return int64(len(fc.data))
}
fi, sizeErr := fc.Stat()
if sizeErr != nil {
fmt.Fprintf(os.Stderr, "ERROR: unable to get size of file '%s' (%s)", fc.FilePath, sizeErr)
os.Exit(4)
}
return fi.Size()
}
// ReadFile ioutil.ReadFile, possibly cached
func (fc *FileContext) ReadFile() ([]byte, error) {
if len(fc.data) > 0 {
return fc.data, nil
}
return ioutil.ReadFile(fc.FilePath)
}
func (fc *FileContext) RecordResult(Code string, Success bool, Detail map[string]interface{}) {
fc.tests = append(fc.tests, TestResult{
Code, Success, Detail,
})
fc.outputResult(Code, Success, Detail)
if failFast && !Success {
os.Exit(1)
}
}
func (fc *FileContext) outputResult(Code string, Success bool, Detail map[string]interface{}) {
if showTests.String() == "none" {
return
}
if Success && showTests.String() == "failing" {
return
}
if OutputFormat.String() == "json" {
testData := map[string]interface{}{
"file": fc.FilePath,
"success": Success,
"test": Code,
}
if showDetail {
testData["detail"] = Detail
}
fmt.Printf("%s\n", EncodeJSON(testData))
} else if OutputFormat.String() == "text" {
fmt.Printf("INFO: %s %s %s", IfThenElse(Success, "PASS", "FAIL"), Code, fc.FilePath)
if showDetail && Detail != nil {
fmt.Printf(" %s", EncodeJSON(Detail))
}
fmt.Printf("\n")
}
}
func (fc *FileContext) Reset() {
fc.tests = nil
}
func (fc *FileContext) Success() bool {
for _, test := range fc.tests {
if !test.Success {
return false
}
}
return true
}
func basicTests(fc *FileContext) {
if fileSize.Exists() {
fc.RecordResult("fileSize", fileSize.Check(uint64(fc.Size())), map[string]interface{}{
"actualSize": fc.Size(),
"desiredSize": fileSize.String(),
})
}
}
func EncodeJSON(data interface{}) string {
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
jsonErr := enc.Encode(data)
if jsonErr != nil {
// can this happen?
return jsonErr.Error()
}
return strings.TrimRight(buf.String(), "\n")
}
// IfThenElse is a substitute for golang missing a ternary operator
func IfThenElse(condition bool, a interface{}, b interface{}) interface{} {
if condition {
return a
}
return b
}