From 2da9ab55be124dfa5dd745d27ea313dbb56965c6 Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Sat, 7 Jan 2017 18:06:15 -0800 Subject: [PATCH] fix: allow non-normalized paths as input to mkdir (#635) Adds tests to make sure that non-normalized paths (i.e. path/to/./dir) are valid for a few commands, including mkdir() which previously failed when given the -p flag. Fixes #634 --- src/mkdir.js | 2 +- test/cp.js | 8 +++++++- test/ls.js | 10 ++++++++++ test/mkdir.js | 7 +++++++ test/rm.js | 9 +++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/mkdir.js b/src/mkdir.js index f211bc8..0b8c6fe 100644 --- a/src/mkdir.js +++ b/src/mkdir.js @@ -75,7 +75,7 @@ function _mkdir(options, dirs) { try { if (options.fullpath) { - mkdirSyncRecursive(dir); + mkdirSyncRecursive(path.resolve(dir)); } else { fs.mkdirSync(dir, parseInt('0777', 8)); } diff --git a/test/cp.js b/test/cp.js index a7faec9..dcd9b08 100644 --- a/test/cp.js +++ b/test/cp.js @@ -259,7 +259,6 @@ test( ); test('recursive, everything exists, no force flag', t => { - shell.cp('-R', 'resources/cp', t.context.tmp); const result = shell.cp('-R', 'resources/cp', t.context.tmp); t.falsy(shell.error()); // crash test only t.falsy(result.stderr); @@ -647,3 +646,10 @@ test('Test with recursive option and symlinks.', t => { t.falsy(shell.test('-L', 'sym.lnk')); }); }); + +test('recursive, with a non-normalized path', t => { + const result = shell.cp('-R', 'resources/../resources/./cp', t.context.tmp); + t.falsy(shell.error()); // crash test only + t.falsy(result.stderr); + t.is(result.code, 0); +}); diff --git a/test/ls.js b/test/ls.js index cc5cca2..56372a5 100644 --- a/test/ls.js +++ b/test/ls.js @@ -466,3 +466,13 @@ test('Check stderr field', t => { t.truthy(shell.error()); t.is('ls: no such file or directory: /asdfasdf', result.stderr); }); + +test('non-normalized paths are still ok with -R', t => { + const result = shell.ls('-R', 'resources/./ls/../ls'); + t.falsy(shell.error()); + t.is(result.code, 0); + t.truthy(result.indexOf('a_dir') > -1); + t.truthy(result.indexOf('a_dir/b_dir') > -1); + t.truthy(result.indexOf('a_dir/b_dir/z') > -1); + t.is(result.length, 9); +}); diff --git a/test/mkdir.js b/test/mkdir.js index 343828f..7f02999 100644 --- a/test/mkdir.js +++ b/test/mkdir.js @@ -147,3 +147,10 @@ test('globbed dir', t => { t.truthy(fs.existsSync(`${t.context.tmp}/mydir`)); t.falsy(fs.existsSync(`${t.context.tmp}/m*ir`)); // doesn't create literal name }); + +test('non-normalized paths are still ok with -p', t => { + const result = shell.mkdir('-p', `${t.context.tmp}/asdf/../asdf/./`); + t.falsy(shell.error()); + t.is(result.code, 0); + t.truthy(fs.existsSync(`${t.context.tmp}/asdf`)); +}); diff --git a/test/rm.js b/test/rm.js index a3f6b10..f6ead97 100644 --- a/test/rm.js +++ b/test/rm.js @@ -282,3 +282,12 @@ test('remove broken symbolic link', t => { t.falsy(fs.existsSync(`${t.context.tmp}/rm/fake.lnk`)); } }); + +test('recursive dir removal, for non-normalized path', t => { + shell.mkdir('-p', `${t.context.tmp}/a/b/c`); + t.truthy(fs.existsSync(`${t.context.tmp}/a/b/c`)); + const result = shell.rm('-rf', `${t.context.tmp}/a/.././a`); + t.falsy(shell.error()); + t.is(result.code, 0); + t.falsy(fs.existsSync(`${t.context.tmp}/a`)); +});