Skip to content

Commit 2da9ab5

Browse files
nfischerfreitagbr
authored andcommitted
fix: allow non-normalized paths as input to mkdir (shelljs#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 shelljs#634
1 parent a3e622b commit 2da9ab5

File tree

5 files changed

+34
-2
lines changed

5 files changed

+34
-2
lines changed

src/mkdir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function _mkdir(options, dirs) {
7575

7676
try {
7777
if (options.fullpath) {
78-
mkdirSyncRecursive(dir);
78+
mkdirSyncRecursive(path.resolve(dir));
7979
} else {
8080
fs.mkdirSync(dir, parseInt('0777', 8));
8181
}

test/cp.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ test(
259259
);
260260

261261
test('recursive, everything exists, no force flag', t => {
262-
shell.cp('-R', 'resources/cp', t.context.tmp);
263262
const result = shell.cp('-R', 'resources/cp', t.context.tmp);
264263
t.falsy(shell.error()); // crash test only
265264
t.falsy(result.stderr);
@@ -647,3 +646,10 @@ test('Test with recursive option and symlinks.', t => {
647646
t.falsy(shell.test('-L', 'sym.lnk'));
648647
});
649648
});
649+
650+
test('recursive, with a non-normalized path', t => {
651+
const result = shell.cp('-R', 'resources/../resources/./cp', t.context.tmp);
652+
t.falsy(shell.error()); // crash test only
653+
t.falsy(result.stderr);
654+
t.is(result.code, 0);
655+
});

test/ls.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,3 +466,13 @@ test('Check stderr field', t => {
466466
t.truthy(shell.error());
467467
t.is('ls: no such file or directory: /asdfasdf', result.stderr);
468468
});
469+
470+
test('non-normalized paths are still ok with -R', t => {
471+
const result = shell.ls('-R', 'resources/./ls/../ls');
472+
t.falsy(shell.error());
473+
t.is(result.code, 0);
474+
t.truthy(result.indexOf('a_dir') > -1);
475+
t.truthy(result.indexOf('a_dir/b_dir') > -1);
476+
t.truthy(result.indexOf('a_dir/b_dir/z') > -1);
477+
t.is(result.length, 9);
478+
});

test/mkdir.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,10 @@ test('globbed dir', t => {
147147
t.truthy(fs.existsSync(`${t.context.tmp}/mydir`));
148148
t.falsy(fs.existsSync(`${t.context.tmp}/m*ir`)); // doesn't create literal name
149149
});
150+
151+
test('non-normalized paths are still ok with -p', t => {
152+
const result = shell.mkdir('-p', `${t.context.tmp}/asdf/../asdf/./`);
153+
t.falsy(shell.error());
154+
t.is(result.code, 0);
155+
t.truthy(fs.existsSync(`${t.context.tmp}/asdf`));
156+
});

test/rm.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,12 @@ test('remove broken symbolic link', t => {
282282
t.falsy(fs.existsSync(`${t.context.tmp}/rm/fake.lnk`));
283283
}
284284
});
285+
286+
test('recursive dir removal, for non-normalized path', t => {
287+
shell.mkdir('-p', `${t.context.tmp}/a/b/c`);
288+
t.truthy(fs.existsSync(`${t.context.tmp}/a/b/c`));
289+
const result = shell.rm('-rf', `${t.context.tmp}/a/.././a`);
290+
t.falsy(shell.error());
291+
t.is(result.code, 0);
292+
t.falsy(fs.existsSync(`${t.context.tmp}/a`));
293+
});

0 commit comments

Comments
 (0)