2012-07-04 16:53:12 -05:00
|
|
|
//! Path data type and helper functions
|
2012-03-15 20:58:14 -05:00
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
export Path;
|
2012-03-15 20:58:14 -05:00
|
|
|
export consts;
|
|
|
|
export path_is_absolute;
|
|
|
|
export path_sep;
|
|
|
|
export dirname;
|
|
|
|
export basename;
|
|
|
|
export connect;
|
|
|
|
export connect_many;
|
|
|
|
export split;
|
|
|
|
export splitext;
|
|
|
|
export normalize;
|
|
|
|
|
2012-06-14 21:09:36 -05:00
|
|
|
// FIXME: This type should probably be constrained (#2624)
|
2012-07-04 16:53:12 -05:00
|
|
|
/// A path or fragment of a filesystem path
|
2012-08-14 15:38:35 -05:00
|
|
|
type Path = ~str;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-03-02 19:20:00 -06:00
|
|
|
mod consts {
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The primary path separator character for the platform
|
|
|
|
*
|
|
|
|
* On all platforms it is '/'
|
|
|
|
*/
|
2012-03-02 19:20:00 -06:00
|
|
|
const path_sep: char = '/';
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* The secondary path separator character for the platform
|
|
|
|
*
|
|
|
|
* On Unixes it is '/'. On Windows it is '\'.
|
|
|
|
*/
|
2012-03-02 19:20:00 -06:00
|
|
|
const alt_path_sep: char = '/';
|
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-03-02 19:20:00 -06:00
|
|
|
mod consts {
|
|
|
|
const path_sep: char = '/';
|
|
|
|
const alt_path_sep: char = '\\';
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Indicates whether a path is absolute.
|
|
|
|
*
|
|
|
|
* A path is considered absolute if it begins at the filesystem root ("/") or,
|
|
|
|
* on Windows, begins with a drive letter.
|
|
|
|
*/
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-08-14 15:38:35 -05:00
|
|
|
fn path_is_absolute(p: Path) -> bool {
|
2012-03-02 19:20:00 -06:00
|
|
|
str::char_at(p, 0u) == '/'
|
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-07-14 12:05:49 -05:00
|
|
|
fn path_is_absolute(p: ~str) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
return str::char_at(p, 0u) == '/' ||
|
2012-03-02 19:20:00 -06:00
|
|
|
str::char_at(p, 1u) == ':'
|
|
|
|
&& (str::char_at(p, 2u) == consts::path_sep
|
|
|
|
|| str::char_at(p, 2u) == consts::alt_path_sep);
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Get the default path separator for the host platform
|
2012-08-01 19:30:05 -05:00
|
|
|
fn path_sep() -> ~str { return str::from_char(consts::path_sep); }
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn split_dirname_basename (pp: Path) -> {dirname: ~str, basename: ~str} {
|
2012-08-06 14:34:08 -05:00
|
|
|
match str::rfind(pp, |ch|
|
2012-03-02 19:20:00 -06:00
|
|
|
ch == consts::path_sep || ch == consts::alt_path_sep
|
2012-06-30 18:19:07 -05:00
|
|
|
) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(i) => {
|
2012-08-03 21:59:04 -05:00
|
|
|
dirname: str::slice(pp, 0u, i),
|
|
|
|
basename: str::slice(pp, i + 1u, str::len(pp))
|
|
|
|
},
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {dirname: ~".", basename: pp}
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Get the directory portion of a path
|
|
|
|
*
|
|
|
|
* Returns all of the path up to, but excluding, the final path separator.
|
|
|
|
* The dirname of "/usr/share" will be "/usr", but the dirname of
|
|
|
|
* "/usr/share/" is "/usr/share".
|
|
|
|
*
|
|
|
|
* If the path is not prefixed with a directory, then "." is returned.
|
|
|
|
*/
|
2012-08-14 15:38:35 -05:00
|
|
|
fn dirname(pp: Path) -> Path {
|
2012-08-01 19:30:05 -05:00
|
|
|
return split_dirname_basename(pp).dirname;
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Get the file name portion of a path
|
|
|
|
*
|
|
|
|
* Returns the portion of the path after the final path separator.
|
|
|
|
* The basename of "/usr/share" will be "share". If there are no
|
|
|
|
* path separators in the path then the returned path is identical to
|
|
|
|
* the provided path. If an empty path is provided or the path ends
|
|
|
|
* with a path separator then an empty path is returned.
|
|
|
|
*/
|
2012-08-14 15:38:35 -05:00
|
|
|
fn basename(pp: Path) -> Path {
|
2012-08-01 19:30:05 -05:00
|
|
|
return split_dirname_basename(pp).basename;
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Connects to path segments
|
|
|
|
*
|
|
|
|
* Given paths `pre` and `post, removes any trailing path separator on `pre`
|
|
|
|
* and any leading path separator on `post`, and returns the concatenation of
|
|
|
|
* the two with a single path separator between them.
|
|
|
|
*/
|
2012-08-14 15:38:35 -05:00
|
|
|
fn connect(pre: Path, post: Path) -> Path {
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut pre_ = pre;
|
|
|
|
let mut post_ = post;
|
2012-03-02 19:20:00 -06:00
|
|
|
let sep = consts::path_sep as u8;
|
|
|
|
let pre_len = str::len(pre);
|
|
|
|
let post_len = str::len(post);
|
2012-06-24 22:18:18 -05:00
|
|
|
unsafe {
|
|
|
|
if pre_len > 1u && pre[pre_len-1u] == sep {
|
|
|
|
str::unsafe::pop_byte(pre_);
|
|
|
|
}
|
|
|
|
if post_len > 1u && post[0] == sep {
|
|
|
|
str::unsafe::shift_byte(post_);
|
|
|
|
}
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return pre_ + path_sep() + post_;
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Connects a vector of path segments into a single path.
|
|
|
|
*
|
|
|
|
* Inserts path separators as needed.
|
|
|
|
*/
|
2012-08-14 15:38:35 -05:00
|
|
|
fn connect_many(paths: ~[Path]) -> Path {
|
2012-08-01 19:30:05 -05:00
|
|
|
return if vec::len(paths) == 1u {
|
2012-03-02 19:20:00 -06:00
|
|
|
paths[0]
|
|
|
|
} else {
|
|
|
|
let rest = vec::slice(paths, 1u, vec::len(paths));
|
|
|
|
connect(paths[0], connect_many(rest))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Split a path into its individual components
|
|
|
|
*
|
|
|
|
* Splits a given path by path separators and returns a vector containing
|
|
|
|
* each piece of the path. On Windows, if the path is absolute then
|
|
|
|
* the first element of the returned vector will be the drive letter
|
|
|
|
* followed by a colon.
|
|
|
|
*/
|
2012-08-14 15:38:35 -05:00
|
|
|
fn split(p: Path) -> ~[Path] {
|
2012-06-30 18:19:07 -05:00
|
|
|
str::split_nonempty(p, |c| {
|
2012-03-02 19:20:00 -06:00
|
|
|
c == consts::path_sep || c == consts::alt_path_sep
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Split a path into the part before the extension and the extension
|
|
|
|
*
|
|
|
|
* Split a path into a pair of strings with the first element being the
|
|
|
|
* filename without the extension and the second being either empty or the
|
|
|
|
* file extension including the period. Leading periods in the basename are
|
|
|
|
* ignored. If the path includes directory components then they are included
|
|
|
|
* in the filename part of the result pair.
|
|
|
|
*/
|
2012-08-14 15:38:35 -05:00
|
|
|
fn splitext(p: Path) -> (~str, ~str) {
|
2012-07-14 00:57:48 -05:00
|
|
|
if str::is_empty(p) { (~"", ~"") }
|
2012-03-02 19:20:00 -06:00
|
|
|
else {
|
|
|
|
let parts = str::split_char(p, '.');
|
|
|
|
if vec::len(parts) > 1u {
|
2012-07-14 00:57:48 -05:00
|
|
|
let base = str::connect(vec::init(parts), ~".");
|
2012-03-08 14:08:47 -06:00
|
|
|
// We just checked that parts is non-empty, so this is safe
|
2012-07-14 00:57:48 -05:00
|
|
|
let ext = ~"." + vec::last(parts);
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn is_dotfile(base: ~str) -> bool {
|
2012-03-02 19:20:00 -06:00
|
|
|
str::is_empty(base)
|
|
|
|
|| str::ends_with(
|
|
|
|
base, str::from_char(consts::path_sep))
|
|
|
|
|| str::ends_with(
|
|
|
|
base, str::from_char(consts::alt_path_sep))
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn ext_contains_sep(ext: ~str) -> bool {
|
2012-03-02 19:20:00 -06:00
|
|
|
vec::len(split(ext)) > 1u
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn no_basename(ext: ~str) -> bool {
|
2012-03-02 19:20:00 -06:00
|
|
|
str::ends_with(
|
|
|
|
ext, str::from_char(consts::path_sep))
|
|
|
|
|| str::ends_with(
|
|
|
|
ext, str::from_char(consts::alt_path_sep))
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_dotfile(base)
|
|
|
|
|| ext_contains_sep(ext)
|
|
|
|
|| no_basename(ext) {
|
2012-07-14 00:57:48 -05:00
|
|
|
(p, ~"")
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
|
|
|
(base, ext)
|
|
|
|
}
|
|
|
|
} else {
|
2012-07-14 00:57:48 -05:00
|
|
|
(p, ~"")
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Collapses redundant path separators.
|
|
|
|
*
|
|
|
|
* Does not follow symbolic links.
|
|
|
|
*
|
|
|
|
* # Examples
|
|
|
|
*
|
|
|
|
* * '/a/../b' becomes '/b'
|
|
|
|
* * 'a/./b/' becomes 'a/b/'
|
|
|
|
* * 'a/b/../../../' becomes '..'
|
|
|
|
* * '/a/b/c/../d/./../../e/' becomes '/a/e/'
|
|
|
|
*/
|
2012-08-14 15:38:35 -05:00
|
|
|
fn normalize(p: Path) -> Path {
|
2012-03-02 19:20:00 -06:00
|
|
|
let s = split(p);
|
|
|
|
let s = strip_dots(s);
|
|
|
|
let s = rollup_doubledots(s);
|
|
|
|
|
2012-07-13 20:43:52 -05:00
|
|
|
let s = if vec::is_not_empty(s) {
|
2012-03-02 19:20:00 -06:00
|
|
|
connect_many(s)
|
|
|
|
} else {
|
2012-07-14 00:57:48 -05:00
|
|
|
~""
|
2012-03-02 19:20:00 -06:00
|
|
|
};
|
|
|
|
let s = reabsolute(p, s);
|
|
|
|
let s = reterminate(p, s);
|
|
|
|
|
|
|
|
let s = if str::len(s) == 0u {
|
2012-07-14 00:57:48 -05:00
|
|
|
~"."
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
|
|
|
s
|
|
|
|
};
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return s;
|
2012-03-02 19:20:00 -06:00
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn strip_dots(s: ~[Path]) -> ~[Path] {
|
2012-06-30 18:19:07 -05:00
|
|
|
vec::filter_map(s, |elem|
|
2012-07-14 00:57:48 -05:00
|
|
|
if elem == ~"." {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::None
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
option::Some(elem)
|
2012-06-30 18:19:07 -05:00
|
|
|
})
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn rollup_doubledots(s: ~[Path]) -> ~[Path] {
|
2012-03-02 19:20:00 -06:00
|
|
|
if vec::is_empty(s) {
|
2012-08-01 19:30:05 -05:00
|
|
|
return ~[];
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut t = ~[];
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut i = vec::len(s);
|
|
|
|
let mut skip = 0;
|
2012-05-08 19:33:48 -05:00
|
|
|
while i != 0u {
|
2012-03-02 19:20:00 -06:00
|
|
|
i -= 1u;
|
2012-07-14 00:57:48 -05:00
|
|
|
if s[i] == ~".." {
|
2012-03-02 19:20:00 -06:00
|
|
|
skip += 1;
|
|
|
|
} else {
|
|
|
|
if skip == 0 {
|
2012-06-25 22:00:46 -05:00
|
|
|
vec::push(t, s[i]);
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
|
|
|
skip -= 1;
|
|
|
|
}
|
|
|
|
}
|
2012-05-08 19:33:48 -05:00
|
|
|
}
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut t = vec::reversed(t);
|
2012-03-02 19:20:00 -06:00
|
|
|
while skip > 0 {
|
2012-07-14 00:57:48 -05:00
|
|
|
vec::push(t, ~"..");
|
2012-03-02 19:20:00 -06:00
|
|
|
skip -= 1;
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return t;
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(unix)]
|
2012-08-14 15:38:35 -05:00
|
|
|
fn reabsolute(orig: Path, n: Path) -> Path {
|
2012-03-02 19:20:00 -06:00
|
|
|
if path_is_absolute(orig) {
|
2012-03-14 14:16:46 -05:00
|
|
|
path_sep() + n
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
2012-03-14 14:16:46 -05:00
|
|
|
n
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-08-14 15:38:35 -05:00
|
|
|
fn reabsolute(orig: Path, newp: Path) -> Path {
|
2012-03-02 19:20:00 -06:00
|
|
|
if path_is_absolute(orig) && orig[0] == consts::path_sep as u8 {
|
2012-03-14 14:16:46 -05:00
|
|
|
str::from_char(consts::path_sep) + newp
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
2012-03-14 14:16:46 -05:00
|
|
|
newp
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 15:38:35 -05:00
|
|
|
fn reterminate(orig: Path, newp: Path) -> Path {
|
2012-03-02 19:20:00 -06:00
|
|
|
let last = orig[str::len(orig) - 1u];
|
|
|
|
if last == consts::path_sep as u8
|
|
|
|
|| last == consts::path_sep as u8 {
|
2012-08-01 19:30:05 -05:00
|
|
|
return newp + path_sep();
|
2012-03-02 19:20:00 -06:00
|
|
|
} else {
|
2012-08-01 19:30:05 -05:00
|
|
|
return newp;
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn test_connect() {
|
|
|
|
let slash = path_sep();
|
2012-07-14 00:57:48 -05:00
|
|
|
log(error, connect(~"a", ~"b"));
|
|
|
|
assert (connect(~"a", ~"b") == ~"a" + slash + ~"b");
|
|
|
|
assert (connect(~"a" + slash, ~"b") == ~"a" + slash + ~"b");
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn ps() -> ~str {
|
2012-03-02 19:20:00 -06:00
|
|
|
path_sep()
|
|
|
|
}
|
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
fn aps() -> ~str {
|
|
|
|
~"/"
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split1() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = split(~"a" + ps() + ~"b");
|
|
|
|
let expected = ~[~"a", ~"b"];
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split2() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = split(~"a" + aps() + ~"b");
|
|
|
|
let expected = ~[~"a", ~"b"];
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split3() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = split(ps() + ~"a" + ps() + ~"b");
|
|
|
|
let expected = ~[~"a", ~"b"];
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split4() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = split(~"a" + ps() + ~"b" + aps() + ~"c");
|
|
|
|
let expected = ~[~"a", ~"b", ~"c"];
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize1() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"a/b/..");
|
|
|
|
let expected = ~"a";
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize2() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"/a/b/..");
|
|
|
|
let expected = ~"/a";
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize3() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"a/../b");
|
|
|
|
let expected = ~"b";
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize4() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"/a/../b");
|
|
|
|
let expected = ~"/b";
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize5() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"a/.");
|
|
|
|
let expected = ~"a";
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize6() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"a/./b/");
|
|
|
|
let expected = ~"a/b/";
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize7() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"a/..");
|
|
|
|
let expected = ~".";
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize8() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"../../..");
|
|
|
|
let expected = ~"../../..";
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize9() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"a/b/../../..");
|
|
|
|
let expected = ~"..";
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize10() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"/a/b/c/../d/./../../e/");
|
|
|
|
let expected = ~"/a/e/";
|
2012-03-02 19:20:00 -06:00
|
|
|
log(error, actual);
|
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normalize11() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let actual = normalize(~"/a/..");
|
|
|
|
let expected = ~"/";
|
2012-03-02 19:20:00 -06:00
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-03-02 19:20:00 -06:00
|
|
|
fn normalize12() {
|
2012-07-14 13:05:10 -05:00
|
|
|
let actual = normalize(~"C:/whatever");
|
|
|
|
let expected = ~"C:/whatever";
|
2012-03-02 19:20:00 -06:00
|
|
|
log(error, actual);
|
|
|
|
assert actual == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-06-07 23:38:25 -05:00
|
|
|
#[cfg(windows)]
|
2012-03-02 19:20:00 -06:00
|
|
|
fn path_is_absolute_win32() {
|
2012-07-14 13:05:10 -05:00
|
|
|
assert path_is_absolute(~"C:/whatever");
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn splitext_empty() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let (base, ext) = splitext(~"");
|
|
|
|
assert base == ~"";
|
|
|
|
assert ext == ~"";
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn splitext_ext() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let (base, ext) = splitext(~"grum.exe");
|
|
|
|
assert base == ~"grum";
|
|
|
|
assert ext == ~".exe";
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn splitext_noext() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let (base, ext) = splitext(~"grum");
|
|
|
|
assert base == ~"grum";
|
|
|
|
assert ext == ~"";
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn splitext_dotfile() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let (base, ext) = splitext(~".grum");
|
|
|
|
assert base == ~".grum";
|
|
|
|
assert ext == ~"";
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn splitext_path_ext() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let (base, ext) = splitext(~"oh/grum.exe");
|
|
|
|
assert base == ~"oh/grum";
|
|
|
|
assert ext == ~".exe";
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn splitext_path_noext() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let (base, ext) = splitext(~"oh/grum");
|
|
|
|
assert base == ~"oh/grum";
|
|
|
|
assert ext == ~"";
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn splitext_dot_in_path() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let (base, ext) = splitext(~"oh.my/grum");
|
|
|
|
assert base == ~"oh.my/grum";
|
|
|
|
assert ext == ~"";
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn splitext_nobasename() {
|
2012-07-14 00:57:48 -05:00
|
|
|
let (base, ext) = splitext(~"oh.my/");
|
|
|
|
assert base == ~"oh.my/";
|
|
|
|
assert ext == ~"";
|
2012-03-02 19:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|