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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/grep.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want this under 'valids' or 'invalids?
Even though it's a non-0 exit code, I would personally consider testing for the absence of something to be a "valid" use case, but I'm happy to move it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spot LGTM

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