forked from lambdaisland/kaocha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.clj
55 lines (51 loc) · 2.03 KB
/
util.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(ns kaocha.util
(:require [kaocha.platform :as platform]))
(defn lib-path
"Returns the path for a lib"
^String [lib ext]
(str
(.. (name lib)
(replace \- \_)
(replace \. \/))
"." ext))
(defn ns-file
"Find the file for a given namespace (symbol), tries to mimic the resolution
logic of [[clojure.core/load]]."
[ns-sym]
(some
#(.getResource (clojure.lang.RT/baseLoader) (lib-path ns-sym %))
["class" "cljc" "clj"]))
(defn compiler-exception-file-and-line
"Try to get the file and line number from a CompilerException"
[^Throwable error]
;; On Clojure we get a clojure.lang.Compiler$CompilerException, on babashka we
;; get a clojure.lang.ExceptionInfo. Both implement the IExceptioninfo
;; interface, so we have a uniform way of getting the location info, although
;; what Clojure calls `:source` babashka calls `:file`. Calling `ex-data` on
;; other exceptions will return `nil`. Note that we can't actually test
;; for `(instance? IExceptioninfo)`, since babashka includes the ExceptionInfo
;; class, but not the IExceptionInfo interface. Instead we assume that if we
;; get the right `ex-data` that this is the exception we're looking for.
(let [{:keys [type line file source]} (ex-data error)
file (or source file)]
(if (and type file line)
[file line]
(when-let [error (.getCause error)]
(recur error)))))
(defn minimal-test-event
"Return a reduced version of a test event map, so debug output doesn't blow up
too much, e.g. in case of deeply nested testables in there."
[m]
(cond-> (select-keys m [:type
:file
:line
:var
:ns
:expected
:actual
:message
:kaocha/testable
:debug
::printed-expression])
(:kaocha/testable m)
(update :kaocha/testable select-keys [:kaocha.testable/id :kaocha.testable/type])))