Skip to content

Commit

Permalink
Ignore non file URLs when checking for directory or file extensions
Browse files Browse the repository at this point in the history
In a URLClassLoader not all URLs have to be files or directories.
Eg. Spring boot's fat jars add entries like "jar:file:..." to the
classpath
  • Loading branch information
fp7 authored and bbatsov committed Feb 23, 2020
1 parent f960bee commit 9fe7a70
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bugs fixed

* [#83](https://github.com/clojure-emacs/orchard/pull/83): Ignore non file URLs when checking for directory or file extensions.

## 0.5.6 (2020-02-14)

### Bugs fixed
Expand Down
17 changes: 14 additions & 3 deletions src/orchard/misc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,27 @@
[]
(not (nil? (boot-fake-classpath))))

(defn url?
"Check whether the argument is an url"
[u]
(instance? java.net.URL u))

(defn directory?
"Whether the argument is a directory"
"Whether the argument is a directory or an url that points to a directory"
[f]
(.isDirectory (io/as-file f)))
(if (url? f)
(and (= (.getProtocol ^java.net.URL f) "file")
(.isDirectory (io/as-file f)))
(.isDirectory (io/as-file f))))

(defn file-ext?
"Whether the argument's path ends in one of the specified case-insensitive
file extensions"
[f & exts]
(let [file (io/as-file f)]
(when-let [file (if (url? f)
(when (= (.getProtocol ^java.net.URL f) "file")
(io/as-file f))
(io/as-file f))]
(some (fn [ext]
(.endsWith (.. file getName toLowerCase) ext))
exts)))
Expand Down
8 changes: 8 additions & 0 deletions test/orchard/misc_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@
(is (nil? (misc/namespace-sym nil)))
(is (= 'unqualified (misc/namespace-sym 'unqualified)))
(is (= 'qualified (misc/namespace-sym 'qualified/sym))))

(deftest file-ext?
(is (misc/file-ext? (java.net.URL. "file:/tmp/foo.jar") ".jar"))
(is (not (misc/file-ext? (java.net.URL. "file:/tmp/foo.war") ".jar")))
(is (not (misc/file-ext? (java.net.URL. "jar:file:/tmp/foo.jar!/BOOT-INF/lib/test.jar") ".jar"))))

(deftest directory?
(is (misc/directory? (.toURL (.toURI (java.io.File. ""))))))

0 comments on commit 9fe7a70

Please sign in to comment.