Skip to content

Commit 40d1431

Browse files
vemvbbatsov
authored andcommitted
Make read-ns-form handle invalid files
1 parent 0b30bf3 commit 40d1431

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/refactor_nrepl/core.clj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,22 @@
122122
path)]
123123
(with-open [file-reader (or (some-> path-string FileReader.)
124124
(some-> path-file FileReader.))]
125-
(parse/read-ns-decl (readers/indexing-push-back-reader
126-
(PushbackReader. file-reader))))))
125+
(try
126+
(parse/read-ns-decl (readers/indexing-push-back-reader
127+
(PushbackReader. file-reader)))
128+
(catch Exception _ nil)))))
127129
([dialect path]
128130
(let [^String path-string (when (string? path)
129131
path)
130132
^File path-file (when-not path-string
131133
path)]
132134
(with-open [file-reader (or (some-> path-string FileReader.)
133135
(some-> path-file FileReader.))]
134-
(parse/read-ns-decl (readers/indexing-push-back-reader
135-
(PushbackReader. file-reader))
136-
{:read-cond :allow :features #{dialect}})))))
136+
(try
137+
(parse/read-ns-decl (readers/indexing-push-back-reader
138+
(PushbackReader. file-reader))
139+
{:read-cond :allow :features #{dialect}})
140+
(catch Exception _ nil))))))
137141

138142
(defn- data-file?
139143
"True of f is named like a clj file but represents data.

test/refactor_nrepl/core_test.clj

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
(ns refactor-nrepl.core-test
2-
(:require [clojure.test :refer [deftest is testing]]
3-
[refactor-nrepl.config :as config]
4-
[refactor-nrepl.core :refer [ignore-dir-on-classpath?]]))
2+
(:require
3+
[clojure.test :refer [are deftest is testing]]
4+
[refactor-nrepl.config :as config]
5+
[refactor-nrepl.core :refer [ignore-dir-on-classpath? read-ns-form]])
6+
(:import
7+
(java.io File)))
58

69
(defmacro assert-ignored-paths
710
[paths pred]
@@ -24,3 +27,14 @@
2427
[#".+checkouts/.+" #"resources"])]
2528
(assert-ignored-paths not-ignored false?)
2629
(assert-ignored-paths (concat always-ignored sometimes-ignored) true?)))))
30+
31+
(deftest test-read-ns-form
32+
(are [input expected] (testing input
33+
(assert (-> input File. .exists))
34+
(is (= expected
35+
(read-ns-form input)))
36+
true)
37+
"test-resources/readable_file_incorrect_aliases.clj" nil
38+
"testproject/src/com/example/one.clj" '(ns com.example.one
39+
(:require [com.example.two :as two :refer [foo]]
40+
[com.example.four :as four]))))

0 commit comments

Comments
 (0)