From 1fecba3226e1f8288f7d4d2e7ae0fb3ca60c57a8 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Mon, 28 Mar 2016 21:06:31 -0400 Subject: [PATCH] tests/{readlink, realpath}: fix tests on Windows I fixed the tests that assumed paths used a Unix directory separator. --- tests/common/util.rs | 36 +++++++++++++++++++++++++++--------- tests/readlink.rs | 10 ++++------ tests/realpath.rs | 6 +++--- 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/tests/common/util.rs b/tests/common/util.rs index 097683652..50cbde9dc 100755 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -49,6 +49,25 @@ macro_rules! assert_no_error( ); ); +#[macro_export] +macro_rules! path_concat { + ($e:expr, ..$n:expr) => {{ + let n = $n; + let mut pb = std::path::PathBuf::new(); + for _ in 0..n { + pb.push($e); + } + pb.to_str().unwrap().to_owned() + }}; + ($($e:expr),*) => {{ + let mut pb = std::path::PathBuf::new(); + $( + pb.push($e); + )* + pb.to_str().unwrap().to_owned() + }}; +} + pub struct CmdResult { pub success: bool, pub stdout: String, @@ -97,15 +116,6 @@ pub fn log_info, U: AsRef>(msg: T, par: U) { println!("{}: {}", msg.as_ref(), par.as_ref()); } - -pub fn repeat_str(s: &str, n: u32) -> String { - let mut repeated = String::new(); - for _ in 0..n { - repeated.push_str(s); - } - repeated -} - pub fn recursive_copy(src: &Path, dest: &Path) -> Result<()> { if try!(fs::metadata(src)).is_dir() { for entry in try!(fs::read_dir(src)) { @@ -123,6 +133,14 @@ pub fn recursive_copy(src: &Path, dest: &Path) -> Result<()> { Ok(()) } +pub fn get_root_path() -> &'static str { + if cfg!(windows) { + "C:\\" + } else { + "/" + } +} + /// A scoped, temporary file that is removed upon drop. pub struct ScopedFile { path: PathBuf, diff --git a/tests/readlink.rs b/tests/readlink.rs index eb138e712..5cad3a0c4 100644 --- a/tests/readlink.rs +++ b/tests/readlink.rs @@ -33,9 +33,7 @@ fn test_canonicalize_existing() { #[test] fn test_canonicalize_missing() { let (at, mut ucmd) = testing(UTIL_NAME); - let mut expected = at.root_dir_resolved(); - expected.push_str("/"); - expected.push_str(GIBBERISH); + let expected = path_concat!(at.root_dir_resolved(), GIBBERISH); let out = ucmd.arg("-m") .arg(GIBBERISH) @@ -49,7 +47,7 @@ fn test_canonicalize_missing() { fn test_long_redirection_to_current_dir() { let (at, mut ucmd) = testing(UTIL_NAME); // Create a 256-character path to current directory - let dir = repeat_str("./", 128); + let dir = path_concat!(".", ..128); let out = ucmd.arg("-n") .arg("-m") .arg(dir) @@ -63,12 +61,12 @@ fn test_long_redirection_to_current_dir() { fn test_long_redirection_to_root() { let (_, mut ucmd) = testing(UTIL_NAME); // Create a 255-character path to root - let dir = repeat_str("../", 85); + let dir = path_concat!("..", ..85); let out = ucmd.arg("-n") .arg("-m") .arg(dir) .run() .stdout; - assert_eq!(out, "/"); + assert_eq!(out, get_root_path()); } diff --git a/tests/realpath.rs b/tests/realpath.rs index fa775da21..479cdd7cc 100644 --- a/tests/realpath.rs +++ b/tests/realpath.rs @@ -17,7 +17,7 @@ fn test_current_directory() { fn test_long_redirection_to_current_dir() { let (at, mut ucmd) = testing(UTIL_NAME); // Create a 256-character path to current directory - let dir = repeat_str("./", 128); + let dir = path_concat!(".", ..128); let out = ucmd.arg(dir).run().stdout; assert_eq!(out.trim_right(), at.root_dir_resolved()); @@ -27,8 +27,8 @@ fn test_long_redirection_to_current_dir() { fn test_long_redirection_to_root() { let (_, mut ucmd) = testing(UTIL_NAME); // Create a 255-character path to root - let dir = repeat_str("../", 85); + let dir = path_concat!("..", ..85); let out = ucmd.arg(dir).run().stdout; - assert_eq!(out.trim_right(), "/"); + assert_eq!(out.trim_right(), get_root_path()); }