Skip to content

Commit 445abbe

Browse files
RenjiSannsylvestre
authored andcommitted
tests(date): Add tests for --reference
1 parent 7ebbfd3 commit 445abbe

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

tests/by-util/test_date.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::SystemTime;
2+
13
// This file is part of the uutils coreutils package.
24
//
35
// For the full copyright and license information, please view the LICENSE
@@ -517,10 +519,39 @@ fn test_date_successive_file() {
517519
2023-04-01 12:00:00\n\
518520
2023-04-15 18:30:00",
519521
);
520-
ucmd
521-
.arg("-f")
522+
ucmd.arg("-f")
522523
.arg("not-existent-file")
523524
.arg("-f")
524525
.arg("file-with-dates")
525526
.succeeds();
526-
}
527+
}
528+
529+
#[test]
530+
fn test_date_reference() {
531+
let (at, mut ucmd) = at_and_ucmd!();
532+
533+
at.touch_and_set_modified("ref-file", SystemTime::UNIX_EPOCH);
534+
535+
ucmd.arg("-r")
536+
.arg("ref-file")
537+
.succeeds()
538+
.stdout_is("Thu Jan 1 00:00:00 1970\n");
539+
}
540+
541+
#[test]
542+
fn test_date_successive_reference() {
543+
const FILE: &str = "file-with-dates";
544+
let (at, mut ucmd) = at_and_ucmd!();
545+
546+
at.write(
547+
FILE,
548+
"2023-03-27 08:30:00\n\
549+
2023-04-01 12:00:00\n\
550+
2023-04-15 18:30:00",
551+
);
552+
ucmd.arg("-r")
553+
.arg("not-existent-file")
554+
.arg("-r")
555+
.arg("file-with-dates")
556+
.succeeds();
557+
}

tests/common/util.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use std::process::{Child, Command, ExitStatus, Output, Stdio};
4646
use std::rc::Rc;
4747
use std::sync::mpsc::{self, RecvTimeoutError};
4848
use std::thread::{sleep, JoinHandle};
49-
use std::time::{Duration, Instant};
49+
use std::time::{Duration, Instant, SystemTime};
5050
use std::{env, hint, mem, thread};
5151
use tempfile::{Builder, TempDir};
5252

@@ -974,6 +974,13 @@ impl AtPath {
974974
File::create(self.plus(file)).unwrap();
975975
}
976976

977+
pub fn touch_and_set_modified<P: AsRef<Path>>(&self, file: P, modified: SystemTime) {
978+
let file = file.as_ref();
979+
log_info("touch", self.plus_as_string(file));
980+
let f = File::create(self.plus(file)).unwrap();
981+
f.set_modified(modified).unwrap();
982+
}
983+
977984
#[cfg(not(windows))]
978985
pub fn mkfifo(&self, fifo: &str) {
979986
let full_path = self.plus_as_string(fifo);

0 commit comments

Comments
 (0)