2 var test = require('tap').test
3 var requireInject = require('require-inject')
4 var writeFileAtomic = requireInject('../index', {
6 writeFile: function (tmpfile, data, options, cb) {
7 if (/nowrite/.test(tmpfile)) return cb(new Error('ENOWRITE'))
10 chown: function (tmpfile, uid, gid, cb) {
11 if (/nochown/.test(tmpfile)) return cb(new Error('ENOCHOWN'))
14 rename: function (tmpfile, filename, cb) {
15 if (/norename/.test(tmpfile)) return cb(new Error('ENORENAME'))
18 unlink: function (tmpfile, cb) {
19 if (/nounlink/.test(tmpfile)) return cb(new Error('ENOUNLINK'))
22 writeFileSync: function (tmpfile, data, options) {
23 if (/nowrite/.test(tmpfile)) throw new Error('ENOWRITE')
25 chownSync: function (tmpfile, uid, gid) {
26 if (/nochown/.test(tmpfile)) throw new Error('ENOCHOWN')
28 renameSync: function (tmpfile, filename) {
29 if (/norename/.test(tmpfile)) throw new Error('ENORENAME')
31 unlinkSync: function (tmpfile) {
32 if (/nounlink/.test(tmpfile)) throw new Error('ENOUNLINK')
36 var writeFileAtomicSync = writeFileAtomic.sync
38 test('async tests', function (t) {
40 writeFileAtomic('good', 'test', {mode: '0777'}, function (err) {
41 t.notOk(err, 'No errors occur when passing in options')
43 writeFileAtomic('good', 'test', function (err) {
44 t.notOk(err, 'No errors occur when NOT passing in options')
46 writeFileAtomic('nowrite', 'test', function (err) {
47 t.is(err.message, 'ENOWRITE', 'writeFile failures propagate')
49 writeFileAtomic('nochown', 'test', {chown: {uid: 100, gid: 100}}, function (err) {
50 t.is(err.message, 'ENOCHOWN', 'Chown failures propagate')
52 writeFileAtomic('nochown', 'test', function (err) {
53 t.notOk(err, 'No attempt to chown when no uid/gid passed in')
55 writeFileAtomic('norename', 'test', function (err) {
56 t.is(err.message, 'ENORENAME', 'Rename errors propagate')
58 writeFileAtomic('norename nounlink', 'test', function (err) {
59 t.is(err.message, 'ENORENAME', 'Failure to unlink the temp file does not clobber the original error')
63 test('sync tests', function (t) {
65 var throws = function (shouldthrow, msg, todo) {
67 try { todo() } catch (e) { err = e }
68 t.is(shouldthrow, err.message, msg)
70 var noexception = function (msg, todo) {
72 try { todo() } catch (e) { err = e }
76 noexception('No errors occur when passing in options', function () {
77 writeFileAtomicSync('good', 'test', {mode: '0777'})
79 noexception('No errors occur when NOT passing in options', function () {
80 writeFileAtomicSync('good', 'test')
82 throws('ENOWRITE', 'writeFile failures propagate', function () {
83 writeFileAtomicSync('nowrite', 'test')
85 throws('ENOCHOWN', 'Chown failures propagate', function () {
86 writeFileAtomicSync('nochown', 'test', {chown: {uid: 100, gid: 100}})
88 noexception('No attempt to chown when no uid/gid passed in', function () {
89 writeFileAtomicSync('nochown', 'test')
91 throws('ENORENAME', 'Rename errors propagate', function () {
92 writeFileAtomicSync('norename', 'test')
94 throws('ENORENAME', 'Failure to unlink the temp file does not clobber the original error', function () {
95 writeFileAtomicSync('norename nounlink', 'test')