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

Add refactor-nrepl.find-symbol-test #325

Merged
merged 5 commits into from
Sep 3, 2021
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
1 change: 0 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ commands:
paths:
- ~/.m2
- .cpcache
- repo
key: clojure-<< parameters.cache_version >>-{{ checksum "/tmp/clojure_cache_seed" }}

# The jobs are relatively simple. One runs utility commands against
Expand Down
14 changes: 9 additions & 5 deletions src/refactor_nrepl/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,22 @@
path)]
(with-open [file-reader (or (some-> path-string FileReader.)
(some-> path-file FileReader.))]
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader))))))
(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.))]
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader))
{:read-cond :allow :features #{dialect}})))))
(try
(parse/read-ns-decl (readers/indexing-push-back-reader
(PushbackReader. file-reader))
{:read-cond :allow :features #{dialect}})
(catch Exception _ nil))))))

(defn- data-file?
"True of f is named like a clj file but represents data.
Expand Down
2 changes: 1 addition & 1 deletion test/global_test_setup.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

(def set-refresh-dirs
(try
(require '[refactor-nrepl.inlined-deps.toolsnamespace.v1v1v0.clojure.tools.namespace.repl :refer [set-refresh-dirs]])
(require '[refactor-nrepl.inlined-deps.toolsnamespace.v1v1v0.clojure.tools.namespace.repl])
@(resolve 'refactor-nrepl.inlined-deps.toolsnamespace.v1v1v0.clojure.tools.namespace.repl/set-refresh-dirs)
(catch Exception _
(require '[clojure.tools.namespace.repl])
Expand Down
20 changes: 17 additions & 3 deletions test/refactor_nrepl/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
(ns refactor-nrepl.core-test
(:require [clojure.test :refer [deftest is testing]]
[refactor-nrepl.config :as config]
[refactor-nrepl.core :refer [ignore-dir-on-classpath?]]))
(:require
[clojure.test :refer [are deftest is testing]]
[refactor-nrepl.config :as config]
[refactor-nrepl.core :refer [ignore-dir-on-classpath? read-ns-form]])
(:import
(java.io File)))

(defmacro assert-ignored-paths
[paths pred]
Expand All @@ -24,3 +27,14 @@
[#".+checkouts/.+" #"resources"])]
(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
(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]))))
23 changes: 23 additions & 0 deletions test/refactor_nrepl/find_symbol_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns refactor-nrepl.find-symbol-test
(:require
[clojure.test :refer [deftest is]]
[refactor-nrepl.unreadable-files :refer [ignore-errors-str]]
[refactor-nrepl.find.find-symbol :as sut])
(:import
(java.io File)))

(def from-file-path
(-> "testproject/src/com/move/ns_to_be_moved.clj" File. .getAbsolutePath))

(deftest works
(let [found (sut/find-symbol {:file from-file-path
:ns "com.move.ns-to-be-moved"
:line 11
:column 7
:name "fn-to-be-moved"
:ignore-errors ignore-errors-str
:dir "testproject/src"})]
(is (seq found)
(pr-str found))
(is (= 4 (->> found (map :file) distinct count))
"Finds different files with references to the queried symbol")))
26 changes: 14 additions & 12 deletions test/refactor_nrepl/integration_tests.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns refactor-nrepl.integration-tests
(:require [clojure.test :refer [deftest is use-fixtures]]
(:require [clojure.test :refer [deftest is use-fixtures testing]]
[nrepl.server :as nrepl]
[refactor-nrepl middleware
[analyzer :as analyzer]
Expand Down Expand Up @@ -36,15 +36,15 @@
:line 6 :column 19
:name "foo" :dir test-project-dir)
result (remove keyword? response)]
(testing (pr-str result)
(is (= 3 (count result)) (format "expected 3 results but got %d" (count result)))
(is (every? (partial re-matches #"(?s).*(one|two)\.clj.*") result) "one.clj or two.clj not found in result")

(is (= 3 (count result)) (format "expected 3 results but got %d" (count result)))
(is (every? (partial re-matches #"(?s).*(one|two)\.clj.*") result) "one.clj or two.clj not found in result")

(is (some (partial re-matches #"(?s).*one.clj \[2\].*") result) "call of foo not found in ns com.example.one")
(is (some (partial re-matches #"(?s).*one.clj \[2\].*") result) "call of foo not found in ns com.example.one")

(is (some (partial re-matches #"(?s).*one.clj \[6\].*") result) "call of foo not found in ns com.example.one")
(is (some (partial re-matches #"(?s).*one.clj \[6\].*") result) "call of foo not found in ns com.example.one")

(is (some (partial re-matches #"(?s).*two.clj \[3\].*") result) "def of foo not found in ns com.example.two")))
(is (some (partial re-matches #"(?s).*two.clj \[3\].*") result) "def of foo not found in ns com.example.two"))))

(defn ns-ast-throw-error-for-five [^String content]
(if (.contains content "com.example.five")
Expand All @@ -60,14 +60,16 @@
:name "foo" :dir test-project-dir)
result (remove keyword? response)]

(is (= 3 (count result)) (format "expected 3 results but got %d" (count result)))
(is (every? (partial re-matches #"(?s).*(one|two)\.clj.*") result) "one.clj or two.clj not found in result")
(testing (pr-str result)

(is (some (partial re-matches #"(?s).*one.clj \[2\].*") result) "call of foo not found in ns com.example.one")
(is (= 3 (count result)) (format "expected 3 results but got %d" (count result)))
(is (every? (partial re-matches #"(?s).*(one|two)\.clj.*") result) "one.clj or two.clj not found in result")

(is (some (partial re-matches #"(?s).*one.clj \[6\].*") result) "call of foo not found in ns com.example.one")
(is (some (partial re-matches #"(?s).*one.clj \[2\].*") result) "call of foo not found in ns com.example.one")

(is (some (partial re-matches #"(?s).*two.clj \[3\].*") result) "def of foo not found in ns com.example.two"))))
(is (some (partial re-matches #"(?s).*one.clj \[6\].*") result) "call of foo not found in ns com.example.one")

(is (some (partial re-matches #"(?s).*two.clj \[3\].*") result) "def of foo not found in ns com.example.two")))))

(deftest test-shouldnt-find-str-in-assert
(let [transport (connect :port 7777)
Expand Down
3 changes: 3 additions & 0 deletions testproject/src/com/example/one.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@

(defn from-registry [k]
(k four/registry))

;; Tries reproducing https://github.com/clojure-emacs/clj-refactor.el/issues/485
(set! *warn-on-reflection* true)
3 changes: 3 additions & 0 deletions testproject/src/com/move/ns_to_be_moved.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
(ns com.move.ns-to-be-moved)

;; Tries reproducing https://github.com/clojure-emacs/clj-refactor.el/issues/485
(set! *warn-on-reflection* true)

(def var-to-be-moved)
(def ^:private private-var-to-be-moved)

Expand Down