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

options return true #417

Merged
merged 2 commits into from
Jul 11, 2024
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
47 changes: 38 additions & 9 deletions src/find/matchers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ fn build_matcher_tree(
args: &[&str],
config: &mut Config,
arg_index: usize,
expecting_bracket: bool,
mut expecting_bracket: bool,
) -> Result<(usize, Box<dyn Matcher>), Box<dyn Error>> {
let mut top_level_matcher = ListMatcherBuilder::new();

Expand Down Expand Up @@ -400,7 +400,7 @@ fn build_matcher_tree(
}
i += 1;
regex_type = regex::RegexType::from_str(args[i])?;
None
Some(TrueMatcher.into_box())
}
"-regex" => {
if i >= args.len() - 1 {
Expand Down Expand Up @@ -695,38 +695,38 @@ fn build_matcher_tree(
"-noleaf" => {
// No change of behavior
config.no_leaf_dirs = true;
None
Some(TrueMatcher.into_box())
}
"-d" | "-depth" => {
// TODO add warning if it appears after actual testing criterion
config.depth_first = true;
None
Some(TrueMatcher.into_box())
}
"-mount" | "-xdev" => {
// TODO add warning if it appears after actual testing criterion
config.same_file_system = true;
None
Some(TrueMatcher.into_box())
}
"-sorted" => {
// TODO add warning if it appears after actual testing criterion
config.sorted_output = true;
None
Some(TrueMatcher.into_box())
}
"-maxdepth" => {
if i >= args.len() - 1 {
return Err(From::from(format!("missing argument to {}", args[i])));
}
config.max_depth = convert_arg_to_number(args[i], args[i + 1])?;
i += 1;
None
Some(TrueMatcher.into_box())
}
"-mindepth" => {
if i >= args.len() - 1 {
return Err(From::from(format!("missing argument to {}", args[i])));
}
config.min_depth = convert_arg_to_number(args[i], args[i + 1])?;
i += 1;
None
Some(TrueMatcher.into_box())
}
"-help" | "--help" => {
config.help_requested = true;
Expand Down Expand Up @@ -772,6 +772,12 @@ fn build_matcher_tree(
}
}
};
i += 1;
if config.help_requested || config.version_requested {
// Ignore anything, even invalid expressions, after -help/-version
expecting_bracket = false;
break;
}
if let Some(submatcher) = possible_submatcher {
if invert_next_matcher {
top_level_matcher.new_and_condition(NotMatcher::new(submatcher));
Expand All @@ -780,7 +786,6 @@ fn build_matcher_tree(
top_level_matcher.new_and_condition(submatcher);
}
}
i += 1;
}
if expecting_bracket {
return Err(From::from(
Expand Down Expand Up @@ -1482,4 +1487,28 @@ mod tests {
}
}
}

#[test]
fn build_top_level_matcher_option_logical() {
let mut config = Config::default();
build_top_level_matcher(&["-maxdepth", "0", "-a", "-print"], &mut config)
.expect("logical operators like -a should work with options");
assert_eq!(config.max_depth, 0);
}

#[test]
fn build_top_level_matcher_help_invalid() {
let mut config = Config::default();
build_top_level_matcher(&["(", "-help", "-a"], &mut config)
.expect("-help should stop parsing");
assert!(config.help_requested);
}

#[test]
fn build_top_level_matcher_version_invalid() {
let mut config = Config::default();
build_top_level_matcher(&["(", "-version", "-o", ")", ")"], &mut config)
.expect("-version should stop parsing");
assert!(config.version_requested);
}
}
22 changes: 22 additions & 0 deletions src/find/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,4 +1236,26 @@ mod tests {

assert_eq!(rc, 0);
}

#[test]
fn find_maxdepth_and() {
let deps = FakeDependencies::new();
let rc = find_main(
&[
"find",
&fix_up_slashes("./test_data/depth"),
"-maxdepth",
"0",
"-a",
"-print",
],
&deps,
);

assert_eq!(rc, 0);
assert_eq!(
deps.get_output_as_string(),
fix_up_slashes("./test_data/depth\n")
);
}
}
Loading