This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
/
basic.js
97 lines (94 loc) · 3.56 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
"use strict";
var test = require('tap').test;
var requireInject = require('require-inject');
var writeFileAtomic = requireInject('../index', {
'graceful-fs': {
writeFile: function (tmpfile, data, options, cb) {
if (/nowrite/.test(tmpfile)) return cb('ENOWRITE');
cb();
},
chown: function (tmpfile, uid, gid, cb) {
if (/nochown/.test(tmpfile)) return cb('ENOCHOWN');
cb();
},
rename: function (tmpfile, filename, cb) {
if (/norename/.test(tmpfile)) return cb('ENORENAME');
cb();
},
unlink: function (tmpfile, cb) {
if (/nounlink/.test(tmpfile)) return cb('ENOUNLINK');
cb();
},
writeFileSync: function (tmpfile, data, options) {
if (/nowrite/.test(tmpfile)) throw 'ENOWRITE';
},
chownSync: function (tmpfile, uid, gid) {
if (/nochown/.test(tmpfile)) throw 'ENOCHOWN';
},
renameSync: function (tmpfile, filename) {
if (/norename/.test(tmpfile)) throw 'ENORENAME';
},
unlinkSync: function (tmpfile) {
if (/nounlink/.test(tmpfile)) throw 'ENOUNLINK';
},
}
});
var writeFileAtomicSync = writeFileAtomic.sync;
test('async tests', function (t) {
t.plan(7);
writeFileAtomic('good', 'test', {mode: '0777'}, function (err) {
t.notOk(err, 'No errors occur when passing in options');
});
writeFileAtomic('good', 'test', function (err) {
t.notOk(err, 'No errors occur when NOT passing in options');
});
writeFileAtomic('nowrite', 'test', function (err) {
t.is(err, 'ENOWRITE', 'writeFile failures propagate');
});
writeFileAtomic('nochown', 'test', {chown: {uid:100,gid:100}}, function (err) {
t.is(err, 'ENOCHOWN', 'Chown failures propagate');
});
writeFileAtomic('nochown', 'test', function (err) {
t.notOk(err, 'No attempt to chown when no uid/gid passed in');
});
writeFileAtomic('norename', 'test', function (err) {
t.is(err, 'ENORENAME', 'Rename errors propagate');
});
writeFileAtomic('norename nounlink', 'test', function (err) {
t.is(err, 'ENORENAME', 'Failure to unlink the temp file does not clobber the original error');
});
});
test('sync tests', function (t) {
t.plan(7);
var throws = function (shouldthrow, msg, todo) {
var err;
try { todo() } catch (e) { err = e }
t.is(shouldthrow,err,msg);
}
var noexception = function (msg, todo) {
var err;
try { todo() } catch (e) { err = e }
t.notOk(err,msg);
}
noexception('No errors occur when passing in options',function (){
writeFileAtomicSync('good', 'test', {mode: '0777'});
})
noexception('No errors occur when NOT passing in options',function (){
writeFileAtomicSync('good', 'test');
});
throws('ENOWRITE', 'writeFile failures propagate', function () {
writeFileAtomicSync('nowrite', 'test');
});
throws('ENOCHOWN', 'Chown failures propagate', function () {
writeFileAtomicSync('nochown', 'test', {chown: {uid:100,gid:100}});
});
noexception('No attempt to chown when no uid/gid passed in', function (){
writeFileAtomicSync('nochown', 'test');
});
throws('ENORENAME', 'Rename errors propagate', function (){
writeFileAtomicSync('norename', 'test');
});
throws('ENORENAME', 'Failure to unlink the temp file does not clobber the original error', function (){
writeFileAtomicSync('norename nounlink', 'test');
});
});