Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the library run on 1.2.0 and stable #7

Merged
merged 2 commits into from
Nov 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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="
Expand Down
59 changes: 46 additions & 13 deletions src/xdg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![cfg(unix)]
#![cfg_attr(test, feature(path_relative_from))]

use std::iter;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -378,15 +377,30 @@ fn create_directory<P>(home: &PathBuf, path: P) -> IoResult<PathBuf>
Ok(full_path)
}

fn path_exists<P: ?Sized + AsRef<Path>>(path: &P) -> bool {
fn inner(path: &Path) -> bool {
fs::metadata(path).is_ok()
}
inner(path.as_ref())
}

#[cfg(test)]
fn path_is_dir<P: ?Sized + AsRef<Path>>(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<P>(home: &PathBuf, dirs: &Vec<PathBuf>, path: P) -> Option<PathBuf>
where P: AsRef<Path> {
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)
}
}
Expand Down Expand Up @@ -430,9 +444,28 @@ fn make_absolute<P>(path: P) -> PathBuf where P: AsRef<Path> {
env::current_dir().unwrap().join(path.as_ref())
}

#[cfg(test)]
fn iter_after<A, I, J>(mut iter: I, mut prefix: J) -> Option<I> where
I: Iterator<Item=A> + Clone, J: Iterator<Item=A>, 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<P>(path: P) -> PathBuf where P: AsRef<Path> {
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)]
Expand All @@ -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]
Expand Down Expand Up @@ -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();
Expand All @@ -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]
Expand Down