Skip to content

Commit a28f8bd

Browse files
committed
Fix NPE when pretty/report called outside test
`report` can be run from code fixtures which caused `pretty/testing-vars-str` to throw a NullPointerException because *testing-vars* is nil inside fixtures.
1 parent eb6b000 commit a28f8bd

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

eftest/src/eftest/output_capture.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
(def ^:dynamic *test-buffer* nil)
66

77
(defn read-test-buffer []
8-
(String. (.toByteArray *test-buffer*)))
8+
(when *test-buffer*
9+
(String. (.toByteArray *test-buffer*))))
910

1011
(def active-buffers (atom #{}))
1112

eftest/src/eftest/report/pretty.clj

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@
3232

3333
(defn- testing-vars-str [{:keys [file line]}]
3434
(let [test-var (first test/*testing-vars*)]
35-
(str (:clojure-frame *fonts*) (-> test-var meta :ns ns-name) "/"
36-
(:function-name *fonts*) (-> test-var meta :name) (:reset *fonts*)
35+
(str (:clojure-frame *fonts*)
36+
(if test-var
37+
(-> test-var meta :ns ns-name)
38+
"unknown-namespace")
39+
"/"
40+
(:function-name *fonts*)
41+
(if test-var
42+
(-> test-var meta :name)
43+
"unknown-test")
44+
(:reset *fonts*)
3745
(when (or file line)
3846
(str " (" (:source *fonts*) file ":" line (:reset *fonts*) ")")))))
3947

eftest/test/eftest/report_test.clj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,21 @@
4848
pretty-nil
4949
"\n")
5050
result))))
51+
52+
(deftest report-outside-testing-vars
53+
(let [pretty-nil (puget/pprint-str nil {:print-color true
54+
:print-meta false})]
55+
(is (= (str "\nFAIL in unknown-namespace/unknown-test (report_test.clj:10)\nfoo\nexpected: "
56+
pretty-nil
57+
"\n actual: "
58+
pretty-nil
59+
"\n")
60+
(with-out-str
61+
(binding [*testing-vars* nil
62+
pretty/*fonts* {}
63+
*report-counters* (ref *initial-report-counters*)
64+
*test-out* *out*]
65+
(pretty/report {:type :fail
66+
:message "foo"
67+
:file "report_test.clj"
68+
:line 10})))))))

0 commit comments

Comments
 (0)