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

manage the special case 'find %A+ #454

Merged
merged 2 commits into from
Sep 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
18 changes: 12 additions & 6 deletions src/find/matchers/printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,24 @@ enum TimeFormat {

impl TimeFormat {
fn apply(&self, time: SystemTime) -> Result<Cow<'static, str>, Box<dyn Error>> {
const CTIME_FORMAT: &str = "%a %b %d %H:%M:%S.%f0 %Y";

let formatted = match self {
TimeFormat::SinceEpoch => {
let duration = time.duration_since(SystemTime::UNIX_EPOCH)?;
format!("{}.{:09}0", duration.as_secs(), duration.subsec_nanos())
}
TimeFormat::Ctime => DateTime::<Local>::from(time)
.format(CTIME_FORMAT)
.to_string(),
TimeFormat::Ctime => {
const CTIME_FORMAT: &str = "%a %b %d %H:%M:%S.%f0 %Y";

DateTime::<Local>::from(time)
.format(CTIME_FORMAT)
.to_string()
}
TimeFormat::Strftime(format) => {
DateTime::<Local>::from(time).format(format).to_string()
// Handle a special case
let custom_format = format.replace("%+", "%Y-%m-%d+%H:%M:%S%.f0");
DateTime::<Local>::from(time)
.format(&custom_format)
.to_string()
}
};

Expand Down
23 changes: 23 additions & 0 deletions tests/find_cmd_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use assert_cmd::Command;
use predicates::prelude::*;
use regex::Regex;
use serial_test::serial;
use std::fs::{self, File};
use std::io::{Read, Write};
Expand Down Expand Up @@ -367,6 +368,28 @@
./test_data/simple/subdir/ABBBC subdir/ABBBC f\n",
)));

fs::create_dir_all("a").expect("Failed to create directory 'a'");
let output = Command::cargo_bin("find")
.expect("found binary")
.args(["a", "-printf", "%A+"])
.assert()
.success()
.get_output()
.stdout
.clone();

let output_str = String::from_utf8(output).expect("Invalid UTF-8 in output");

println!("Actual output: '{}'", output_str.trim());

let re = Regex::new(r"^\d{4}-\d{2}-\d{2}\+\d{2}:\d{2}:\d{2}\.\d{9}0$")
.expect("Failed to compile regex");

assert!(

Check warning on line 388 in tests/find_cmd_tests.rs

View check run for this annotation

Codecov / codecov/patch

tests/find_cmd_tests.rs#L388

Added line #L388 was not covered by tests
re.is_match(output_str.trim()),
"Output did not match expected timestamp format"
);

Command::cargo_bin("find")
.expect("found binary")
.args([
Expand Down
Loading