From 92d3f3982e05b6c6f69ebef08858ee6ceebc8eaf Mon Sep 17 00:00:00 2001 From: Brandon Freitag Date: Tue, 2 May 2017 19:54:56 -0700 Subject: [PATCH] Fix common.expand error (#709) * When glob.sync errors, return original array element * Add test for cp with empty string --- src/common.js | 13 ++++++++++--- src/cp.js | 1 + test/common.js | 18 ++++++++++++++++++ test/cp.js | 7 +++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/common.js b/src/common.js index e0d343b..30b166d 100644 --- a/src/common.js +++ b/src/common.js @@ -257,9 +257,16 @@ function expand(list) { if (typeof listEl !== 'string') { expanded.push(listEl); } else { - var ret = glob.sync(listEl, config.globOptions); - // if glob fails, interpret the string literally - expanded = expanded.concat(ret.length > 0 ? ret : [listEl]); + var ret; + try { + ret = glob.sync(listEl, config.globOptions); + // if nothing matched, interpret the string literally + ret = ret.length > 0 ? ret : [listEl]; + } catch (e) { + // if glob fails, interpret the string literally + ret = [listEl]; + } + expanded = expanded.concat(ret); } }); return expanded; diff --git a/src/cp.js b/src/cp.js index 25b895e..500df2b 100644 --- a/src/cp.js +++ b/src/cp.js @@ -237,6 +237,7 @@ function _cp(options, sources, dest) { sources.forEach(function (src, srcIndex) { if (!fs.existsSync(src)) { + if (src === '') src = "''"; // if src was empty string, display empty string common.error('no such file or directory: ' + src, { continue: true }); return; // skip file } diff --git a/test/common.js b/test/common.js index 68051b7..024af6f 100644 --- a/test/common.js +++ b/test/common.js @@ -170,6 +170,24 @@ test('broken links still expand', t => { t.deepEqual(result, ['resources/badlink']); }); +test('empty array', t => { + const result = common.expand([]); + t.falsy(shell.error()); + t.deepEqual(result, []); +}); + +test('empty string', t => { + const result = common.expand(['']); + t.falsy(shell.error()); + t.deepEqual(result, ['']); +}); + +test('non-string', t => { + const result = common.expand([5]); + t.falsy(shell.error()); + t.deepEqual(result, [5]); +}); + test('common.parseOptions (normal case)', t => { const result = common.parseOptions('-Rf', { R: 'recursive', diff --git a/test/cp.js b/test/cp.js index d8ec034..e8cece0 100644 --- a/test/cp.js +++ b/test/cp.js @@ -98,6 +98,13 @@ test('too many sources #2', t => { t.is(result.stderr, 'cp: dest is not a directory (too many sources)'); }); +test('empty string source', t => { + const result = shell.cp('', 'dest'); + t.truthy(shell.error()); + t.is(result.code, 1); + t.is(result.stderr, "cp: no such file or directory: ''"); +}); + // // Valids //