Skip to content
Open
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
34 changes: 25 additions & 9 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Local> = modification_time.into();
let format = match style {
Expand Down Expand Up @@ -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<Local> = get_modification_time(missing_file, TimestampStyle::Iso)
.parse()
.unwrap();

assert_eq!(m_time, DateTime::<Local>::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<Local> = SystemTime::now().into();
let m_time: DateTime<Local> = get_modification_time(invalid_file, TimestampStyle::Iso)
let m_time: DateTime<Local> = get_modification_time("-", TimestampStyle::Iso)
.parse()
.unwrap();

Expand Down
56 changes: 56 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,62 @@ fn missing_file_as_empty() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

// TZ is not honored on Windows.
#[cfg(unix)]
#[test]
fn new_file_missing_side_dated_at_epoch() -> Result<(), Box<dyn std::error::Error>> {
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()
)));
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you run the upstream testsuite on this?
TESTS=new-file ./tests/run-upstream-testsuite.sh


// 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<dyn std::error::Error>> {
let tmp_dir = tempdir()?;
Expand Down
Loading