Skip to content

Commit

Permalink
Merge branch 'main' into xargs-replace
Browse files Browse the repository at this point in the history
  • Loading branch information
YDX-2147483647 authored May 23, 2024
2 parents b4b5121 + 0693242 commit 32986a8
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "findutils"
version = "0.5.0"
version = "0.5.1"
homepage = "https://github.com/uutils/findutils"
repository = "https://github.com/uutils/findutils"
edition = "2021"
Expand Down Expand Up @@ -54,6 +54,6 @@ ci = ["github"]
# The installers to generate for each app
installers = []
# Target platforms to build apps for (Rust target-triple syntax)
targets = ["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
targets = ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "aarch64-unknown-linux-musl", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl", "x86_64-pc-windows-msvc"]
# Publish jobs to run in CI
pr-run-mode = "plan"
55 changes: 54 additions & 1 deletion src/find/matchers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn convert_arg_to_comparable_value(
option_name: &str,
value_as_string: &str,
) -> Result<ComparableValue, Box<dyn Error>> {
let re = Regex::new(r"([+-]?)(\d+)$")?;
let re = Regex::new(r"^([+-]?)(\d+)$")?;
if let Some(groups) = re.captures(value_as_string) {
if let Ok(val) = groups[2].parse::<u64>() {
return Ok(match &groups[1] {
Expand Down 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 @@ -1260,6 +1282,20 @@ mod tests {
}
}

#[test]
fn convert_exception_arg_to_comparable_value_test() {
let exception_args = ["1%2", "1%2%3", "1a2", "1%2a", "abc", "-", "+", "%"];

for arg in exception_args {
let comparable = convert_arg_to_comparable_value("test", arg);
assert!(
comparable.is_err(),
"{} should be parse to Comparable correctly",
arg
);
}
}

#[test]
fn parse_date_str_to_timestamps_test() {
let full_date_timestamps = parse_date_str_to_timestamps("jan 01, 2025 00:00:01").unwrap();
Expand All @@ -1284,6 +1320,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
30 changes: 17 additions & 13 deletions src/find/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,15 +921,17 @@ mod tests {
let args = ["-newerat", "-newerBt", "-newerct", "-newermt"];
let times = ["jan 01, 2000", "jan 01, 2000 00:00:00"];

for (arg, time) in args.iter().zip(times.iter()) {
let deps = FakeDependencies::new();
let rc = find_main(&["find", "./test_data/simple/subdir", arg, time], &deps);
for arg in args {
for time in times {
let deps = FakeDependencies::new();
let rc = find_main(&["find", "./test_data/simple/subdir", arg, time], &deps);

assert_eq!(rc, 0);
assert_eq!(
deps.get_output_as_string(),
fix_up_slashes("./test_data/simple/subdir\n./test_data/simple/subdir/ABBBC\n"),
);
assert_eq!(rc, 0);
assert_eq!(
deps.get_output_as_string(),
fix_up_slashes("./test_data/simple/subdir\n./test_data/simple/subdir/ABBBC\n"),
);
}
}
}

Expand All @@ -942,12 +944,14 @@ mod tests {
let args = ["-newerat", "-newerBt", "-newerct", "-newermt"];
let times = ["jan 01, 2037", "jan 01, 2037 00:00:00"];

for (arg, time) in args.iter().zip(times.iter()) {
let deps = FakeDependencies::new();
let rc = find_main(&["find", "./test_data/simple/subdir", arg, time], &deps);
for arg in args {
for time in times {
let deps = FakeDependencies::new();
let rc = find_main(&["find", "./test_data/simple/subdir", arg, time], &deps);

assert_eq!(rc, 0);
assert_eq!(deps.get_output_as_string(), "");
assert_eq!(rc, 0);
assert_eq!(deps.get_output_as_string(), "");
}
}
}

Expand Down
69 changes: 69 additions & 0 deletions tests/find_cmd_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,32 @@ fn find_accessible() {
.stdout(predicate::str::contains("abbbc").not());
}

#[test]
fn find_time() {
let args = ["1", "+1", "-1"];
let exception_args = ["1%2", "1%2%3", "1a2", "1%2a", "abc", "-", "+", "%"];

["-ctime", "-atime", "-mtime"].iter().for_each(|flag| {
args.iter().for_each(|arg| {
Command::cargo_bin("find")
.expect("found binary")
.args([".", flag, arg])
.assert()
.success()
.stderr(predicate::str::is_empty());
});

exception_args.iter().for_each(|arg| {
Command::cargo_bin("find")
.expect("found binary")
.args([".", flag, arg])
.assert()
.failure()
.stdout(predicate::str::is_empty());
});
});
}

#[test]
fn expression_empty_parentheses() {
Command::cargo_bin("find")
Expand All @@ -518,3 +544,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());
}
}
}

0 comments on commit 32986a8

Please sign in to comment.