-
Notifications
You must be signed in to change notification settings - Fork 776
/
Copy pathmove-sync.test.js
353 lines (265 loc) · 10.1 KB
/
move-sync.test.js
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
'use strict'
const fs = require('graceful-fs')
const os = require('os')
const fse = require(process.cwd())
const path = require('path')
const assert = require('assert')
const rimraf = require('rimraf')
/* global afterEach, beforeEach, describe, it */
function createSyncErrFn (errCode) {
const fn = function () {
const err = new Error()
err.code = errCode
throw err
}
return fn
}
const originalRenameSync = fs.renameSync
const originalLinkSync = fs.linkSync
function setUpMockFs (errCode) {
fs.renameSync = createSyncErrFn(errCode)
fs.linkSync = createSyncErrFn(errCode)
}
function tearDownMockFs () {
fs.renameSync = originalRenameSync
fs.linkSync = originalLinkSync
}
describe('moveSync()', () => {
let TEST_DIR
beforeEach(() => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'move-sync')
fse.emptyDirSync(TEST_DIR)
// Create fixtures:
fs.writeFileSync(path.join(TEST_DIR, 'a-file'), 'sonic the hedgehog\n')
fs.mkdirSync(path.join(TEST_DIR, 'a-folder'))
fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-file'), 'tails\n')
fs.mkdirSync(path.join(TEST_DIR, 'a-folder/another-folder'))
fs.writeFileSync(path.join(TEST_DIR, 'a-folder/another-folder/file3'), 'knuckles\n')
})
afterEach(done => rimraf(TEST_DIR, done))
it('should not move if src and dest are the same', () => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/a-file`
fse.moveSync(src, dest)
// assert src not affected
const contents = fs.readFileSync(src, 'utf8')
const expected = /^sonic the hedgehog\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
})
it('should error if src and dest are the same and src does not exist', () => {
const src = `${TEST_DIR}/non-existent`
const dest = src
assert.throws(() => fse.moveSync(src, dest))
})
it('should rename a file on the same device', () => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/a-file-dest`
fse.moveSync(src, dest)
const contents = fs.readFileSync(dest, 'utf8')
const expected = /^sonic the hedgehog\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
})
it('should not overwrite the destination by default', () => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/a-folder/another-file`
// verify file exists already
assert(fs.existsSync(dest))
try {
fse.moveSync(src, dest)
} catch (err) {
assert.ok(err && err.code === 'EEXIST', 'throw EEXIST')
}
})
it('should not overwrite if overwrite = false', () => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/a-folder/another-file`
// verify file exists already
assert(fs.existsSync(dest))
try {
fse.moveSync(src, dest, {overwrite: false})
} catch (err) {
assert.ok(err && err.code === 'EEXIST', 'throw EEXIST')
}
})
it('should overwrite file if overwrite = true', () => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/a-folder/another-file`
// verify file exists already
assert(fs.existsSync(dest))
fse.moveSync(src, dest, {overwrite: true})
const contents = fs.readFileSync(dest, 'utf8')
const expected = /^sonic the hedgehog\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
})
it('should overwrite the destination directory if overwrite = true', function (done) {
// Create src
const src = path.join(TEST_DIR, 'src')
fse.ensureDirSync(src)
fse.mkdirsSync(path.join(src, 'some-folder'))
fs.writeFileSync(path.join(src, 'some-file'), 'hi')
const dest = path.join(TEST_DIR, 'a-folder')
// verify dest has stuff in it
const pathsBefore = fs.readdirSync(dest)
assert(pathsBefore.indexOf('another-file') >= 0)
assert(pathsBefore.indexOf('another-folder') >= 0)
fse.moveSync(src, dest, {overwrite: true})
// verify dest does not have old stuff
const pathsAfter = fs.readdirSync(dest)
assert.strictEqual(pathsAfter.indexOf('another-file'), -1)
assert.strictEqual(pathsAfter.indexOf('another-folder'), -1)
// verify dest has new stuff
assert(pathsAfter.indexOf('some-file') >= 0)
assert(pathsAfter.indexOf('some-folder') >= 0)
done()
})
it('should create directory structure by default', () => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/does/not/exist/a-file-dest`
// verify dest directory does not exist
assert(!fs.existsSync(path.dirname(dest)))
fse.moveSync(src, dest)
const contents = fs.readFileSync(dest, 'utf8')
const expected = /^sonic the hedgehog\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
})
it('should work across devices', () => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/a-file-dest`
setUpMockFs('EXDEV')
fse.moveSync(src, dest)
const contents = fs.readFileSync(dest, 'utf8')
const expected = /^sonic the hedgehog\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
tearDownMockFs()
})
it('should move folders', () => {
const src = `${TEST_DIR}/a-folder`
const dest = `${TEST_DIR}/a-folder-dest`
// verify it doesn't exist
assert(!fs.existsSync(dest))
fse.moveSync(src, dest)
const contents = fs.readFileSync(dest + '/another-file', 'utf8')
const expected = /^tails\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
})
it('should move folders across devices with EISDIR error', () => {
const src = `${TEST_DIR}/a-folder`
const dest = `${TEST_DIR}/a-folder-dest`
setUpMockFs('EISDIR')
fse.moveSync(src, dest)
const contents = fs.readFileSync(dest + '/another-folder/file3', 'utf8')
const expected = /^knuckles\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
tearDownMockFs()
})
it('should overwrite folders across devices', () => {
const src = `${TEST_DIR}/a-folder`
const dest = `${TEST_DIR}/a-folder-dest`
fs.mkdirSync(dest)
setUpMockFs('EXDEV')
fse.moveSync(src, dest, {overwrite: true})
const contents = fs.readFileSync(dest + '/another-folder/file3', 'utf8')
const expected = /^knuckles\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
tearDownMockFs()
})
it('should move folders across devices with EXDEV error', () => {
const src = `${TEST_DIR}/a-folder`
const dest = `${TEST_DIR}/a-folder-dest`
setUpMockFs('EXDEV')
fse.moveSync(src, dest)
const contents = fs.readFileSync(dest + '/another-folder/file3', 'utf8')
const expected = /^knuckles\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
tearDownMockFs()
})
it('should move folders across devices with EPERM error', () => {
const src = `${TEST_DIR}/a-folder`
const dest = `${TEST_DIR}/a-folder-dest`
setUpMockFs('EPERM')
fse.moveSync(src, dest)
const contents = fs.readFileSync(dest + '/another-folder/file3', 'utf8')
const expected = /^knuckles\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
tearDownMockFs()
})
it('should move folders across devices with ENOTSUP error', () => {
const src = `${TEST_DIR}/a-folder`
const dest = `${TEST_DIR}/a-folder-dest`
setUpMockFs('ENOTSUP')
fse.moveSync(src, dest)
const contents = fs.readFileSync(dest + '/another-folder/file3', 'utf8')
const expected = /^knuckles\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
tearDownMockFs()
})
describe('clobber', () => {
it('should be an alias for overwrite', () => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/a-folder/another-file`
// verify file exists already
assert(fs.existsSync(dest))
fse.moveSync(src, dest, {clobber: true})
const contents = fs.readFileSync(dest, 'utf8')
const expected = /^sonic the hedgehog\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
})
})
describe('> when trying to move a folder into itself', () => {
it('should produce an error', () => {
const SRC_DIR = path.join(TEST_DIR, 'src')
const DEST_DIR = path.join(TEST_DIR, 'src', 'dest')
assert(!fs.existsSync(SRC_DIR))
fs.mkdirSync(SRC_DIR)
assert(fs.existsSync(SRC_DIR))
try {
fse.moveSync(SRC_DIR, DEST_DIR)
} catch (err) {
assert(err.message, `Cannot move ${SRC_DIR} into itself ${DEST_DIR}.`)
assert(fs.existsSync(SRC_DIR))
assert(!fs.existsSync(DEST_DIR))
}
})
})
describe('> when trying to move a file into its parent subdirectory', () => {
it('should move successfully', () => {
const src = `${TEST_DIR}/a-file`
const dest = `${TEST_DIR}/dest/a-file-dest`
fse.moveSync(src, dest)
const contents = fs.readFileSync(dest, 'utf8')
const expected = /^sonic the hedgehog\r?\n$/
assert.ok(contents.match(expected), `${contents} match ${expected}`)
})
})
describe('> when actually trying to move a folder across devices', () => {
const differentDevice = '/mnt'
let __skipTests = false
// must set this up, if not, exit silently
if (!fs.existsSync(differentDevice)) {
console.log('Skipping cross-device moveSync test')
__skipTests = true
}
// make sure we have permission on device
try {
fs.writeFileSync(path.join(differentDevice, 'file'), 'hi')
} catch (err) {
console.log("Can't write to device. Skipping moveSync test.")
__skipTests = true
}
const _it = __skipTests ? it.skip : it
describe('> just the folder', () => {
_it('should move the folder', () => {
const src = '/mnt/some/weird/dir-really-weird'
const dest = path.join(TEST_DIR, 'device-weird')
if (!fs.existsSync(src)) {
fse.mkdirpSync(src)
}
assert(!fs.existsSync(dest))
assert(fs.lstatSync(src).isDirectory())
fse.moveSync(src, dest)
assert(fs.existsSync(dest))
assert(fs.lstatSync(dest).isDirectory())
})
})
})
})