Skip to content
This repository has been archived by the owner on Jun 19, 2019. It is now read-only.

Commit

Permalink
Merge pull request #73 from seanmwalker/master
Browse files Browse the repository at this point in the history
Windows permission change before unlinking files and symlinks is in and has better tests.
  • Loading branch information
Ryan McGrath committed Jan 17, 2014
2 parents 5c716f9 + 99106ac commit 97f22f7
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 10 deletions.
31 changes: 22 additions & 9 deletions lib/wrench.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/

var fs = require("fs"),
_path = require("path");
_path = require("path"),
isWindows = !!process.platform.match(/^win/);

/* wrench.readdirSyncRecursive("directory_path");
*
Expand Down Expand Up @@ -122,7 +123,7 @@ exports.readdirRecursive = function(baseDir, fn) {



/* wrench.rmdirSyncRecursive("directory_path", forceDelete, failSilent);
/* wrench.rmdirSyncRecursive("directory_path", failSilent);
*
* Recursively dives through directories and obliterates everything about it. This is a
* Sync-function, which blocks things until it's done. No idea why anybody would want an
Expand All @@ -134,22 +135,34 @@ exports.rmdirSyncRecursive = function(path, failSilent) {
try {
files = fs.readdirSync(path);
} catch (err) {

if(failSilent) return;
throw new Error(err.message);
}

/* Loop through and delete everything in the sub-tree after checking it */
for(var i = 0; i < files.length; i++) {
var currFile = fs.lstatSync(_path.join(path, files[i]));
var file = _path.join(path, files[i]);
var currFile = fs.lstatSync(file);

if(currFile.isDirectory()) // Recursive function back to the beginning
exports.rmdirSyncRecursive(_path.join(path, files[i]));
if(currFile.isDirectory()) {
// Recursive function back to the beginning
exports.rmdirSyncRecursive(file);
} else if(currFile.isSymbolicLink()) {
// Unlink symlinks
if (isWindows) {
fs.chmodSync(file, 666) // Windows needs this unless joyent/node#3006 is resolved..
}

else if(currFile.isSymbolicLink()) // Unlink symlinks
fs.unlinkSync(_path.join(path, files[i]));
fs.unlinkSync(file);
} else {
// Assume it's a file - perhaps a try/catch belongs here?
if (isWindows) {
fs.chmodSync(file, 666) // Windows needs this unless joyent/node#3006 is resolved..
}

else // Assume it's a file - perhaps a try/catch belongs here?
fs.unlinkSync(_path.join(path, files[i]));
fs.unlinkSync(file);
}
}

/* Now that we know everything in the sub-tree has been deleted, we can delete the main
Expand Down
74 changes: 74 additions & 0 deletions tests/rmdirSyncRecursive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var testCase = require('nodeunit').testCase;
var fs = require('fs');
var wrench = require('../lib/wrench');
var path = require('path');

module.exports = testCase({
test_rmdirSyncRecursive: function(test) {
var dir = __dirname + '/_tmp2/foo/bar';

wrench.mkdirSyncRecursive(dir, '777');

var f1Path = path.join(dir, 'test1.txt');
var f2Path = path.join(path.dirname(dir), 'test2.txt');
var f3Path = path.join(path.dirname(path.dirname(dir)), 'test3.txt');

fs.writeFileSync(f1Path, 'foo bar baz');
fs.writeFileSync(f2Path, 'foo bar baz');
fs.writeFileSync(f3Path, 'foo bar baz');

fs.chmodSync(f1Path, '777');
fs.chmodSync(f2Path, '777');
fs.chmodSync(f3Path, '777');

test.equals(fs.existsSync(dir), true, 'Dir should exist - mkdirSyncRecursive not working?');
test.equals(fs.existsSync(f1Path), true, 'File should exist');
test.equals(fs.existsSync(f2Path), true, 'File should exist');
test.equals(fs.existsSync(f3Path), true, 'File should exist');

wrench.rmdirSyncRecursive(dir);

test.equals(fs.existsSync(dir), false, 'Dir should not exist now...');
test.equals(fs.existsSync(f1Path), false, 'File should not exist');
test.equals(fs.existsSync(f2Path), true, 'File should exist');
test.equals(fs.existsSync(f3Path), true, 'File should exist');

wrench.rmdirSyncRecursive(path.dirname(path.dirname(dir)));

test.done();
},

test_rmdirSyncRecursiveFromRoot: function(test) {
var dir = __dirname + '/_tmp3/foo/bar';

wrench.mkdirSyncRecursive(dir, '777');

var f1Path = path.join(dir, 'test1.txt');
var f2Path = path.join(path.dirname(dir), 'test2.txt');
var f3Path = path.join(path.dirname(path.dirname(dir)), 'test3.txt');

fs.writeFileSync(f1Path, 'foo bar baz');
fs.writeFileSync(f2Path, 'foo bar baz');
fs.writeFileSync(f3Path, 'foo bar baz');

fs.chmodSync(f1Path, '777');
fs.chmodSync(f2Path, '777');
fs.chmodSync(f3Path, '777');

test.equals(fs.existsSync(dir), true, 'Dir should exist - mkdirSyncRecursive not working?');
test.equals(fs.existsSync(f1Path), true, 'File should exist');
test.equals(fs.existsSync(f2Path), true, 'File should exist');
test.equals(fs.existsSync(f3Path), true, 'File should exist');

wrench.rmdirSyncRecursive(path.dirname(path.dirname(dir)));

test.equals(fs.existsSync(dir), false, 'Dir should not exist now...');
test.equals(fs.existsSync(f1Path), false, 'File should not exist');
test.equals(fs.existsSync(f2Path), false, 'File should not exist');
test.equals(fs.existsSync(f3Path), false, 'File should not exist');

test.done();
}
});

// vim: et ts=4 sw=4
3 changes: 2 additions & 1 deletion tests/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
module.exports = {
group_mkdir: require('./mkdir'),
group_readdir: require('./readdir'),
group_copydir: require('./copydirsync_unix')
group_copydir: require('./copydirsync_unix'),
group_rmdir: require('./rmdirSyncRecursive')
};

0 comments on commit 97f22f7

Please sign in to comment.