Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate irrelevant parts of stacktrace #420

Merged
merged 7 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Added kaocha.stacktrace/*stacktrace-stop-list* binding to stop printing a stacktrace after matching a string

## Added

## Fixed
Expand Down Expand Up @@ -1069,4 +1071,4 @@ namespace.
- The configuration format has changed, you should now start with the `#kaocha
{}` tagged reader literal in `tests.edn` to provide defaults. If you want more
control then overwrite `tests.edn` with the output of `--print-config` and
tweak.
tweak.
152 changes: 152 additions & 0 deletions doc/config/bindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ You can configure dynamic vars from `tests.edn`, these will be bound to the

- `kaocha.stacktrace/*stacktrace-filters* []` disable filtering of
stacktraces, showing all stack frames
- `kaocha.stacktrace/*stacktrace-stop-list* []` disable the shortening
of the stacktrace (by default stops printing when it sees "kaocha.ns")
- `clojure.pprint/*print-right-margin* 120` Make pretty printing use longer
line lengths
- `clojure.test.check.clojure-test/*report-completion* false, clojure.test.check.clojure-test/*report-trials* false`
Expand Down Expand Up @@ -52,3 +54,153 @@ You can configure dynamic vars from `tests.edn`, these will be bound to the



## Stacktrace filtering

- <em>Given </em> a file named "tests.edn" with:

``` clojure
#kaocha/v1
{:bindings {kaocha.stacktrace/*stacktrace-filters* ["clojure.core"]}}
```


- <em>And </em> a file named "test/my/erroring_test.clj" with:

``` clojure
(ns my.erroring-test
(:require [clojure.test :refer :all]))

(deftest stacktrace-test
(is (throw (java.lang.Exception.))))

```


- <em>When </em> I run `bin/kaocha`

- <em>Then </em> the output should contain:

``` nil
clojure.lang
```


- <em>And </em> the output should not contain

``` nil
clojure.core
```



## Stacktrace filtering turned off

- <em>Given </em> a file named "tests.edn" with:

``` clojure
#kaocha/v1
{:bindings {kaocha.stacktrace/*stacktrace-filters* []}}
```


- <em>And </em> a file named "test/my/erroring_test.clj" with:

``` clojure
(ns my.erroring-test
(:require [clojure.test :refer :all]))

(deftest stacktrace-test
(is (throw (java.lang.Exception.))))

```


- <em>When </em> I run `bin/kaocha`

- <em>Then </em> the output should contain:

``` nil
clojure.core
```



## Stacktrace shortening

- <em>Given </em> a file named "tests.edn" with:

``` clojure
#kaocha/v1
{:bindings {kaocha.stacktrace/*stacktrace-stop-list* ["kaocha.runner"]}}
```


- <em>And </em> a file named "test/my/erroring_test.clj" with:

``` clojure
(ns my.erroring-test
(:require [clojure.test :refer :all]))

(deftest stacktrace-test
(is (throw (java.lang.Exception.))))

```


- <em>When </em> I run `bin/kaocha`

- <em>Then </em> the output should contain:

``` nil
(Rest of stacktrace elided)
```


- <em>And </em> the output should not contain

``` nil
kaocha.runner$
```



## Disable stacktrace shortening

- <em>Given </em> a file named "tests.edn" with:

``` clojure
#kaocha/v1
{:bindings {kaocha.stacktrace/*stacktrace-filters* []
kaocha.stacktrace/*stacktrace-stop-list* []}}
```


- <em>And </em> a file named "test/my/erroring_test.clj" with:

``` clojure
(ns my.erroring-test
(:require [clojure.test :refer :all]))

(deftest stacktrace-test
(is (throw (java.lang.Exception.))))

```


- <em>When </em> I run `bin/kaocha`

- <em>Then </em> the output should contain:

``` nil
kaocha.runner$
```


- <em>And </em> the output should not contain

``` nil
(Rest of stacktrace elided)
```



29 changes: 18 additions & 11 deletions src/kaocha/stacktrace.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
"clojure.lang."
"clojure.core"
"clojure.main"
"orchestra."
"kaocha.monkey_patch"])

(defn elide-element? [e]
(some #(str/starts-with? (.getClassName ^StackTraceElement e) %) *stacktrace-filters*))

(def ^:dynamic *stacktrace-stop-list* ["kaocha.ns"
"lambdaisland.tools.namespace.reload"])

(defn sentinel-element? [e]
(some #(str/starts-with? (.getClassName ^StackTraceElement e) %) *stacktrace-stop-list*))

(defn print-stack-trace
"Prints a Clojure-oriented stack trace of tr, a Throwable.
Prints a maximum of n stack frames (default: unlimited). Does not print
Expand All @@ -35,16 +40,18 @@
(let [n (cond-> n n dec)]
(if (= 0 n)
(println " ... and " (count st) "more")
(if (elide-element? e)
(do
(when (not eliding?)
(println " ..."))
(recur st true n))
(do
(print " ")
(st/print-trace-element e)
(newline)
(recur st false n))))))))))
(if (sentinel-element? e)
(println "(Rest of stacktrace elided)")
(if (elide-element? e)
(do
(when (not eliding?)
(println " ..."))
(recur st true n))
(do
(print " ")
(st/print-trace-element e)
(newline)
(recur st false n)))))))))))

(defn print-cause-trace
"Like print-stack-trace but prints chained exceptions (causes)."
Expand Down
99 changes: 99 additions & 0 deletions test/features/config/bindings.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Feature: Configuration: Bindings

- `kaocha.stacktrace/*stacktrace-filters* []` disable filtering of
stacktraces, showing all stack frames
- `kaocha.stacktrace/*stacktrace-stop-list* []` disable the shortening
of the stacktrace (by default stops printing when it sees "kaocha.ns")
- `clojure.pprint/*print-right-margin* 120` Make pretty printing use longer
line lengths
- `clojure.test.check.clojure-test/*report-completion* false, clojure.test.check.clojure-test/*report-trials* false`
Expand All @@ -38,3 +40,100 @@ Feature: Configuration: Bindings
"""
1 tests, 1 assertions, 0 failures.
"""

Scenario: Stacktrace filtering
Given a file named "tests.edn" with:
""" clojure
#kaocha/v1
{:bindings {kaocha.stacktrace/*stacktrace-filters* ["clojure.core"]}}
"""
And a file named "test/my/erroring_test.clj" with:
""" clojure
(ns my.erroring-test
(:require [clojure.test :refer :all]))

(deftest stacktrace-test
(is (throw (java.lang.Exception.))))

"""
When I run `bin/kaocha`
Then the output should contain:
"""
clojure.lang
"""
And the output should not contain
"""
clojure.core
"""

Scenario: Stacktrace filtering turned off
Given a file named "tests.edn" with:
""" clojure
#kaocha/v1
{:bindings {kaocha.stacktrace/*stacktrace-filters* []}}
"""
And a file named "test/my/erroring_test.clj" with:
""" clojure
(ns my.erroring-test
(:require [clojure.test :refer :all]))

(deftest stacktrace-test
(is (throw (java.lang.Exception.))))

"""
When I run `bin/kaocha`
Then the output should contain:
"""
clojure.core
"""

Scenario: Stacktrace shortening
Given a file named "tests.edn" with:
""" clojure
#kaocha/v1
{:bindings {kaocha.stacktrace/*stacktrace-stop-list* ["kaocha.runner"]}}
"""
And a file named "test/my/erroring_test.clj" with:
""" clojure
(ns my.erroring-test
(:require [clojure.test :refer :all]))

(deftest stacktrace-test
(is (throw (java.lang.Exception.))))

"""
When I run `bin/kaocha`
Then the output should contain:
"""
(Rest of stacktrace elided)
"""
And the output should not contain
"""
kaocha.runner$
"""

Scenario: Disable stacktrace shortening
Given a file named "tests.edn" with:
""" clojure
#kaocha/v1
{:bindings {kaocha.stacktrace/*stacktrace-filters* []
kaocha.stacktrace/*stacktrace-stop-list* []}}
"""
And a file named "test/my/erroring_test.clj" with:
""" clojure
(ns my.erroring-test
(:require [clojure.test :refer :all]))

(deftest stacktrace-test
(is (throw (java.lang.Exception.))))

"""
When I run `bin/kaocha`
Then the output should contain:
"""
kaocha.runner$
"""
And the output should not contain
"""
(Rest of stacktrace elided)
"""
3 changes: 2 additions & 1 deletion tests.edn
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

:kaocha.hooks/pre-load [kaocha.assertions/load-assertions]

:kaocha/bindings {kaocha.stacktrace/*stacktrace-filters* []}
:kaocha/bindings {kaocha.stacktrace/*stacktrace-filters* []
kaocha.stacktrace/*stacktrace-stop-list* []}

:reporter kaocha.report/documentation}