Fix common.expand error (#709)

* When glob.sync errors, return original array element

* Add test for cp with empty string
This commit is contained in:
Brandon Freitag 2017-05-02 19:54:56 -07:00 committed by GitHub
parent bbe521b57d
commit 92d3f3982e
4 changed files with 36 additions and 3 deletions

View File

@ -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;

View File

@ -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
}

View File

@ -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',

View File

@ -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
//