-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigestmap_test.go
259 lines (246 loc) · 6.52 KB
/
digestmap_test.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
package ocfl_test
import (
"reflect"
"sort"
"testing"
"github.com/srerickson/ocfl-go"
)
var invalidPaths = []string{
"",
".",
"/file1.txt",
"../file1.txt",
"./file.txt",
"dir//file.txt",
"dir/./file.txt",
"dir/../file.txt",
}
var validMaps = map[string]ocfl.DigestMap{
"empty": {},
"single file": {"abcde": {"file.txt"}},
"multiple files": {
"abcde1": {"file.txt", "file2.txt"},
"abcde2": {"nested/directory/file.csv"},
},
}
var invalidMaps = map[string]ocfl.DigestMap{
"missing paths": {
"abcd": {},
},
"duplicate path for same digest": {
"abcd": {"file.txt", "file.txt"},
},
"duplicate path for separate digests": {
"abcd1": {"file.txt"},
"abcd2": {"file.txt"},
},
"directory/file conflict": {
"abcd1": {"a/b"},
"abcd2": {"a/b/file.txt"},
},
"duplicate digests, differenct cases": {
"abcd1": {"file1.txt"},
"ABCD1": {"file2.txt"},
},
}
func testMapValid(t *testing.T, desc string, digests ocfl.DigestMap, expOK bool) {
t.Helper()
t.Run(desc, func(t *testing.T) {
err := digests.Valid()
if err == nil && !expOK {
t.Fatal("invalid map was found to be valid")
}
if err != nil && expOK {
t.Fatalf("valid map was found to be invalid, with error: %s", err)
}
})
}
func TestDigestMapValid(t *testing.T) {
for _, p := range invalidPaths {
desc := "invalid path: " + p
digest := ocfl.DigestMap{"abcd": {p}}
testMapValid(t, desc, digest, false)
}
for desc, digests := range invalidMaps {
testMapValid(t, desc, digests, false)
}
for desc, digests := range validMaps {
testMapValid(t, desc, digests, true)
}
}
func TestMapEq(t *testing.T) {
type eqTest struct {
a ocfl.DigestMap
b ocfl.DigestMap
expect bool
}
eqTests := map[string]eqTest{
"empty maps": {expect: true},
"same": {
a: ocfl.DigestMap{"abc": {"1", "2", "3"}},
b: ocfl.DigestMap{"abc": {"1", "2", "3"}},
expect: true},
"same with mixed case dgiests": {
a: ocfl.DigestMap{"ABC": {"1", "2", "3"}},
b: ocfl.DigestMap{"abc": {"1", "2", "3"}},
expect: true},
"same with different ordered paths": {
a: ocfl.DigestMap{"abc": {"1", "2", "3"}},
b: ocfl.DigestMap{"abc": {"1", "3", "2"}},
expect: true},
"different digests": {
a: ocfl.DigestMap{"abc1": {"1", "2", "3"}},
b: ocfl.DigestMap{"abc2": {"1", "2", "3"}},
expect: false},
"different paths": {
a: ocfl.DigestMap{"abc": {"1", "2"}},
b: ocfl.DigestMap{"abc": {"1", "2", "3"}},
expect: false},
}
for n, eqt := range eqTests {
t.Run(n, func(t *testing.T) {
if eq := eqt.a.Eq(eqt.b); eqt.expect != eq {
t.Errorf("Eq() got=%v, expect=%v", eq, eqt.expect)
}
})
}
}
func TestDigestMapMerge(t *testing.T) {
type mergeTest struct {
m1 ocfl.DigestMap
m2 ocfl.DigestMap
replace bool
resultPaths ocfl.PathMap
isValid bool
}
mergeTests := map[string]mergeTest{
"valid-empty": {
m1: ocfl.DigestMap{},
m2: ocfl.DigestMap{},
replace: false,
resultPaths: ocfl.PathMap{},
isValid: true,
},
"valid-m1-empty": {
m1: ocfl.DigestMap{},
m2: ocfl.DigestMap{"abc1": {"dir/file1"}},
replace: false,
resultPaths: ocfl.PathMap{"dir/file1": "abc1"},
isValid: true,
},
"valid-m2-empty": {
m1: ocfl.DigestMap{"abc1": {"dir/file1"}},
m2: ocfl.DigestMap{},
replace: false,
resultPaths: ocfl.PathMap{"dir/file1": "abc1"},
isValid: true,
},
"valid-mixed-digest": {
m1: ocfl.DigestMap{
"ABC1": {"dir/file1"},
"ABC2": {"dir/file2"},
},
m2: ocfl.DigestMap{"abc1": {"dir/file1"}},
replace: false,
resultPaths: ocfl.PathMap{
"dir/file1": "abc1",
"dir/file2": "abc2",
},
isValid: true,
},
"valid-combine-digest": {
m1: ocfl.DigestMap{"abc1": {"dir/file1"}},
m2: ocfl.DigestMap{"abc1": {"dir/file2"}},
replace: false,
resultPaths: ocfl.PathMap{
"dir/file1": "abc1",
"dir/file2": "abc1",
},
isValid: true,
},
"invalid-noreplace": {
m1: ocfl.DigestMap{"abc1": {"dir/file"}},
m2: ocfl.DigestMap{"abc2": {"dir/file"}},
replace: false,
resultPaths: nil,
isValid: false,
},
"valid-replace": {
m1: ocfl.DigestMap{"abc1": {"dir/file"}},
m2: ocfl.DigestMap{"abc2": {"dir/file"}},
replace: true,
resultPaths: ocfl.PathMap{"dir/file": "abc2"},
isValid: true,
},
"invalid-conflict_existing_file": {
m1: ocfl.DigestMap{"abc1": {"dir/file"}},
m2: ocfl.DigestMap{"abc2": {"dir/file/file"}},
replace: true,
resultPaths: nil,
isValid: false,
},
"invalid-conflict_existing_dir": {
m1: ocfl.DigestMap{"abc1": {"dir/file"}},
m2: ocfl.DigestMap{"abc2": {"dir"}},
replace: true,
resultPaths: nil,
isValid: false,
},
}
for name, mtest := range mergeTests {
t.Run(name, func(t *testing.T) {
result, err := mtest.m1.Merge(mtest.m2, mtest.replace)
if err != nil && mtest.isValid {
t.Error("Merge() returned error for valid case: ", err)
}
if err == nil && !mtest.isValid {
t.Error("Merge() returned no error for invalid case")
}
if mtest.isValid {
resultPaths := result.PathMap()
if !reflect.DeepEqual(resultPaths, mtest.resultPaths) {
t.Errorf("Merge() return unexpcted result.\n got=%v\n expected=%v", resultPaths, mtest.resultPaths)
}
}
})
}
}
func TestDigestMapRemap(t *testing.T) {
t.Run("Rename", func(t *testing.T) {
dm := ocfl.DigestMap{
"abc1": {"dir/file.txt", "delete.txt"},
"abc2": {"file.txt"},
}
dm.Remap(
ocfl.Rename("dir", "new"),
ocfl.Rename("file.txt", "file2.txt"),
ocfl.Remove("delete.txt"),
ocfl.Rename(".", "root"),
)
paths := dm.Paths()
sort.Strings(paths)
expect := []string{"root/file2.txt", "root/new/file.txt"}
if !reflect.DeepEqual(expect, paths) {
t.Fatalf("got %v, expected %v", paths, expect)
}
})
t.Run("Remove", func(t *testing.T) {
// Remove on its own
out := ocfl.Remove("delete.txt")([]string{"delete.txt", "keep.txt"})
if len(out) != 1 || out[0] != "keep.txt" {
t.Error("Remove() didn't remove the expected file")
}
// Remap with Remove
dm := ocfl.DigestMap{
"abc1": {"keep.txt", "delete.txt"},
"abc2": {"save.txt"},
}
dm.Remap(ocfl.Remove("delete.txt"))
if dm.GetDigest("delete.txt") != "" {
t.Error("Remap() with Remove() file not removed")
}
if dm.GetDigest("keep.txt") == "" || dm.GetDigest("save.txt") == "" {
t.Error("Remap() with Remove() removed a file it shouldn't have")
}
})
}