From 2a0914f39072bbd1468349ed788f2531600d5b2f Mon Sep 17 00:00:00 2001 From: esarbanis Date: Sun, 29 May 2016 23:44:50 +0300 Subject: [PATCH] Provision for root path when splitting Closes #16 --- lib/util.js | 2 +- test/lib/util.spec.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/util.js b/lib/util.js index cb468545..34fccd9a 100644 --- a/lib/util.js +++ b/lib/util.js @@ -12,7 +12,7 @@ var uniqueDirs = (exports.uniqueDirs = function(files) { var dirs = {}; files.forEach(function(filepath) { var parts = path.dirname(filepath).split(path.sep); - var partial = parts[0]; + var partial = parts[0] || '/'; dirs[partial] = true; for (var i = 1, ii = parts.length; i < ii; ++i) { partial = path.join(partial, parts[i]); diff --git a/test/lib/util.spec.js b/test/lib/util.spec.js index afa44fce..3132725c 100644 --- a/test/lib/util.spec.js +++ b/test/lib/util.spec.js @@ -66,6 +66,29 @@ describe('util', function() { assert.deepEqual(got, expected); }); + + it('gets a list of unique directories on absolute paths', function() { + var absoluteFiles = files.map(function(path) { + return '/' + path; + }); + // not comparing order here, so we sort both + var got = util.uniqueDirs(absoluteFiles).sort(); + + var expected = [ + '/', + '/a1', + '/a2', + path.join('/a1', 'b1'), + path.join('/a1', 'b1', 'c1'), + path.join('/a1', 'b1', 'c2'), + path.join('/a1', 'b2'), + path.join('/a1', 'b2', 'c1'), + path.join('/a1', 'b2', 'c2'), + path.join('/a2', 'b1') + ].sort(); + + assert.deepEqual(got, expected); + }); }); describe('dirsToCreate', function() {