This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
archiver_test.go
266 lines (257 loc) · 5.71 KB
/
archiver_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
260
261
262
263
264
265
266
package archiver
import (
"reflect"
"runtime"
"strings"
"testing"
)
func TestTrimTopDir(t *testing.T) {
for _, tc := range []struct {
input string
want string
}{
{input: "a/b/c", want: "b/c"},
{input: "a", want: "a"},
{input: "abc/def", want: "def"},
{input: "/abc/def", want: "def"},
} {
tc := tc
t.Run(tc.input, func(t *testing.T) {
got := trimTopDir(tc.input)
if got != tc.want {
t.Errorf("want: '%s', got: '%s')", tc.want, got)
}
})
}
}
func TestTopDir(t *testing.T) {
for _, tc := range []struct {
input string
want string
}{
{input: "a/b/c", want: "a"},
{input: "a", want: "a"},
{input: "abc/def", want: "abc"},
{input: "/abc/def", want: "abc"},
} {
tc := tc
t.Run(tc.input, func(t *testing.T) {
got := topDir(tc.input)
if got != tc.want {
t.Errorf("want: '%s', got: '%s')", tc.want, got)
}
})
}
}
func TestFileIsIncluded(t *testing.T) {
for i, tc := range []struct {
included []string
candidate string
expect bool
}{
{
included: []string{"a"},
candidate: "a",
expect: true,
},
{
included: []string{"a", "b", "a/b"},
candidate: "b",
expect: true,
},
{
included: []string{"a", "b", "c/d"},
candidate: "c/d/e",
expect: true,
},
{
included: []string{"a"},
candidate: "a/b/c",
expect: true,
},
{
included: []string{"a"},
candidate: "aa/b/c",
expect: false,
},
{
included: []string{"a", "b", "c/d"},
candidate: "b/c",
expect: true,
},
{
included: []string{"a/"},
candidate: "a",
expect: false,
},
{
included: []string{"a/"},
candidate: "a/",
expect: true,
},
{
included: []string{"a"},
candidate: "a/",
expect: true,
},
{
included: []string{"a/b"},
candidate: "a/",
expect: false,
},
} {
actual := fileIsIncluded(tc.included, tc.candidate)
if actual != tc.expect {
t.Errorf("Test %d (included=%v candidate=%v): expected %t but got %t",
i, tc.included, tc.candidate, tc.expect, actual)
}
}
}
func TestSkipList(t *testing.T) {
for i, tc := range []struct {
start skipList
add string
expect skipList
}{
{
start: skipList{"a", "b", "c"},
add: "d",
expect: skipList{"a", "b", "c", "d"},
},
{
start: skipList{"a", "b", "c"},
add: "b",
expect: skipList{"a", "b", "c"},
},
{
start: skipList{"a", "b", "c"},
add: "b/c", // don't add because b implies b/c
expect: skipList{"a", "b", "c"},
},
{
start: skipList{"a", "b", "c"},
add: "b/c/", // effectively same as above
expect: skipList{"a", "b", "c"},
},
{
start: skipList{"a", "b/", "c"},
add: "b", // effectively same as b/
expect: skipList{"a", "b/", "c"},
},
{
start: skipList{"a", "b/c", "c"},
add: "b", // replace b/c because b is broader
expect: skipList{"a", "c", "b"},
},
} {
start := make(skipList, len(tc.start))
copy(start, tc.start)
tc.start.add(tc.add)
if !reflect.DeepEqual(tc.start, tc.expect) {
t.Errorf("Test %d (start=%v add=%v): expected %v but got %v",
i, start, tc.add, tc.expect, tc.start)
}
}
}
func TestNameOnDiskToNameInArchive(t *testing.T) {
for i, tc := range []struct {
windows bool // only run this test on Windows
rootOnDisk string // user says they want to archive this file/folder
nameOnDisk string // the walk encounters a file with this name (with rootOnDisk as a prefix)
rootInArchive string // file should be placed in this dir within the archive (rootInArchive becomes a prefix)
expect string // final filename in archive
}{
{
rootOnDisk: "a",
nameOnDisk: "a/b/c",
rootInArchive: "",
expect: "a/b/c",
},
{
rootOnDisk: "a/b",
nameOnDisk: "a/b/c",
rootInArchive: "",
expect: "b/c",
},
{
rootOnDisk: "a/b/",
nameOnDisk: "a/b/c",
rootInArchive: "",
expect: "c",
},
{
rootOnDisk: "a/b/",
nameOnDisk: "a/b/c",
rootInArchive: ".",
expect: "c",
},
{
rootOnDisk: "a/b/c",
nameOnDisk: "a/b/c",
rootInArchive: "",
expect: "c",
},
{
rootOnDisk: "a/b",
nameOnDisk: "a/b/c",
rootInArchive: "foo",
expect: "foo/c",
},
{
rootOnDisk: "a",
nameOnDisk: "a/b/c",
rootInArchive: "foo",
expect: "foo/b/c",
},
{
rootOnDisk: "a",
nameOnDisk: "a/b/c",
rootInArchive: "foo/",
expect: "foo/a/b/c",
},
{
rootOnDisk: "a/",
nameOnDisk: "a/b/c",
rootInArchive: "foo",
expect: "foo/b/c",
},
{
rootOnDisk: "a/",
nameOnDisk: "a/b/c",
rootInArchive: "foo",
expect: "foo/b/c",
},
{
windows: true,
rootOnDisk: `C:\foo`,
nameOnDisk: `C:\foo\bar`,
rootInArchive: "",
expect: "foo/bar",
},
{
windows: true,
rootOnDisk: `C:\foo`,
nameOnDisk: `C:\foo\bar`,
rootInArchive: "subfolder",
expect: "subfolder/bar",
},
} {
if !strings.HasPrefix(tc.nameOnDisk, tc.rootOnDisk) {
t.Errorf("Test %d: Invalid test case! Filename (on disk) will have rootOnDisk as a prefix according to the fs.WalkDirFunc godoc.", i)
continue
}
if tc.windows && runtime.GOOS != "windows" {
t.Logf("Test %d: Skipping test that is only compatible with Windows", i)
continue
}
if !tc.windows && runtime.GOOS == "windows" {
t.Logf("Test %d: Skipping test that is not compatible with Windows", i)
continue
}
actual := nameOnDiskToNameInArchive(tc.nameOnDisk, tc.rootOnDisk, tc.rootInArchive)
if actual != tc.expect {
t.Errorf("Test %d: Got '%s' but expected '%s' (nameOnDisk=%s rootOnDisk=%s rootInArchive=%s)",
i, actual, tc.expect, tc.nameOnDisk, tc.rootOnDisk, tc.rootInArchive)
}
}
}