fixed some test & added Filesystem

This commit is contained in:
lukas 2020-01-17 13:45:42 +01:00
parent 151be9f92f
commit be796c16bf
4 changed files with 49 additions and 44 deletions

View file

@ -75,35 +75,44 @@
[resource :- Resource] [resource :- Resource]
(= :file (:resource-type resource))) (= :file (:resource-type resource)))
(s/defn create-FileSystem
[Resourece-Uri :- Uri]
(let [path-to-filesystem (st/join (pop (st/split (.toString Resourece-Uri) #"!")))]
(try
(FileSystems/newFileSystem
(java.net.URI. path-to-filesystem)
{})
(catch Exception e
nil))))
; contains either a jar or a file ; contains either a jar or a file
(s/defn path-from-cp ; :- JavaPath (s/defn path-from-cp ; :- JavaPath
[resource-path :- ShortPath] [resource-path :- ShortPath]
(try (try
(let [path-from-cp (Paths/get (.toURI (io/resource resource-path)))] ; check if contains jar: (let [Resource-Uri (.toURI (io/resource resource-path))] ; check if contains jar:
(when (Files/exists path-from-cp NoLinkOption) (when (st/starts-with? (.toString Resource-Uri) "jar:")
path-from-cp)) (create-FileSystem Resource-Uri))
(when (Files/exists (Paths/get Resource-Uri) NoLinkOption)
(Paths/get Resource-Uri)))
(catch Exception e (catch Exception e
nil))) nil)))
(s/defn path-from-fs ; :- JavaPath (s/defn path-from-fs ; :- JavaPath
[fs-prefix :- Prefix [full-path :- ShortPath]
resource-path :- ShortPath] (let [path-from-fs (Paths/get (java.net.URI. (str "file://" full-path)))] ;fragile
(let [path-from-fs (Paths/get (java.net.URI. (str "file://" fs-prefix resource-path)))] ;fragile
(try (try
(when (Files/exists path-from-fs NoLinkOption) (when (Files/exists path-from-fs NoLinkOption)
path-from-fs) path-from-fs)
(catch Exception e (catch Exception e
nil)))) nil))))
; TODO move to path-from-cp (s/defn construct-full-path
(s/defn path-from-jar ; :- JavaPath [fs-prefix
[resource-path :- ShortPath] base-path
(let [uri (.toUri (io/resource "dummy"))] resource-path]
(try (if (st/starts-with? fs-prefix "./")
(when (Files/exists path-from-fs NoLinkOption) (str (st/replace fs-prefix #"\./" (str (java.lang.System/getProperty "user.dir") "/")) "/" base-path "/" resource-path)
path-from-fs) (str fs-prefix "/" base-path "/" resource-path)))
(catch Exception e
nil))))
(defn resource-from-cp-or-fs ; :- Resource (defn resource-from-cp-or-fs ; :- Resource
[fs-prefix ; :- Prefix [fs-prefix ; :- Prefix
@ -112,14 +121,15 @@
& {:keys [from-cp from-fs] & {:keys [from-cp from-fs]
:or {from-cp true :or {from-cp true
from-fs true}}] from-fs true}}]
(let [full-path (if (empty? base-path) (let [full-path (construct-full-path fs-prefix base-path resource-path)
cp-path (if (empty? base-path)
resource-path resource-path
(str base-path "/" resource-path)) (str base-path "/" resource-path))
path-from-fs (if from-fs path-from-fs (if from-fs
(path-from-fs fs-prefix full-path) (path-from-fs full-path)
nil) nil)
path-from-cp (if from-cp path-from-cp (if from-cp
(path-from-cp full-path) (path-from-cp cp-path)
nil)] nil)]
(cond (cond
(some? path-from-fs) (some? path-from-fs)
@ -204,7 +214,7 @@
[fs-prefix ;:- Prefix [fs-prefix ;:- Prefix
base-path ;:- ShortPath base-path ;:- ShortPath
source-paths ;:- [ShortPath] source-paths ;:- [ShortPath]
target-path ;:- ShortPath #TODO is actually no path ??? target-path ;:- ShortPath
ignore-patterns ;:- s/Str ignore-patterns ;:- s/Str
] ]
(let [resource-paths (let [resource-paths

View file

@ -50,12 +50,3 @@
(doseq [resource resources] (doseq [resource resources]
(io/make-parents (io/file (str target-path "/" (:path resource)))) (io/make-parents (io/file (str target-path "/" (:path resource))))
(.mkdir (io/file (str target-path "/" (:path resource))))))) (.mkdir (io/file (str target-path "/" (:path resource)))))))
; TODO lookup if we need to close the stream
(defn extract-file-from-jar
[Uri
file-path]
(let [zippath (.getPath
(FileSystems/getFileSystem (java.net.URI. Uri)) file-path
(into-array String '()))]
(apply str (map char (Files/readAllBytes zippath)))))

View file

@ -11,7 +11,9 @@
[clojure.java.io :as io] [clojure.java.io :as io]
[schema.core :as s] [schema.core :as s]
[cryogen-core.file-test-tools :as ftt] [cryogen-core.file-test-tools :as ftt]
[cryogen-core.classpath-able-io :as sut])) [cryogen-core.classpath-able-io :as sut])
(:import [java.nio.file FileSystems Paths Files LinkOption StandardCopyOption]
[java.nio.file.attribute FileAttribute]))
(s/set-fn-validation! true) (s/set-fn-validation! true)
@ -19,6 +21,8 @@
(def target "target/tmp") (def target "target/tmp")
(def NoLinkOption (into-array [LinkOption/NOFOLLOW_LINKS]))
; TODO: Fix this test! ; TODO: Fix this test!
(deftest test-file-from-cp (deftest test-file-from-cp
(is (is
@ -29,27 +33,27 @@
(deftest test-resource-from-cp-or-fs (deftest test-resource-from-cp-or-fs
(is (is
(.exists (Files/exists
(:file (:java-path
(sut/resource-from-cp-or-fs (sut/resource-from-cp-or-fs
"./test-resources/" "./test-resources"
"templates/themes/bootstrap4-test" "templates/themes/bootstrap4-test"
"js")))) "js")) NoLinkOption))
(is (is
(.exists (Files/exists
(:file (:java-path
(sut/resource-from-cp-or-fs (sut/resource-from-cp-or-fs
"./" "" ".gitkeep")))) "./" "" ".gitkeep")) NoLinkOption))
(is (is
(some? (sut/resource-from-cp-or-fs (some? (sut/resource-from-cp-or-fs
"./test-resources/" "./test-resources"
"templates/themes/bootstrap4-test" "templates/themes/bootstrap4-test"
"js"))) "js")))
(is (is
(some? (sut/resource-from-cp-or-fs (some? (sut/resource-from-cp-or-fs
"./not-existing-so-load-from-cp" "" ".gitkeep"))) "./not-existing-so-load-from-cp" "" ".gitkeep")))
(is (= (is (=
{:path "js/subdir" {:short-path "js/subdir"
:source-type :classpath :source-type :classpath
:resource-type :dir} :resource-type :dir}
(ftt/filter-object (ftt/filter-object
@ -63,7 +67,7 @@
[] []
(sut/get-resources-recursive "" "templates/themes/bootstrap4-test" ["not-existing"]))) (sut/get-resources-recursive "" "templates/themes/bootstrap4-test" ["not-existing"])))
(is (= (is (=
[{:path "js/dummy.js" [{:short-path "js/dummy.js"
:source-type :classpath :source-type :classpath
:resource-type :file}] :resource-type :file}]
(map ftt/filter-object (map ftt/filter-object
@ -77,7 +81,7 @@
["js/subdir" ["js/subdir"
"js/subdir/subdummy.js" "js/subdir/subdummy.js"
"js/subdir/test.js"] "js/subdir/test.js"]
(sort (map :path (sort (map :short-path
(sut/get-resources-recursive (sut/get-resources-recursive
"" "templates/themes/bootstrap4-test" ["js/subdir"]))))) "" "templates/themes/bootstrap4-test" ["js/subdir"])))))
(is (= (is (=
@ -92,7 +96,7 @@
"./js/subdir" "./js/subdir"
"./js/subdir/subdummy.js" "./js/subdir/subdummy.js"
"./js/subdir/test.js"] "./js/subdir/test.js"]
(sort (map :path (sort (map :short-path
(sut/get-resources-recursive (sut/get-resources-recursive
"" "templates/themes/bootstrap4-test" ["."])))))) "" "templates/themes/bootstrap4-test" ["."]))))))

View file

@ -19,6 +19,6 @@
(defn filter-object (defn filter-object
[e] [e]
{:path (:path e) {:short-path (:short-path e)
:source-type (:source-type e) :source-type (:source-type e)
:resource-type (:resource-type e)}) :resource-type (:resource-type e)})