1var path = require('path') 2 3var test = require('tap').test 4var statSync = require('graceful-fs').statSync 5var mkdtemp = require('tmp').dir 6var mkdirp = require('mkdirp') 7 8var vacuum = require('../vacuum.js') 9 10// CONSTANTS 11var TEMP_OPTIONS = { 12 unsafeCleanup: true, 13 mode: '0700' 14} 15var SHORT_PATH = path.join('i', 'am', 'a', 'path') 16var REMOVE_PATH = path.join(SHORT_PATH, 'of', 'a', 'certain', 'length') 17var OTHER_PATH = path.join(SHORT_PATH, 'of', 'no', 'qualities') 18 19var messages = [] 20function log () { messages.push(Array.prototype.slice.call(arguments).join(' ')) } 21 22var testBase, testPath, otherPath 23test('xXx setup xXx', function (t) { 24 mkdtemp(TEMP_OPTIONS, function (er, tmpdir) { 25 t.ifError(er, 'temp directory exists') 26 27 testBase = path.resolve(tmpdir, SHORT_PATH) 28 testPath = path.resolve(tmpdir, REMOVE_PATH) 29 otherPath = path.resolve(tmpdir, OTHER_PATH) 30 31 mkdirp(testPath, function (er) { 32 t.ifError(er, 'made test path') 33 34 mkdirp(otherPath, function (er) { 35 t.ifError(er, 'made other path') 36 37 t.end() 38 }) 39 }) 40 }) 41}) 42 43test('remove up to a point', function (t) { 44 vacuum(testPath, {purge: false, base: testBase, log: log}, function (er) { 45 t.ifError(er, 'cleaned up to base') 46 47 t.equal(messages.length, 4, 'got 3 removal & 1 finish message') 48 t.equal( 49 messages[3], 'quitting because other entries in ' + testBase + '/of', 50 'got expected final message' 51 ) 52 53 var stat 54 var verifyPath = testPath 55 function verify () { stat = statSync(verifyPath) } 56 57 for (var i = 0; i < 3; i++) { 58 t.throws(verify, verifyPath + ' cannot be statted') 59 t.notOk(stat && stat.isDirectory(), verifyPath + ' is totally gone') 60 verifyPath = path.dirname(verifyPath) 61 } 62 63 t.doesNotThrow(function () { 64 stat = statSync(otherPath) 65 }, otherPath + ' can be statted') 66 t.ok(stat && stat.isDirectory(), otherPath + ' is still a directory') 67 68 var intersection = path.join(testBase, 'of') 69 t.doesNotThrow(function () { 70 stat = statSync(intersection) 71 }, intersection + ' can be statted') 72 t.ok(stat && stat.isDirectory(), intersection + ' is still a directory') 73 74 t.end() 75 }) 76}) 77