-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.go
102 lines (81 loc) · 2.36 KB
/
files.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
package testutils
import (
"encoding/json"
"fmt"
"reflect"
"testing"
)
func ExpectFileExists(io FileIo, t *testing.T, filePath string) {
t.Helper()
fi, err := io.Stat(filePath)
ExpectError(t, nil, err)
ExpectBool(t, false, fi.IsDir())
}
func ExpectFileNotExists(io FileIo, t *testing.T, filePath string) {
t.Helper()
fi, _ := io.Stat(filePath)
ExpectValue(t, nil, fi)
}
func ExpectFileJson(io FileIo, t *testing.T, filePath string, data map[string]interface{}) {
t.Helper()
fi, err := io.Stat(filePath)
ExpectError(t, nil, err)
ExpectBool(t, false, fi.IsDir())
content, err := io.ReadFile(filePath)
ExpectError(t, nil, err)
var contentJson map[string]interface{}
err = json.Unmarshal(content, &contentJson)
ExpectError(t, nil, err)
dbg, _ := json.MarshalIndent(contentJson, "", " ")
fmt.Printf("%s\n", string(dbg))
ExpectBool(t, true, reflect.DeepEqual(data, contentJson))
}
func isDataStored(context string, allData interface{}, part interface{}) bool {
if reflect.TypeOf(allData) != reflect.TypeOf(part) {
fmt.Printf("%s has different types\n", context)
return false
}
switch val := part.(type) {
case []interface{}:
allArray := allData.([]interface{})
if len(allArray) != len(val) {
fmt.Printf("%s array lengths are different\n", context)
return false
}
for pos, item := range val {
if !isDataStored(fmt.Sprintf("%s[%d]", context, pos), allArray[pos], item) {
return false
}
}
return true
case map[string]interface{}:
allMap := allData.(map[string]interface{})
for k, v := range val {
allV, exist := allMap[k]
if !exist || !isDataStored(fmt.Sprintf("%s.%s", context, k), allV, v) {
return false
}
}
return true
default:
if !reflect.DeepEqual(allData, part) {
fmt.Printf("%s has different values \"%v\" vs \"%v\"\n", context, allData, part)
return false
}
return true
}
}
func ExpectFileJsonPart(io FileIo, t *testing.T, filePath string, subsetData map[string]interface{}) {
t.Helper()
fi, err := io.Stat(filePath)
ExpectError(t, nil, err)
ExpectBool(t, false, fi.IsDir())
content, err := io.ReadFile(filePath)
ExpectError(t, nil, err)
var fullJson map[string]interface{}
err = json.Unmarshal(content, &fullJson)
ExpectError(t, nil, err)
dbg, _ := json.MarshalIndent(fullJson, "", " ")
fmt.Printf("%s\n", string(dbg))
ExpectBool(t, true, isDataStored("root", fullJson, subsetData))
}