Skip to content

Commit

Permalink
Make classpath-seq handle non-existing files
Browse files Browse the repository at this point in the history
Fixes #125
  • Loading branch information
vemv committed Sep 23, 2021
1 parent 7d7f752 commit 24d59c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/orchard/java/classpath.clj
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,16 @@
as relative paths."
[^URL url]
(let [f (io/as-file url)]
(if (misc/archive? url)
(cond
(not (.exists f))
[]

(misc/archive? url)
(->> (enumeration-seq (.entries (JarFile. f)))
(filter #(not (.isDirectory ^JarEntry %)))
(map #(.getName ^JarEntry %)))

:else
(->> (file-seq f)
(filter #(not (.isDirectory ^File %)))
(map #(.getPath (.relativize (.toURI url) (.toURI ^File %))))))))
Expand Down
29 changes: 26 additions & 3 deletions test/orchard/java/classpath_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,32 @@
(deftest classpath-resources-test
(testing "Iterating classpath resources"
(testing "returns non-empty lists"
(doseq [entry (map cp/classpath-seq (cp/classpath))]
(is (seq entry)
(pr-str entry))))
(let [dev-resources-path (-> "dev-resources" File. .getAbsolutePath)
the-classpath (cp/classpath)
corpus (->> the-classpath
(filter (fn [^URL u]
(let [f (-> u io/as-file)]
;; filter out intentionally non-existing files
;; (which we put in the :test classpath for reproducing certain bug)
(and (-> f .exists)
(not (= (.getAbsolutePath f)
;; remove dev-resources, only present in the :dev profile:
dev-resources-path)))))))
^File non-existing-jar (->> the-classpath
(filter (fn [u]
;; Find the non-existing jar declared under the :test profile:
(-> u io/as-file str (.includes "does-not-exist.jar"))))
first)]
(assert (seq corpus)
"There's something to test")
(assert non-existing-jar
"The classpath includes he non-existing jar")
(is (not (.exists non-existing-jar))
"Orchard will succeed even in presence of an entry in the classpath that refers to a non-existing.jar")
(doseq [item corpus
:let [entry (cp/classpath-seq item)]]
(is (seq entry)
(pr-str [item entry])))))
(testing "returns relative paths"
(doseq [^String entry (mapcat cp/classpath-seq (cp/classpath))]
(is (not (-> entry File. .isAbsolute))))))))
Expand Down

0 comments on commit 24d59c5

Please sign in to comment.