Skip to content
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

Ignore non file URLs when checking for directory or file extensions #83

Merged
merged 1 commit into from
Feb 23, 2020
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
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can pull this and higher.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean like this ?

(and (url? f)
        (= (.getProtocol ^java.net.URL f) "file")
        (.isDirectory (io/as-file f)))

Then it would not check nonURLs correctly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of something like:

(and (url? f) (= (.getProtocol ^java.net.URL f) "file")) ...

With a more complex conditional you can even get rid of the duplicated .isDirectory check, but this will probably make the code hard to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but i still do not understand.
You mean like this?

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I just realized where your confusion was coming from. If we structure the condition in this manner, the else branch won't be correct. I should stop reviewing code while doing something else. :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem

(.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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can combine this condition with the previous using and (for instance). Or you can just replace the condition with one or/and.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still something to do here?

(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. ""))))))