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

Ensure ns-query map is built correctly #618

Merged
merged 1 commit into from
Jun 25, 2019
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: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Bugs fixed

* [#618](https://github.com/clojure-emacs/cider-nrepl/pull/618): Fix apropos to honor exclude-regexps to filter out namespaces by regex
* [#605](https://github.com/clojure-emacs/cider-nrepl/pull/605): Fix `ns-vars-with-meta` to return public vars.

### Changes
Expand Down
14 changes: 10 additions & 4 deletions src/cider/nrepl/middleware/apropos.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

;;; ## Middleware

(defn apropos [msg]
{:apropos-matches
(apropos/find-symbols
(defn- msg->var-query-map [msg]
(let [ns-query (select-keys msg [:exactly :project? :load-project-ns? :has-tests?
:include-regexps :exclude-regexps])]
(cond-> msg
;; Compatibility for the pre-var-query API
(:privates? msg)
Expand All @@ -28,11 +28,17 @@
(:docs? msg)
(assoc :full-doc? true)

true
(assoc-in [:var-query :ns-query] ns-query)

true
(update :var-query util.coerce/var-query)

(:ns msg)
(update :ns (comp find-ns symbol))))})
(update :ns (comp find-ns symbol)))))

(defn apropos [msg]
{:apropos-matches (-> msg msg->var-query-map apropos/find-symbols)})

(defn handle-apropos [handler msg]
(with-safe-transport handler msg
Expand Down
17 changes: 16 additions & 1 deletion test/clj/cider/nrepl/middleware/apropos_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(ns cider.nrepl.middleware.apropos-test
(:require
[cider.nrepl.middleware.apropos :refer [apropos]]
[cider.nrepl.middleware.apropos :refer [apropos] :as apropos]
[cider.nrepl.test-session :as session]
[clojure.string :as str]
[clojure.test :refer :all]))
Expand All @@ -9,6 +9,14 @@

(use-fixtures :each session/session-fixture)

(deftest msg->var-query-map-test
(testing "Constructs the ns-query map correctly"
(let [msg {:exclude-regexps ["^cider.nrepl" "^refactor-nrepl" "^nrepl"]
:query "spelling"}
query-map (#'apropos/msg->var-query-map msg)]
(is (contains? (:var-query query-map) :ns-query))
Copy link
Member

Choose a reason for hiding this comment

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

Won't it be better to just compare the result with the expected map?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i thought about that but i think the returned map will have regex's rather than strings for the exclude-regexps and I wasn't sure of the semantics of comparing regexes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and it appears they compare by reference. (= #"bob" #"bob") is false. so it would require some wrangling that i wasn't into at the time :) I can take another crack at it later if you like.

Copy link
Member

Choose a reason for hiding this comment

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

Got it. It's fine like this.

(is (= 3 (count (-> query-map :var-query :ns-query :exclude-regexps)))))))

(deftest integration-test
(testing "Apropos op, typical case"
(let [response (session/message {:op "apropos" :query "handle-apropos"})
Expand All @@ -17,6 +25,13 @@
(is (= (:type match) "function"))
(is (= (:name match) "cider.nrepl.middleware.apropos/handle-apropos"))))

(testing "Exclude namespaces typical case"
(let [response (session/message {:op "apropos" :query "handle-apropos"
:exclude-regexps ["cider.nrepl.middleware.apropos"]})
match (get-in response [:apropos-matches 0])]
(is (empty? match))
(is (= (:status response) #{"done"}))))

(testing "Apropos op, but specialized cases (invoked with prefix argument)"
(testing "Fails to get a private var because private? unset"
(let [response (session/message {:op "apropos" :query "my-private-var"})
Expand Down