Skip to content

Commit

Permalink
Merge pull request #25 from foxel/remove_callback
Browse files Browse the repository at this point in the history
Added removeCallback passing
  • Loading branch information
raszi committed Jul 9, 2014
2 parents 5e38178 + 9714ae5 commit 744b974
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
.idea/
47 changes: 37 additions & 10 deletions lib/tmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,21 @@ function _createTmpFile(options, callback) {
fs.open(name, _c.O_CREAT | _c.O_EXCL | _c.O_RDWR, opts.mode || 0600, function _fileCreated(err, fd) {
if (err) return cb(err);

if (!opts.keep) _removeObjects.unshift([ fs.unlinkSync, name ]);
var removeCallback = _prepareRemoveCallback(fs.unlinkSync.bind(fs), name);

cb(null, name, fd);
if (!opts.keep) {
_removeObjects.unshift(removeCallback);
}

cb(null, name, fd, removeCallback);
});
});
}

/**
* Removes files and folders in a directory recursively.
*
* @param {String} path
* @param {String} dir
*/
function _rmdirRecursiveSync(dir) {
var files = fs.readdirSync(dir);
Expand All @@ -197,6 +201,26 @@ function _rmdirRecursiveSync(dir) {
fs.rmdirSync(dir);
}

/**
*
* @param {Function} removeFunction
* @param {String} path
* @returns {Function}
* @private
*/
function _prepareRemoveCallback(removeFunction, path) {
var called = false;
return function() {
if (called) {
return;
}

removeFunction(path);

called = true;
};
}

/**
* Creates a temporary directory.
*
Expand All @@ -218,15 +242,18 @@ function _createTmpDir(options, callback) {
fs.mkdir(name, opts.mode || 0700, function _dirCreated(err) {
if (err) return cb(err);

var removeCallback = _prepareRemoveCallback(
opts.unsafeCleanup
? _rmdirRecursiveSync
: fs.rmdirSync.bind(fs),
name
);

if (!opts.keep) {
if (opts.unsafeCleanup) {
_removeObjects.unshift([ _rmdirRecursiveSync, name ]);
} else {
_removeObjects.unshift([ fs.rmdirSync, name ]);
}
_removeObjects.unshift(removeCallback);
}

cb(null, name);
cb(null, name, removeCallback);
});
});
}
Expand All @@ -243,7 +270,7 @@ function _garbageCollector() {

for (var i = 0, length = _removeObjects.length; i < length; i++) {
try {
_removeObjects[i][0].call(null, _removeObjects[i][1]);
_removeObjects[i].call(null);
} catch (e) {
// already removed?
}
Expand Down
13 changes: 13 additions & 0 deletions test/dir-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,18 @@ vows.describe('Directory creation').addBatch({
'should not return with an error': assert.isNull,
'should return with a name': Test.assertName,
'should be a directory': _testDir(040700)
},

'remove callback': {
topic: function () {
tmp.dir(this.callback);
},

'should not return with an error': assert.isNull,
'should return with a name': Test.assertName,
'removeCallback should remove directory': function (_err, name, removeCallback) {
removeCallback();
assert.ok(!existsSync(name), "Directory should be removed");
}
}
}).exportTo(module);
13 changes: 13 additions & 0 deletions test/file-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ vows.describe('File creation').addBatch({
'should not exist': function (err, name) {
assert.ok(!existsSync(name), "File should be removed");
}
},

'remove callback': {
topic: function () {
tmp.file(this.callback);
},

'should not return with an error': assert.isNull,
'should return with a name': Test.assertName,
'removeCallback should remove file': function (_err, name, _fd, removeCallback) {
removeCallback();
assert.ok(!existsSync(name), "File should be removed");
}
}

}).exportTo(module);

0 comments on commit 744b974

Please sign in to comment.