Skip to content

Commit

Permalink
Guard read-ns-form against non-existing files
Browse files Browse the repository at this point in the history
Fixes #142
  • Loading branch information
vemv committed Feb 8, 2022
1 parent 12ae90a commit dfa9248
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* [#142](https://github.com/clojure-emacs/refactor-nrepl/issues/142): Guard `read-ns-form` against non-existing files.

## 3.3.1

* [#363](https://github.com/clojure-emacs/refactor-nrepl/issues/363): Fix a memoization bug in `clean-namespace`.
Expand Down
34 changes: 19 additions & 15 deletions src/refactor_nrepl/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,29 @@
(let [^String path-string (when (string? path)
path)
^File path-file (when-not path-string
path)]
(with-open [file-reader (or (some-> path-string FileReader.)
(some-> path-file FileReader.))]
(try
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader)))
(catch Exception _ nil)))))
path)
^File file (or path-file (File. path-string))]
;; Check for file existence, because clj-refactor.el or other clients might have bugs:
(when (some-> file .exists)
(with-open [file-reader (FileReader. file)]
(try
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader)))
(catch Exception _ nil))))))
([dialect path]
(let [^String path-string (when (string? path)
path)
^File path-file (when-not path-string
path)]
(with-open [file-reader (or (some-> path-string FileReader.)
(some-> path-file FileReader.))]
(try
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader))
{:read-cond :allow :features #{dialect}})
(catch Exception _ nil))))))
path)
^File file (or path-file (File. path-string))]
;; Check for file existence, because clj-refactor.el or other clients might have bugs:
(when (some-> file .exists)
(with-open [file-reader (FileReader. file)]
(try
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader))
{:read-cond :allow :features #{dialect}})
(catch Exception _ nil)))))))

(defn cljc-extension? [^String path]
(.endsWith path ".cljc"))
Expand Down
5 changes: 4 additions & 1 deletion test/refactor_nrepl/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@

(deftest test-read-ns-form
(are [input expected] (testing input
(assert (-> input File. .exists))
(case input
"alkjafas/does_not_exist.clj" (assert (not (-> input File. .exists)))
(assert (-> input File. .exists)))
(is (= expected
(sut/read-ns-form input)))
true)
"alkjafas/does_not_exist.clj" nil
"test-resources/readable_file_incorrect_aliases.clj" nil
"testproject/src/com/example/one.clj" '(ns com.example.one
(:require [com.example.two :as two :refer [foo]]
Expand Down

0 comments on commit dfa9248

Please sign in to comment.