Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion path.carp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
19 changes: 19 additions & 0 deletions test/path.carp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading