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

find: Implement -anewer and -cnewer. #386

Merged
merged 6 commits into from
May 22, 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
39 changes: 39 additions & 0 deletions src/find/matchers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,29 @@ fn parse_date_str_to_timestamps(date_str: &str) -> Option<i64> {
/// X and Y from the -newerXY string.
/// X and Y are constrained to a/B/c/m and t.
/// such as: "-neweraB" -> Some(a, B) "-neweraD" -> None
///
/// Additionally, there is support for the -anewer and -cnewer short arguments. as follows:
/// 1. -anewer is equivalent to -neweram
/// 2. -cnewer is equivalent to - newercm
///
/// If -newer is used it will be resolved to -newermm.
fn parse_str_to_newer_args(input: &str) -> Option<(String, String)> {
if input.is_empty() {
return None;
}

if input == "-newer" {
return Some(("m".to_string(), "m".to_string()));
}

if input == "-anewer" {
return Some(("a".to_string(), "m".to_string()));
}

if input == "-cnewer" {
return Some(("c".to_string(), "m".to_string()));
}

let re = Regex::new(r"-newer([aBcm])([aBcmt])").unwrap();
if let Some(captures) = re.captures(input) {
let x = captures.get(1)?.as_str().to_string();
Expand Down Expand Up @@ -1284,6 +1306,23 @@ mod tests {

#[test]
fn parse_str_to_newer_args_test() {
// test for error case
let arg = parse_str_to_newer_args("");
assert!(arg.is_none());

// test for short options
// -newer equivalent to -newermm
let arg = parse_str_to_newer_args("-newer").unwrap();
assert_eq!(("m".to_string(), "m".to_string()), arg);

// -anewer equivalent to -neweram
let arg = parse_str_to_newer_args("-anewer").unwrap();
assert_eq!(("a".to_string(), "m".to_string()), arg);

// -cnewer equivalent to - newercm
let arg = parse_str_to_newer_args("-cnewer").unwrap();
assert_eq!(("c".to_string(), "m".to_string()), arg);

let x_options = ["a", "B", "c", "m"];
let y_options = ["a", "B", "c", "m", "t"];

Expand Down
20 changes: 8 additions & 12 deletions src/find/matchers/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,16 +506,12 @@ mod tests {
#[test]
fn newer_option_matcher() {
#[cfg(target_os = "linux")]
let x_options = ["a", "c", "m"];
let options = ["a", "c", "m"];
#[cfg(not(target_os = "linux"))]
let x_options = ["a", "B", "c", "m"];
#[cfg(target_os = "linux")]
let y_options = ["a", "c", "m"];
#[cfg(not(target_os = "linux"))]
let y_options = ["a", "B", "c", "m"];
let options = ["a", "B", "c", "m"];

for x_option in &x_options {
for y_option in &y_options {
for x_option in options {
for y_option in options {
let temp_dir = Builder::new().prefix("example").tempdir().unwrap();
let temp_dir_path = temp_dir.path().to_string_lossy();
let new_file_name = "newFile";
Expand All @@ -526,8 +522,8 @@ mod tests {
let old_file = get_dir_entry_for("test_data", "simple");
let deps = FakeDependencies::new();
let matcher = NewerOptionMatcher::new(
(*x_option).to_string(),
(*y_option).to_string(),
x_option.to_string(),
y_option.to_string(),
&old_file.path().to_string_lossy(),
);

Expand All @@ -542,8 +538,8 @@ mod tests {
// thus causing the Matcher to generate an IO error after matching.
let _ = fs::remove_file(&*new_file.path().to_string_lossy());
let matcher = NewerOptionMatcher::new(
(*x_option).to_string(),
(*y_option).to_string(),
x_option.to_string(),
y_option.to_string(),
&old_file.path().to_string_lossy(),
);
assert!(
Expand Down
43 changes: 43 additions & 0 deletions tests/find_cmd_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,46 @@ fn expression_empty_parentheses() {
))
.stdout(predicate::str::is_empty());
}

#[test]
#[serial(working_dir)]
fn find_newer_xy() {
#[cfg(target_os = "linux")]
let options = ["a", "c", "m"];
#[cfg(not(target_os = "linux"))]
let options = ["a", "B", "c", "m"];

for x in options {
for y in options {
let arg = &format!("-newer{x}{y}");
Command::cargo_bin("find")
.expect("found binary")
.args([
"./test_data/simple/subdir",
arg,
"./test_data/simple/subdir/ABBBC",
])
.assert()
.success()
.stderr(predicate::str::is_empty());
}
}

#[cfg(target_os = "linux")]
let args = ["-newerat", "-newerct", "-newermt"];
#[cfg(not(target_os = "linux"))]
let args = ["-newerat", "-newerBt", "-newerct", "-newermt"];
let times = ["jan 01, 2000", "jan 01, 2000 00:00:00"];

for arg in args {
for time in times {
let arg = &format!("{arg}{time}");
Command::cargo_bin("find")
.expect("found binary")
.args(["./test_data/simple/subdir", arg, time])
.assert()
.success()
.stderr(predicate::str::is_empty());
}
}
}
Loading