From 50b769970d4d4e6eec86686646b6be9751dfd96f Mon Sep 17 00:00:00 2001 From: go-ive Date: Sun, 10 May 2015 14:11:33 +0200 Subject: [PATCH] added handling for directories with no permission --- lib/file.js | 7 ++++--- tests/file_spec.js | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/file.js b/lib/file.js index 1eb92df..ca952d4 100644 --- a/lib/file.js +++ b/lib/file.js @@ -114,6 +114,7 @@ exports.walk = function (start, callback) { if (stat.isDirectory()) { fs.readdir(start, function (err, files) { + if(!files) return callback(new Error("path: " + start + " is not accessible.")); var coll = files.reduce(function (acc, i) { var abspath = path.join(start, i); @@ -184,12 +185,12 @@ exports.path = {}; exports.path.abspath = function (to) { var from; switch (to.charAt(0)) { - case "~": from = process.env.HOME; to = to.substr(1); break - case path.sep: from = ""; break + case "~": from = process.env.HOME; to = to.substr(1); break; + case path.sep: from = ""; break; default : from = process.cwd(); break } return path.join(from, to); -} +}; exports.path.relativePath = function (base, compare) { base = base.split(path.sep); diff --git a/tests/file_spec.js b/tests/file_spec.js index 38477b5..cfc8cea 100644 --- a/tests/file_spec.js +++ b/tests/file_spec.js @@ -111,3 +111,13 @@ describe("file.path#relativePath", function () { done(); }); }); + +describe("should handle directory with no access permission", function(){ + it('should write error when no permission', function(done){ + file.walk('test_resource/nopermdir', function (err) { + assert.notEqual(err, null); + assert.equal(err.message, 'path: test_resource/nopermdir is not accessible.'); + done(); + }); + }); +}); \ No newline at end of file