mirror of
https://github.com/shelljs/shelljs.git
synced 2026-01-18 16:03:37 +00:00
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:
parent
bbe521b57d
commit
92d3f3982e
@ -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;
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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',
|
||||
|
||||
@ -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
|
||||
//
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user