Skip to content

Make classpath-seq handle non-existing files #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bugs Fixed

* [#123](https://github.com/clojure-emacs/orchard/pull/123): Fix info lookups from namespaces that don't yet exist
* [#125](https://github.com/clojure-emacs/orchard/pull/125): Don't fail if the classpath references a non-existing .jar

## 0.7.1 (2021-04-18)

Expand Down
9 changes: 5 additions & 4 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@

:test {:dependencies [[org.clojure/java.classpath "1.0.0"]]
:resource-paths ["test-resources"
"not-a.jar"]
"not-a.jar"
"does-not-exist.jar"]
;; Initialize the cache verbosely, as usual, so that possible issues can be more easily diagnosed:
:jvm-opts ["-Dorchard.initialize-cache.silent=false"]}

Expand All @@ -121,16 +122,16 @@
(pjstadig.humane-test-output/activate!)]
:test-refresh {:changes-only true}}

:cljfmt {:plugins [[lein-cljfmt "0.6.4"]]
:cljfmt {:plugins [[lein-cljfmt "0.8.0"]]
:cljfmt {:indents {as-> [[:inner 0]]
with-debug-bindings [[:inner 0]]
merge-meta [[:inner 0]]
letfn [[:block 1] [:inner 2]]}}}

:clj-kondo [:test
{:dependencies [[clj-kondo "2021.03.31"]]}]
{:dependencies [[clj-kondo "2021.09.15"]]}]

:eastwood {:plugins [[jonase/eastwood "0.9.6"]]
:eastwood {:plugins [[jonase/eastwood "0.9.9"]]
:eastwood {:exclude-namespaces [~(if jdk8?
'orchard.java.parser
'orchard.java.legacy-parser)]}}})
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
1 change: 0 additions & 1 deletion src/orchard/spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
;; clojure version ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defn get-spec [v] (spec "get-spec" v))

(defn describe [s] (spec "describe" s))
Expand Down
34 changes: 31 additions & 3 deletions test/orchard/java/classpath_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,37 @@
(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))))
;; The non-existing .jar can get misteriously created (is it Lein?).
;; Work around it:
(-> "does-not-exist.jar" File. .delete)

(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 (.contains "does-not-exist.jar"))))
first)]
(assert (seq corpus)
"There's something to test")
(assert non-existing-jar
"The classpath includes the non-existing jar")
(testing "Orchard will succeed even in presence of an entry in the classpath that refers to a non-existing.jar"
(is (not (-> non-existing-jar io/as-file .exists))
(pr-str 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