diff --git a/src/libcore/path.rs b/src/libcore/path.rs index 235dd686a1d..9afa3d830a3 100644 --- a/src/libcore/path.rs +++ b/src/libcore/path.rs @@ -32,6 +32,8 @@ trait GenericPath { pure fn push_rel((&self)) -> self; pure fn push_many((&[~str])) -> self; pure fn pop() -> self; + + pure fn normalize() -> self; } #[cfg(windows)] @@ -68,7 +70,7 @@ impl PosixPath : GenericPath { let mut components = str::split_nonempty(s, |c| c == '/'); let is_absolute = (s.len() != 0 && s[0] == '/' as u8); return PosixPath { is_absolute: is_absolute, - components: normalize(components) } + components: components } } pure fn dirname() -> ~str { @@ -175,14 +177,13 @@ impl PosixPath : GenericPath { } pure fn push_many(cs: &[~str]) -> PosixPath { - return PosixPath { components: normalize(self.components + cs), + return PosixPath { components: self.components + cs, ..self } } pure fn push(s: &str) -> PosixPath { let mut cs = self.components; unchecked { vec::push(cs, move str::from_slice(s)); } - cs = normalize(cs); return PosixPath { components: move cs, ..self } } @@ -194,6 +195,13 @@ impl PosixPath : GenericPath { } return PosixPath { components: move cs, ..self } } + + pure fn normalize() -> PosixPath { + return PosixPath { + components: normalize(self.components), + ..self + } + } } @@ -251,7 +259,7 @@ impl WindowsPath : GenericPath { return WindowsPath { host: host, device: device, is_absolute: is_absolute, - components: normalize(components) } + components: components } } pure fn dirname() -> ~str { @@ -358,14 +366,13 @@ impl WindowsPath : GenericPath { } pure fn push_many(cs: &[~str]) -> WindowsPath { - return WindowsPath { components: normalize(self.components + cs), + return WindowsPath { components: self.components + cs, ..self } } pure fn push(s: &str) -> WindowsPath { let mut cs = self.components; unchecked { vec::push(cs, move str::from_slice(s)); } - cs = normalize(cs); return WindowsPath { components: move cs, ..self } } @@ -377,6 +384,13 @@ impl WindowsPath : GenericPath { } return WindowsPath { components: move cs, ..self } } + + pure fn normalize() -> WindowsPath { + return WindowsPath { + components: normalize(self.components), + ..self + } + } } @@ -399,19 +413,22 @@ pure fn normalize(components: &[~str]) -> ~[~str] { mod posix { + #[cfg(test)] + fn mk(s: &str) -> PosixPath { from_str::(s) } + + #[cfg(test)] + fn t(wp: &PosixPath, s: &str) { + let ss = wp.to_str(); + let sss = str::from_slice(s); + if (ss != sss) { + debug!("got %s", ss); + debug!("expected %s", sss); + assert ss == sss; + } + } + #[test] fn test_posix_paths() { - fn mk(s: &str) -> PosixPath { from_str::(s) } - fn t(wp: &PosixPath, s: &str) { - let ss = wp.to_str(); - let sss = str::from_slice(s); - if (ss != sss) { - debug!("got %s", ss); - debug!("expected %s", sss); - assert ss == sss; - } - } - t(&(mk("hi")), "hi"); t(&(mk("/lib")), "/lib"); t(&(mk("hi/there")), "hi/there"); @@ -425,13 +442,10 @@ mod posix { .with_dirname("hi")), "hi/there.txt"); t(&(mk("hi/there.txt") - .with_dirname(".")), "there.txt"); - - t(&(mk("a/b/../c/././/../foo.txt/")), - "a/foo.txt"); + .with_dirname(".")), "./there.txt"); t(&(mk("a/b/c") - .push("..")), "a/b"); + .push("..")), "a/b/c/.."); t(&(mk("there.txt") .with_filetype("o")), "there.o"); @@ -461,6 +475,17 @@ mod posix { } + #[test] + fn test_normalize() { + t(&(mk("hi/there.txt") + .with_dirname(".").normalize()), "there.txt"); + + t(&(mk("a/b/../c/././/../foo.txt/").normalize()), + "a/foo.txt"); + + t(&(mk("a/b/c") + .push("..").normalize()), "a/b"); + } } // Various windows helpers, and tests for the impl. diff --git a/src/rustc/back/rpath.rs b/src/rustc/back/rpath.rs index 0d02821a24c..fa02b617606 100644 --- a/src/rustc/back/rpath.rs +++ b/src/rustc/back/rpath.rs @@ -117,6 +117,8 @@ fn get_rpath_relative_to_output(os: session::os, fn get_relative_to(abs1: &Path, abs2: &Path) -> Path { assert abs1.is_absolute; assert abs2.is_absolute; + let abs1 = abs1.normalize(); + let abs2 = abs2.normalize(); debug!("finding relative path from %s to %s", abs1.to_str(), abs2.to_str()); let split1 = abs1.components; @@ -295,7 +297,7 @@ mod test { let o = session::os_linux; let res = get_rpath_relative_to_output(o, &Path("bin/rustc"), &Path("lib/libstd.so")); - assert res == Path("$ORIGIN/../lib"); + assert res.to_str() == ~"$ORIGIN/../lib"; } #[test] @@ -304,7 +306,7 @@ mod test { let o = session::os_freebsd; let res = get_rpath_relative_to_output(o, &Path("bin/rustc"), &Path("lib/libstd.so")); - assert res == Path("$ORIGIN/../lib"); + assert res.to_str() == ~"$ORIGIN/../lib"; } #[test] @@ -315,7 +317,7 @@ mod test { let res = get_rpath_relative_to_output(o, &Path("bin/rustc"), &Path("lib/libstd.so")); - assert res == Path("@executable_path/../lib"); + assert res.to_str() == ~"@executable_path/../lib"; } #[test]