Merge pull request #634 from jbcrail/add-realpath-tests

Refactor and add tests for realpath.
This commit is contained in:
Heather 2015-06-04 09:47:05 +03:00
commit 9cfe018e66
4 changed files with 72 additions and 12 deletions

View file

@ -178,6 +178,7 @@ TEST_PROGS := \
ptx \
pwd \
readlink \
realpath \
rm \
seq \
sort \

View file

@ -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
}

View file

@ -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() {

46
test/realpath.rs Normal file
View file

@ -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, "/");
}