Ported date tests to Windows

This commit is contained in:
Daniel 2019-02-04 16:22:59 -05:00 committed by Pierre Peltier
parent 625f4924f8
commit 36f7b35025

View file

@ -55,16 +55,52 @@ impl Date {
}
}
#[cfg(all(test, unix))]
#[cfg(test)]
mod test {
use super::Date;
use crate::color::{Colors, Theme};
use crate::flags::{DateFlag, Flags};
use ansi_term::Colour;
use std::process::Command;
use std::{env, fs};
use std::process::{Command, ExitStatus};
use std::path::Path;
use std::io;
use time;
#[cfg(unix)]
fn cross_platform_touch(path: &Path, date: &time::Tm) -> io::Result<ExitStatus> {
Command::new("touch")
.arg("-t")
.arg(date.strftime("%Y%m%d%H%M.%S").unwrap().to_string())
.arg(&path)
.status()
}
#[cfg(windows)]
fn cross_platform_touch(path: &Path, date: &time::Tm) -> io::Result<ExitStatus> {
use std::process::Stdio;
let copy_success = Command::new("cmd")
.arg("/C")
.arg("copy")
.arg("NUL")
.arg(path)
.stdout(Stdio::null()) // Windows doesn't have a quiet flag
.status()?
.success();
assert!(copy_success, "failed to create empty file");
Command::new("powershell")
.arg("-Command")
.arg("$(Get-Item")
.arg(path)
.arg(").lastwritetime=$(Get-Date \"")
.arg(date.strftime("%m/%d/%Y %H:%M:%S").unwrap().to_string())
.arg("\")")
.status()
}
#[test]
fn test_an_hour_old_file_color() {
let mut file_path = env::temp_dir();
@ -72,14 +108,10 @@ mod test {
let creation_date = (time::now() - time::Duration::seconds(4)).to_local();
let success = Command::new("touch")
.arg("-t")
.arg(creation_date.strftime("%Y%m%d%H%M.%S").unwrap().to_string())
.arg(&file_path)
.status()
let success = cross_platform_touch(&file_path, &creation_date)
.unwrap()
.success();
assert_eq!(true, success, "failed to exec touch");
assert!(success, "failed to exec touch");
let colors = Colors::new(Theme::Default);
let date = Date::from(&file_path.metadata().unwrap());
@ -100,14 +132,10 @@ mod test {
let creation_date = (time::now() - time::Duration::hours(4)).to_local();
let success = Command::new("touch")
.arg("-t")
.arg(creation_date.strftime("%Y%m%d%H%M.%S").unwrap().to_string())
.arg(&file_path)
.status()
let success = cross_platform_touch(&file_path, &creation_date)
.unwrap()
.success();
assert_eq!(true, success, "failed to exec touch");
assert!(success, "failed to exec touch");
let colors = Colors::new(Theme::Default);
let date = Date::from(&file_path.metadata().unwrap());
@ -126,22 +154,12 @@ mod test {
let mut file_path = env::temp_dir();
file_path.push("test_a_several_days_old_file_color.tmp");
let creation_date = time::now() - time::Duration::days(2);
let creation_date = time::now_utc() - time::Duration::days(2);
let success = Command::new("touch")
.arg("-t")
.arg(
creation_date
.to_local()
.strftime("%Y%m%d%H%M.%S")
.unwrap()
.to_string(),
)
.arg(&file_path)
.status()
let success = cross_platform_touch(&file_path, &creation_date.to_local())
.unwrap()
.success();
assert_eq!(true, success, "failed to exec touch");
assert!(success, "failed to exec touch");
let colors = Colors::new(Theme::Default);
let date = Date::from(&file_path.metadata().unwrap());
@ -162,20 +180,10 @@ mod test {
let creation_date = time::now() - time::Duration::days(2);
let success = Command::new("touch")
.arg("-t")
.arg(
creation_date
.to_local()
.strftime("%Y%m%d%H%M.%S")
.unwrap()
.to_string(),
)
.arg(&file_path)
.status()
let success = cross_platform_touch(&file_path, &creation_date.to_local())
.unwrap()
.success();
assert_eq!(true, success, "failed to exec touch");
assert!(success, "failed to exec touch");
let colors = Colors::new(Theme::Default);
let date = Date::from(&file_path.metadata().unwrap());