Skip to content

Commit

Permalink
read-ns-form: rethrow FileNotFoundExceptions more informatively
Browse files Browse the repository at this point in the history
Fixes #142
  • Loading branch information
vemv committed Feb 10, 2022
1 parent b03efa4 commit 6c67655
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [#173](https://github.com/clojure-emacs/refactor-nrepl/issues/173): `rename-file-or-dir`: rename more kinds of constructs in dependent namespaces: namespace-qualified maps, fully-qualified functions, metadata.
* [#194](https://github.com/clojure-emacs/refactor-nrepl/issues/194): Don't prune `require` forms if they are needed for a given `import` to work.
* [#142](https://github.com/clojure-emacs/refactor-nrepl/issues/142): `read-ns-form`: report more informatively when a non-existing file is being processed.

## 3.3.1

Expand Down
39 changes: 20 additions & 19 deletions src/refactor_nrepl/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[refactor-nrepl.s-expressions :as sexp]
[refactor-nrepl.util :as util :refer [normalize-to-unix-path]])
(:import
(java.io File FileReader PushbackReader StringReader)))
(java.io File FileNotFoundException FileReader PushbackReader StringReader)))

;; Require our `fs` customizations before `fs` is loaded:
(require '[refactor-nrepl.fs])
Expand Down Expand Up @@ -152,28 +152,29 @@

(defn read-ns-form
([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)))
(catch Exception _ nil)))))
(read-ns-form nil path))
([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))]
(try
(with-open [file-reader (FileReader. file)]
(try
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader))
(if dialect
{:read-cond :allow :features #{dialect}}
nil))
(catch Exception _ nil)))
(catch FileNotFoundException e
(throw (ex-info (format "No such file: %s. This typically indicates an invalid request client-side."
(pr-str path))
{:path path
:dialect dialect
:file (str file)}
e)))))))

(defn cljc-extension? [^String path]
(.endsWith path ".cljc"))
Expand Down
23 changes: 13 additions & 10 deletions test/refactor_nrepl/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@
(assert-ignored-paths not-ignored false?)
(assert-ignored-paths (concat always-ignored sometimes-ignored) true?)))))

(deftest test-read-ns-form
(are [input expected] (testing input
(assert (-> input File. .exists))
(is (= expected
(sut/read-ns-form input)))
true)
"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]]
[com.example.four :as four]))))
(deftest read-ns-form-test
(let [valid-filename "testproject/src/com/example/one.clj"]
(is (= (sut/read-ns-form valid-filename)
(sut/read-ns-form :clj valid-filename)))
(are [input expected] (testing input
(assert (-> input File. .exists))
(is (= expected
(sut/read-ns-form input)))
true)
"test-resources/readable_file_incorrect_aliases.clj" nil
valid-filename '(ns com.example.one
(:require [com.example.two :as two :refer [foo]]
[com.example.four :as four])))))

(deftest source-files-with-clj-like-extension-test
(let [result (sut/source-files-with-clj-like-extension true)]
Expand Down

0 comments on commit 6c67655

Please sign in to comment.