Fork me on GitHub
#nbb
<
2022-07-08
>
borkdude09:07:49

Now pushed a fix which will also let you bundle reagent + react into your bundle

genRaiy10:07:45

I'm trying to use with-redefs in an nbb test but it's not picking up the overrides.

1
genRaiy10:07:56

here is some code

genRaiy10:07:12

(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))))))

genRaiy10:07:21

am I holding it wrong?

borkdude10:07:51

async + with-redefs probably is the issue.

genRaiy10:07:08

I tried it outside also

borkdude10:07:59

user=> (defn foo [] :foo)
#'user/foo
user=> (with-redefs [foo (fn [] :bar)] (foo))
:bar

genRaiy10:07:02

(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))))))

borkdude10:07:34

your issue is still async + with-redefs

genRaiy10:07:52

so is that a general problem with CLJS?

borkdude10:07:40

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

genRaiy10:07:56

seems like there are some hacks out there ... I'll search a bit more

borkdude11:07:25

you can do (alter-var-root #'var old-value) in the finally

borkdude11:07:46

this only works in nbb / SCI, not in CLJS (since that doesn't have alter-var-root)

borkdude11:07:59

in CLJS you would write (set! ...)

borkdude11:07:12

but I still have to support that on global non-dynamic vars in the CLJS side of things

genRaiy11:07:09

ah, ok ... let me try the alter-var-root approach

genRaiy11:07:18

nope, I have no idea how to get this to work

borkdude11:07:22

just a minute

genRaiy11:07:25

(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))))))

borkdude11:07:11

You should first capture the old values of the vars and then restore them in the finally

borkdude11:07:41

(let [old-fn my-var] ... (p/finally (alter-var-root #'my-var (constantly old-fn)))

borkdude11:07:57

but I'm pushing a new version of nbb which will make this slighly easier, just 1 sec

borkdude11:07:40

Hmm computer trouble, my kbd stopped working. Was about to publish

borkdude11:07:28

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

borkdude11:07:40

I'll reboot my computer....

borkdude11:07:49

0.6.124 is being pushed now on CI

genRaiy11:07:52

dumb question perhaps: given that I'm just running a test - why do I need to change everything back?

borkdude11:07:11

You don't have to change, that's up to you

borkdude11:07:26

It's up to you to screw up and don't revert the screw up, your problem :)

genRaiy11:07:33

but the inversion is confusing in the async case

genRaiy11:07:00

you were saying that I have to do the set! in the finally? Or am I missing the point?

borkdude11:07:14

just look at my example and it should be clear, right?

genRaiy11:07:48

your example does not use aync

genRaiy11:07:00

I thought that's what you said was another issue

borkdude11:07:08

it doesn't matter. just replace finally with `p/finally

genRaiy11:07:35

ok - I'll see what happens 🙂

borkdude11:07:19

Something like this, pseudo-code:

(let [old-foo foo] (set! foo (fn [] 2)) (-> (p/do (p/delay 1000) (foo)) (p/finally (set! foo old-foo))

genRaiy13:07:02

Thanks man! I'll buy you an avb at DCD, if not before

🍻 1