-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Max depth option #111
Max depth option #111
Conversation
I'm not sure of how to write a unit test because I think I would just be testing Let me know if you are more clever than me in this regard. |
@@ -136,6 +136,9 @@ Less common options: | |||
-L, --follow | |||
Follow symlinks. | |||
|
|||
--maxdepth NUM | |||
Descend at most NUM directories below the command line arguments. | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add this flag to doc/rg.1.md
and run the convert script to create a new doc/rg.1
file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe clarify that a value of 0
searches the current directory only.
WalkDir::new(path).follow_links(self.follow).max_depth(self.maxdepth) | ||
} else { | ||
WalkDir::new(path).follow_links(self.follow) | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I like this better, stylistically:
let mut wd = WalkDir::new(path).follow_links(self.follow);
if let Some(maxdepth) = self.maxdepth {
wd = wd.max_depth(maxdepth);
}
@@ -681,7 +687,12 @@ impl Args { | |||
|
|||
/// Create a new recursive directory iterator at the path given. | |||
pub fn walker(&self, path: &Path) -> Result<walk::Iter> { | |||
let wd = WalkDir::new(path).follow_links(self.follow); | |||
let wd = | |||
if self.maxdepth > 0 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want to allow a value of 0
to mean "search current directory." In this case, maxdepth
should be an Option<usize>
, which docopt will handle correctly.
@gsquire Could you write an integration test please? It would be good to follow the pattern I've been using whenever I introduce new command line flags: https://github.com/BurntSushi/ripgrep/blob/master/tests/tests.rs#L781-L820 Other than that and a few nits, this looks good, thanks! |
1b8404b
to
b4ed9e5
Compare
I rebased onto master and squashed. Is that the style of integration test you expected? |
|
||
cmd.arg("--maxdepth").arg("1"); | ||
|
||
wd.assert_err(&mut cmd); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a file one/foo
with far
in it, and assert that you get results? That seems like a slightly better test since it makes sure search still works, but that it also respects the depth setting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so I think with one/foo
, you do indeed need to set maxdepth
to 2
. The argument, .
, corresponds to depth 0
. one
is depth 1
and one/foo
is depth 2
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, I thought the count applied to directories and not their contents. I'll update it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The count applies to whether to descend into a directory or not. At depth 0
, you don't descend ever, you just yield whatever was given. So a walkdir of ./
at depth 0
yields ./
. At depth 1
, you descend once, so that a walkdir of ./
at depth 1
yields one
, but doesn't descend into one
. Finally, at depth 2
, a walkdir of ./
yields ./one
and then ./one/too
and also ./one/foo
, but doesn't descend into ./one/too
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense, thanks for a detailed explanation. I suppose it's really a "tree" after all.
@gsquire That's perfect! I had one last suggestion and then this is ready to go. Thanks so much! |
@BurntSushi Doing this causes the test to fail: wd.create_dir("one");
wd.create("one/foo", "far");
wd.create_dir("one/too");
wd.create("one/too/many", "far");
cmd.arg("--maxdepth").arg("1");
let lines: String = wd.stdout(&mut cmd);
let expected = path("one/pass:far\n");
assert_eq!(lines, expected); But changing the depth to 2 passes...am I misunderstanding the depth used in |
I goofed. Looks like your initial implementation is correct. I was off by On Sep 27, 2016 2:40 PM, "Garrett Squire" [email protected] wrote:
|
It's okay, just wanted to verify. Quoting from the GNU find man page:
So no need to update anything? I have to say, I thought having max depth of one and having a file in a directory below |
@gsquire Ah OK. Looks good. I think just updating the test to make sure it both ignores a deeper directory and gets search results from a shallower directory should be sufficient. Also, I think the docs need to be updated. Probably saying "current directory" is wrong and we should say "starting points" like |
Sweet, I know the phrasing is important. I updated that and the test. Hope it's good to go! |
CR and add integration test
Looks like the build failure happened on master too...strange. |
Yup this looks great. The failure looks unrelated. Thanks so much for doing this and putting up with me! :-) |
I'm happy to be able to contribute, you're a Rust machine :) Congrats on this project! |
Closes #109