Skip to content

Commit 2e0c0a9

Browse files
puredangerstuarthalloway
authored andcommitted
CLJ-1856 Switch from reporting test failures based on stack depth to removing leading unwanted stack frames based on class name. Deprecated but did not remove old function.
Signed-off-by: Stuart Halloway <stu@cognitect.com>
1 parent 786fc5a commit 2e0c0a9

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/clj/clojure/test.clj

+17-4
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@
233233
"}
234234
clojure.test
235235
(:require [clojure.template :as temp]
236-
[clojure.stacktrace :as stack]))
236+
[clojure.stacktrace :as stack]
237+
[clojure.string :as str]))
237238

238239
;; Nothing is marked "private" here, so you can rebind things to plug
239240
;; in your own testing or reporting frameworks.
@@ -331,14 +332,22 @@
331332
:added "1.1"}
332333
report :type)
333334

334-
(defn- file-and-line
335+
(defn- file-and-line
336+
{:deprecated "1.8"}
335337
[^Throwable exception depth]
336338
(let [stacktrace (.getStackTrace exception)]
337339
(if (< depth (count stacktrace))
338340
(let [^StackTraceElement s (nth stacktrace depth)]
339341
{:file (.getFileName s) :line (.getLineNumber s)})
340342
{:file nil :line nil})))
341343

344+
(defn- stacktrace-file-and-line
345+
[stacktrace]
346+
(if (seq stacktrace)
347+
(let [^StackTraceElement s (first stacktrace)]
348+
{:file (.getFileName s) :line (.getLineNumber s)})
349+
{:file nil :line nil}))
350+
342351
(defn do-report
343352
"Add file and line information to a test result and call report.
344353
If you are writing a custom assert-expr method, call this function
@@ -348,8 +357,12 @@
348357
(report
349358
(case
350359
(:type m)
351-
:fail (merge (file-and-line (new java.lang.Throwable) 1) m)
352-
:error (merge (file-and-line (:actual m) 0) m)
360+
:fail (merge (stacktrace-file-and-line (drop-while
361+
#(let [cl-name (.getClassName ^StackTraceElement %)]
362+
(or (str/starts-with? cl-name "java.lang.")
363+
(str/starts-with? cl-name "clojure.test$")))
364+
(.getStackTrace (Thread/currentThread)))) m)
365+
:error (merge (stacktrace-file-and-line (.getStackTrace ^Throwable (:actual m))) m)
353366
m)))
354367

355368
(defmethod report :default [m]

test/clojure/test_clojure/test.clj

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
[])
7979
t (doto (Exception.) (.setStackTrace empty-stack))]
8080
(is (map? (#'clojure.test/file-and-line t 0)) "Should pass")
81+
(is (map? (#'clojure.test/stacktrace-file-and-line empty-stack)) "Should pass")
8182
(is (string? (with-out-str (stack/print-stack-trace t))) "Should pass")))
8283

8384
(deftest #^{:has-meta true} can-add-metadata-to-tests

0 commit comments

Comments
 (0)