diff --git a/src/cryogen_core/classpath_able_io/fs.clj b/src/cryogen_core/classpath_able_io/fs.clj index 4fe8656..6c84881 100644 --- a/src/cryogen_core/classpath_able_io/fs.clj +++ b/src/cryogen_core/classpath_able_io/fs.clj @@ -17,19 +17,25 @@ ; ----------------------- Domain functions ------------------------ (def no-link-option (into-array [LinkOption/NOFOLLOW_LINKS])) -(def follow-link-option (into-array [])) +(def follow-link-option (into-array LinkOption [])) (defn user-dir [] (java.lang.System/getProperty "user.dir")) -(s/defn path +(defn absolut-path + [& path-elements] + (let [path (.normalize + (Paths/get (first path-elements) + (into-array String (rest path-elements))))] + (if (.isAbsolute path) + path + (Paths/get (user-dir) (into-array String path-elements))))) + +(defn path-if-exists [full-path] (let [path-from-fs (Paths/get (URI. (str "file://" (user-dir) "/" full-path)))] - (try - (when (Files/exists path-from-fs follow-link-option) - path-from-fs) - (catch Exception e - nil)))) + (when (Files/exists path-from-fs follow-link-option) + path-from-fs))) (defn create-resource ([virtual-path diff --git a/test/cryogen_core/classpath_able_io/fs_test.clj b/test/cryogen_core/classpath_able_io/fs_test.clj index 7ae1c2b..8cbe313 100644 --- a/test/cryogen_core/classpath_able_io/fs_test.clj +++ b/test/cryogen_core/classpath_able_io/fs_test.clj @@ -15,9 +15,17 @@ (def fs-root "fs_root") -(deftest test-path +(deftest test-absolut-path (is - (sut/path (str fs-root "/dummy/dummy_from_fs"))) + (= (str (sut/user-dir) "/" fs-root "/dummy/dummy_from_fs") + (.toString (sut/absolut-path fs-root "/dummy/dummy_from_fs")))) + (is + (= (str (sut/user-dir) "/" fs-root "/not-existing") + (.toString (sut/absolut-path (str fs-root "/not-existing")))))) + +(deftest test-path-if-exists + (is + (sut/path-if-exists (str fs-root "/dummy/dummy_from_fs"))) (is (= nil - (sut/path (str fs-root "/not-existing"))))) + (sut/path-if-exists (str fs-root "/not-existing")))))