-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs-change_test.go
260 lines (241 loc) Β· 8.91 KB
/
fs-change_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
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: change", 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] directory exists, [to] directory missing, [no-clash]",
should: "succeed",
op: "Change",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Change.From.Directory,
to: lab.Static.FS.Change.To.Directory,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.require)).To(Succeed())
Expect(require(root, entry.from)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
destination := fS.Calc().Base(entry.to)
Expect(fS.Change(entry.from, destination)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expect(luna.AsDirectory(lab.Static.FS.Change.Destination)).To(luna.ExistInFS(fS))
Expect(luna.AsDirectory(destination)).NotTo(luna.ExistInFS(fS))
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] file missing, [no-clash]",
should: "succeed",
op: "Change",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Change.From.File,
to: lab.Static.FS.Change.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) {
Expect(fS.Change(entry.from, entry.to)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
calc := fS.Calc()
Expect(luna.AsFile(calc.Join(lab.Static.FS.Scratch, entry.to))).To(luna.ExistInFS(fS))
Expect(luna.AsDirectory(entry.to)).NotTo(luna.ExistInFS(fS))
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] file missing, [no-clash]",
should: "succeed",
op: "Change",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Change.From.File,
to: lab.Static.FS.Change.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) {
Expect(fS.Change(entry.from, entry.to)).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
calc := fS.Calc()
file := calc.Join(lab.Static.FS.Scratch, entry.to)
Expect(luna.AsFile(file)).To(luna.ExistInFS(fS))
Expect(luna.AsDirectory(entry.to)).NotTo(luna.ExistInFS(fS))
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] directory exists, [to] directory exists, [clash]",
should: "fail, overwrite/merge not supported",
op: "Change",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Change.From.Directory,
to: lab.Static.FS.Change.To.Directory,
arrange: func(entry fsTE[nef.UniversalFS], _ nef.UniversalFS) {
Expect(require(root, entry.from)).To(Succeed())
Expect(require(root, entry.to)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
destination := fS.Calc().Base(entry.to)
Expect(fS.Change(entry.from, destination)).NotTo(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
luna.IsLinkError(fS.Change(entry.from, destination), entry.should)
Expect(luna.AsDirectory(destination)).NotTo(luna.ExistInFS(fS))
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] file exists [clash]",
should: "succeed, ignored",
op: "Change",
require: lab.Static.FS.Scratch,
from: lab.Static.FS.Change.From.File,
to: lab.Static.FS.Change.To.File,
arrange: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
Expect(require(root,
entry.require,
entry.from,
)).To(Succeed())
Expect(require(root,
entry.require,
fS.Calc().Join(lab.Static.FS.Scratch, entry.to),
)).To(Succeed())
},
action: func(entry fsTE[nef.UniversalFS], fS nef.UniversalFS) {
err := fS.Change(entry.from, entry.to)
if entry.overwrite {
Expect(err).To(Succeed(),
fmt.Sprintf("OVERWRITE: %v", entry.overwrite),
)
Expect(luna.AsFile(entry.from)).NotTo(luna.ExistInFS(fS))
return
}
Expect(nef.IsBinaryFsOpError(err)).To(BeTrue())
Expect(luna.AsFile(entry.from)).To(luna.ExistInFS(fS))
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] path missing",
should: "fail",
op: "Change",
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.Change(entry.from, entry.to)
Expect(err).NotTo(Succeed(), fmt.Sprintf("OVERWRITE: %v", entry.overwrite))
Expect(nef.IsBinaryFsOpError(err)).To(BeTrue())
},
}),
// 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
// Move which is not the intended use of Change.
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] name does not exist, [no-clash]",
should: "fail, [to] path should not include directory path",
op: "Change",
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) {
IsInvalidPathError(
fS.Change(entry.from, entry.to), entry.should,
)
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] file exists, [to] equal to [from], [clash]",
should: "fail, [to] path should not include directory path",
op: "Change",
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) {
IsInvalidPathError(
fS.Change(entry.from, entry.to), entry.should,
)
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] directory exists, [to] name does not exist, [no-clash]",
should: "fail, [to] path should not include directory path",
op: "Change",
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) {
IsInvalidPathError(
fS.Change(entry.from, entry.to), entry.should,
)
},
}),
Entry(nil, fsTE[nef.UniversalFS]{
given: "[from] directory exists, [to] equal to [from], [clash]",
should: "fail, [to] path should not include directory path",
op: "Change",
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) {
IsInvalidPathError(
fS.Change(entry.from, entry.to), entry.should,
)
},
}),
)
})