coreutils/tests/test_touch.rs

300 lines
8.5 KiB
Rust
Raw Normal View History

2016-08-27 23:12:58 +00:00
extern crate uu_touch;
use self::uu_touch::filetime::{self, FileTime};
extern crate time;
2015-08-12 01:38:10 +00:00
use common::util::*;
2015-08-12 01:38:10 +00:00
fn get_file_times(at: &AtPath, path: &str) -> (FileTime, FileTime) {
let m = at.metadata(path);
2020-04-13 18:36:03 +00:00
(
FileTime::from_last_access_time(&m),
FileTime::from_last_modification_time(&m),
)
2015-08-12 01:38:10 +00:00
}
2016-08-27 23:12:58 +00:00
fn get_symlink_times(at: &AtPath, path: &str) -> (FileTime, FileTime) {
let m = at.symlink_metadata(path);
2020-04-13 18:36:03 +00:00
(
FileTime::from_last_access_time(&m),
FileTime::from_last_modification_time(&m),
)
2016-08-27 23:12:58 +00:00
}
fn set_file_times(at: &AtPath, path: &str, atime: FileTime, mtime: FileTime) {
filetime::set_file_times(&at.plus_as_string(path), atime, mtime).unwrap()
2015-08-12 01:38:10 +00:00
}
// Adjusts for local timezone
fn str_to_filetime(format: &str, s: &str) -> FileTime {
let mut tm = time::strptime(s, format).unwrap();
tm.tm_utcoff = time::now().tm_utcoff;
let ts = tm.to_timespec();
FileTime::from_unix_time(ts.sec as i64, ts.nsec as u32)
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_default() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_default_file";
ucmd.arg(file).succeeds().no_stderr();
2016-08-27 23:12:58 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_no_create_file_absent() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_no_create_file_absent";
ucmd.arg("-c").arg(file).succeeds().no_stderr();
2015-08-12 01:38:10 +00:00
assert!(!at.file_exists(file));
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_no_create_file_exists() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_no_create_file_exists";
at.touch(file);
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
ucmd.arg("-c").arg(file).succeeds().no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_set_mdhm_time() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_set_mdhm_time";
ucmd.args(&["-t", "01011234", file]).succeeds().no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
2020-04-13 18:36:03 +00:00
let start_of_year = str_to_filetime(
"%Y%m%d%H%M",
&format!("{}01010000", 1900 + time::now().tm_year),
);
let (atime, mtime) = get_file_times(&at, file);
2015-08-12 01:38:10 +00:00
assert_eq!(atime, mtime);
2020-04-13 18:36:03 +00:00
assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45240);
assert_eq!(mtime.unix_seconds() - start_of_year.unix_seconds(), 45240);
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_set_mdhms_time() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_set_mdhms_time";
2020-04-13 18:36:03 +00:00
ucmd.args(&["-t", "01011234.56", file])
.succeeds()
.no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
2020-04-13 18:36:03 +00:00
let start_of_year = str_to_filetime(
"%Y%m%d%H%M.%S",
&format!("{}01010000.00", 1900 + time::now().tm_year),
);
let (atime, mtime) = get_file_times(&at, file);
2015-08-12 01:38:10 +00:00
assert_eq!(atime, mtime);
2020-04-13 18:36:03 +00:00
assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45296);
assert_eq!(mtime.unix_seconds() - start_of_year.unix_seconds(), 45296);
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_set_ymdhm_time() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_set_ymdhm_time";
2020-04-13 18:36:03 +00:00
ucmd.args(&["-t", "1501011234", file])
.succeeds()
.no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
let start_of_year = str_to_filetime("%y%m%d%H%M", "1501010000");
let (atime, mtime) = get_file_times(&at, file);
2015-08-12 01:38:10 +00:00
assert_eq!(atime, mtime);
2020-04-13 18:36:03 +00:00
assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45240);
assert_eq!(mtime.unix_seconds() - start_of_year.unix_seconds(), 45240);
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_set_ymdhms_time() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_set_ymdhms_time";
2020-04-13 18:36:03 +00:00
ucmd.args(&["-t", "1501011234.56", file])
.succeeds()
.no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
let start_of_year = str_to_filetime("%y%m%d%H%M.%S", "1501010000.00");
let (atime, mtime) = get_file_times(&at, file);
2015-08-12 01:38:10 +00:00
assert_eq!(atime, mtime);
2020-04-13 18:36:03 +00:00
assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45296);
assert_eq!(mtime.unix_seconds() - start_of_year.unix_seconds(), 45296);
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_set_cymdhm_time() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_set_cymdhm_time";
2020-04-13 18:36:03 +00:00
ucmd.args(&["-t", "201501011234", file])
.succeeds()
.no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501010000");
let (atime, mtime) = get_file_times(&at, file);
2015-08-12 01:38:10 +00:00
assert_eq!(atime, mtime);
2020-04-13 18:36:03 +00:00
assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45240);
assert_eq!(mtime.unix_seconds() - start_of_year.unix_seconds(), 45240);
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_set_cymdhms_time() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_set_cymdhms_time";
2020-04-13 18:36:03 +00:00
ucmd.args(&["-t", "201501011234.56", file])
.succeeds()
.no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
let start_of_year = str_to_filetime("%Y%m%d%H%M.%S", "201501010000.00");
let (atime, mtime) = get_file_times(&at, file);
2015-08-12 01:38:10 +00:00
assert_eq!(atime, mtime);
2020-04-13 18:36:03 +00:00
assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45296);
assert_eq!(mtime.unix_seconds() - start_of_year.unix_seconds(), 45296);
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_set_only_atime() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_set_only_atime";
2020-04-13 18:36:03 +00:00
ucmd.args(&["-t", "201501011234", "-a", file])
.succeeds()
.no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501010000");
let (atime, mtime) = get_file_times(&at, file);
2015-08-12 01:38:10 +00:00
assert!(atime != mtime);
2020-04-13 18:36:03 +00:00
assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45240);
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_set_only_mtime() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_set_only_mtime";
2020-04-13 18:36:03 +00:00
ucmd.args(&["-t", "201501011234", "-m", file])
.succeeds()
.no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501010000");
let (atime, mtime) = get_file_times(&at, file);
2015-08-12 01:38:10 +00:00
assert!(atime != mtime);
2020-04-13 18:36:03 +00:00
assert_eq!(mtime.unix_seconds() - start_of_year.unix_seconds(), 45240);
2015-08-12 01:38:10 +00:00
}
#[test]
fn test_touch_set_both() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_set_both";
2020-04-13 18:36:03 +00:00
ucmd.args(&["-t", "201501011234", "-a", "-m", file])
.succeeds()
.no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501010000");
let (atime, mtime) = get_file_times(&at, file);
2015-08-12 01:38:10 +00:00
assert_eq!(atime, mtime);
2020-04-13 18:36:03 +00:00
assert_eq!(atime.unix_seconds() - start_of_year.unix_seconds(), 45240);
assert_eq!(mtime.unix_seconds() - start_of_year.unix_seconds(), 45240);
2015-08-12 01:38:10 +00:00
}
2016-08-27 23:12:58 +00:00
#[test]
fn test_touch_no_dereference() {
let (at, mut ucmd) = at_and_ucmd!();
let file_a = "test_touch_no_dereference_a";
let file_b = "test_touch_no_dereference_b";
let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501010000");
let end_of_year = str_to_filetime("%Y%m%d%H%M", "201512312359");
at.touch(file_a);
set_file_times(&at, file_a, start_of_year, start_of_year);
at.symlink_file(file_a, file_b);
2016-08-27 23:12:58 +00:00
assert!(at.file_exists(file_a));
assert!(at.is_symlink(file_b));
2020-04-13 18:36:03 +00:00
ucmd.args(&["-t", "201512312359", "-h", file_b])
.succeeds()
.no_stderr();
2016-08-27 23:12:58 +00:00
let (atime, mtime) = get_symlink_times(&at, file_b);
assert_eq!(atime, mtime);
assert_eq!(atime, end_of_year);
assert_eq!(mtime, end_of_year);
let (atime, mtime) = get_file_times(&at, file_a);
assert_eq!(atime, mtime);
assert_eq!(atime, start_of_year);
assert_eq!(mtime, start_of_year);
}
2015-08-12 01:38:10 +00:00
#[test]
fn test_touch_reference() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file_a = "test_touch_reference_a";
let file_b = "test_touch_reference_b";
let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501010000");
at.touch(file_a);
set_file_times(&at, file_a, start_of_year, start_of_year);
assert!(at.file_exists(file_a));
2015-08-12 01:38:10 +00:00
ucmd.args(&["-r", file_a, file_b]).succeeds().no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file_b));
2015-08-12 01:38:10 +00:00
let (atime, mtime) = get_file_times(&at, file_b);
2015-08-12 01:38:10 +00:00
assert_eq!(atime, mtime);
assert_eq!(atime, start_of_year);
assert_eq!(mtime, start_of_year);
}
#[test]
fn test_touch_set_date() {
let (at, mut ucmd) = at_and_ucmd!();
2015-08-12 01:38:10 +00:00
let file = "test_touch_set_date";
2020-04-13 18:36:03 +00:00
ucmd.args(&["-d", "Thu Jan 01 12:34:00 2015", file])
.succeeds()
.no_stderr();
2015-08-12 01:38:10 +00:00
assert!(at.file_exists(file));
2015-08-12 01:38:10 +00:00
let start_of_year = str_to_filetime("%Y%m%d%H%M", "201501011234");
let (atime, mtime) = get_file_times(&at, file);
2015-08-12 01:38:10 +00:00
assert_eq!(atime, mtime);
assert_eq!(atime, start_of_year);
assert_eq!(mtime, start_of_year);
}