From 163e04955fb647098265648c17c249371f1dda95 Mon Sep 17 00:00:00 2001 From: William Yardley Date: Wed, 7 Nov 2018 09:37:01 -0800 Subject: [PATCH] fix: Exit 1 with no output if no match (#900) 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 --- src/grep.js | 4 ++++ test/grep.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/grep.js b/src/grep.js index b696792a..8b3f0226 100644 --- a/src/grep.js +++ b/src/grep.js @@ -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; diff --git a/test/grep.js b/test/grep.js index f973640c..8f6a0c6a 100644 --- a/test/grep.js +++ b/test/grep.js @@ -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 // @@ -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');