diff --git a/src/lib.rs b/src/lib.rs index b4839a8..857d183 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ #![deny(missing_docs)] #![deny(warnings)] -#![feature(core, io, env, path)] +#![feature(core, io, path, std_misc, net, path_ext)] //! A set of constructors for mocking Iron objects. @@ -26,14 +26,15 @@ pub mod mock { use hyper::http::HttpReader; - use std::old_io::net::ip::ToSocketAddr; + use std::io::Read; + use std::net::SocketAddr; /// Create a new mock Request with the given method, url, and data. pub fn new<'a, R>(method: method::Method, path: Url, data: &'a mut R) -> Request<'a> - where R: Reader { - let reader = HttpReader::EofReader(data as &'a mut Reader); - let addr = "127.0.0.1:3000".to_socket_addr().unwrap(); + where R: Read { + let reader = HttpReader::EofReader(data as &'a mut Read); + let addr: SocketAddr = "127.0.0.1:3000".parse().unwrap(); let mut headers = Headers::new(); let host = Url::parse("http://127.0.0.1:3000").unwrap() @@ -66,24 +67,17 @@ mod test { use super::super::mock::request; use iron::method; use iron::Url; + use std::io::{Read,Cursor}; - #[test] fn test_new() { - let req = request::new(method::Get, "localhost:3000"); + #[test] fn test_request() { + let ref mut data = Cursor::new("Hello Google!".as_bytes()); + let mut req = request::new(method::Get, Url::parse("http://localhost:3000").unwrap(), data); assert_eq!(req.method, method::Get); assert_eq!(format!("{}", req.url).as_slice(), "http://localhost:3000/"); - } - - #[test] fn test_at() { - let req = request::at(method::Post, Url::parse("http://www.google.com/").unwrap()); - assert_eq!(req.method, method::Post); - assert_eq!(format!("{}", req.url).as_slice(), "http://www.google.com:80/"); - } - #[test] fn test_at_with() { - let req = request::at_with(method::Put, Url::parse("http://www.google.com/").unwrap(), "Hello Google!"); - assert_eq!(req.method, method::Put); - assert_eq!(format!("{}", req.url).as_slice(), "http://www.google.com:80/"); - assert_eq!(req.body.as_slice(), b"Hello Google!"); + let mut body_buf = Vec::new(); + req.body.read_to_end(&mut body_buf).ok().unwrap(); + assert_eq!(&*body_buf, b"Hello Google!"); } } } diff --git a/src/project_builder.rs b/src/project_builder.rs index ab43b95..aeafc58 100644 --- a/src/project_builder.rs +++ b/src/project_builder.rs @@ -1,23 +1,26 @@ -use std::old_io::fs::{self, PathExtensions}; -use std::old_io::IoResult; -use std::old_io as io; +use std::fs; +use std::fs::PathExt; +use std::io::{self, Write}; use std::env; -use std::old_path::{Path, BytesContainer}; +use std::path::{Path, PathBuf, AsPath}; use std::fmt::Debug; use uuid::Uuid; +use std::ffi::IntoBytes; +use std::sync::{StaticMutex, MUTEX_INIT}; static IRON_INTEGRATION_TEST_DIR : &'static str = "iron-integration-tests"; +static FILE_OP_LOCK: StaticMutex = MUTEX_INIT; #[derive(Debug, PartialEq, Clone)] struct FileBuilder { - path: Path, + path: PathBuf, body: Vec } impl FileBuilder { - /// creates new instance of ProjectBuilder - pub fn new(path: Path, body: &[u8]) -> FileBuilder { - FileBuilder { path: path, body: body.to_vec() } + /// creates new instance of FileBuilder + pub fn new(path: P, body: B) -> FileBuilder { + FileBuilder { path: path.as_path().to_path_buf(), body: body.into_bytes() } } fn mk(&self) -> Result<(), String> { @@ -33,8 +36,9 @@ impl FileBuilder { self.path.display())) } - fn dirname(&self) -> Path { - Path::new(self.path.dirname()) + // FIXME: Panics if there's no parent + fn dirname(&self) -> &Path { + &self.path.parent().expect("parent directory") } } @@ -46,7 +50,7 @@ impl FileBuilder { #[derive(Debug, PartialEq, Clone)] pub struct ProjectBuilder { name: String, - root: Path, + root: PathBuf, files: Vec, } @@ -73,9 +77,9 @@ impl ProjectBuilder { } /// Add a new file to the temporary directory with the given contents. - pub fn file(mut self, path: B, body: C) -> ProjectBuilder - where B: BytesContainer, C: BytesContainer { - self.files.push(FileBuilder::new(self.root.join(path), body.container_as_bytes())); + pub fn file(mut self, path: P, body: B) -> ProjectBuilder + where P: AsPath, B: IntoBytes { + self.files.push(FileBuilder::new(self.root.join(&path), body)); self } @@ -96,16 +100,18 @@ impl ProjectBuilder { impl Drop for ProjectBuilder { fn drop(&mut self) { - match self.root().dir_path().rm_rf() { - Ok(_) => debug!("Successfully cleaned up the test directory; path = {}", self.root().dir_path().display()), - Err(e) => debug!("Failed to cleanup the test directory; path = {}; {}", self.root().dir_path().display(), e) + match self.root().parent().map(BuilderPathExt::rm_rf) { + Some(Ok(_)) => debug!("Successfully cleaned up the test directory; path = {:?}", self.root().parent().unwrap()), + Some(Err(e)) => debug!("Failed to cleanup the test directory; path = {:?}; {}", self.root().parent().unwrap(), e), + None => debug!("Failed to cleanup the test directory; no parent") } } } // Recursively creates the directory with all subdirectories fn mkdir_recursive(path: &Path) -> Result<(), String> { - fs::mkdir_recursive(path, io::USER_DIR) + let _l = FILE_OP_LOCK.lock().unwrap(); + fs::create_dir_all(path) .with_err_msg(format!("could not create directory; path={}", path.display())) } @@ -128,11 +134,11 @@ impl ErrMsg for Result { // Current test root path. // Will be located in target/iron-integration-tests/test- -fn root(id: Uuid) -> Path { - integration_tests_dir().join(format!("test-{}", id)) +fn root(id: Uuid) -> PathBuf { + integration_tests_dir().join(&format!("test-{}", id)) } -fn integration_tests_dir() -> Path { +fn integration_tests_dir() -> PathBuf { env::current_exe() .map(|mut p| { p.pop(); p.join(IRON_INTEGRATION_TEST_DIR) }) .unwrap() @@ -140,15 +146,16 @@ fn integration_tests_dir() -> Path { /// Convenience methods on Path -pub trait PathExt { +pub trait BuilderPathExt { /// Deletes directory in Path recursively - fn rm_rf(&self) -> IoResult<()>; + fn rm_rf(&self) -> io::Result<()>; } -impl PathExt for Path { - fn rm_rf(&self) -> IoResult<()> { +impl BuilderPathExt for Path { + fn rm_rf(&self) -> io::Result<()> { + let _l = FILE_OP_LOCK.lock().unwrap(); if self.exists() { - fs::rmdir_recursive(self) + fs::remove_dir_all(self) } else { Ok(()) }