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
11 changes: 10 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ export const shouldReadStdin = (args) => {
if (parsedArgs.n && (cmd === 'head' || cmd === 'tail')) {
requiredNumArgs++;
}
if ((parsedArgs.A || parsedArgs.B || parsedArgs.C) && cmd === 'grep') {
requiredNumArgs++;
}

return Boolean(!process.stdin.isTTY && parsedArgs._.length < requiredNumArgs);
if (process.stdin.isTTY) {
return false;
}
if (parsedArgs._.length >= requiredNumArgs) {
return false;
}
return true;
};
13 changes: 13 additions & 0 deletions test/specs/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ describe('cli', () => {
shouldReadStdin(['head', '-n', '1']).should.equal(true);
shouldReadStdin(['tail', '-n', '1']).should.equal(true);
shouldReadStdin(['grep', '-i', 'a.*z']).should.equal(true);
shouldReadStdin(['grep', '-A', 2, 'a.*z']).should.equal(true);
shouldReadStdin(['grep', '-B', 2, 'a.*z']).should.equal(true);
shouldReadStdin(['grep', '-C', 2, 'a.*z']).should.equal(true);
});

it('does not read stdin if process.stdin is a TTY', () => {
Expand Down Expand Up @@ -433,10 +436,20 @@ describe('cli', () => {
afterEach(() => {
shell.rm('-f', testFileName);
});

it('works with regex syntax', () => {
const ret = cli('grep', 'fo*', testFileName);
ret.stdout.should.equal('foo\nf\nsomething foo\n');
});

it('correctly parses -A/-B/-C flags', () => {
let ret = cli('grep', '-A', '1', 'foo', testFileName);
ret.stdout.should.equal('foo\nf\n--\nsomething foo\n');
ret = cli('grep', '-B', '1', 'foo', testFileName);
ret.stdout.should.equal('1st line\nfoo\n--\ndoes not match\nsomething foo\n');
ret = cli('grep', '-C', '1', 'foo', testFileName);
ret.stdout.should.equal('1st line\nfoo\nf\ndoes not match\nsomething foo\n');
});
});

describe('chmod', () => {
Expand Down