Skip to content

Commit

Permalink
Split needs to handle the unix root as a special case
Browse files Browse the repository at this point in the history
  • Loading branch information
miner committed Jan 5, 2012
1 parent 145d3ca commit cc5f059
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/fs/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,20 @@
(.mkdirs (file path))
path)

(def unix-root "The root of a unix system is /, nil on Windows"
(when (= File/separator "/") File/separator))


(defn split
"Split path to componenets."
"Split path to components."
[path]
(seq (.split (str path) (str "\\Q" File/separator "\\E"))))
(let [pathstr (str path)
jregx (str "\\Q" File/separator "\\E")]
(cond (= pathstr unix-root) (list unix-root)
(and unix-root (.startsWith pathstr unix-root))
;; unix absolute path
(cons unix-root (seq (.split (subs pathstr 1) jregx)))
:else (seq (.split pathstr jregx)))))

(defn rename
"Rename old-path to new-path. Only works on files."
Expand Down
9 changes: 8 additions & 1 deletion test/fs/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@
(fact
(split (file "test/fs")) => (has-suffix ["test" "fs"]))

(when unix-root
(fact
(split (file "/tmp/foo/bar.txt")) => '("/" "tmp" "foo" "bar.txt")
(split (file "/")) => '("/")
(split "/") => '("/")
(split "") => '("")))

(fact
(let [f (temp-file)
new-f (str f "-new")]
Expand Down Expand Up @@ -271,4 +278,4 @@
(path-ns "foo/bar/baz_quux.clj") => 'foo.bar.baz-quux)

(fact
(str (ns-path 'foo.bar.baz-quux)) => (has-suffix "foo/bar/baz_quux.clj"))
(str (ns-path 'foo.bar.baz-quux)) => (has-suffix "foo/bar/baz_quux.clj"))

0 comments on commit cc5f059

Please sign in to comment.