Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Exit 1 with empty string if no match #901

Merged
merged 1 commit into from Nov 13, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 => {
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: ');
wyardley marked this conversation as resolved.
Show resolved Hide resolved
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