This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-08
Channels
- # announcements (2)
- # babashka (100)
- # beginners (25)
- # biff (7)
- # calva (13)
- # cider (24)
- # clj-kondo (39)
- # cljsrn (2)
- # clojure (22)
- # clojure-dev (13)
- # clojure-europe (12)
- # clojure-gamedev (3)
- # clojure-losangeles (2)
- # clojure-nl (1)
- # clojure-norway (3)
- # clojure-spec (11)
- # clojure-uk (2)
- # clojurescript (20)
- # core-async (8)
- # cursive (7)
- # data-science (2)
- # datomic (14)
- # emacs (6)
- # events (7)
- # fulcro (9)
- # honeysql (1)
- # kaocha (24)
- # lambdaisland (3)
- # leiningen (6)
- # lsp (30)
- # membrane (7)
- # missionary (10)
- # nbb (48)
- # nextjournal (13)
- # off-topic (6)
- # parinfer (4)
- # pathom (1)
- # polylith (1)
- # reagent (7)
- # rewrite-clj (6)
- # ring (11)
- # sci (7)
- # shadow-cljs (8)
- # sql (13)
I'm trying to use with-redefs
in an nbb
test but it's not picking up the overrides.
(deftest management-tokens-test
(testing "that we can store, retrieve and delete management tokens"
(async done
(with-redefs [aws/store-management-token (fn [cid tok] (local-secrets/put-secret cid tok))
aws/fetch-management-token (fn [cid] (local-secrets/get-secret cid))
aws/delete-management-token (fn [cid] (local-secrets/delete-secret cid))]
(-> (p/let [card-id (apply str (string/split (str (random-uuid)) #"-"))
token (apply str (reverse card-id))
stored (aws/store-management-token card-id token)
fetched (aws/fetch-management-token card-id)
deleted (aws/delete-management-token card-id true)
card-name (str "Modulr/" card-id)]
(is (= 200 (get-in stored [:$metadata :httpStatusCode])))
(is (= (:Name stored) card-name))
(is (= fetched token))
(is (= 200 (get-in deleted [:$metadata :httpStatusCode])))
(is (= (:Name deleted) card-name)))
(p/finally done))))))
user=> (defn foo [] :foo)
#'user/foo
user=> (with-redefs [foo (fn [] :bar)] (foo))
:bar
(deftest management-tokens-test
(testing "that we can store, retrieve and delete management tokens"
(with-redefs [aws/store-management-token (fn [cid tok] (local-secrets/put-secret cid tok))
aws/fetch-management-token (fn [cid] (local-secrets/get-secret cid))
aws/delete-management-token (fn [cid] (local-secrets/delete-secret cid))]
(async done
(-> (p/let [card-id (apply str (string/split (str (random-uuid)) #"-"))
token (apply str (reverse card-id))
stored (aws/store-management-token card-id token)
fetched (aws/fetch-management-token card-id)
deleted (aws/delete-management-token card-id true)
card-name (str "Modulr/" card-id)]
(is (= 200 (get-in stored [:$metadata :httpStatusCode])))
(is (= (:Name stored) card-name))
(is (= fetched token))
(is (= 200 (get-in deleted [:$metadata :httpStatusCode])))
(is (= (:Name deleted) card-name)))
(p/finally done))))))
yes, how with-redefs works: it restores the bindings at the end of your body. but your work will happen in the future, when the bindings have already been restored
but I still have to support that on global non-dynamic vars in the CLJS side of things
(deftest management-tokens-test
(testing "that we can store, retrieve and delete management tokens"
(async done
(-> (p/let [card-id (apply str (string/split (str (random-uuid)) #"-"))
token (apply str (reverse card-id))
stored (aws/store-management-token card-id token)
fetched (aws/fetch-management-token card-id)
deleted (aws/delete-management-token card-id true)
card-name (str "Modulr/" card-id)]
(is (= 200 (get-in stored [:$metadata :httpStatusCode])))
(is (= (:Name stored) card-name))
(is (= fetched token))
(is (= 200 (get-in deleted [:$metadata :httpStatusCode])))
(is (= (:Name deleted) card-name)))
(p/finally
(do (alter-var-root #'aws/store-management-token (fn [c t] (local-secrets/put-secret c t)))
(alter-var-root #'aws/fetch-management-token (fn [c] (local-secrets/get-secret c)))
(alter-var-root #'aws/delete-management-token (fn [c] (local-secrets/delete-secret c)))
done))))))
You should first capture the old values of the vars and then restore them in the finally
but I'm pushing a new version of nbb which will make this slighly easier, just 1 sec
Ah, so, in: 0.6.124:
$ node lib/nbb_main.js
Welcome to nbb v0.6.123!
user=> (defn foo [] 1)
#'user/foo
user=> (let [old-foo foo _ (set! foo (fn [] 2))] (try (foo) (finally (set! foo old-foo))))
2
user=> (foo)
1
dumb question perhaps: given that I'm just running a test - why do I need to change everything back?
you were saying that I have to do the set!
in the finally
? Or am I missing the point?