diff --git a/.travis.yml b/.travis.yml index ed1ed4f..d46bc39 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,8 @@ language: rust rust: + - 1.2.0 + - stable + - beta - nightly env: - secure: "ZmdqljmHk9oOyCJWLKWJ4ayl/7rExqss+1ZIw26zeKontCdWQ9K5NvGmbk69mD9BL07aX1O1jIaSiSrqIqV6f8/BsguADvdBUyqfdLAXQy28we+17xTphmS6vh3uT1ayqGBBhB3Qy/6JnEBp5vFdcMbwSXq02+/MwCp/wbozk4junnJ0Gg0BOK/6FUQBBnlflg1Tm5KS3pZw5H7/7GtNRW4K9ApM6EBHqBhEc8GGemCoh6q2WUabSnaxev1Bql5ZogN7wDrf5Ohpvzqv5wU25aWwRc3eJngGRelHmlGtCS1VpWSZHE4nahWj6nLIsW6Kr620Dy2ZPvNuQ/+OCC/eKNPifRlgMN2Ff1eRz9irQ6sc+KCyDmaMpAA92f1IF7y9AolGKjiTWYxKMT+0g7lbPV+WoE1+pfbsZcyOGGMLxzxxlYxSukgXN55Eqs8ag9jakjP0GtCVU3tL4nO6zyq7+UhTleWPXuTKwG64C7q3UZZUgpayzzKcJdcCdozEwuokF/kI1+o4AiF65uFw+SrQ07okJnUm86p3QhRtNSdTY6hbbLGuELQ5DbikkKoavV8WdjjqViy4tU2WafdJvA67sZvEP4h6eVvAWz+/3uZoAJyXFbfulq40XB+Fzxl/5ionnM9md86UDsX9W64dSVl+2vovj+5Jejm3wQLsuJheVG4=" diff --git a/src/xdg.rs b/src/xdg.rs index c2627f4..55d0dc0 100644 --- a/src/xdg.rs +++ b/src/xdg.rs @@ -1,5 +1,4 @@ #![cfg(unix)] -#![cfg_attr(test, feature(path_relative_from))] use std::iter; use std::path::{Path, PathBuf}; @@ -147,7 +146,7 @@ impl BaseDirectories // If XDG_RUNTIME_DIR is in the environment but not secure, // do not allow recovery. if let Some(ref runtime_dir) = runtime_dir { - match runtime_dir.metadata() { + match fs::metadata(runtime_dir) { Err(_) => { panic!("$XDG_RUNTIME_DIR must be accessible by the current user"); } @@ -378,15 +377,30 @@ fn create_directory

(home: &PathBuf, path: P) -> IoResult Ok(full_path) } +fn path_exists>(path: &P) -> bool { + fn inner(path: &Path) -> bool { + fs::metadata(path).is_ok() + } + inner(path.as_ref()) +} + +#[cfg(test)] +fn path_is_dir>(path: &P) -> bool { + fn inner(path: &Path) -> bool { + fs::metadata(path).map(|m| m.is_dir()).unwrap_or(false) + } + inner(path.as_ref()) +} + fn read_file

(home: &PathBuf, dirs: &Vec, path: P) -> Option where P: AsRef { let full_path = home.join(path.as_ref()); - if full_path.exists() { + if path_exists(&full_path) { return Some(full_path) } for dir in dirs.iter() { let full_path = dir.join(path.as_ref()); - if full_path.exists() { + if path_exists(&full_path) { return Some(full_path) } } @@ -430,9 +444,28 @@ fn make_absolute

(path: P) -> PathBuf where P: AsRef { env::current_dir().unwrap().join(path.as_ref()) } +#[cfg(test)] +fn iter_after(mut iter: I, mut prefix: J) -> Option where + I: Iterator + Clone, J: Iterator, A: PartialEq +{ + loop { + let mut iter_next = iter.clone(); + match (iter_next.next(), prefix.next()) { + (Some(x), Some(y)) => { + if x != y { return None } + } + (Some(_), None) => return Some(iter), + (None, None) => return Some(iter), + (None, Some(_)) => return None, + } + iter = iter_next; + } +} + #[cfg(test)] fn make_relative

(path: P) -> PathBuf where P: AsRef { - path.as_ref().relative_from(&env::current_dir().unwrap()).unwrap().to_owned() + iter_after(path.as_ref().components(), env::current_dir().unwrap().components()) + .unwrap().as_path().to_owned() } #[cfg(test)] @@ -448,9 +481,9 @@ fn make_env(vars: Vec<(&'static str, String)>) -> #[test] fn test_files_exists() { - assert!(Path::new("test_files").exists()); - assert!(Path::new("test_files/runtime-bad") - .metadata().unwrap().permissions().mode() & 0o077 != 0); + assert!(path_exists("test_files")); + assert!(fs::metadata("test_files/runtime-bad") + .unwrap().permissions().mode() & 0o077 != 0); } #[test] @@ -516,12 +549,12 @@ fn test_runtime_good() { ])); xd.create_runtime_directory("foo").unwrap(); - assert!(Path::new("test_files/runtime-good/foo").is_dir()); + assert!(path_is_dir("test_files/runtime-good/foo")); let w = xd.place_runtime_file("bar/baz").unwrap(); - assert!(Path::new("test_files/runtime-good/bar").is_dir()); - assert!(!Path::new("test_files/runtime-good/bar/baz").exists()); + assert!(path_is_dir("test_files/runtime-good/bar")); + assert!(!path_exists("test_files/runtime-good/bar/baz")); File::create(&w).unwrap(); - assert!(Path::new("test_files/runtime-good/bar/baz").exists()); + assert!(path_exists("test_files/runtime-good/bar/baz")); assert!(xd.find_runtime_file("bar/baz") == Some(w.clone())); File::open(&w).unwrap(); fs::remove_file(&w).unwrap(); @@ -534,7 +567,7 @@ fn test_runtime_good() { assert!(xd.list_runtime_files("bar").is_empty()); assert!(xd.find_runtime_file("foo/qux").is_none()); assert!(xd.find_runtime_file("qux/foo").is_none()); - assert!(!Path::new("test_files/runtime-good/qux").exists()); + assert!(!path_exists("test_files/runtime-good/qux")); } #[test]