Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5da1dda

Browse files
wyardleynfischer
authored andcommitted
fix: Exit 1 with no output if no match (#900) (#901)
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
1 parent d4d1317 commit 5da1dda

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/grep.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ function _grep(options, regex, files) {
6868
}
6969
});
7070

71+
if (grep.length === 0 && common.state.errorCode !== 2) {
72+
// We didn't hit the error above, but pattern didn't match
73+
common.error('', { silent: true });
74+
}
7175
return grep.join('\n') + '\n';
7276
}
7377
module.exports = _grep;

test/grep.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ test('if at least one file is missing, this should be an error', t => {
4949
t.is(result.code, 2);
5050
});
5151

52+
test("multiple files, one doesn't exist, one doesn't match", t => {
53+
const result = shell.grep(/oogabooga/, 'test/resources/file1.txt',
54+
'test/resources/filedoesnotexist.txt');
55+
t.truthy(shell.error());
56+
t.is(result.code, 2);
57+
});
58+
5259
//
5360
// Valids
5461
//
@@ -127,6 +134,17 @@ test('one file, * in string-regex, make sure * is not globbed', t => {
127134
t.is(result.toString(), 'this line ends in.js\nlllllllllllllllll.js\n');
128135
});
129136

137+
test("one file, pattern doesn't match", t => {
138+
const result = shell.grep('notfoundstring', 'test/resources/grep/file');
139+
t.truthy(shell.error());
140+
t.is(result.toString(), '');
141+
t.is(result.stdout, '');
142+
// TODO(#900): "grep: " isn't really the correct stderr output, but we need a
143+
// non-empty string so `shell.error()` is truthy.
144+
t.is(result.stderr, 'grep: ');
145+
t.is(result.code, 1);
146+
});
147+
130148
test('-l option', t => {
131149
const result = shell.grep('-l', 'test1', 'test/resources/file1', 'test/resources/file2',
132150
'test/resources/file1.txt');

0 commit comments

Comments
 (0)