touch: add test

This commit is contained in:
knight42 2016-08-28 07:12:58 +08:00
parent d0196b82ad
commit c277793f38
2 changed files with 44 additions and 4 deletions

View file

@ -291,6 +291,13 @@ impl AtPath {
}
}
pub fn symlink_metadata(&self, path: &str) -> fs::Metadata {
match fs::symlink_metadata(&self.plus(path)) {
Ok(m) => m,
Err(e) => panic!("{}", e),
}
}
pub fn metadata(&self, path: &str) -> fs::Metadata {
match fs::metadata(&self.plus(path)) {
Ok(m) => m,

View file

@ -1,9 +1,9 @@
extern crate filetime;
extern crate uu_touch;
use self::uu_touch::filetime::{self, FileTime};
extern crate time;
use common::util::*;
use self::filetime::FileTime;
fn get_file_times(at: &AtPath, path: &str) -> (FileTime, FileTime) {
let m = at.metadata(path);
@ -11,6 +11,12 @@ fn get_file_times(at: &AtPath, path: &str) -> (FileTime, FileTime) {
FileTime::from_last_modification_time(&m))
}
fn get_symlink_times(at: &AtPath, path: &str) -> (FileTime, FileTime) {
let m = at.symlink_metadata(path);
(FileTime::from_last_access_time(&m),
FileTime::from_last_modification_time(&m))
}
fn set_file_times(at: &AtPath, path: &str, atime: FileTime, mtime: FileTime) {
filetime::set_file_times(&at.plus_as_string(path), atime, mtime).unwrap()
}
@ -29,7 +35,7 @@ fn test_touch_default() {
let file = "test_touch_default_file";
ucmd.arg(file).succeeds().no_stderr();
assert!(at.file_exists(file));
}
@ -215,6 +221,33 @@ fn test_touch_set_both() {
45240);
}
#[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_a, file_b);
assert!(at.file_exists(file_a));
assert!(at.is_symlink(file_b));
ucmd.args(&["-t", "201512312359", "-h", file_b]).succeeds().no_stderr();
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);
}
#[test]
fn test_touch_reference() {
let (at, mut ucmd) = at_and_ucmd!();