-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathbasic.js
446 lines (422 loc) · 12.7 KB
/
basic.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
'use strict'
const t = require('tap')
let expectClose = 0
let closeCalled = 0
let expectCloseSync = 0
let closeSyncCalled = 0
const createErr = code => Object.assign(new Error(code), { code })
let unlinked = []
const writeFileAtomic = t.mock('..', {
fs: {
realpath (filename, cb) {
return cb(null, filename)
},
open (tmpfile, options, mode, cb) {
if (/noopen/.test(tmpfile)) {
return cb(createErr('ENOOPEN'))
}
expectClose++
cb(null, tmpfile)
},
write (fd) {
const cb = arguments[arguments.length - 1]
if (/nowrite/.test(fd)) {
return cb(createErr('ENOWRITE'))
}
cb()
},
fsync (fd, cb) {
if (/nofsync/.test(fd)) {
return cb(createErr('ENOFSYNC'))
}
cb()
},
close (fd, cb) {
closeCalled++
cb()
},
chown (tmpfile, uid, gid, cb) {
if (/nochown/.test(tmpfile)) {
return cb(createErr('ENOCHOWN'))
}
if (/enosys/.test(tmpfile)) {
return cb(createErr('ENOSYS'))
}
if (/einval/.test(tmpfile)) {
return cb(createErr('EINVAL'))
}
if (/eperm/.test(tmpfile)) {
return cb(createErr('EPERM'))
}
cb()
},
chmod (tmpfile, mode, cb) {
if (/nochmod/.test(tmpfile)) {
return cb(createErr('ENOCHMOD'))
}
if (/enosys/.test(tmpfile)) {
return cb(createErr('ENOSYS'))
}
if (/eperm/.test(tmpfile)) {
return cb(createErr('EPERM'))
}
if (/einval/.test(tmpfile)) {
return cb(createErr('EINVAL'))
}
cb()
},
rename (tmpfile, filename, cb) {
if (/norename/.test(tmpfile)) {
return cb(createErr('ENORENAME'))
}
cb()
},
unlink (tmpfile, cb) {
if (/nounlink/.test(tmpfile)) {
return cb(createErr('ENOUNLINK'))
}
cb()
},
stat (tmpfile, cb) {
if (/nostat/.test(tmpfile)) {
return cb(createErr('ENOSTAT'))
}
cb()
},
realpathSync (filename) {
return filename
},
openSync (tmpfile) {
if (/noopen/.test(tmpfile)) {
throw createErr('ENOOPEN')
}
expectCloseSync++
return tmpfile
},
writeSync (fd) {
if (/nowrite/.test(fd)) {
throw createErr('ENOWRITE')
}
},
fsyncSync (fd) {
if (/nofsync/.test(fd)) {
throw createErr('ENOFSYNC')
}
},
closeSync () {
closeSyncCalled++
},
chownSync (tmpfile) {
if (/nochown/.test(tmpfile)) {
throw createErr('ENOCHOWN')
}
if (/enosys/.test(tmpfile)) {
throw createErr('ENOSYS')
}
if (/einval/.test(tmpfile)) {
throw createErr('EINVAL')
}
if (/eperm/.test(tmpfile)) {
throw createErr('EPERM')
}
},
chmodSync (tmpfile) {
if (/nochmod/.test(tmpfile)) {
throw createErr('ENOCHMOD')
}
if (/enosys/.test(tmpfile)) {
throw createErr('ENOSYS')
}
if (/einval/.test(tmpfile)) {
throw createErr('EINVAL')
}
if (/eperm/.test(tmpfile)) {
throw createErr('EPERM')
}
},
renameSync (tmpfile) {
if (/norename/.test(tmpfile)) {
throw createErr('ENORENAME')
}
},
unlinkSync (tmpfile) {
if (/nounlink/.test(tmpfile)) {
throw createErr('ENOUNLINK')
}
unlinked.push(tmpfile)
},
statSync (tmpfile) {
if (/nostat/.test(tmpfile)) {
throw createErr('ENOSTAT')
}
},
},
})
const writeFileAtomicSync = writeFileAtomic.sync
t.test('getTmpname', t => {
const getTmpname = writeFileAtomic._getTmpname
const a = getTmpname('abc.def')
const b = getTmpname('abc.def')
t.not(a, b, 'different invocations of getTmpname get different results')
t.end()
})
t.test('cleanupOnExit', t => {
const file = 'tmpname'
unlinked = []
const cleanup = writeFileAtomic._cleanupOnExit(() => file)
cleanup()
t.strictSame(unlinked, [file], 'cleanup code unlinks')
const cleanup2 = writeFileAtomic._cleanupOnExit('nounlink')
t.doesNotThrow(cleanup2, 'exceptions are caught')
unlinked = []
t.end()
})
t.test('async tests', t => {
t.plan(2)
expectClose = 0
closeCalled = 0
t.teardown(() => {
t.parent.equal(closeCalled, expectClose, 'async tests closed all files')
expectClose = 0
closeCalled = 0
})
t.test('non-root tests', t => {
t.plan(19)
writeFileAtomic('good', 'test', { mode: '0777' }, err => {
t.notOk(err, 'No errors occur when passing in options')
})
writeFileAtomic('good', 'test', 'utf8', err => {
t.notOk(err, 'No errors occur when passing in options as string')
})
writeFileAtomic('good', 'test', undefined, err => {
t.notOk(err, 'No errors occur when NOT passing in options')
})
writeFileAtomic('good', 'test', err => {
t.notOk(err)
})
writeFileAtomic('noopen', 'test', err => {
t.equal(err && err.message, 'ENOOPEN', 'fs.open failures propagate')
})
writeFileAtomic('nowrite', 'test', err => {
t.equal(err && err.message, 'ENOWRITE', 'fs.writewrite failures propagate')
})
writeFileAtomic('nowrite', Buffer.from('test', 'utf8'), err => {
t.equal(err && err.message, 'ENOWRITE', 'fs.writewrite failures propagate for buffers')
})
writeFileAtomic('nochown', 'test', { chown: { uid: 100, gid: 100 } }, err => {
t.equal(err && err.message, 'ENOCHOWN', 'Chown failures propagate')
})
writeFileAtomic('nochown', 'test', err => {
t.notOk(err, 'No attempt to chown when no uid/gid passed in')
})
writeFileAtomic('nochmod', 'test', { mode: parseInt('741', 8) }, err => {
t.equal(err && err.message, 'ENOCHMOD', 'Chmod failures propagate')
})
writeFileAtomic('nofsyncopt', 'test', { fsync: false }, err => {
t.notOk(err, 'fsync skipped if options.fsync is false')
})
writeFileAtomic('norename', 'test', err => {
t.equal(err && err.message, 'ENORENAME', 'Rename errors propagate')
})
writeFileAtomic('norename nounlink', 'test', err => {
t.equal(err && err.message, 'ENORENAME',
'Failure to unlink the temp file does not clobber the original error')
})
writeFileAtomic('nofsync', 'test', err => {
t.equal(err && err.message, 'ENOFSYNC', 'Fsync failures propagate')
})
writeFileAtomic('enosys', 'test', err => {
t.notOk(err, 'No errors on ENOSYS')
})
writeFileAtomic('einval', 'test', { mode: 0o741 }, err => {
t.notOk(err, 'No errors on EINVAL for non root')
})
writeFileAtomic('eperm', 'test', { mode: 0o741 }, err => {
t.notOk(err, 'No errors on EPERM for non root')
})
writeFileAtomic('einval', 'test', { chown: { uid: 100, gid: 100 } }, err => {
t.notOk(err, 'No errors on EINVAL for non root')
})
writeFileAtomic('eperm', 'test', { chown: { uid: 100, gid: 100 } }, err => {
t.notOk(err, 'No errors on EPERM for non root')
})
})
t.test('errors for root', t => {
const { getuid } = process
process.getuid = () => 0
t.teardown(() => {
process.getuid = getuid
})
t.plan(2)
writeFileAtomic('einval', 'test', { chown: { uid: 100, gid: 100 } }, err => {
t.match(err, { code: 'EINVAL' })
})
writeFileAtomic('einval', 'test', { mode: 0o741 }, err => {
t.match(err, { code: 'EINVAL' })
})
})
})
t.test('sync tests', t => {
t.plan(2)
closeSyncCalled = 0
expectCloseSync = 0
t.teardown(() => {
t.parent.equal(closeSyncCalled, expectCloseSync, 'sync closed all files')
expectCloseSync = 0
closeSyncCalled = 0
})
const throws = function (t, shouldthrow, msg, todo) {
let err
try {
todo()
} catch (e) {
err = e
}
t.equal(shouldthrow, err && err.message, msg)
}
const noexception = function (t, msg, todo) {
let err
try {
todo()
} catch (e) {
err = e
}
t.error(err, msg)
}
let tmpfile
t.test('non-root', t => {
t.plan(22)
noexception(t, 'No errors occur when passing in options', () => {
writeFileAtomicSync('good', 'test', { mode: '0777' })
})
noexception(t, 'No errors occur when passing in options as string', () => {
writeFileAtomicSync('good', 'test', 'utf8')
})
noexception(t, 'No errors occur when NOT passing in options', () => {
writeFileAtomicSync('good', 'test')
})
noexception(t, 'fsync never called if options.fsync is falsy', () => {
writeFileAtomicSync('good', 'test', { fsync: false })
})
noexception(t, 'tmpfileCreated is called on success', () => {
writeFileAtomicSync('good', 'test', {
tmpfileCreated (gottmpfile) {
tmpfile = gottmpfile
},
})
t.match(tmpfile, /^good\.\d+$/, 'tmpfileCreated called for success')
})
tmpfile = undefined
throws(t, 'ENOOPEN', 'fs.openSync failures propagate', () => {
writeFileAtomicSync('noopen', 'test', {
tmpfileCreated (gottmpfile) {
tmpfile = gottmpfile
},
})
})
t.equal(tmpfile, undefined, 'tmpfileCreated not called for open failure')
throws(t, 'ENOWRITE', 'fs.writeSync failures propagate', () => {
writeFileAtomicSync('nowrite', 'test', {
tmpfileCreated (gottmpfile) {
tmpfile = gottmpfile
},
})
})
t.match(tmpfile, /^nowrite\.\d+$/, 'tmpfileCreated called for failure after open')
throws(t, 'ENOCHOWN', 'Chown failures propagate', () => {
writeFileAtomicSync('nochown', 'test', { chown: { uid: 100, gid: 100 } })
})
noexception(t, 'No attempt to chown when false passed in', () => {
writeFileAtomicSync('nochown', 'test', { chown: false })
})
noexception(t, 'No errors occured when chown is undefined and original file owner used', () => {
writeFileAtomicSync('chowncopy', 'test', { chown: undefined })
})
throws(t, 'ENORENAME', 'Rename errors propagate', () => {
writeFileAtomicSync('norename', 'test')
})
throws(t, 'ENORENAME',
'Failure to unlink the temp file does not clobber the original error', () => {
writeFileAtomicSync('norename nounlink', 'test')
})
throws(t, 'ENOFSYNC', 'Fsync errors propagate', () => {
writeFileAtomicSync('nofsync', 'test')
})
noexception(t, 'No errors on ENOSYS', () => {
writeFileAtomicSync('enosys', 'test', { chown: { uid: 100, gid: 100 } })
})
noexception(t, 'No errors on EINVAL for non root', () => {
writeFileAtomicSync('einval', 'test', { chown: { uid: 100, gid: 100 } })
})
noexception(t, 'No errors on EPERM for non root', () => {
writeFileAtomicSync('eperm', 'test', { chown: { uid: 100, gid: 100 } })
})
throws(t, 'ENOCHMOD', 'Chmod failures propagate', () => {
writeFileAtomicSync('nochmod', 'test', { mode: 0o741 })
})
noexception(t, 'No errors on EPERM for non root', () => {
writeFileAtomicSync('eperm', 'test', { mode: 0o741 })
})
noexception(t, 'No attempt to chmod when no mode provided', () => {
writeFileAtomicSync('nochmod', 'test', { mode: false })
})
})
t.test('errors for root', t => {
const { getuid } = process
process.getuid = () => 0
t.teardown(() => {
process.getuid = getuid
})
t.plan(2)
throws(t, 'EINVAL', 'Chown error as root user', () => {
writeFileAtomicSync('einval', 'test', { chown: { uid: 100, gid: 100 } })
})
throws(t, 'EINVAL', 'Chmod error as root user', () => {
writeFileAtomicSync('einval', 'test', { mode: 0o741 })
})
})
})
t.test('promises', async t => {
let tmpfile
closeCalled = 0
expectClose = 0
t.teardown(() => {
t.parent.equal(closeCalled, expectClose, 'promises closed all files')
closeCalled = 0
expectClose = 0
})
await writeFileAtomic('good', 'test', {
tmpfileCreated (gottmpfile) {
tmpfile = gottmpfile
},
})
t.match(tmpfile, /^good\.\d+$/, 'tmpfileCreated is called for success')
await writeFileAtomic('good', 'test', {
tmpfileCreated () {
return Promise.resolve()
},
})
await t.rejects(writeFileAtomic('good', 'test', {
tmpfileCreated () {
return Promise.reject(new Error('reject from tmpfileCreated'))
},
}))
await t.rejects(writeFileAtomic('good', 'test', {
tmpfileCreated () {
throw new Error('throw from tmpfileCreated')
},
}))
tmpfile = undefined
await t.rejects(writeFileAtomic('noopen', 'test', {
tmpfileCreated (gottmpfile) {
tmpfile = gottmpfile
},
}))
t.equal(tmpfile, undefined, 'tmpfileCreated is not called on open failure')
await t.rejects(writeFileAtomic('nowrite', 'test', {
tmpfileCreated (gottmpfile) {
tmpfile = gottmpfile
},
}))
t.match(tmpfile, /^nowrite\.\d+$/, 'tmpfileCreated is called if failure is after open')
})