diff --git a/Makefile b/Makefile index b4e463020..a15425d91 100644 --- a/Makefile +++ b/Makefile @@ -178,6 +178,7 @@ TEST_PROGS := \ ptx \ pwd \ readlink \ + realpath \ rm \ seq \ sort \ diff --git a/test/common/util.rs b/test/common/util.rs index 2d940e6c3..f8db8a8a8 100644 --- a/test/common/util.rs +++ b/test/common/util.rs @@ -1,7 +1,12 @@ #![allow(dead_code)] +use std::env; use std::fs::{self, File}; use std::io::{Read, Write}; +#[cfg(unix)] +use std::os::unix::fs::symlink as symlink_file; +#[cfg(windows)] +use std::os::windows::fs::symlink_file; use std::path::Path; use std::process::{Command, Stdio}; use std::str::from_utf8; @@ -74,6 +79,10 @@ pub fn touch(file: &str) { File::create(Path::new(file)).unwrap(); } +pub fn symlink(src: &str, dst: &str) { + symlink_file(src, dst).unwrap(); +} + pub fn cleanup(path: &'static str) { let p = Path::new(path); match fs::metadata(p) { @@ -85,3 +94,15 @@ pub fn cleanup(path: &'static str) { Err(_) => {} } } + +pub fn current_directory() -> String { + env::current_dir().unwrap().into_os_string().into_string().unwrap() +} + +pub fn repeat_str(s: &str, n: u32) -> String { + let mut repeated = String::new(); + for _ in 0 .. n { + repeated.push_str(s); + } + repeated +} diff --git a/test/readlink.rs b/test/readlink.rs index 3e495cbec..6471d1d92 100644 --- a/test/readlink.rs +++ b/test/readlink.rs @@ -1,21 +1,13 @@ -use std::env; use std::process::Command; use std::str; +use util::*; static PROGNAME: &'static str = "./readlink"; static GIBBERISH: &'static str = "supercalifragilisticexpialidocious"; -fn current_directory() -> String { - env::current_dir().unwrap().into_os_string().into_string().unwrap() -} - -fn repeat_str(s: &str, n: u32) -> String { - let mut repeated = String::new(); - for _ in 0 .. n { - repeated.push_str(s); - } - repeated -} +#[path = "common/util.rs"] +#[macro_use] +mod util; #[test] fn test_canonicalize() { diff --git a/test/realpath.rs b/test/realpath.rs new file mode 100644 index 000000000..53cd539da --- /dev/null +++ b/test/realpath.rs @@ -0,0 +1,46 @@ +use std::process::Command; +use std::str; +use util::*; + +static PROGNAME: &'static str = "./realpath"; + +#[path = "common/util.rs"] +#[macro_use] +mod util; + +#[test] +fn test_current_directory() { + let po = Command::new(PROGNAME) + .arg(".") + .output() + .unwrap_or_else(|err| panic!("{}", err)); + + let out = str::from_utf8(&po.stdout[..]).unwrap().trim_right(); + assert_eq!(out, current_directory()); +} + +#[test] +fn test_long_redirection_to_current_dir() { + // Create a 256-character path to current directory + let dir = repeat_str("./", 128); + let po = Command::new(PROGNAME) + .arg(dir) + .output() + .unwrap_or_else(|err| panic!("{}", err)); + + let out = str::from_utf8(&po.stdout[..]).unwrap().trim_right(); + assert_eq!(out, current_directory()); +} + +#[test] +fn test_long_redirection_to_root() { + // Create a 255-character path to root + let dir = repeat_str("../", 85); + let po = Command::new(PROGNAME) + .arg(dir) + .output() + .unwrap_or_else(|err| panic!("{}", err)); + + let out = str::from_utf8(&po.stdout[..]).unwrap().trim_right(); + assert_eq!(out, "/"); +}