2013-09-01 12:44:07 -07:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! POSIX file path handling
|
|
|
|
|
|
|
|
use container::Container;
|
|
|
|
use c_str::{CString, ToCStr};
|
|
|
|
use clone::Clone;
|
|
|
|
use cmp::Eq;
|
|
|
|
use from_str::FromStr;
|
2013-09-26 17:21:59 -07:00
|
|
|
use iter::{AdditiveIterator, Extendable, Iterator, Map};
|
2013-09-01 12:44:07 -07:00
|
|
|
use option::{Option, None, Some};
|
|
|
|
use str;
|
|
|
|
use str::Str;
|
2013-09-26 12:58:56 -07:00
|
|
|
use to_bytes::IterBytes;
|
2013-09-01 12:44:07 -07:00
|
|
|
use util;
|
|
|
|
use vec;
|
2013-09-26 17:21:59 -07:00
|
|
|
use vec::{CopyableVector, RSplitIterator, SplitIterator, Vector, VectorVector};
|
2013-10-05 19:49:32 -07:00
|
|
|
use super::{BytesContainer, GenericPath, GenericPathUnsafe};
|
2013-09-01 12:44:07 -07:00
|
|
|
|
2013-09-26 14:24:06 -07:00
|
|
|
#[cfg(not(target_os = "win32"))]
|
|
|
|
use libc;
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
/// Iterator that yields successive components of a Path as &[u8]
|
|
|
|
pub type ComponentIter<'self> = SplitIterator<'self, u8>;
|
|
|
|
/// Iterator that yields components of a Path in reverse as &[u8]
|
|
|
|
pub type RevComponentIter<'self> = RSplitIterator<'self, u8>;
|
|
|
|
|
|
|
|
/// Iterator that yields successive components of a Path as Option<&str>
|
|
|
|
pub type StrComponentIter<'self> = Map<'self, &'self [u8], Option<&'self str>,
|
|
|
|
ComponentIter<'self>>;
|
|
|
|
/// Iterator that yields components of a Path in reverse as Option<&str>
|
|
|
|
pub type RevStrComponentIter<'self> = Map<'self, &'self [u8], Option<&'self str>,
|
|
|
|
RevComponentIter<'self>>;
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
/// Represents a POSIX file path
|
|
|
|
#[deriving(Clone, DeepClone)]
|
|
|
|
pub struct Path {
|
|
|
|
priv repr: ~[u8], // assumed to never be empty or contain NULs
|
|
|
|
priv sepidx: Option<uint> // index of the final separator in repr
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The standard path separator character
|
|
|
|
pub static sep: u8 = '/' as u8;
|
|
|
|
|
|
|
|
/// Returns whether the given byte is a path separator
|
|
|
|
#[inline]
|
|
|
|
pub fn is_sep(u: &u8) -> bool {
|
|
|
|
*u == sep
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for Path {
|
2013-08-27 00:51:08 -07:00
|
|
|
#[inline]
|
2013-09-01 12:44:07 -07:00
|
|
|
fn eq(&self, other: &Path) -> bool {
|
|
|
|
self.repr == other.repr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Path {
|
|
|
|
fn from_str(s: &str) -> Option<Path> {
|
2013-10-05 19:49:32 -07:00
|
|
|
Path::new_opt(s)
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToCStr for Path {
|
|
|
|
#[inline]
|
|
|
|
fn to_c_str(&self) -> CString {
|
|
|
|
// The Path impl guarantees no internal NUL
|
|
|
|
unsafe { self.as_vec().to_c_str_unchecked() }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
unsafe fn to_c_str_unchecked(&self) -> CString {
|
|
|
|
self.as_vec().to_c_str_unchecked()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 12:58:56 -07:00
|
|
|
impl IterBytes for Path {
|
|
|
|
#[inline]
|
|
|
|
fn iter_bytes(&self, lsb0: bool, f: &fn(buf: &[u8]) -> bool) -> bool {
|
|
|
|
self.repr.iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
impl GenericPathUnsafe for Path {
|
2013-10-05 19:49:32 -07:00
|
|
|
unsafe fn new_unchecked<T: BytesContainer>(path: T) -> Path {
|
|
|
|
let path = Path::normalize(path.container_as_bytes());
|
2013-09-01 12:44:07 -07:00
|
|
|
assert!(!path.is_empty());
|
|
|
|
let idx = path.rposition_elem(&sep);
|
|
|
|
Path{ repr: path, sepidx: idx }
|
|
|
|
}
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
unsafe fn set_dirname_unchecked<T: BytesContainer>(&mut self, dirname: T) {
|
|
|
|
let dirname = dirname.container_as_bytes();
|
2013-09-01 12:44:07 -07:00
|
|
|
match self.sepidx {
|
|
|
|
None if bytes!(".") == self.repr || bytes!("..") == self.repr => {
|
|
|
|
self.repr = Path::normalize(dirname);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let mut v = vec::with_capacity(dirname.len() + self.repr.len() + 1);
|
|
|
|
v.push_all(dirname);
|
|
|
|
v.push(sep);
|
|
|
|
v.push_all(self.repr);
|
|
|
|
self.repr = Path::normalize(v);
|
|
|
|
}
|
|
|
|
Some(0) if self.repr.len() == 1 && self.repr[0] == sep => {
|
|
|
|
self.repr = Path::normalize(dirname);
|
|
|
|
}
|
2013-09-01 23:35:32 -07:00
|
|
|
Some(idx) if self.repr.slice_from(idx+1) == bytes!("..") => {
|
|
|
|
self.repr = Path::normalize(dirname);
|
|
|
|
}
|
2013-09-01 12:44:07 -07:00
|
|
|
Some(idx) if dirname.is_empty() => {
|
|
|
|
let v = Path::normalize(self.repr.slice_from(idx+1));
|
|
|
|
self.repr = v;
|
|
|
|
}
|
|
|
|
Some(idx) => {
|
|
|
|
let mut v = vec::with_capacity(dirname.len() + self.repr.len() - idx);
|
|
|
|
v.push_all(dirname);
|
|
|
|
v.push_all(self.repr.slice_from(idx));
|
|
|
|
self.repr = Path::normalize(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.sepidx = self.repr.rposition_elem(&sep);
|
|
|
|
}
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
unsafe fn set_filename_unchecked<T: BytesContainer>(&mut self, filename: T) {
|
|
|
|
let filename = filename.container_as_bytes();
|
2013-09-01 12:44:07 -07:00
|
|
|
match self.sepidx {
|
|
|
|
None if bytes!("..") == self.repr => {
|
|
|
|
let mut v = vec::with_capacity(3 + filename.len());
|
|
|
|
v.push_all(dot_dot_static);
|
|
|
|
v.push(sep);
|
|
|
|
v.push_all(filename);
|
|
|
|
self.repr = Path::normalize(v);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.repr = Path::normalize(filename);
|
|
|
|
}
|
|
|
|
Some(idx) if self.repr.slice_from(idx+1) == bytes!("..") => {
|
|
|
|
let mut v = vec::with_capacity(self.repr.len() + 1 + filename.len());
|
|
|
|
v.push_all(self.repr);
|
|
|
|
v.push(sep);
|
|
|
|
v.push_all(filename);
|
|
|
|
self.repr = Path::normalize(v);
|
|
|
|
}
|
|
|
|
Some(idx) => {
|
2013-08-27 00:51:08 -07:00
|
|
|
let mut v = vec::with_capacity(idx + 1 + filename.len());
|
2013-09-01 12:44:07 -07:00
|
|
|
v.push_all(self.repr.slice_to(idx+1));
|
|
|
|
v.push_all(filename);
|
|
|
|
self.repr = Path::normalize(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.sepidx = self.repr.rposition_elem(&sep);
|
|
|
|
}
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
unsafe fn push_unchecked<T: BytesContainer>(&mut self, path: T) {
|
|
|
|
let path = path.container_as_bytes();
|
2013-09-01 12:44:07 -07:00
|
|
|
if !path.is_empty() {
|
|
|
|
if path[0] == sep {
|
|
|
|
self.repr = Path::normalize(path);
|
|
|
|
} else {
|
|
|
|
let mut v = vec::with_capacity(self.repr.len() + path.len() + 1);
|
|
|
|
v.push_all(self.repr);
|
|
|
|
v.push(sep);
|
|
|
|
v.push_all(path);
|
|
|
|
self.repr = Path::normalize(v);
|
|
|
|
}
|
|
|
|
self.sepidx = self.repr.rposition_elem(&sep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericPath for Path {
|
|
|
|
#[inline]
|
|
|
|
fn as_vec<'a>(&'a self) -> &'a [u8] {
|
|
|
|
self.repr.as_slice()
|
|
|
|
}
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
fn into_vec(self) -> ~[u8] {
|
|
|
|
self.repr
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_str(self) -> Option<~str> {
|
|
|
|
str::from_utf8_owned_opt(self.repr)
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
fn dirname<'a>(&'a self) -> &'a [u8] {
|
|
|
|
match self.sepidx {
|
|
|
|
None if bytes!("..") == self.repr => self.repr.as_slice(),
|
|
|
|
None => dot_static,
|
|
|
|
Some(0) => self.repr.slice_to(1),
|
|
|
|
Some(idx) if self.repr.slice_from(idx+1) == bytes!("..") => self.repr.as_slice(),
|
|
|
|
Some(idx) => self.repr.slice_to(idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
fn filename<'a>(&'a self) -> Option<&'a [u8]> {
|
2013-09-01 12:44:07 -07:00
|
|
|
match self.sepidx {
|
2013-09-26 17:21:59 -07:00
|
|
|
None if bytes!(".") == self.repr || bytes!("..") == self.repr => None,
|
|
|
|
None => Some(self.repr.as_slice()),
|
|
|
|
Some(idx) if self.repr.slice_from(idx+1) == bytes!("..") => None,
|
|
|
|
Some(0) if self.repr.slice_from(1).is_empty() => None,
|
|
|
|
Some(idx) => Some(self.repr.slice_from(idx+1))
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 14:38:26 -07:00
|
|
|
fn pop(&mut self) -> Option<~[u8]> {
|
2013-09-01 12:44:07 -07:00
|
|
|
match self.sepidx {
|
|
|
|
None if bytes!(".") == self.repr => None,
|
|
|
|
None => {
|
|
|
|
let mut v = ~['.' as u8];
|
|
|
|
util::swap(&mut v, &mut self.repr);
|
|
|
|
self.sepidx = None;
|
|
|
|
Some(v)
|
|
|
|
}
|
|
|
|
Some(0) if bytes!("/") == self.repr => None,
|
|
|
|
Some(idx) => {
|
|
|
|
let v = self.repr.slice_from(idx+1).to_owned();
|
|
|
|
if idx == 0 {
|
|
|
|
self.repr.truncate(idx+1);
|
|
|
|
} else {
|
|
|
|
self.repr.truncate(idx);
|
|
|
|
}
|
|
|
|
self.sepidx = self.repr.rposition_elem(&sep);
|
|
|
|
Some(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
fn root_path(&self) -> Option<Path> {
|
|
|
|
if self.is_absolute() {
|
2013-10-05 19:49:32 -07:00
|
|
|
Some(Path::new("/"))
|
2013-09-26 17:21:59 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[inline]
|
|
|
|
fn is_absolute(&self) -> bool {
|
|
|
|
self.repr[0] == sep
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_ancestor_of(&self, other: &Path) -> bool {
|
|
|
|
if self.is_absolute() != other.is_absolute() {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
let mut ita = self.component_iter();
|
|
|
|
let mut itb = other.component_iter();
|
|
|
|
if bytes!(".") == self.repr {
|
|
|
|
return itb.next() != Some(bytes!(".."));
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
match (ita.next(), itb.next()) {
|
|
|
|
(None, _) => break,
|
2013-10-02 20:26:28 -07:00
|
|
|
(Some(a), Some(b)) if a == b => { continue },
|
2013-09-01 12:44:07 -07:00
|
|
|
(Some(a), _) if a == bytes!("..") => {
|
|
|
|
// if ita contains only .. components, it's an ancestor
|
|
|
|
return ita.all(|x| x == bytes!(".."));
|
|
|
|
}
|
|
|
|
_ => return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_relative_from(&self, base: &Path) -> Option<Path> {
|
|
|
|
if self.is_absolute() != base.is_absolute() {
|
|
|
|
if self.is_absolute() {
|
|
|
|
Some(self.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let mut ita = self.component_iter();
|
|
|
|
let mut itb = base.component_iter();
|
|
|
|
let mut comps = ~[];
|
|
|
|
loop {
|
|
|
|
match (ita.next(), itb.next()) {
|
|
|
|
(None, None) => break,
|
|
|
|
(Some(a), None) => {
|
|
|
|
comps.push(a);
|
|
|
|
comps.extend(&mut ita);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
(None, _) => comps.push(dot_dot_static),
|
|
|
|
(Some(a), Some(b)) if comps.is_empty() && a == b => (),
|
|
|
|
(Some(a), Some(b)) if b == bytes!(".") => comps.push(a),
|
|
|
|
(Some(_), Some(b)) if b == bytes!("..") => return None,
|
|
|
|
(Some(a), Some(_)) => {
|
|
|
|
comps.push(dot_dot_static);
|
|
|
|
for _ in itb {
|
|
|
|
comps.push(dot_dot_static);
|
|
|
|
}
|
|
|
|
comps.push(a);
|
|
|
|
comps.extend(&mut ita);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-05 19:49:32 -07:00
|
|
|
Some(Path::new(comps.connect_vec(&sep)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ends_with_path(&self, child: &Path) -> bool {
|
|
|
|
if !child.is_relative() { return false; }
|
|
|
|
let mut selfit = self.rev_component_iter();
|
|
|
|
let mut childit = child.rev_component_iter();
|
|
|
|
loop {
|
|
|
|
match (selfit.next(), childit.next()) {
|
|
|
|
(Some(a), Some(b)) => if a != b { return false; },
|
|
|
|
(Some(_), None) => break,
|
|
|
|
(None, Some(_)) => return false,
|
|
|
|
(None, None) => break
|
|
|
|
}
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
2013-10-05 19:49:32 -07:00
|
|
|
true
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Path {
|
2013-10-05 19:49:32 -07:00
|
|
|
/// Returns a new Path from a byte vector or string
|
2013-09-01 12:44:07 -07:00
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// Raises the `null_byte` condition if the vector contains a NUL.
|
|
|
|
#[inline]
|
2013-10-05 19:49:32 -07:00
|
|
|
pub fn new<T: BytesContainer>(path: T) -> Path {
|
|
|
|
GenericPath::new(path)
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
/// Returns a new Path from a byte vector or string, if possible
|
2013-09-26 00:54:30 -07:00
|
|
|
#[inline]
|
2013-10-05 19:49:32 -07:00
|
|
|
pub fn new_opt<T: BytesContainer>(path: T) -> Option<Path> {
|
|
|
|
GenericPath::new_opt(path)
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a normalized byte vector representation of a path, by removing all empty
|
|
|
|
/// components, and unnecessary . and .. components.
|
2013-10-02 22:29:46 -07:00
|
|
|
fn normalize<V: Vector<u8>+CopyableVector<u8>>(v: V) -> ~[u8] {
|
2013-09-01 12:44:07 -07:00
|
|
|
// borrowck is being very picky
|
|
|
|
let val = {
|
|
|
|
let is_abs = !v.as_slice().is_empty() && v.as_slice()[0] == sep;
|
|
|
|
let v_ = if is_abs { v.as_slice().slice_from(1) } else { v.as_slice() };
|
2013-08-27 00:51:08 -07:00
|
|
|
let comps = normalize_helper(v_, is_abs);
|
2013-09-01 12:44:07 -07:00
|
|
|
match comps {
|
|
|
|
None => None,
|
|
|
|
Some(comps) => {
|
|
|
|
if is_abs && comps.is_empty() {
|
|
|
|
Some(~[sep])
|
|
|
|
} else {
|
|
|
|
let n = if is_abs { comps.len() } else { comps.len() - 1} +
|
|
|
|
comps.iter().map(|v| v.len()).sum();
|
|
|
|
let mut v = vec::with_capacity(n);
|
|
|
|
let mut it = comps.move_iter();
|
|
|
|
if !is_abs {
|
|
|
|
match it.next() {
|
|
|
|
None => (),
|
|
|
|
Some(comp) => v.push_all(comp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for comp in it {
|
|
|
|
v.push(sep);
|
|
|
|
v.push_all(comp);
|
|
|
|
}
|
|
|
|
Some(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match val {
|
|
|
|
None => v.into_owned(),
|
|
|
|
Some(val) => val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator that yields each component of the path in turn.
|
|
|
|
/// Does not distinguish between absolute and relative paths, e.g.
|
|
|
|
/// /a/b/c and a/b/c yield the same set of components.
|
|
|
|
/// A path of "/" yields no components. A path of "." yields one component.
|
|
|
|
pub fn component_iter<'a>(&'a self) -> ComponentIter<'a> {
|
|
|
|
let v = if self.repr[0] == sep {
|
|
|
|
self.repr.slice_from(1)
|
|
|
|
} else { self.repr.as_slice() };
|
|
|
|
let mut ret = v.split_iter(is_sep);
|
|
|
|
if v.is_empty() {
|
|
|
|
// consume the empty "" component
|
|
|
|
ret.next();
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
2013-09-26 17:21:59 -07:00
|
|
|
|
|
|
|
/// Returns an iterator that yields each component of the path in reverse.
|
|
|
|
/// See component_iter() for details.
|
|
|
|
pub fn rev_component_iter<'a>(&'a self) -> RevComponentIter<'a> {
|
|
|
|
let v = if self.repr[0] == sep {
|
|
|
|
self.repr.slice_from(1)
|
|
|
|
} else { self.repr.as_slice() };
|
|
|
|
let mut ret = v.rsplit_iter(is_sep);
|
|
|
|
if v.is_empty() {
|
|
|
|
// consume the empty "" component
|
|
|
|
ret.next();
|
|
|
|
}
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator that yields each component of the path as Option<&str>.
|
|
|
|
/// See component_iter() for details.
|
|
|
|
pub fn str_component_iter<'a>(&'a self) -> StrComponentIter<'a> {
|
|
|
|
self.component_iter().map(str::from_utf8_slice_opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an iterator that yields each component of the path in reverse as Option<&str>.
|
|
|
|
/// See component_iter() for details.
|
|
|
|
pub fn rev_str_component_iter<'a>(&'a self) -> RevStrComponentIter<'a> {
|
|
|
|
self.rev_component_iter().map(str::from_utf8_slice_opt)
|
|
|
|
}
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// None result means the byte vector didn't need normalizing
|
2013-08-27 00:51:08 -07:00
|
|
|
fn normalize_helper<'a>(v: &'a [u8], is_abs: bool) -> Option<~[&'a [u8]]> {
|
2013-09-01 12:44:07 -07:00
|
|
|
if is_abs && v.as_slice().is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let mut comps: ~[&'a [u8]] = ~[];
|
|
|
|
let mut n_up = 0u;
|
|
|
|
let mut changed = false;
|
2013-08-27 00:51:08 -07:00
|
|
|
for comp in v.split_iter(is_sep) {
|
2013-09-01 12:44:07 -07:00
|
|
|
if comp.is_empty() { changed = true }
|
|
|
|
else if comp == bytes!(".") { changed = true }
|
|
|
|
else if comp == bytes!("..") {
|
|
|
|
if is_abs && comps.is_empty() { changed = true }
|
|
|
|
else if comps.len() == n_up { comps.push(dot_dot_static); n_up += 1 }
|
2013-09-26 14:38:26 -07:00
|
|
|
else { comps.pop(); changed = true }
|
2013-09-01 12:44:07 -07:00
|
|
|
} else { comps.push(comp) }
|
|
|
|
}
|
|
|
|
if changed {
|
|
|
|
if comps.is_empty() && !is_abs {
|
|
|
|
if v == bytes!(".") {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
comps.push(dot_static);
|
|
|
|
}
|
|
|
|
Some(comps)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME (#8169): Pull this into parent module once visibility works
|
|
|
|
#[inline(always)]
|
|
|
|
fn contains_nul(v: &[u8]) -> bool {
|
|
|
|
v.iter().any(|&x| x == 0)
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
static dot_static: &'static [u8] = bytes!(".");
|
|
|
|
static dot_dot_static: &'static [u8] = bytes!("..");
|
2013-09-01 12:44:07 -07:00
|
|
|
|
2013-09-26 14:24:06 -07:00
|
|
|
// Stat support
|
|
|
|
#[cfg(not(target_os = "win32"))]
|
|
|
|
impl Path {
|
|
|
|
/// Calls stat() on the represented file and returns the resulting libc::stat
|
|
|
|
pub fn stat(&self) -> Option<libc::stat> {
|
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
|
|
|
do self.with_c_str |buf| {
|
|
|
|
let mut st = super::stat::arch::default_stat();
|
|
|
|
match unsafe { libc::stat(buf as *libc::c_char, &mut st) } {
|
|
|
|
0 => Some(st),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns whether the represented file exists
|
|
|
|
pub fn exists(&self) -> bool {
|
|
|
|
match self.stat() {
|
|
|
|
None => false,
|
|
|
|
Some(_) => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the filesize of the represented file
|
|
|
|
pub fn get_size(&self) -> Option<i64> {
|
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(st) => Some(st.st_size as i64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the mode of the represented file
|
|
|
|
pub fn get_mode(&self) -> Option<uint> {
|
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(st) => Some(st.st_mode as uint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
impl Path {
|
|
|
|
/// Returns the atime of the represented file, as (secs, nsecs)
|
|
|
|
pub fn get_atime(&self) -> Option<(i64, int)> {
|
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(st) => Some((st.st_atime as i64, st.st_atime_nsec as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the mtime of the represented file, as (secs, nsecs)
|
|
|
|
pub fn get_mtime(&self) -> Option<(i64, int)> {
|
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(st) => Some((st.st_mtime as i64, st.st_mtime_nsec as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the ctime of the represented file, as (secs, nsecs)
|
|
|
|
pub fn get_ctime(&self) -> Option<(i64, int)> {
|
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(st) => Some((st.st_ctime as i64, st.st_ctime_nsec as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
impl Path {
|
|
|
|
/// Calls lstat() on the represented file and returns the resulting libc::stat
|
|
|
|
pub fn lstat(&self) -> Option<libc::stat> {
|
|
|
|
#[fixed_stack_segment]; #[inline(never)];
|
|
|
|
do self.with_c_str |buf| {
|
|
|
|
let mut st = super::stat::arch::default_stat();
|
|
|
|
match unsafe { libc::lstat(buf, &mut st) } {
|
|
|
|
0 => Some(st),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
impl Path {
|
|
|
|
/// Returns the birthtime of the represented file
|
|
|
|
pub fn get_birthtime(&self) -> Option<(i64, int)> {
|
|
|
|
match self.stat() {
|
|
|
|
None => None,
|
|
|
|
Some(st) => Some((st.st_birthtime as i64, st.st_birthtime_nsec as int))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2013-09-26 17:21:59 -07:00
|
|
|
use option::{Option, Some, None};
|
2013-09-30 17:13:13 -07:00
|
|
|
use iter::Iterator;
|
2013-09-01 12:44:07 -07:00
|
|
|
use str;
|
|
|
|
use vec::Vector;
|
|
|
|
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $exp:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
|
|
|
assert_eq!(path.as_str(), Some($exp));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $exp:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
|
|
|
assert_eq!(path.as_vec(), $exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
macro_rules! b(
|
|
|
|
($($arg:expr),+) => (
|
|
|
|
bytes!($($arg),+)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_paths() {
|
2013-10-05 19:49:32 -07:00
|
|
|
let empty: &[u8] = [];
|
|
|
|
t!(v: Path::new(empty), b!("."));
|
|
|
|
t!(v: Path::new(b!("/")), b!("/"));
|
|
|
|
t!(v: Path::new(b!("a/b/c")), b!("a/b/c"));
|
|
|
|
t!(v: Path::new(b!("a/b/c", 0xff)), b!("a/b/c", 0xff));
|
|
|
|
t!(v: Path::new(b!(0xff, "/../foo", 0x80)), b!("foo", 0x80));
|
|
|
|
let p = Path::new(b!("a/b/c", 0xff));
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(p.as_str(), None);
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: Path::new(""), ".");
|
|
|
|
t!(s: Path::new("/"), "/");
|
|
|
|
t!(s: Path::new("hi"), "hi");
|
|
|
|
t!(s: Path::new("hi/"), "hi");
|
|
|
|
t!(s: Path::new("/lib"), "/lib");
|
|
|
|
t!(s: Path::new("/lib/"), "/lib");
|
|
|
|
t!(s: Path::new("hi/there"), "hi/there");
|
|
|
|
t!(s: Path::new("hi/there.txt"), "hi/there.txt");
|
|
|
|
|
|
|
|
t!(s: Path::new("hi/there/"), "hi/there");
|
|
|
|
t!(s: Path::new("hi/../there"), "there");
|
|
|
|
t!(s: Path::new("../hi/there"), "../hi/there");
|
|
|
|
t!(s: Path::new("/../hi/there"), "/hi/there");
|
|
|
|
t!(s: Path::new("foo/.."), ".");
|
|
|
|
t!(s: Path::new("/foo/.."), "/");
|
|
|
|
t!(s: Path::new("/foo/../.."), "/");
|
|
|
|
t!(s: Path::new("/foo/../../bar"), "/bar");
|
|
|
|
t!(s: Path::new("/./hi/./there/."), "/hi/there");
|
|
|
|
t!(s: Path::new("/./hi/./there/./.."), "/hi");
|
|
|
|
t!(s: Path::new("foo/../.."), "..");
|
|
|
|
t!(s: Path::new("foo/../../.."), "../..");
|
|
|
|
t!(s: Path::new("foo/../../bar"), "../bar");
|
|
|
|
|
|
|
|
assert_eq!(Path::new(b!("foo/bar")).into_vec(), b!("foo/bar").to_owned());
|
|
|
|
assert_eq!(Path::new(b!("/foo/../../bar")).into_vec(),
|
2013-09-01 12:44:07 -07:00
|
|
|
b!("/bar").to_owned());
|
2013-10-05 19:49:32 -07:00
|
|
|
assert_eq!(Path::new("foo/bar").into_str(), Some(~"foo/bar"));
|
|
|
|
assert_eq!(Path::new("/foo/../../bar").into_str(), Some(~"/bar"));
|
2013-09-01 12:44:07 -07:00
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
let p = Path::new(b!("foo/bar", 0x80));
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(p.as_str(), None);
|
2013-10-05 19:49:32 -07:00
|
|
|
assert_eq!(Path::new(b!("foo", 0xff, "/bar")).into_str(), None);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
2013-09-26 00:54:30 -07:00
|
|
|
#[test]
|
|
|
|
fn test_opt_paths() {
|
2013-10-05 19:49:32 -07:00
|
|
|
assert_eq!(Path::new_opt(b!("foo/bar", 0)), None);
|
|
|
|
t!(v: Path::new_opt(b!("foo/bar")).unwrap(), b!("foo/bar"));
|
|
|
|
assert_eq!(Path::new_opt("foo/bar\0"), None);
|
|
|
|
t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar");
|
2013-09-26 00:54:30 -07:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_null_byte() {
|
2013-09-26 17:21:59 -07:00
|
|
|
use path::null_byte::cond;
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
let mut handled = false;
|
|
|
|
let mut p = do cond.trap(|v| {
|
|
|
|
handled = true;
|
|
|
|
assert_eq!(v.as_slice(), b!("foo/bar", 0));
|
|
|
|
(b!("/bar").to_owned())
|
|
|
|
}).inside {
|
2013-10-05 19:49:32 -07:00
|
|
|
Path::new(b!("foo/bar", 0))
|
2013-09-01 12:44:07 -07:00
|
|
|
};
|
|
|
|
assert!(handled);
|
|
|
|
assert_eq!(p.as_vec(), b!("/bar"));
|
|
|
|
|
|
|
|
handled = false;
|
|
|
|
do cond.trap(|v| {
|
|
|
|
handled = true;
|
|
|
|
assert_eq!(v.as_slice(), b!("f", 0, "o"));
|
|
|
|
(b!("foo").to_owned())
|
|
|
|
}).inside {
|
|
|
|
p.set_filename(b!("f", 0, "o"))
|
|
|
|
};
|
|
|
|
assert!(handled);
|
|
|
|
assert_eq!(p.as_vec(), b!("/foo"));
|
|
|
|
|
|
|
|
handled = false;
|
|
|
|
do cond.trap(|v| {
|
|
|
|
handled = true;
|
|
|
|
assert_eq!(v.as_slice(), b!("null/", 0, "/byte"));
|
|
|
|
(b!("null/byte").to_owned())
|
|
|
|
}).inside {
|
|
|
|
p.set_dirname(b!("null/", 0, "/byte"));
|
|
|
|
};
|
|
|
|
assert!(handled);
|
|
|
|
assert_eq!(p.as_vec(), b!("null/byte/foo"));
|
|
|
|
|
|
|
|
handled = false;
|
|
|
|
do cond.trap(|v| {
|
|
|
|
handled = true;
|
|
|
|
assert_eq!(v.as_slice(), b!("f", 0, "o"));
|
|
|
|
(b!("foo").to_owned())
|
|
|
|
}).inside {
|
|
|
|
p.push(b!("f", 0, "o"));
|
|
|
|
};
|
|
|
|
assert!(handled);
|
|
|
|
assert_eq!(p.as_vec(), b!("null/byte/foo/foo"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_null_byte_fail() {
|
2013-09-26 17:21:59 -07:00
|
|
|
use path::null_byte::cond;
|
2013-09-01 12:44:07 -07:00
|
|
|
use task;
|
|
|
|
|
|
|
|
macro_rules! t(
|
|
|
|
($name:expr => $code:block) => (
|
|
|
|
{
|
|
|
|
let mut t = task::task();
|
|
|
|
t.supervised();
|
|
|
|
t.name($name);
|
|
|
|
let res = do t.try $code;
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(~"new() w/nul" => {
|
2013-09-01 12:44:07 -07:00
|
|
|
do cond.trap(|_| {
|
|
|
|
(b!("null", 0).to_owned())
|
|
|
|
}).inside {
|
2013-10-05 19:49:32 -07:00
|
|
|
Path::new(b!("foo/bar", 0))
|
2013-09-01 12:44:07 -07:00
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
t!(~"set_filename w/nul" => {
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p = Path::new(b!("foo/bar"));
|
2013-09-01 12:44:07 -07:00
|
|
|
do cond.trap(|_| {
|
|
|
|
(b!("null", 0).to_owned())
|
|
|
|
}).inside {
|
|
|
|
p.set_filename(b!("foo", 0))
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
t!(~"set_dirname w/nul" => {
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p = Path::new(b!("foo/bar"));
|
2013-09-01 12:44:07 -07:00
|
|
|
do cond.trap(|_| {
|
|
|
|
(b!("null", 0).to_owned())
|
|
|
|
}).inside {
|
|
|
|
p.set_dirname(b!("foo", 0))
|
|
|
|
};
|
|
|
|
})
|
|
|
|
|
|
|
|
t!(~"push w/nul" => {
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p = Path::new(b!("foo/bar"));
|
2013-09-01 12:44:07 -07:00
|
|
|
do cond.trap(|_| {
|
|
|
|
(b!("null", 0).to_owned())
|
|
|
|
}).inside {
|
|
|
|
p.push(b!("foo", 0))
|
|
|
|
};
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-09-26 02:10:16 -07:00
|
|
|
#[test]
|
|
|
|
fn test_display_str() {
|
2013-10-06 18:51:49 -07:00
|
|
|
macro_rules! t(
|
|
|
|
($path:expr, $disp:ident, $exp:expr) => (
|
|
|
|
{
|
|
|
|
let path = Path::new($path);
|
|
|
|
assert_eq!(path.$disp().to_str(), ~$exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
t!("foo", display, "foo");
|
|
|
|
t!(b!("foo", 0x80), display, "foo\uFFFD");
|
|
|
|
t!(b!("foo", 0xff, "bar"), display, "foo\uFFFDbar");
|
|
|
|
t!(b!("foo", 0xff, "/bar"), filename_display, "bar");
|
|
|
|
t!(b!("foo/", 0xff, "bar"), filename_display, "\uFFFDbar");
|
|
|
|
t!(b!("/"), filename_display, "");
|
|
|
|
|
|
|
|
macro_rules! t(
|
|
|
|
($path:expr, $exp:expr) => (
|
|
|
|
{
|
|
|
|
let mut called = false;
|
|
|
|
let path = Path::new($path);
|
|
|
|
do path.display().with_str |s| {
|
|
|
|
assert_eq!(s, $exp);
|
|
|
|
called = true;
|
|
|
|
};
|
|
|
|
assert!(called);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
($path:expr, $exp:expr, filename) => (
|
|
|
|
{
|
|
|
|
let mut called = false;
|
|
|
|
let path = Path::new($path);
|
|
|
|
do path.filename_display().with_str |s| {
|
|
|
|
assert_eq!(s, $exp);
|
|
|
|
called = true;
|
|
|
|
|
|
|
|
};
|
|
|
|
assert!(called);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!("foo", "foo");
|
|
|
|
t!(b!("foo", 0x80), "foo\uFFFD");
|
|
|
|
t!(b!("foo", 0xff, "bar"), "foo\uFFFDbar");
|
|
|
|
t!(b!("foo", 0xff, "/bar"), "bar", filename);
|
|
|
|
t!(b!("foo/", 0xff, "bar"), "\uFFFDbar", filename);
|
|
|
|
t!(b!("/"), "", filename);
|
2013-09-26 02:10:16 -07:00
|
|
|
}
|
|
|
|
|
2013-10-01 14:03:41 -07:00
|
|
|
#[test]
|
|
|
|
fn test_display() {
|
|
|
|
macro_rules! t(
|
|
|
|
($path:expr, $exp:expr, $expf:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
2013-10-01 14:03:41 -07:00
|
|
|
let f = format!("{}", path.display());
|
|
|
|
assert_eq!(f.as_slice(), $exp);
|
|
|
|
let f = format!("{}", path.filename_display());
|
|
|
|
assert_eq!(f.as_slice(), $expf);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(b!("foo"), "foo", "foo");
|
|
|
|
t!(b!("foo/bar"), "foo/bar", "bar");
|
|
|
|
t!(b!("/"), "/", "");
|
|
|
|
t!(b!("foo", 0xff), "foo\uFFFD", "foo\uFFFD");
|
|
|
|
t!(b!("foo", 0xff, "/bar"), "foo\uFFFD/bar", "bar");
|
|
|
|
t!(b!("foo/", 0xff, "bar"), "foo/\uFFFDbar", "\uFFFDbar");
|
|
|
|
t!(b!(0xff, "foo/bar", 0xff), "\uFFFDfoo/bar\uFFFD", "bar\uFFFD");
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_components() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $op:ident, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(path.$op(), ($exp).as_bytes());
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(s: $path:expr, $op:ident, $exp:expr, opt) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
2013-09-30 17:13:13 -07:00
|
|
|
let left = path.$op().map(|&x| str::from_utf8_slice(x));
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(left, $exp);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $op:ident, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(path.$op(), $exp);
|
|
|
|
}
|
2013-09-26 17:21:59 -07:00
|
|
|
);
|
2013-09-01 12:44:07 -07:00
|
|
|
)
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
t!(v: b!("a/b/c"), filename, Some(b!("c")));
|
|
|
|
t!(v: b!("a/b/c", 0xff), filename, Some(b!("c", 0xff)));
|
|
|
|
t!(v: b!("a/b", 0xff, "/c"), filename, Some(b!("c")));
|
|
|
|
t!(s: "a/b/c", filename, Some("c"), opt);
|
|
|
|
t!(s: "/a/b/c", filename, Some("c"), opt);
|
|
|
|
t!(s: "a", filename, Some("a"), opt);
|
|
|
|
t!(s: "/a", filename, Some("a"), opt);
|
|
|
|
t!(s: ".", filename, None, opt);
|
|
|
|
t!(s: "/", filename, None, opt);
|
|
|
|
t!(s: "..", filename, None, opt);
|
|
|
|
t!(s: "../..", filename, None, opt);
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
t!(v: b!("a/b/c"), dirname, b!("a/b"));
|
|
|
|
t!(v: b!("a/b/c", 0xff), dirname, b!("a/b"));
|
|
|
|
t!(v: b!("a/b", 0xff, "/c"), dirname, b!("a/b", 0xff));
|
|
|
|
t!(s: "a/b/c", dirname, "a/b");
|
|
|
|
t!(s: "/a/b/c", dirname, "/a/b");
|
|
|
|
t!(s: "a", dirname, ".");
|
|
|
|
t!(s: "/a", dirname, "/");
|
|
|
|
t!(s: ".", dirname, ".");
|
|
|
|
t!(s: "/", dirname, "/");
|
|
|
|
t!(s: "..", dirname, "..");
|
|
|
|
t!(s: "../..", dirname, "../..");
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
t!(v: b!("hi/there.txt"), filestem, Some(b!("there")));
|
|
|
|
t!(v: b!("hi/there", 0x80, ".txt"), filestem, Some(b!("there", 0x80)));
|
|
|
|
t!(v: b!("hi/there.t", 0x80, "xt"), filestem, Some(b!("there")));
|
|
|
|
t!(s: "hi/there.txt", filestem, Some("there"), opt);
|
|
|
|
t!(s: "hi/there", filestem, Some("there"), opt);
|
|
|
|
t!(s: "there.txt", filestem, Some("there"), opt);
|
|
|
|
t!(s: "there", filestem, Some("there"), opt);
|
|
|
|
t!(s: ".", filestem, None, opt);
|
|
|
|
t!(s: "/", filestem, None, opt);
|
|
|
|
t!(s: "foo/.bar", filestem, Some(".bar"), opt);
|
|
|
|
t!(s: ".bar", filestem, Some(".bar"), opt);
|
|
|
|
t!(s: "..bar", filestem, Some("."), opt);
|
|
|
|
t!(s: "hi/there..txt", filestem, Some("there."), opt);
|
|
|
|
t!(s: "..", filestem, None, opt);
|
|
|
|
t!(s: "../..", filestem, None, opt);
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
t!(v: b!("hi/there.txt"), extension, Some(b!("txt")));
|
|
|
|
t!(v: b!("hi/there", 0x80, ".txt"), extension, Some(b!("txt")));
|
|
|
|
t!(v: b!("hi/there.t", 0x80, "xt"), extension, Some(b!("t", 0x80, "xt")));
|
|
|
|
t!(v: b!("hi/there"), extension, None);
|
|
|
|
t!(v: b!("hi/there", 0x80), extension, None);
|
|
|
|
t!(s: "hi/there.txt", extension, Some("txt"), opt);
|
|
|
|
t!(s: "hi/there", extension, None, opt);
|
|
|
|
t!(s: "there.txt", extension, Some("txt"), opt);
|
|
|
|
t!(s: "there", extension, None, opt);
|
|
|
|
t!(s: ".", extension, None, opt);
|
|
|
|
t!(s: "/", extension, None, opt);
|
|
|
|
t!(s: "foo/.bar", extension, None, opt);
|
|
|
|
t!(s: ".bar", extension, None, opt);
|
|
|
|
t!(s: "..bar", extension, Some("bar"), opt);
|
|
|
|
t!(s: "hi/there..txt", extension, Some("txt"), opt);
|
|
|
|
t!(s: "..", extension, None, opt);
|
|
|
|
t!(s: "../..", extension, None, opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $join:expr) => (
|
|
|
|
{
|
|
|
|
let path = ($path);
|
|
|
|
let join = ($join);
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p1 = Path::new(path);
|
2013-09-01 12:44:07 -07:00
|
|
|
let p2 = p1.clone();
|
2013-10-05 19:49:32 -07:00
|
|
|
p1.push(join);
|
|
|
|
assert_eq!(p1, p2.join(join));
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "..");
|
|
|
|
t!(s: "/a/b/c", "d");
|
|
|
|
t!(s: "a/b", "c/d");
|
|
|
|
t!(s: "a/b", "/c/d");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_push_path() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $push:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p = Path::new($path);
|
|
|
|
let push = Path::new($push);
|
2013-09-01 12:44:07 -07:00
|
|
|
p.push_path(&push);
|
|
|
|
assert_eq!(p.as_str(), Some($exp));
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "d", "a/b/c/d");
|
|
|
|
t!(s: "/a/b/c", "d", "/a/b/c/d");
|
|
|
|
t!(s: "a/b", "c/d", "a/b/c/d");
|
|
|
|
t!(s: "a/b", "/c/d", "/c/d");
|
|
|
|
t!(s: "a/b", ".", "a/b");
|
|
|
|
t!(s: "a/b", "../c", "a/c");
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_push_many() {
|
|
|
|
use to_man = at_vec::to_managed_move;
|
|
|
|
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $push:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p = Path::new($path);
|
|
|
|
p.push_many($push);
|
2013-09-26 17:21:59 -07:00
|
|
|
assert_eq!(p.as_str(), Some($exp));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $push:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p = Path::new($path);
|
2013-09-26 17:21:59 -07:00
|
|
|
p.push_many($push);
|
|
|
|
assert_eq!(p.as_vec(), $exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
|
|
|
|
t!(s: "a/b/c", ["d", "/e"], "/e");
|
|
|
|
t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
|
|
|
|
t!(s: "a/b/c", [~"d", ~"e"], "a/b/c/d/e");
|
|
|
|
t!(s: "a/b/c", [@"d", @"e"], "a/b/c/d/e");
|
|
|
|
t!(v: b!("a/b/c"), [b!("d"), b!("e")], b!("a/b/c/d/e"));
|
|
|
|
t!(v: b!("a/b/c"), [b!("d"), b!("/e"), b!("f")], b!("/e/f"));
|
|
|
|
t!(v: b!("a/b/c"), [b!("d").to_owned(), b!("e").to_owned()], b!("a/b/c/d/e"));
|
|
|
|
t!(v: b!("a/b/c"), [to_man(b!("d").to_owned()), to_man(b!("e").to_owned())],
|
|
|
|
b!("a/b/c/d/e"));
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_pop() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $left:expr, $right:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p = Path::new($path);
|
2013-09-26 14:38:26 -07:00
|
|
|
let file = p.pop_str();
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(p.as_str(), Some($left));
|
|
|
|
assert_eq!(file.map(|s| s.as_slice()), $right);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: [$($path:expr),+], [$($left:expr),+], Some($($right:expr),+)) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p = Path::new(b!($($path),+));
|
2013-09-26 14:38:26 -07:00
|
|
|
let file = p.pop();
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(p.as_vec(), b!($($left),+));
|
|
|
|
assert_eq!(file.map(|v| v.as_slice()), Some(b!($($right),+)));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: [$($path:expr),+], [$($left:expr),+], None) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p = Path::new(b!($($path),+));
|
2013-09-26 14:38:26 -07:00
|
|
|
let file = p.pop();
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(p.as_vec(), b!($($left),+));
|
|
|
|
assert_eq!(file, None);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(v: ["a/b/c"], ["a/b"], Some("c"));
|
|
|
|
t!(v: ["a"], ["."], Some("a"));
|
|
|
|
t!(v: ["."], ["."], None);
|
|
|
|
t!(v: ["/a"], ["/"], Some("a"));
|
|
|
|
t!(v: ["/"], ["/"], None);
|
|
|
|
t!(v: ["a/b/c", 0x80], ["a/b"], Some("c", 0x80));
|
|
|
|
t!(v: ["a/b", 0x80, "/c"], ["a/b", 0x80], Some("c"));
|
|
|
|
t!(v: [0xff], ["."], Some(0xff));
|
|
|
|
t!(v: ["/", 0xff], ["/"], Some(0xff));
|
|
|
|
t!(s: "a/b/c", "a/b", Some("c"));
|
|
|
|
t!(s: "a", ".", Some("a"));
|
|
|
|
t!(s: ".", ".", None);
|
|
|
|
t!(s: "/a", "/", Some("a"));
|
|
|
|
t!(s: "/", "/", None);
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
assert_eq!(Path::new(b!("foo/bar", 0x80)).pop_str(), None);
|
|
|
|
assert_eq!(Path::new(b!("foo", 0x80, "/bar")).pop_str(), Some(~"bar"));
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_root_path() {
|
2013-10-05 19:49:32 -07:00
|
|
|
assert_eq!(Path::new(b!("a/b/c")).root_path(), None);
|
|
|
|
assert_eq!(Path::new(b!("/a/b/c")).root_path(), Some(Path::new("/")));
|
2013-09-26 17:21:59 -07:00
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_join() {
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(v: Path::new(b!("a/b/c")).join(b!("..")), b!("a/b"));
|
|
|
|
t!(v: Path::new(b!("/a/b/c")).join(b!("d")), b!("/a/b/c/d"));
|
|
|
|
t!(v: Path::new(b!("a/", 0x80, "/c")).join(b!(0xff)), b!("a/", 0x80, "/c/", 0xff));
|
|
|
|
t!(s: Path::new("a/b/c").join(".."), "a/b");
|
|
|
|
t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d");
|
|
|
|
t!(s: Path::new("a/b").join("c/d"), "a/b/c/d");
|
|
|
|
t!(s: Path::new("a/b").join("/c/d"), "/c/d");
|
|
|
|
t!(s: Path::new(".").join("a/b"), "a/b");
|
|
|
|
t!(s: Path::new("/").join("a/b"), "/a/b");
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_join_path() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $join:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
|
|
|
let join = Path::new($join);
|
2013-09-01 12:44:07 -07:00
|
|
|
let res = path.join_path(&join);
|
|
|
|
assert_eq!(res.as_str(), Some($exp));
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "..", "a/b");
|
|
|
|
t!(s: "/a/b/c", "d", "/a/b/c/d");
|
|
|
|
t!(s: "a/b", "c/d", "a/b/c/d");
|
|
|
|
t!(s: "a/b", "/c/d", "/c/d");
|
|
|
|
t!(s: ".", "a/b", "a/b");
|
|
|
|
t!(s: "/", "a/b", "/a/b");
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_join_many() {
|
|
|
|
use to_man = at_vec::to_managed_move;
|
|
|
|
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $join:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
|
|
|
let res = path.join_many($join);
|
2013-09-26 17:21:59 -07:00
|
|
|
assert_eq!(res.as_str(), Some($exp));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $join:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
2013-09-26 17:21:59 -07:00
|
|
|
let res = path.join_many($join);
|
|
|
|
assert_eq!(res.as_vec(), $exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", ["d", "e"], "a/b/c/d/e");
|
|
|
|
t!(s: "a/b/c", ["..", "d"], "a/b/d");
|
|
|
|
t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
|
|
|
|
t!(s: "a/b/c", [~"d", ~"e"], "a/b/c/d/e");
|
|
|
|
t!(s: "a/b/c", [@"d", @"e"], "a/b/c/d/e");
|
|
|
|
t!(v: b!("a/b/c"), [b!("d"), b!("e")], b!("a/b/c/d/e"));
|
|
|
|
t!(v: b!("a/b/c"), [b!("d").to_owned(), b!("e").to_owned()], b!("a/b/c/d/e"));
|
|
|
|
t!(v: b!("a/b/c"), [to_man(b!("d").to_owned()), to_man(b!("e").to_owned())],
|
|
|
|
b!("a/b/c/d/e"));
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_with_helpers() {
|
2013-10-05 19:49:32 -07:00
|
|
|
let empty: &[u8] = [];
|
|
|
|
|
|
|
|
t!(v: Path::new(b!("a/b/c")).with_dirname(b!("d")), b!("d/c"));
|
|
|
|
t!(v: Path::new(b!("a/b/c")).with_dirname(b!("d/e")), b!("d/e/c"));
|
|
|
|
t!(v: Path::new(b!("a/", 0x80, "b/c")).with_dirname(b!(0xff)), b!(0xff, "/c"));
|
|
|
|
t!(v: Path::new(b!("a/b/", 0x80)).with_dirname(b!("/", 0xcd)),
|
2013-09-01 12:44:07 -07:00
|
|
|
b!("/", 0xcd, "/", 0x80));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: Path::new("a/b/c").with_dirname("d"), "d/c");
|
|
|
|
t!(s: Path::new("a/b/c").with_dirname("d/e"), "d/e/c");
|
|
|
|
t!(s: Path::new("a/b/c").with_dirname(""), "c");
|
|
|
|
t!(s: Path::new("a/b/c").with_dirname("/"), "/c");
|
|
|
|
t!(s: Path::new("a/b/c").with_dirname("."), "c");
|
|
|
|
t!(s: Path::new("a/b/c").with_dirname(".."), "../c");
|
|
|
|
t!(s: Path::new("/").with_dirname("foo"), "foo");
|
|
|
|
t!(s: Path::new("/").with_dirname(""), ".");
|
|
|
|
t!(s: Path::new("/foo").with_dirname("bar"), "bar/foo");
|
|
|
|
t!(s: Path::new("..").with_dirname("foo"), "foo");
|
|
|
|
t!(s: Path::new("../..").with_dirname("foo"), "foo");
|
|
|
|
t!(s: Path::new("..").with_dirname(""), ".");
|
|
|
|
t!(s: Path::new("../..").with_dirname(""), ".");
|
|
|
|
t!(s: Path::new("foo").with_dirname(".."), "../foo");
|
|
|
|
t!(s: Path::new("foo").with_dirname("../.."), "../../foo");
|
|
|
|
|
|
|
|
t!(v: Path::new(b!("a/b/c")).with_filename(b!("d")), b!("a/b/d"));
|
|
|
|
t!(v: Path::new(b!("a/b/c", 0xff)).with_filename(b!(0x80)), b!("a/b/", 0x80));
|
|
|
|
t!(v: Path::new(b!("/", 0xff, "/foo")).with_filename(b!(0xcd)),
|
2013-09-01 12:44:07 -07:00
|
|
|
b!("/", 0xff, "/", 0xcd));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d");
|
|
|
|
t!(s: Path::new(".").with_filename("foo"), "foo");
|
|
|
|
t!(s: Path::new("/a/b/c").with_filename("d"), "/a/b/d");
|
|
|
|
t!(s: Path::new("/").with_filename("foo"), "/foo");
|
|
|
|
t!(s: Path::new("/a").with_filename("foo"), "/foo");
|
|
|
|
t!(s: Path::new("foo").with_filename("bar"), "bar");
|
|
|
|
t!(s: Path::new("/").with_filename("foo/"), "/foo");
|
|
|
|
t!(s: Path::new("/a").with_filename("foo/"), "/foo");
|
|
|
|
t!(s: Path::new("a/b/c").with_filename(""), "a/b");
|
|
|
|
t!(s: Path::new("a/b/c").with_filename("."), "a/b");
|
|
|
|
t!(s: Path::new("a/b/c").with_filename(".."), "a");
|
|
|
|
t!(s: Path::new("/a").with_filename(""), "/");
|
|
|
|
t!(s: Path::new("foo").with_filename(""), ".");
|
|
|
|
t!(s: Path::new("a/b/c").with_filename("d/e"), "a/b/d/e");
|
|
|
|
t!(s: Path::new("a/b/c").with_filename("/d"), "a/b/d");
|
|
|
|
t!(s: Path::new("..").with_filename("foo"), "../foo");
|
|
|
|
t!(s: Path::new("../..").with_filename("foo"), "../../foo");
|
|
|
|
t!(s: Path::new("..").with_filename(""), "..");
|
|
|
|
t!(s: Path::new("../..").with_filename(""), "../..");
|
|
|
|
|
|
|
|
t!(v: Path::new(b!("hi/there", 0x80, ".txt")).with_filestem(b!(0xff)),
|
2013-09-01 12:44:07 -07:00
|
|
|
b!("hi/", 0xff, ".txt"));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(v: Path::new(b!("hi/there.txt", 0x80)).with_filestem(b!(0xff)),
|
2013-09-01 12:44:07 -07:00
|
|
|
b!("hi/", 0xff, ".txt", 0x80));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(v: Path::new(b!("hi/there", 0xff)).with_filestem(b!(0x80)), b!("hi/", 0x80));
|
|
|
|
t!(v: Path::new(b!("hi", 0x80, "/there")).with_filestem(empty), b!("hi", 0x80));
|
|
|
|
t!(s: Path::new("hi/there.txt").with_filestem("here"), "hi/here.txt");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_filestem(""), "hi/.txt");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_filestem("."), "hi/..txt");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_filestem(".."), "hi/...txt");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_filestem("/"), "hi/.txt");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_filestem("foo/bar"), "hi/foo/bar.txt");
|
|
|
|
t!(s: Path::new("hi/there.foo.txt").with_filestem("here"), "hi/here.txt");
|
|
|
|
t!(s: Path::new("hi/there").with_filestem("here"), "hi/here");
|
|
|
|
t!(s: Path::new("hi/there").with_filestem(""), "hi");
|
|
|
|
t!(s: Path::new("hi").with_filestem(""), ".");
|
|
|
|
t!(s: Path::new("/hi").with_filestem(""), "/");
|
|
|
|
t!(s: Path::new("hi/there").with_filestem(".."), ".");
|
|
|
|
t!(s: Path::new("hi/there").with_filestem("."), "hi");
|
|
|
|
t!(s: Path::new("hi/there.").with_filestem("foo"), "hi/foo.");
|
|
|
|
t!(s: Path::new("hi/there.").with_filestem(""), "hi");
|
|
|
|
t!(s: Path::new("hi/there.").with_filestem("."), ".");
|
|
|
|
t!(s: Path::new("hi/there.").with_filestem(".."), "hi/...");
|
|
|
|
t!(s: Path::new("/").with_filestem("foo"), "/foo");
|
|
|
|
t!(s: Path::new(".").with_filestem("foo"), "foo");
|
|
|
|
t!(s: Path::new("hi/there..").with_filestem("here"), "hi/here.");
|
|
|
|
t!(s: Path::new("hi/there..").with_filestem(""), "hi");
|
|
|
|
|
|
|
|
t!(v: Path::new(b!("hi/there", 0x80, ".txt")).with_extension(b!("exe")),
|
2013-09-01 12:44:07 -07:00
|
|
|
b!("hi/there", 0x80, ".exe"));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(v: Path::new(b!("hi/there.txt", 0x80)).with_extension(b!(0xff)),
|
2013-09-01 12:44:07 -07:00
|
|
|
b!("hi/there.", 0xff));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(v: Path::new(b!("hi/there", 0x80)).with_extension(b!(0xff)),
|
2013-09-01 12:44:07 -07:00
|
|
|
b!("hi/there", 0x80, ".", 0xff));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(v: Path::new(b!("hi/there.", 0xff)).with_extension(empty), b!("hi/there"));
|
|
|
|
t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_extension(""), "hi/there");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_extension("."), "hi/there..");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_extension(".."), "hi/there...");
|
|
|
|
t!(s: Path::new("hi/there").with_extension("txt"), "hi/there.txt");
|
|
|
|
t!(s: Path::new("hi/there").with_extension("."), "hi/there..");
|
|
|
|
t!(s: Path::new("hi/there").with_extension(".."), "hi/there...");
|
|
|
|
t!(s: Path::new("hi/there.").with_extension("txt"), "hi/there.txt");
|
|
|
|
t!(s: Path::new("hi/.foo").with_extension("txt"), "hi/.foo.txt");
|
|
|
|
t!(s: Path::new("hi/there.txt").with_extension(".foo"), "hi/there..foo");
|
|
|
|
t!(s: Path::new("/").with_extension("txt"), "/");
|
|
|
|
t!(s: Path::new("/").with_extension("."), "/");
|
|
|
|
t!(s: Path::new("/").with_extension(".."), "/");
|
|
|
|
t!(s: Path::new(".").with_extension("txt"), ".");
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_setters() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $set:ident, $with:ident, $arg:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
|
|
|
let arg = $arg;
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p1 = Path::new(path);
|
2013-09-01 12:44:07 -07:00
|
|
|
p1.$set(arg);
|
2013-10-05 19:49:32 -07:00
|
|
|
let p2 = Path::new(path);
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(p1, p2.$with(arg));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $set:ident, $with:ident, $arg:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
|
|
|
let arg = $arg;
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut p1 = Path::new(path);
|
2013-09-01 12:44:07 -07:00
|
|
|
p1.$set(arg);
|
2013-10-05 19:49:32 -07:00
|
|
|
let p2 = Path::new(path);
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(p1, p2.$with(arg));
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(v: b!("a/b/c"), set_dirname, with_dirname, b!("d"));
|
|
|
|
t!(v: b!("a/b/c"), set_dirname, with_dirname, b!("d/e"));
|
|
|
|
t!(v: b!("a/", 0x80, "/c"), set_dirname, with_dirname, b!(0xff));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: "a/b/c", set_dirname, with_dirname, "d");
|
|
|
|
t!(s: "a/b/c", set_dirname, with_dirname, "d/e");
|
|
|
|
t!(s: "/", set_dirname, with_dirname, "foo");
|
|
|
|
t!(s: "/foo", set_dirname, with_dirname, "bar");
|
|
|
|
t!(s: "a/b/c", set_dirname, with_dirname, "");
|
|
|
|
t!(s: "../..", set_dirname, with_dirname, "x");
|
|
|
|
t!(s: "foo", set_dirname, with_dirname, "../..");
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
t!(v: b!("a/b/c"), set_filename, with_filename, b!("d"));
|
|
|
|
t!(v: b!("/"), set_filename, with_filename, b!("foo"));
|
|
|
|
t!(v: b!(0x80), set_filename, with_filename, b!(0xff));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: "a/b/c", set_filename, with_filename, "d");
|
|
|
|
t!(s: "/", set_filename, with_filename, "foo");
|
|
|
|
t!(s: ".", set_filename, with_filename, "foo");
|
|
|
|
t!(s: "a/b", set_filename, with_filename, "");
|
|
|
|
t!(s: "a", set_filename, with_filename, "");
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
t!(v: b!("hi/there.txt"), set_filestem, with_filestem, b!("here"));
|
|
|
|
t!(v: b!("hi/there", 0x80, ".txt"), set_filestem, with_filestem, b!("here", 0xff));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: "hi/there.txt", set_filestem, with_filestem, "here");
|
|
|
|
t!(s: "hi/there.", set_filestem, with_filestem, "here");
|
|
|
|
t!(s: "hi/there", set_filestem, with_filestem, "here");
|
|
|
|
t!(s: "hi/there.txt", set_filestem, with_filestem, "");
|
|
|
|
t!(s: "hi/there", set_filestem, with_filestem, "");
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
t!(v: b!("hi/there.txt"), set_extension, with_extension, b!("exe"));
|
|
|
|
t!(v: b!("hi/there.t", 0x80, "xt"), set_extension, with_extension, b!("exe", 0xff));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: "hi/there.txt", set_extension, with_extension, "exe");
|
|
|
|
t!(s: "hi/there.", set_extension, with_extension, "txt");
|
|
|
|
t!(s: "hi/there", set_extension, with_extension, "txt");
|
|
|
|
t!(s: "hi/there.txt", set_extension, with_extension, "");
|
|
|
|
t!(s: "hi/there", set_extension, with_extension, "");
|
|
|
|
t!(s: ".", set_extension, with_extension, "txt");
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_add_extension() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $ext:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut path = Path::new($path);
|
|
|
|
path.add_extension($ext);
|
2013-09-26 17:21:59 -07:00
|
|
|
assert_eq!(path.as_str(), Some($exp));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $ext:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let mut path = Path::new($path);
|
2013-09-26 17:21:59 -07:00
|
|
|
path.add_extension($ext);
|
|
|
|
assert_eq!(path.as_vec(), $exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "hi/there.txt", "foo", "hi/there.txt.foo");
|
|
|
|
t!(s: "hi/there.", "foo", "hi/there..foo");
|
|
|
|
t!(s: "hi/there", ".foo", "hi/there..foo");
|
|
|
|
t!(s: "hi/there.txt", "", "hi/there.txt");
|
|
|
|
t!(v: b!("hi/there.txt"), b!("foo"), b!("hi/there.txt.foo"));
|
|
|
|
t!(v: b!("hi/there"), b!("bar"), b!("hi/there.bar"));
|
|
|
|
t!(v: b!("/"), b!("foo"), b!("/"));
|
|
|
|
t!(v: b!("."), b!("foo"), b!("."));
|
|
|
|
t!(v: b!("hi/there.", 0x80, "foo"), b!(0xff), b!("hi/there.", 0x80, "foo.", 0xff));
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_getters() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
2013-09-26 17:21:59 -07:00
|
|
|
let filename = $filename;
|
|
|
|
assert!(path.filename_str() == filename,
|
2013-10-01 23:41:59 -07:00
|
|
|
"{}.filename_str(): Expected `{:?}`, found {:?}",
|
2013-09-26 17:21:59 -07:00
|
|
|
path.as_str().unwrap(), filename, path.filename_str());
|
|
|
|
let dirname = $dirname;
|
|
|
|
assert!(path.dirname_str() == dirname,
|
2013-10-01 23:41:59 -07:00
|
|
|
"`{}`.dirname_str(): Expected `{:?}`, found `{:?}`",
|
2013-09-26 17:21:59 -07:00
|
|
|
path.as_str().unwrap(), dirname, path.dirname_str());
|
|
|
|
let filestem = $filestem;
|
|
|
|
assert!(path.filestem_str() == filestem,
|
2013-10-01 23:41:59 -07:00
|
|
|
"`{}`.filestem_str(): Expected `{:?}`, found `{:?}`",
|
2013-09-26 17:21:59 -07:00
|
|
|
path.as_str().unwrap(), filestem, path.filestem_str());
|
|
|
|
let ext = $ext;
|
|
|
|
assert!(path.extension_str() == ext,
|
2013-10-01 23:41:59 -07:00
|
|
|
"`{}`.extension_str(): Expected `{:?}`, found `{:?}`",
|
2013-09-26 17:21:59 -07:00
|
|
|
path.as_str().unwrap(), ext, path.extension_str());
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $filename:expr, $dirname:expr, $filestem:expr, $ext:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
|
|
|
assert_eq!(path.filename(), $filename);
|
|
|
|
assert_eq!(path.dirname(), $dirname);
|
|
|
|
assert_eq!(path.filestem(), $filestem);
|
|
|
|
assert_eq!(path.extension(), $ext);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(v: Path::new(b!("a/b/c")), Some(b!("c")), b!("a/b"), Some(b!("c")), None);
|
|
|
|
t!(v: Path::new(b!("a/b/", 0xff)), Some(b!(0xff)), b!("a/b"), Some(b!(0xff)), None);
|
|
|
|
t!(v: Path::new(b!("hi/there.", 0xff)), Some(b!("there.", 0xff)), b!("hi"),
|
2013-09-26 17:21:59 -07:00
|
|
|
Some(b!("there")), Some(b!(0xff)));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None);
|
|
|
|
t!(s: Path::new("."), None, Some("."), None, None);
|
|
|
|
t!(s: Path::new("/"), None, Some("/"), None, None);
|
|
|
|
t!(s: Path::new(".."), None, Some(".."), None, None);
|
|
|
|
t!(s: Path::new("../.."), None, Some("../.."), None, None);
|
|
|
|
t!(s: Path::new("hi/there.txt"), Some("there.txt"), Some("hi"),
|
2013-09-01 12:44:07 -07:00
|
|
|
Some("there"), Some("txt"));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: Path::new("hi/there"), Some("there"), Some("hi"), Some("there"), None);
|
|
|
|
t!(s: Path::new("hi/there."), Some("there."), Some("hi"),
|
2013-09-01 12:44:07 -07:00
|
|
|
Some("there"), Some(""));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None);
|
|
|
|
t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"),
|
2013-09-01 12:44:07 -07:00
|
|
|
Some("."), Some("there"));
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(s: Path::new(b!("a/b/", 0xff)), None, Some("a/b"), None, None);
|
|
|
|
t!(s: Path::new(b!("a/b/", 0xff, ".txt")), None, Some("a/b"), None, Some("txt"));
|
|
|
|
t!(s: Path::new(b!("a/b/c.", 0x80)), None, Some("a/b"), Some("c"), None);
|
|
|
|
t!(s: Path::new(b!(0xff, "/b")), Some("b"), None, Some("b"), None);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dir_file_path() {
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(v: Path::new(b!("hi/there", 0x80)).dir_path(), b!("hi"));
|
|
|
|
t!(v: Path::new(b!("hi", 0xff, "/there")).dir_path(), b!("hi", 0xff));
|
|
|
|
t!(s: Path::new("hi/there").dir_path(), "hi");
|
|
|
|
t!(s: Path::new("hi").dir_path(), ".");
|
|
|
|
t!(s: Path::new("/hi").dir_path(), "/");
|
|
|
|
t!(s: Path::new("/").dir_path(), "/");
|
|
|
|
t!(s: Path::new("..").dir_path(), "..");
|
|
|
|
t!(s: Path::new("../..").dir_path(), "../..");
|
2013-09-01 12:44:07 -07:00
|
|
|
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $exp:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
2013-09-30 17:13:13 -07:00
|
|
|
let left = path.and_then_ref(|p| p.as_str());
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(left, $exp);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $exp:expr) => (
|
|
|
|
{
|
|
|
|
let path = $path;
|
|
|
|
let left = path.map(|p| p.as_vec());
|
|
|
|
assert_eq!(left, $exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-10-05 19:49:32 -07:00
|
|
|
t!(v: Path::new(b!("hi/there", 0x80)).file_path(), Some(b!("there", 0x80)));
|
|
|
|
t!(v: Path::new(b!("hi", 0xff, "/there")).file_path(), Some(b!("there")));
|
|
|
|
t!(s: Path::new("hi/there").file_path(), Some("there"));
|
|
|
|
t!(s: Path::new("hi").file_path(), Some("hi"));
|
|
|
|
t!(s: Path::new(".").file_path(), None);
|
|
|
|
t!(s: Path::new("/").file_path(), None);
|
|
|
|
t!(s: Path::new("..").file_path(), None);
|
|
|
|
t!(s: Path::new("../..").file_path(), None);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_absolute() {
|
2013-09-26 17:21:59 -07:00
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $abs:expr, $rel:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
2013-09-26 17:21:59 -07:00
|
|
|
assert_eq!(path.is_absolute(), $abs);
|
|
|
|
assert_eq!(path.is_relative(), $rel);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
t!(s: "a/b/c", false, true);
|
|
|
|
t!(s: "/a/b/c", true, false);
|
|
|
|
t!(s: "a", false, true);
|
|
|
|
t!(s: "/a", true, false);
|
|
|
|
t!(s: ".", false, true);
|
|
|
|
t!(s: "/", true, false);
|
|
|
|
t!(s: "..", false, true);
|
|
|
|
t!(s: "../..", false, true);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_ancestor_of() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $dest:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
|
|
|
let dest = Path::new($dest);
|
2013-09-01 12:44:07 -07:00
|
|
|
assert_eq!(path.is_ancestor_of(&dest), $exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "a/b/c/d", true);
|
|
|
|
t!(s: "a/b/c", "a/b/c", true);
|
|
|
|
t!(s: "a/b/c", "a/b", false);
|
|
|
|
t!(s: "/a/b/c", "/a/b/c", true);
|
|
|
|
t!(s: "/a/b", "/a/b/c", true);
|
|
|
|
t!(s: "/a/b/c/d", "/a/b/c", false);
|
|
|
|
t!(s: "/a/b", "a/b/c", false);
|
|
|
|
t!(s: "a/b", "/a/b/c", false);
|
|
|
|
t!(s: "a/b/c", "a/b/d", false);
|
|
|
|
t!(s: "../a/b/c", "a/b/c", false);
|
|
|
|
t!(s: "a/b/c", "../a/b/c", false);
|
|
|
|
t!(s: "a/b/c", "a/b/cd", false);
|
|
|
|
t!(s: "a/b/cd", "a/b/c", false);
|
|
|
|
t!(s: "../a/b", "../a/b/c", true);
|
|
|
|
t!(s: ".", "a/b", true);
|
|
|
|
t!(s: ".", ".", true);
|
|
|
|
t!(s: "/", "/", true);
|
|
|
|
t!(s: "/", "/a/b", true);
|
|
|
|
t!(s: "..", "a/b", true);
|
|
|
|
t!(s: "../..", "a/b", true);
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_ends_with_path() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $child:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
|
|
|
let child = Path::new($child);
|
2013-09-26 17:21:59 -07:00
|
|
|
assert_eq!(path.ends_with_path(&child), $exp);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $child:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
|
|
|
let child = Path::new($child);
|
2013-09-26 17:21:59 -07:00
|
|
|
assert_eq!(path.ends_with_path(&child), $exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "c", true);
|
|
|
|
t!(s: "a/b/c", "d", false);
|
|
|
|
t!(s: "foo/bar/quux", "bar", false);
|
|
|
|
t!(s: "foo/bar/quux", "barquux", false);
|
|
|
|
t!(s: "a/b/c", "b/c", true);
|
|
|
|
t!(s: "a/b/c", "a/b/c", true);
|
|
|
|
t!(s: "a/b/c", "foo/a/b/c", false);
|
|
|
|
t!(s: "/a/b/c", "a/b/c", true);
|
|
|
|
t!(s: "/a/b/c", "/a/b/c", false); // child must be relative
|
|
|
|
t!(s: "/a/b/c", "foo/a/b/c", false);
|
|
|
|
t!(s: "a/b/c", "", false);
|
|
|
|
t!(s: "", "", true);
|
|
|
|
t!(s: "/a/b/c", "d/e/f", false);
|
|
|
|
t!(s: "a/b/c", "a/b", false);
|
|
|
|
t!(s: "a/b/c", "b", false);
|
|
|
|
t!(v: b!("a/b/c"), b!("b/c"), true);
|
|
|
|
t!(v: b!("a/b/", 0xff), b!(0xff), true);
|
|
|
|
t!(v: b!("a/b/", 0xff), b!("b/", 0xff), true);
|
|
|
|
}
|
|
|
|
|
2013-09-01 12:44:07 -07:00
|
|
|
#[test]
|
|
|
|
fn test_path_relative_from() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $other:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
|
|
|
let other = Path::new($other);
|
2013-09-01 12:44:07 -07:00
|
|
|
let res = path.path_relative_from(&other);
|
2013-09-30 17:13:13 -07:00
|
|
|
assert_eq!(res.and_then_ref(|x| x.as_str()), $exp);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "a/b/c", "a/b", Some("c"));
|
|
|
|
t!(s: "a/b/c", "a/b/d", Some("../c"));
|
|
|
|
t!(s: "a/b/c", "a/b/c/d", Some(".."));
|
|
|
|
t!(s: "a/b/c", "a/b/c", Some("."));
|
|
|
|
t!(s: "a/b/c", "a/b/c/d/e", Some("../.."));
|
|
|
|
t!(s: "a/b/c", "a/d/e", Some("../../b/c"));
|
|
|
|
t!(s: "a/b/c", "d/e/f", Some("../../../a/b/c"));
|
|
|
|
t!(s: "a/b/c", "/a/b/c", None);
|
|
|
|
t!(s: "/a/b/c", "a/b/c", Some("/a/b/c"));
|
|
|
|
t!(s: "/a/b/c", "/a/b/c/d", Some(".."));
|
|
|
|
t!(s: "/a/b/c", "/a/b", Some("c"));
|
|
|
|
t!(s: "/a/b/c", "/a/b/c/d/e", Some("../.."));
|
|
|
|
t!(s: "/a/b/c", "/a/d/e", Some("../../b/c"));
|
|
|
|
t!(s: "/a/b/c", "/d/e/f", Some("../../../a/b/c"));
|
|
|
|
t!(s: "hi/there.txt", "hi/there", Some("../there.txt"));
|
|
|
|
t!(s: ".", "a", Some(".."));
|
|
|
|
t!(s: ".", "a/b", Some("../.."));
|
|
|
|
t!(s: ".", ".", Some("."));
|
|
|
|
t!(s: "a", ".", Some("a"));
|
|
|
|
t!(s: "a/b", ".", Some("a/b"));
|
|
|
|
t!(s: "..", ".", Some(".."));
|
|
|
|
t!(s: "a/b/c", "a/b/c", Some("."));
|
|
|
|
t!(s: "/a/b/c", "/a/b/c", Some("."));
|
|
|
|
t!(s: "/", "/", Some("."));
|
|
|
|
t!(s: "/", ".", Some("/"));
|
|
|
|
t!(s: "../../a", "b", Some("../../../a"));
|
|
|
|
t!(s: "a", "../../b", None);
|
|
|
|
t!(s: "../../a", "../../b", Some("../a"));
|
|
|
|
t!(s: "../../a", "../../a/b", Some(".."));
|
|
|
|
t!(s: "../../a/b", "../../a", Some("b"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_component_iter() {
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
2013-09-01 12:44:07 -07:00
|
|
|
let comps = path.component_iter().to_owned_vec();
|
|
|
|
let exp: &[&str] = $exp;
|
|
|
|
let exps = exp.iter().map(|x| x.as_bytes()).to_owned_vec();
|
2013-10-01 23:41:59 -07:00
|
|
|
assert!(comps == exps, "component_iter: Expected {:?}, found {:?}",
|
2013-09-26 17:21:59 -07:00
|
|
|
comps, exps);
|
|
|
|
let comps = path.rev_component_iter().to_owned_vec();
|
|
|
|
let exps = exps.move_rev_iter().to_owned_vec();
|
2013-10-01 23:41:59 -07:00
|
|
|
assert!(comps == exps, "rev_component_iter: Expected {:?}, found {:?}",
|
2013-09-26 17:21:59 -07:00
|
|
|
comps, exps);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: [$($arg:expr),+], [$([$($exp:expr),*]),*]) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new(b!($($arg),+));
|
2013-09-01 12:44:07 -07:00
|
|
|
let comps = path.component_iter().to_owned_vec();
|
|
|
|
let exp: &[&[u8]] = [$(b!($($exp),*)),*];
|
2013-10-01 23:41:59 -07:00
|
|
|
assert!(comps.as_slice() == exp, "component_iter: Expected {:?}, found {:?}",
|
2013-09-26 17:21:59 -07:00
|
|
|
comps.as_slice(), exp);
|
|
|
|
let comps = path.rev_component_iter().to_owned_vec();
|
|
|
|
let exp = exp.rev_iter().map(|&x|x).to_owned_vec();
|
2013-10-01 23:41:59 -07:00
|
|
|
assert!(comps.as_slice() == exp,
|
|
|
|
"rev_component_iter: Expected {:?}, found {:?}",
|
2013-09-26 17:21:59 -07:00
|
|
|
comps.as_slice(), exp);
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(v: ["a/b/c"], [["a"], ["b"], ["c"]]);
|
|
|
|
t!(v: ["/", 0xff, "/a/", 0x80], [[0xff], ["a"], [0x80]]);
|
|
|
|
t!(v: ["../../foo", 0xcd, "bar"], [[".."], [".."], ["foo", 0xcd, "bar"]]);
|
|
|
|
t!(s: "a/b/c", ["a", "b", "c"]);
|
|
|
|
t!(s: "a/b/d", ["a", "b", "d"]);
|
|
|
|
t!(s: "a/b/cd", ["a", "b", "cd"]);
|
|
|
|
t!(s: "/a/b/c", ["a", "b", "c"]);
|
|
|
|
t!(s: "a", ["a"]);
|
|
|
|
t!(s: "/a", ["a"]);
|
|
|
|
t!(s: "/", []);
|
|
|
|
t!(s: ".", ["."]);
|
|
|
|
t!(s: "..", [".."]);
|
|
|
|
t!(s: "../..", ["..", ".."]);
|
|
|
|
t!(s: "../../foo", ["..", "..", "foo"]);
|
|
|
|
}
|
2013-09-26 14:38:26 -07:00
|
|
|
|
2013-09-26 17:21:59 -07:00
|
|
|
#[test]
|
|
|
|
fn test_str_component_iter() {
|
|
|
|
macro_rules! t(
|
|
|
|
(v: [$($arg:expr),+], $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new(b!($($arg),+));
|
2013-09-26 17:21:59 -07:00
|
|
|
let comps = path.str_component_iter().to_owned_vec();
|
|
|
|
let exp: &[Option<&str>] = $exp;
|
2013-10-01 23:41:59 -07:00
|
|
|
assert!(comps.as_slice() == exp,
|
|
|
|
"str_component_iter: Expected {:?}, found {:?}",
|
2013-09-26 17:21:59 -07:00
|
|
|
comps.as_slice(), exp);
|
|
|
|
let comps = path.rev_str_component_iter().to_owned_vec();
|
|
|
|
let exp = exp.rev_iter().map(|&x|x).to_owned_vec();
|
|
|
|
assert!(comps.as_slice() == exp,
|
2013-10-01 23:41:59 -07:00
|
|
|
"rev_str_component_iter: Expected {:?}, found {:?}",
|
2013-09-26 17:21:59 -07:00
|
|
|
comps.as_slice(), exp);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(v: ["a/b/c"], [Some("a"), Some("b"), Some("c")]);
|
|
|
|
t!(v: ["/", 0xff, "/a/", 0x80], [None, Some("a"), None]);
|
|
|
|
t!(v: ["../../foo", 0xcd, "bar"], [Some(".."), Some(".."), None]);
|
|
|
|
// str_component_iter is a wrapper around component_iter, so no need to do
|
|
|
|
// the full set of tests
|
|
|
|
}
|
|
|
|
|
2013-09-26 14:38:26 -07:00
|
|
|
#[test]
|
|
|
|
fn test_each_parent() {
|
2013-10-05 19:49:32 -07:00
|
|
|
assert!(Path::new("/foo/bar").each_parent(|_| true));
|
|
|
|
assert!(!Path::new("/foo/bar").each_parent(|_| false));
|
2013-09-26 14:38:26 -07:00
|
|
|
|
|
|
|
macro_rules! t(
|
|
|
|
(s: $path:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
2013-09-26 14:38:26 -07:00
|
|
|
let exp: &[&str] = $exp;
|
|
|
|
let mut comps = exp.iter().map(|&x|x);
|
|
|
|
do path.each_parent |p| {
|
|
|
|
let p = p.as_str();
|
|
|
|
assert!(p.is_some());
|
|
|
|
let e = comps.next();
|
|
|
|
assert!(e.is_some());
|
|
|
|
assert_eq!(p.unwrap(), e.unwrap());
|
|
|
|
true
|
|
|
|
};
|
|
|
|
assert!(comps.next().is_none());
|
|
|
|
}
|
|
|
|
);
|
|
|
|
(v: $path:expr, $exp:expr) => (
|
|
|
|
{
|
2013-10-05 19:49:32 -07:00
|
|
|
let path = Path::new($path);
|
2013-09-26 14:38:26 -07:00
|
|
|
let exp: &[&[u8]] = $exp;
|
|
|
|
let mut comps = exp.iter().map(|&x|x);
|
|
|
|
do path.each_parent |p| {
|
|
|
|
let p = p.as_vec();
|
|
|
|
let e = comps.next();
|
|
|
|
assert!(e.is_some());
|
|
|
|
assert_eq!(p, e.unwrap());
|
|
|
|
true
|
|
|
|
};
|
|
|
|
assert!(comps.next().is_none());
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
t!(s: "/foo/bar", ["/foo/bar", "/foo", "/"]);
|
|
|
|
t!(s: "/foo/bar/baz", ["/foo/bar/baz", "/foo/bar", "/foo", "/"]);
|
|
|
|
t!(s: "/foo", ["/foo", "/"]);
|
|
|
|
t!(s: "/", ["/"]);
|
|
|
|
t!(s: "foo/bar/baz", ["foo/bar/baz", "foo/bar", "foo", "."]);
|
|
|
|
t!(s: "foo/bar", ["foo/bar", "foo", "."]);
|
|
|
|
t!(s: "foo", ["foo", "."]);
|
|
|
|
t!(s: ".", ["."]);
|
|
|
|
t!(s: "..", [".."]);
|
|
|
|
t!(s: "../../foo", ["../../foo", "../.."]);
|
|
|
|
|
|
|
|
t!(v: b!("foo/bar", 0x80), [b!("foo/bar", 0x80), b!("foo"), b!(".")]);
|
|
|
|
t!(v: b!(0xff, "/bar"), [b!(0xff, "/bar"), b!(0xff), b!(".")]);
|
|
|
|
}
|
2013-09-01 12:44:07 -07:00
|
|
|
}
|