/
move-file.js
202 lines (180 loc) · 4.95 KB
/
move-file.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
'use strict'
const {
chmod,
open,
readFile,
readdir,
stat,
} = require('fs/promises')
const fs = require('fs')
const path = require('path')
const t = require('tap')
const moveFile = require('../../lib/util/move-file')
t.test('move a file', function (t) {
const testDir = t.testdir({
src: 'foo',
})
return moveFile(testDir + '/src', testDir + '/dest')
.then(() => {
return readFile(testDir + '/dest', 'utf8')
})
.then((data) => {
t.equal(data, 'foo', 'file data correct')
return stat(testDir + '/src').catch((err) => {
t.ok(err, 'src read error')
t.equal(err.code, 'ENOENT', 'src does not exist')
})
})
})
t.test('does not clobber existing files', function (t) {
const testDir = t.testdir({
src: 'foo',
dest: 'bar',
})
return moveFile(testDir + '/src', testDir + '/dest')
.then(() => {
return readFile(testDir + '/dest', 'utf8')
})
.then((data) => {
t.equal(data, 'bar', 'conflicting file left intact')
return stat(testDir + '/src').catch((err) => {
t.ok(err, 'src read error')
t.equal(err.code, 'ENOENT', 'src file still deleted')
})
})
})
t.test('does not move a file into an existing directory', async t => {
const testDir = t.testdir({
src: 'foo',
dest: {},
})
await moveFile(testDir + '/src', testDir + '/dest')
const files = await readdir(testDir + '/dest')
t.equal(files.length, 0, 'directory remains empty')
})
t.test('does not error if destination file is open', function (t) {
const testDir = t.testdir({
src: 'foo',
dest: 'bar',
})
return open(testDir + '/dest', 'r+').then((fh) => {
return moveFile(testDir + '/src', testDir + '/dest')
.then(() => {
return fh.close()
})
.then(() => {
return readFile(testDir + '/dest', 'utf8')
})
.then((data) => {
t.equal(data, 'bar', 'destination left intact')
return stat(testDir + '/src').catch((err) => {
t.ok(err, 'src read error')
t.equal(err.code, 'ENOENT', 'src does not exist')
})
})
})
})
t.test('fallback to renaming on missing files post-move', async t => {
const testDir = t.testdir({
src: 'foo',
})
const missingFileError = new Error('ENOENT')
missingFileError.code = 'ENOENT'
const mockFS = {
...fs.promises,
async unlink (path) {
throw missingFileError
},
async stat (path) {
throw missingFileError
},
}
const mockedMoveFile = t.mock('../../lib/util/move-file', {
'fs/promises': mockFS,
})
await mockedMoveFile(testDir + '/src', testDir + '/dest')
const data = await readFile(testDir + '/dest', 'utf8')
t.equal(data, 'foo', 'file data correct')
await t.rejects(
stat(testDir + '/src'),
{ code: 'ENOENT' },
'./src does not exist'
)
})
t.test('non ENOENT error on move fallback', async function (t) {
const testDir = t.testdir({
src: 'foo',
})
const missingFileError = new Error('ENOENT')
missingFileError.code = 'ENOENT'
const otherError = new Error('UNKNOWN')
otherError.code = 'OTHER'
const mockFS = {
...fs.promises,
async unlink (path) {
throw missingFileError
},
async stat (path) {
throw otherError
},
}
const mockedMoveFile = t.mock('../../lib/util/move-file', {
'fs/promises': mockFS,
})
await t.rejects(
mockedMoveFile(testDir + '/src', testDir + '/dest'),
{ code: 'OTHER' },
'throws other error'
)
})
t.test('verify weird EPERM on Windows behavior', t => {
const processPlatform = process.platform
Object.defineProperty(process, 'platform', { value: 'win32' })
t.teardown(() => {
Object.defineProperty(process, 'platform', { value: processPlatform })
})
let calledMonkeyPatch = false
const mockFS = {
...fs.promises,
link: async (src, dest) => {
calledMonkeyPatch = true
throw Object.assign(new Error('yolo'), { code: 'EPERM' })
},
}
const mockedMoveFile = t.mock('../../lib/util/move-file.js', {
'fs/promises': mockFS,
})
const testDir = t.testdir({
eperm: {
src: 'epermmy',
},
})
return mockedMoveFile(testDir + '/eperm/src', testDir + '/eperm/dest')
.then(() => t.ok(calledMonkeyPatch, 'called the patched fs.link fn'))
.then(() => t.rejects(readFile('eperm/dest'), {
code: 'ENOENT',
}, 'destination file did not get written'))
.then(() => t.rejects(readFile('eperm/src'), {
code: 'ENOENT',
}, 'src file did get deleted'))
})
t.test(
'errors if dest is not writable',
{
skip: process.platform === 'win32',
},
async t => {
const testDir = t.testdir({
src: 'foo',
dest: {},
})
await chmod(testDir + '/dest', parseInt('400', 8))
await t.rejects(
moveFile(testDir + '/src', path.join(testDir + '/dest', 'file')),
{ code: 'EACCES' },
'error is about permissions'
)
const data = await readFile(testDir + '/src', 'utf8')
t.equal(data, 'foo', 'src contents left intact')
}
)