|
| 1 | +"use strict"; |
| 2 | +var test = require('tap').test; |
| 3 | +var requireInject = require('require-inject'); |
| 4 | +var writeFileAtomic = requireInject('../index', { |
| 5 | + fs: { |
| 6 | + writeFile: function (tmpfile, data, options, cb) { |
| 7 | + if (/nowrite/.test(tmpfile)) return cb('ENOWRITE'); |
| 8 | + cb(); |
| 9 | + }, |
| 10 | + chown: function (tmpfile, uid, gid, cb) { |
| 11 | + if (/nochown/.test(tmpfile)) return cb('ENOCHOWN'); |
| 12 | + cb(); |
| 13 | + }, |
| 14 | + rename: function (tmpfile, filename, cb) { |
| 15 | + if (/norename/.test(tmpfile)) return cb('ENORENAME'); |
| 16 | + cb(); |
| 17 | + }, |
| 18 | + unlink: function (tmpfile, cb) { |
| 19 | + if (/nounlink/.test(tmpfile)) return cb('ENOUNLINK'); |
| 20 | + cb(); |
| 21 | + }, |
| 22 | + writeFileSync: function (tmpfile, data, options) { |
| 23 | + if (/nowrite/.test(tmpfile)) throw 'ENOWRITE'; |
| 24 | + }, |
| 25 | + chownSync: function (tmpfile, uid, gid) { |
| 26 | + if (/nochown/.test(tmpfile)) throw 'ENOCHOWN'; |
| 27 | + }, |
| 28 | + renameSync: function (tmpfile, filename) { |
| 29 | + if (/norename/.test(tmpfile)) throw 'ENORENAME'; |
| 30 | + }, |
| 31 | + unlinkSync: function (tmpfile) { |
| 32 | + if (/nounlink/.test(tmpfile)) throw 'ENOUNLINK'; |
| 33 | + }, |
| 34 | + } |
| 35 | +}); |
| 36 | +var writeFileAtomicSync = writeFileAtomic.sync; |
| 37 | + |
| 38 | +test('async tests', function (t) { |
| 39 | + t.plan(7); |
| 40 | + writeFileAtomic('good', 'test', {mode: '0777'}, function (err) { |
| 41 | + t.notOk(err, 'No errors occur when passing in options'); |
| 42 | + }); |
| 43 | + writeFileAtomic('good', 'test', function (err) { |
| 44 | + t.notOk(err, 'No errors occur when NOT passing in options'); |
| 45 | + }); |
| 46 | + writeFileAtomic('nowrite', 'test', function (err) { |
| 47 | + t.is(err, 'ENOWRITE', 'writeFile failures propagate'); |
| 48 | + }); |
| 49 | + writeFileAtomic('nochown', 'test', {chown: {uid:100,gid:100}}, function (err) { |
| 50 | + t.is(err, 'ENOCHOWN', 'Chown failures propagate'); |
| 51 | + }); |
| 52 | + writeFileAtomic('nochown', 'test', function (err) { |
| 53 | + t.notOk(err, 'No attempt to chown when no uid/gid passed in'); |
| 54 | + }); |
| 55 | + writeFileAtomic('norename', 'test', function (err) { |
| 56 | + t.is(err, 'ENORENAME', 'Rename errors propagate'); |
| 57 | + }); |
| 58 | + writeFileAtomic('norename nounlink', 'test', function (err) { |
| 59 | + t.is(err, 'ENORENAME', 'Failure to unlink the temp file does not clobber the original error'); |
| 60 | + }); |
| 61 | +}); |
| 62 | + |
| 63 | +test('sync tests', function (t) { |
| 64 | + t.plan(7); |
| 65 | + var throws = function (shouldthrow, msg, todo) { |
| 66 | + var err; |
| 67 | + try { todo() } catch (e) { err = e } |
| 68 | + t.is(shouldthrow,err,msg); |
| 69 | + } |
| 70 | + var noexception = function (msg, todo) { |
| 71 | + var err; |
| 72 | + try { todo() } catch (e) { err = e } |
| 73 | + t.notOk(err,msg); |
| 74 | + } |
| 75 | + |
| 76 | + noexception('No errors occur when passing in options',function (){ |
| 77 | + writeFileAtomicSync('good', 'test', {mode: '0777'}); |
| 78 | + }) |
| 79 | + noexception('No errors occur when NOT passing in options',function (){ |
| 80 | + writeFileAtomicSync('good', 'test'); |
| 81 | + }); |
| 82 | + throws('ENOWRITE', 'writeFile failures propagate', function () { |
| 83 | + writeFileAtomicSync('nowrite', 'test'); |
| 84 | + }); |
| 85 | + throws('ENOCHOWN', 'Chown failures propagate', function () { |
| 86 | + writeFileAtomicSync('nochown', 'test', {chown: {uid:100,gid:100}}); |
| 87 | + }); |
| 88 | + noexception('No attempt to chown when no uid/gid passed in', function (){ |
| 89 | + writeFileAtomicSync('nochown', 'test'); |
| 90 | + }); |
| 91 | + throws('ENORENAME', 'Rename errors propagate', function (){ |
| 92 | + writeFileAtomicSync('norename', 'test'); |
| 93 | + }); |
| 94 | + throws('ENORENAME', 'Failure to unlink the temp file does not clobber the original error', function (){ |
| 95 | + writeFileAtomicSync('norename nounlink', 'test'); |
| 96 | + }); |
| 97 | +}); |
0 commit comments