diff --git a/src/utils.rs b/src/utils.rs index 408e13b2..adcb2488 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -241,16 +241,19 @@ pub fn is_posix_locale(category: &str) -> bool { locale == "C" || locale == "POSIX" } -/// Retrieves the modification time of the input file specified by file path -/// If an error occurs, it returns the current system time +/// Retrieves the modification time of the input file specified by file path. +/// A missing file (-N) is dated at the Epoch, other errors at now. pub fn get_modification_time(file_path: &str, style: TimestampStyle) -> String { use chrono::{DateTime, Local}; use std::fs; + use std::io::ErrorKind; use std::time::SystemTime; - let modification_time: SystemTime = fs::metadata(file_path) - .and_then(|m| m.modified()) - .unwrap_or(SystemTime::now()); + let modification_time: SystemTime = match fs::metadata(file_path).and_then(|m| m.modified()) { + Ok(time) => time, + Err(e) if e.kind() == ErrorKind::NotFound && file_path != "-" => SystemTime::UNIX_EPOCH, + Err(_) => SystemTime::now(), + }; let modification_time: DateTime = modification_time.into(); let format = match style { @@ -526,17 +529,30 @@ mod tests { } #[test] - fn invalid_file() { + fn missing_file() { use chrono::{DateTime, Local}; use std::time::SystemTime; - let invalid_file = "target/utils/invalid-file"; + let missing_file = "target/utils/missing-file"; + + // A missing file is dated at the Unix Epoch, like GNU diff does + let m_time: DateTime = get_modification_time(missing_file, TimestampStyle::Iso) + .parse() + .unwrap(); + + assert_eq!(m_time, DateTime::::from(SystemTime::UNIX_EPOCH)); + } + + #[test] + fn stdin() { + use chrono::{DateTime, Local}; + use std::time::SystemTime; // store current time before calling `get_modification_time` - // Because the file is invalid, it will return SystemTime::now() + // Standard input has no path to stat, so it is dated "now", // which will be greater than previously saved time let current_time: DateTime = SystemTime::now().into(); - let m_time: DateTime = get_modification_time(invalid_file, TimestampStyle::Iso) + let m_time: DateTime = get_modification_time("-", TimestampStyle::Iso) .parse() .unwrap(); diff --git a/tests/by-util/test_diff.rs b/tests/by-util/test_diff.rs index 4bf102e9..1fb8ae58 100644 --- a/tests/by-util/test_diff.rs +++ b/tests/by-util/test_diff.rs @@ -246,6 +246,62 @@ fn missing_file_as_empty() -> Result<(), Box> { Ok(()) } +// TZ is not honored on Windows. +#[cfg(unix)] +#[test] +fn new_file_missing_side_dated_at_epoch() -> Result<(), Box> { + let tmp_dir = tempdir()?; + let a = tmp_dir.path().join("a"); + let b = tmp_dir.path().join("b"); + std::fs::create_dir(&a)?; + std::fs::create_dir(&b)?; + let old = a.join("only"); + std::fs::write(&old, "x\n")?; + let missing = b.join("only"); + + // GNU dates the missing side at the Epoch, which tells patch to remove it. + for (option, locale, epoch) in [ + ("-u", "en_US.UTF-8", "1970-01-01 00:00:00.000000000 +0000"), + ("-c", "en_US.UTF-8", "1970-01-01 00:00:00.000000000 +0000"), + ("-c", "C", "Thu Jan 1 00:00:00 1970"), + ] { + for (from, to) in [(&old, &missing), (&missing, &old)] { + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.env("TZ", "UTC0") + .env("LC_ALL", locale) + .arg("diff") + .arg("-N") + .arg(option) + .arg(from) + .arg(to); + cmd.assert() + .code(predicate::eq(1)) + .stdout(predicate::str::contains(format!( + " {}\t{epoch}\n", + missing.display() + ))); + } + } + + // The same goes for files that only exist on one side of a directory diff. + let mut cmd = cargo_bin_cmd!("diffutils"); + cmd.env("TZ", "UTC0") + .env("LC_ALL", "C") + .arg("diff") + .arg("-r") + .arg("-N") + .arg("-u") + .arg(&a) + .arg(&b); + cmd.assert() + .code(predicate::eq(1)) + .stdout(predicate::str::contains(format!( + "+++ {}\t1970-01-01 00:00:00.000000000 +0000\n", + missing.display() + ))); + Ok(()) +} + #[test] fn binary_files() -> Result<(), Box> { let tmp_dir = tempdir()?;