Skip to content

Grep directory support#1044

Open
c-pranshu wants to merge 4 commits intoshelljs:mainfrom
c-pranshu:grep-directory-support
Open

Grep directory support#1044
c-pranshu wants to merge 4 commits intoshelljs:mainfrom
c-pranshu:grep-directory-support

Conversation

@c-pranshu
Copy link
Copy Markdown

This Fixes #998

  • -r allows searching through directories in recursive way
  • works with array syntax
  • works with globs
  • added tests

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Sep 17, 2021

Codecov Report

Merging #1044 (e7fe833) into master (0ae1dd6) will decrease coverage by 0.02%.
The diff coverage is 100.00%.

❗ Current head e7fe833 differs from pull request most recent head 8a7c43e. Consider uploading reports for the commit 8a7c43e to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1044      +/-   ##
==========================================
- Coverage   95.64%   95.61%   -0.03%     
==========================================
  Files          35       35              
  Lines        1332     1347      +15     
==========================================
+ Hits         1274     1288      +14     
- Misses         58       59       +1     
Impacted Files Coverage Δ
src/grep.js 97.67% <100.00%> (-2.33%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0ae1dd6...8a7c43e. Read the comment docs.

Copy link
Copy Markdown
Member

@nfischer nfischer left a comment

Choose a reason for hiding this comment

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

This is cool! Thanks for writing this up. I left some initial comments. I haven't double-checked if the behavior is right though (I spot checked a couple spots, but I got different results than you in one of them), so you may want to look a bit closer there.

Either way, this seems like a good starting point for a valuable feature.

test('works with array syntax', t => {
const result = shell.grep('-r', 'test', ['test/r*', 'package.json'], 'scripts');
t.falsy(shell.error());
t.is(result.split('\n').length, 72);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this the right value? I get a different answer in my shell:

$ grep -r 'test' test/r* package.json scripts | wc -l
69

});

test('works with array syntax', t => {
const result = shell.grep('-r', 'test', ['test/r*', 'package.json'], 'scripts');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please do not include anything outside of the resources/ folder in the test (package.json, scripts, etc.). It makes the test much more difficult to maintain if we do unrelated changes in package.json etc.

I think the same goes for test/r* because that matches test/rm.js. Instead of this, how about adding something like test/resources/grep2 and testing the glob test/resources/g* if you want to test globbing?

const result = shell.grep('-r', /oogabooga/, 'test/resources', 'test/resources/random.txt');
t.truthy(shell.error());
t.is(result.code, 2);
t.is(result.stderr, 'grep: no such file or directory: test/resources/random.txt');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we also need to check result.stdout. My grep will return results for the folders which actually do exist.

t.is(result.split('\n').length, 29);
});

test('multiple directories or files provided', t => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should probably split this into two separate test cases for clarity.

  1. We want a test case where multiple directories/files are provided, but they do not overlap. ex. "test/", "different-folder/file.txt"
  2. We want a test case (like this one) where there is overlap. We should assert that the overlapping files are double-counted (because that's what POSIX grep does). ex. "test/", "test/file.txt" (file.txt should be greped twice).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: grep -R/-r flag

3 participants