1var path = require('path') 2 3var test = require('tap').test 4 5var readdir = require('graceful-fs').readdir 6var readdirSync = require('graceful-fs').readdirSync 7var statSync = require('graceful-fs').statSync 8var writeFile = require('graceful-fs').writeFile 9var mkdirp = require('mkdirp') 10var mkdtemp = require('tmp').dir 11var tmpFile = require('tmp').file 12 13var requireInject = require('require-inject') 14var vacuum = requireInject('../vacuum.js', { 15 'graceful-fs': { 16 'lstat': require('graceful-fs').lstat, 17 'readdir': function (dir, cb) { 18 readdir(dir, function (err, files) { 19 // Simulate racy removal by creating a file after vacuum 20 // thinks the directory is empty 21 if (dir === partialPath && files.length === 0) { 22 tmpFile({dir: dir}, function (err, path, fd) { 23 if (err) throw err 24 cb(err, files) 25 }) 26 } else { 27 cb(err, files) 28 } 29 }) 30 }, 31 'rmdir': require('graceful-fs').rmdir, 32 'unlink': require('graceful-fs').unlink 33 } 34}) 35 36// CONSTANTS 37var TEMP_OPTIONS = { 38 unsafeCleanup: true, 39 mode: '0700' 40} 41var SHORT_PATH = path.join('i', 'am', 'a', 'path') 42var PARTIAL_PATH = path.join(SHORT_PATH, 'that', 'ends', 'at', 'a') 43var FULL_PATH = path.join(PARTIAL_PATH, 'file') 44 45var messages = [] 46function log () { messages.push(Array.prototype.slice.call(arguments).join(' ')) } 47 48var testBase, partialPath, fullPath 49test('xXx setup xXx', function (t) { 50 mkdtemp(TEMP_OPTIONS, function (er, tmpdir) { 51 t.ifError(er, 'temp directory exists') 52 53 testBase = path.resolve(tmpdir, SHORT_PATH) 54 partialPath = path.resolve(tmpdir, PARTIAL_PATH) 55 fullPath = path.resolve(tmpdir, FULL_PATH) 56 57 mkdirp(partialPath, function (er) { 58 t.ifError(er, 'made test path') 59 60 writeFile(fullPath, new Buffer('hi'), function (er) { 61 t.ifError(er, 'made file') 62 63 t.end() 64 }) 65 }) 66 }) 67}) 68 69test('racy removal should quit gracefully', function (t) { 70 vacuum(fullPath, {purge: false, base: testBase, log: log}, function (er) { 71 t.ifError(er, 'cleaned up to base') 72 73 t.equal(messages.length, 3, 'got 2 removal & 1 quit message') 74 t.equal(messages[2], 'quitting because new (racy) entries in ' + partialPath) 75 76 var stat 77 var verifyPath = fullPath 78 79 function verify () { stat = statSync(verifyPath) } 80 81 // handle the file separately 82 t.throws(verify, verifyPath + ' cannot be statted') 83 t.notOk(stat && stat.isFile(), verifyPath + ' is totally gone') 84 verifyPath = path.dirname(verifyPath) 85 86 for (var i = 0; i < 4; i++) { 87 t.doesNotThrow(function () { 88 stat = statSync(verifyPath) 89 }, verifyPath + ' can be statted') 90 t.ok(stat && stat.isDirectory(), verifyPath + ' is still a directory') 91 verifyPath = path.dirname(verifyPath) 92 } 93 94 t.doesNotThrow(function () { 95 stat = statSync(testBase) 96 }, testBase + ' can be statted') 97 t.ok(stat && stat.isDirectory(), testBase + ' is still a directory') 98 99 var files = readdirSync(testBase) 100 t.equal(files.length, 1, 'base directory untouched') 101 102 t.end() 103 }) 104}) 105