Skip to content

Commit

Permalink
Fix and test regression in defonce handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mk committed Jan 26, 2022
1 parent 5aa2a55 commit 571bf11
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/nextjournal/clerk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@
(cache! digest-file var-value))
(let [blob-id (cond no-cache? (view/->hash-str var-value)
(fn? var-value) nil
:else hash)]
(wrapped-with-metadata (cond-> result introduced-var var-from-def) visibility blob-id))))
:else hash)
result (if introduced-var
(var-from-def introduced-var)
result)]
(wrapped-with-metadata result visibility blob-id))))


(defn read+eval-cached [results-last-run ->hash doc-visibility codeblock]
Expand Down
5 changes: 5 additions & 0 deletions test/nextjournal/clerk_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
(is (not= (clerk/eval-string "(ns ^:nextjournal.clerk/no-cache my-random-test-ns) (java.util.UUID/randomUUID)")
(clerk/eval-string "(ns ^:nextjournal.clerk/no-cache my-random-test-ns) (java.util.UUID/randomUUID)"))))

(testing "defonce returns correct result on subsequent evals (when defonce would eval to nil)"
(clerk/eval-string "(ns ^:nextjournal.clerk/no-cache my-defonce-test-ns) (defonce state (atom {}))")
(is (match? {:blocks [map? {:result {:nextjournal/value {::clerk/var-from-def var?}}}]}
(clerk/eval-string "(ns ^:nextjournal.clerk/no-cache my-defonce-test-ns) (defonce state (atom {}))"))))

(testing "assigning viewers from form meta"
(is (match? {:blocks [{:result {:nextjournal/viewer #'nextjournal.clerk/table}}]}
(clerk/eval-string "^{:nextjournal.clerk/viewer nextjournal.clerk/table} (def markup [:h1 \"hi\"])")))
Expand Down

0 comments on commit 571bf11

Please sign in to comment.