diff --git a/path.carp b/path.carp index cc975aa..262c735 100644 --- a/path.carp +++ b/path.carp @@ -195,7 +195,10 @@ an extension if there previously was none.") (defn absolute [p] (if (absolute? p) (Maybe.Just @p) - (Maybe.apply (cwd) &(fn [d] (join &[d @p]))))) + (Maybe.apply (cwd) + &(fn [d] + (let [c (String.char-at &d (dec (String.length &d)))] + (if (separator? &c) (String.append &d p) (join &[d @p]))))))) (doc relative "computes the path to `target` relative to `base`, purely lexically. It is the companion to [absolute](#absolute). diff --git a/test/path.carp b/test/path.carp index 95f2e20..1c8fb48 100644 --- a/test/path.carp +++ b/test/path.carp @@ -3,10 +3,29 @@ (use-all Path Test) +(register chdir (Fn [(Ptr CChar)] Int)) + +(defn absolute-from-root [p] + (let-do [here (Maybe.from (cwd) @".")] + (ignore (chdir (cstr "/"))) + (let-do [res (absolute p)] (ignore (chdir (cstr &here))) res))) + (posix-only (deftest test (assert-equal test "path/joined" &( "path" "joined") " works on paths") (assert-true test (Maybe.just? &(absolute "path")) "absolute works") + (assert-equal test + &(Maybe.apply (cwd) &(fn [d] (fmt "%s/foo" &d))) + &(absolute "foo") + "absolute prefixes the working directory") + (assert-equal test + &(Maybe.Just @"/foo") + &(absolute-from-root "foo") + "absolute does not double the separator at the root") + (assert-equal test + &(Maybe.Just @"/usr") + &(absolute-from-root "/usr") + "absolute keeps an absolute path at the root") (assert-false test (absolute? "path") "absolute? works on relative paths") (assert-false test (relative? "/path") "relative? works on absolute paths") (assert-true test (relative? "path") "relative? works on relative paths")