Skip to content

Commit

Permalink
fix: Exit 1 with no output if no match (#900) (#901)
Browse files Browse the repository at this point in the history
When we can read all files, but none match, exit 1 and return no output. This
matches the behavior of grep more closely.

Fixes #900
  • Loading branch information
wyardley authored and nfischer committed Nov 13, 2018
1 parent d4d1317 commit 5da1dda
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/grep.js
Expand Up @@ -68,6 +68,10 @@ function _grep(options, regex, files) {
}
});

if (grep.length === 0 && common.state.errorCode !== 2) {
// We didn't hit the error above, but pattern didn't match
common.error('', { silent: true });
}
return grep.join('\n') + '\n';
}
module.exports = _grep;
18 changes: 18 additions & 0 deletions test/grep.js
Expand Up @@ -49,6 +49,13 @@ test('if at least one file is missing, this should be an error', t => {
t.is(result.code, 2);
});

test("multiple files, one doesn't exist, one doesn't match", t => {
const result = shell.grep(/oogabooga/, 'test/resources/file1.txt',
'test/resources/filedoesnotexist.txt');
t.truthy(shell.error());
t.is(result.code, 2);
});

//
// Valids
//
Expand Down Expand Up @@ -127,6 +134,17 @@ test('one file, * in string-regex, make sure * is not globbed', t => {
t.is(result.toString(), 'this line ends in.js\nlllllllllllllllll.js\n');
});

test("one file, pattern doesn't match", t => {
const result = shell.grep('notfoundstring', 'test/resources/grep/file');
t.truthy(shell.error());
t.is(result.toString(), '');
t.is(result.stdout, '');
// TODO(#900): "grep: " isn't really the correct stderr output, but we need a
// non-empty string so `shell.error()` is truthy.
t.is(result.stderr, 'grep: ');
t.is(result.code, 1);
});

test('-l option', t => {
const result = shell.grep('-l', 'test1', 'test/resources/file1', 'test/resources/file2',
'test/resources/file1.txt');
Expand Down

0 comments on commit 5da1dda

Please sign in to comment.