|
| 1 | +(ns kit-generator.injections-test |
| 2 | + (:require |
| 3 | + [clojure.test :refer [deftest is testing]] |
| 4 | + [kit.generator.modules.injections :as injections] |
| 5 | + [rewrite-clj.zip :as z])) |
| 6 | + |
| 7 | +;; Fix 1 — EDN injection NPE on missing target |
| 8 | +(deftest inject-edn-missing-target-append |
| 9 | + (testing ":append with nonexistent target returns original data unchanged" |
| 10 | + (let [data (z/of-string "{:a 1}") |
| 11 | + result (injections/inject |
| 12 | + {:type :edn |
| 13 | + :data data |
| 14 | + :target [:nonexistent :path] |
| 15 | + :action :append |
| 16 | + :value ":new-val"})] |
| 17 | + (is (some? result) "should not NPE") |
| 18 | + (is (= "{:a 1}" (z/root-string result)))))) |
| 19 | + |
| 20 | +(deftest inject-edn-missing-target-merge |
| 21 | + (testing ":merge with nonexistent target returns original data unchanged" |
| 22 | + (let [data (z/of-string "{:a 1}") |
| 23 | + result (injections/inject |
| 24 | + {:type :edn |
| 25 | + :data data |
| 26 | + :target [:nonexistent :path] |
| 27 | + :action :merge |
| 28 | + :value "{:b 2}"})] |
| 29 | + (is (some? result) "should not NPE") |
| 30 | + (is (= "{:a 1}" (z/root-string result)))))) |
| 31 | + |
| 32 | +;; Fix 2 — Error map bug in read-files |
| 33 | +(deftest read-files-error-map |
| 34 | + (testing "read-files with nonexistent path throws ex-info with correct :path" |
| 35 | + (let [bad-path "/nonexistent/file.edn"] |
| 36 | + (try |
| 37 | + (injections/read-files {} [bad-path]) |
| 38 | + (is false "should have thrown") |
| 39 | + (catch clojure.lang.ExceptionInfo e |
| 40 | + (is (= bad-path (:path (ex-data e))) |
| 41 | + ":path should be the actual path string, not the keyword :path") |
| 42 | + (is (re-find #"Failed to read asset: " (ex-message e)) |
| 43 | + "message should have a space after 'asset:'")))))) |
| 44 | + |
| 45 | +;; Fix 5 — edn-safe-merge should throw ExceptionInfo, not plain Exception |
| 46 | +(deftest edn-safe-merge-throws-ex-info |
| 47 | + (testing "edn-safe-merge wraps errors in ex-info" |
| 48 | + (try |
| 49 | + ;; Force an error by passing invalid args that will fail during merge |
| 50 | + (injections/edn-safe-merge |
| 51 | + (z/of-string "not-a-map") |
| 52 | + (z/of-string "{:a 1}")) |
| 53 | + (is false "should have thrown") |
| 54 | + (catch clojure.lang.ExceptionInfo _e |
| 55 | + (is true "threw ExceptionInfo as expected")) |
| 56 | + (catch Exception _e |
| 57 | + (is false "should throw ExceptionInfo, not plain Exception"))))) |
0 commit comments