-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs-move_test.go
357 lines (332 loc) Β· 13 KB
/
fs-move_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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package nef_test
import (
"fmt"
. "github.com/onsi/ginkgo/v2" //nolint:revive // ok
. "github.com/onsi/gomega" //nolint:revive // ok
nef "github.com/snivilised/nefilim"
lab "github.com/snivilised/nefilim/internal/laboratory"
"github.com/snivilised/nefilim/test/luna"
)
// Note [clash/no-clash]: when an item is moved to the destination, clash
// refers to the scenario where the source already exists in the destination,
// whereas no-clash is the opposite.
//
// Eg, if moving src/foo.txt to dest/foo.txt, a clash scenario means foo.txt
// already exists in dest/ and no clash where it doesn't. The success of the
// operation depends on wether the overwrite flag has been specified in the
// filesystem.
var _ = Describe("op: move", Ordered, func() {
var (
root string
fS nef.UniversalFS
)
BeforeAll(func() {
root = luna.Repo("test")
})
DescribeTable("fs: UniversalFS",
func(entry fsTE[nef.UniversalFS]) {
for _, overwrite := range []bool{false, true} {
scratch(root)
fS = nef.NewUniversalFS(nef.Rel{
Root: root,
Overwrite: overwrite,
})
Expect(fS.IsRelative()).To(BeTrue())
entry.overwrite = overwrite
if entry.arrange != nil {
entry.arrange(entry, fS)
}
entry.action(entry, fS)
}
},
func(entry fsTE[nef.UniversalFS]) string {
return fmt.Sprintf("π§ͺ ===> given: target is '%v', %v should: '%v'",
entry.given, entry.op, entry.should,
)
},
// π
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] directory exists, [no-clash]",
should: "succeed",
note: "filename not included in the destination path (from/file.txt => to)",
op: "Move",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Move.From.File,
to: lab.Static.FS.Scratch,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require, entry.from)).To(Succeed())
Expect(require(root, lab.Static.FS.Move.Destination)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
Expect(fS.Move(entry.from, lab.Static.FS.Move.Destination)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expect(luna.AsFile(lab.Static.FS.Move.To.File)).To(luna.ExistInFS(fS))
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] directory exists, [clash]",
should: "succeed, only if override",
note: "filename not included in the destination path (from/file.txt => to)",
op: "Move",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Move.From.File,
to: lab.Static.FS.Scratch,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root,
entry.require,
entry.from,
)).To(Succeed())
Expect(require(root,
lab.Static.FS.Move.Destination,
lab.Static.FS.Move.To.File,
)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
if entry.overwrite {
Expect(fS.Move(entry.from, lab.Static.FS.Move.Destination)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expect(luna.AsFile(lab.Static.FS.Move.To.File)).To(luna.ExistInFS(fS))
return
}
Expect(fS.Move(entry.from, lab.Static.FS.Move.Destination)).NotTo(Succeed())
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] directory exists, [no-clash]",
should: "succeed",
note: "filename IS included in the destination path (from/file.txt => to/file.txt)",
op: "Move",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Move.From.File,
to: lab.Static.FS.Scratch,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require, entry.from)).To(Succeed())
Expect(require(root, lab.Static.FS.Move.Destination)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
destination := lab.Static.FS.Move.To.File
Expect(fS.Move(entry.from, destination)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expect(luna.AsFile(destination)).To(luna.ExistInFS(fS))
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] directory exists, [clash]",
should: "succeed, only if override",
note: "filename IS included in the destination path (from/file.txt => to/file.txt)",
op: "Move",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Move.From.File,
to: lab.Static.FS.Scratch,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require, entry.from)).To(Succeed())
if entry.overwrite {
Expect(require(root, lab.Static.FS.Move.Destination)).To(Succeed())
return
}
Expect(require(root,
lab.Static.FS.Move.Destination,
lab.Static.FS.Move.To.File,
)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
destination := lab.Static.FS.Move.To.File
if entry.overwrite {
Expect(fS.Move(entry.from, destination)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expect(luna.AsFile(destination)).To(luna.ExistInFS(fS))
return
}
Expect(fS.Move(entry.from, destination)).NotTo(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] directory exists, [to] directory exists, [no clash]",
should: "succeed",
note: "directory not included in the destination path (from/dir => to)",
op: "Move",
require: lab.Static.FS.Move.From.Directory,
from: lab.Static.FS.Move.From.Directory,
to: lab.Static.FS.Scratch,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require)).To(Succeed())
Expect(require(root, lab.Static.FS.Move.Destination)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
Expect(fS.Move(entry.from, lab.Static.FS.Move.Destination)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expect(luna.AsDirectory(lab.Static.FS.Move.To.Directory)).To(luna.ExistInFS(fS))
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] directory exists, [to] directory exists, [clash]",
should: "fail",
note: "directory not included in the destination path (from/dir => to)",
op: "Move",
require: lab.Static.FS.Move.From.Directory,
from: lab.Static.FS.Move.From.Directory,
to: lab.Static.FS.Scratch,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require)).To(Succeed())
Expect(require(root, lab.Static.FS.Move.Destination)).To(Succeed())
Expect(require(root, lab.Static.FS.Move.To.Directory)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
if entry.overwrite {
Expect(fS.Move(entry.from, lab.Static.FS.Move.Destination)).NotTo(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
return
}
Expect(fS.Move(entry.from, lab.Static.FS.Move.Destination)).NotTo(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] directory exists, [to] directory exists, [no clash]",
should: "succeed",
note: "directory IS included in the destination path (from/dir => to/dir)",
op: "Move",
require: lab.Static.FS.Move.From.Directory,
from: lab.Static.FS.Move.From.Directory,
to: lab.Static.FS.Scratch,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require)).To(Succeed())
Expect(require(root, lab.Static.FS.Move.Destination)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
destination := lab.Static.FS.Move.To.Directory
Expect(fS.Move(entry.from, destination)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expect(luna.AsDirectory(destination)).To(luna.ExistInFS(fS))
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] directory exists, [to] directory exists, [clash]",
should: "fail",
note: "directory IS included in the destination path (from/dir => to/dir)",
op: "Move",
require: lab.Static.FS.Move.From.Directory,
from: lab.Static.FS.Move.From.Directory,
to: lab.Static.FS.Scratch,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require)).To(Succeed())
Expect(require(root, lab.Static.FS.Move.Destination)).To(Succeed())
Expect(require(root, lab.Static.FS.Move.To.Directory)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
destination := lab.Static.FS.Move.To.Directory
Expect(fS.Move(entry.from, destination)).NotTo(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file missing",
should: "fail",
op: "Move",
require: lab.Static.FS.Scratch,
from: lab.Static.Foo,
to: lab.Static.FS.Scratch,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
err := fS.Move(entry.from, entry.to)
Expect(err).NotTo(Succeed(), fmt.Sprintf("OVERWRITE: %v", entry.overwrite))
Expect(nef.IsBinaryFsOpError(err)).To(BeTrue())
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] directory missing",
should: "fail",
op: "Move",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Move.From.File,
to: lab.Static.FS.Move.Destination,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require, entry.from)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
destination := fS.Calc().Join(entry.to, lab.Static.Foo)
Expect(fS.Move(entry.from, destination)).NotTo(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
},
}),
// The following tests are a duplicate of those defined for the rename
// operation π , where the target item is being renamed into the same directory,
// but these should be rejected with an error, because this amounts to a
// rename which is not the intended use of Move. Instead a custom error
// is returned that denotes same directory so that the client can detect this
// and invoke rename, with the same parameters. The exception to this is
// if the from/to names refer to the same file, in which case the Move
// is ignored as a no op.
//
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] name does not exist, [no-clash]",
should: "fail, same directory move, use rename instead",
op: "Move",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Rename.From.File,
to: lab.Static.FS.Rename.To.File,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require, entry.from)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
IsSameDirectoryMoveRejectionError(fS.Move(entry.from, entry.to), entry.should)
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] equal to [from], [clash]",
should: "succeed, ignored",
op: "Move",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Rename.From.File,
to: lab.Static.FS.Rename.From.File,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require, entry.from)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
Expect(fS.Move(entry.from, entry.to)).To(Succeed())
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] directory exists, [to] name does not exist, [no-clash]",
should: "fail, same directory move, use rename instead",
op: "Move",
require: lab.Static.FS.Rename.From.Directory,
from: lab.Static.FS.Rename.From.Directory,
to: lab.Static.FS.Rename.To.Directory,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
IsSameDirectoryMoveRejectionError(fS.Move(entry.from, entry.to), entry.should)
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] directory exists, [to] equal to [from], [clash]",
should: "fail, directory names can't be same",
op: "Move",
require: lab.Static.FS.Rename.From.Directory,
from: lab.Static.FS.Rename.From.Directory,
to: lab.Static.FS.Rename.From.Directory,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
IsSameDirectoryMoveRejectionError(fS.Move(entry.from, entry.to), entry.should)
},
}),
)
})