From 38bf881799d96753321a4747da62e0f17db8b28a Mon Sep 17 00:00:00 2001 From: Paulus Esterhazy Date: Thu, 13 Oct 2022 10:42:30 +0200 Subject: [PATCH 1/7] Truncate stacktrace once the first kaocha ns is encountered --- src/kaocha/stacktrace.clj | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/kaocha/stacktrace.clj b/src/kaocha/stacktrace.clj index c38d1115..4cb80cf6 100644 --- a/src/kaocha/stacktrace.clj +++ b/src/kaocha/stacktrace.clj @@ -13,6 +13,12 @@ (defn elide-element? [e] (some #(str/starts-with? (.getClassName ^StackTraceElement e) %) *stacktrace-filters*)) +(def sentinel-list ["kaocha.ns" + "lambdaisland.tools.namespace.reload"]) + +(defn sentinel-element? [e] + (some #(str/starts-with? (.getClassName ^StackTraceElement e) %) sentinel-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 @@ -35,16 +41,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)." From 943035ca47520a84c7a8c1825cec3d0d073d44af Mon Sep 17 00:00:00 2001 From: Ben Lovell Date: Wed, 14 Jun 2023 20:38:31 +0200 Subject: [PATCH 2/7] Make stacktrace truncation configurable, and add tests/documentation --- doc/config/bindings.md | 152 ++++++++++++++++++++++++++ src/kaocha/stacktrace.clj | 7 +- test/features/config/bindings.feature | 99 +++++++++++++++++ tests.edn | 3 +- 4 files changed, 256 insertions(+), 5 deletions(-) diff --git a/doc/config/bindings.md b/doc/config/bindings.md index 733df011..4d39bd2a 100644 --- a/doc/config/bindings.md +++ b/doc/config/bindings.md @@ -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` @@ -52,3 +54,153 @@ You can configure dynamic vars from `tests.edn`, these will be bound to the +## 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: + +``` nil +clojure.lang +``` + + +- And the output should not contain + +``` nil +clojure.core +``` + + + +## 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: + +``` nil +clojure.core +``` + + + +## Stacktrace shortening + +- Given a file named "tests.edn" with: + +``` clojure +#kaocha/v1 +{:bindings {kaocha.stacktrace/*stacktrace-stop-list* ["kaocha.ns"]}} +``` + + +- 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: + +``` nil +(Rest of stacktrace elided) +``` + + +- And the output should not contain + +``` nil +kaocha.ns +``` + + + +## 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: + +``` nil +kaocha.runner +``` + + +- And the output should not contain + +``` nil +(Rest of stacktrace elided) +``` + + + diff --git a/src/kaocha/stacktrace.clj b/src/kaocha/stacktrace.clj index 4cb80cf6..fec69b9c 100644 --- a/src/kaocha/stacktrace.clj +++ b/src/kaocha/stacktrace.clj @@ -7,17 +7,16 @@ "clojure.lang." "clojure.core" "clojure.main" - "orchestra." "kaocha.monkey_patch"]) (defn elide-element? [e] (some #(str/starts-with? (.getClassName ^StackTraceElement e) %) *stacktrace-filters*)) -(def sentinel-list ["kaocha.ns" - "lambdaisland.tools.namespace.reload"]) +(def ^:dynamic *stacktrace-stop-list* ["kaocha.ns" + "lambdaisland.tools.namespace.reload"]) (defn sentinel-element? [e] - (some #(str/starts-with? (.getClassName ^StackTraceElement e) %) sentinel-list)) + (some #(str/starts-with? (.getClassName ^StackTraceElement e) %) *stacktrace-stop-list*)) (defn print-stack-trace "Prints a Clojure-oriented stack trace of tr, a Throwable. diff --git a/test/features/config/bindings.feature b/test/features/config/bindings.feature index 96c0a270..1dcb7a8b 100644 --- a/test/features/config/bindings.feature +++ b/test/features/config/bindings.feature @@ -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` @@ -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.ns"]}} + """ + 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.ns + """ + + 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) + """ diff --git a/tests.edn b/tests.edn index c8684d33..3f67a1fa 100644 --- a/tests.edn +++ b/tests.edn @@ -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} From 7b6ba598ebdebbf6c540eeba5f32744a5227e37c Mon Sep 17 00:00:00 2001 From: Alys Brooks Date: Fri, 7 Jul 2023 14:39:30 -0500 Subject: [PATCH 3/7] Avoid false negatives on CI by adding "at" On CI, we have plugins enabled that also list namespaces, so adding the at ensures those aren't confused for elements of the stack trace. --- test/features/config/bindings.feature | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/features/config/bindings.feature b/test/features/config/bindings.feature index 1dcb7a8b..57c07085 100644 --- a/test/features/config/bindings.feature +++ b/test/features/config/bindings.feature @@ -59,11 +59,11 @@ Feature: Configuration: Bindings When I run `bin/kaocha` Then the output should contain: """ - clojure.lang + at clojure.lang """ And the output should not contain """ - clojure.core + at clojure.core """ Scenario: Stacktrace filtering turned off @@ -84,7 +84,7 @@ Feature: Configuration: Bindings When I run `bin/kaocha` Then the output should contain: """ - clojure.core + at clojure.core """ Scenario: Stacktrace shortening @@ -109,7 +109,7 @@ Feature: Configuration: Bindings """ And the output should not contain """ - kaocha.ns + at kaocha.ns """ Scenario: Disable stacktrace shortening @@ -131,7 +131,7 @@ Feature: Configuration: Bindings When I run `bin/kaocha` Then the output should contain: """ - kaocha.runner + at kaocha.runner """ And the output should not contain """ From 4fb4f65b9385ae0edd748fd086a402645c5768bf Mon Sep 17 00:00:00 2001 From: Ben Lovell Date: Mon, 18 Sep 2023 18:16:03 +0200 Subject: [PATCH 4/7] Fix paren issue, update docs --- doc/config/bindings.md | 18 +++++++++--------- test/features/config/bindings.feature | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/doc/config/bindings.md b/doc/config/bindings.md index 4d39bd2a..d60c539a 100644 --- a/doc/config/bindings.md +++ b/doc/config/bindings.md @@ -71,7 +71,7 @@ You can configure dynamic vars from `tests.edn`, these will be bound to the (:require [clojure.test :refer :all])) (deftest stacktrace-test - (is (throw (java.lang.Exception.))) + (is (throw (java.lang.Exception.)))) ``` @@ -81,14 +81,14 @@ You can configure dynamic vars from `tests.edn`, these will be bound to the - Then the output should contain: ``` nil -clojure.lang +at clojure.lang ``` - And the output should not contain ``` nil -clojure.core +at clojure.core ``` @@ -110,7 +110,7 @@ clojure.core (:require [clojure.test :refer :all])) (deftest stacktrace-test - (is (throw (java.lang.Exception.))) + (is (throw (java.lang.Exception.)))) ``` @@ -120,7 +120,7 @@ clojure.core - Then the output should contain: ``` nil -clojure.core +at clojure.core ``` @@ -142,7 +142,7 @@ clojure.core (:require [clojure.test :refer :all])) (deftest stacktrace-test - (is (throw (java.lang.Exception.))) + (is (throw (java.lang.Exception.)))) ``` @@ -159,7 +159,7 @@ clojure.core - And the output should not contain ``` nil -kaocha.ns +at kaocha.ns ``` @@ -182,7 +182,7 @@ kaocha.ns (:require [clojure.test :refer :all])) (deftest stacktrace-test - (is (throw (java.lang.Exception.))) + (is (throw (java.lang.Exception.)))) ``` @@ -192,7 +192,7 @@ kaocha.ns - Then the output should contain: ``` nil -kaocha.runner +at kaocha.runner ``` diff --git a/test/features/config/bindings.feature b/test/features/config/bindings.feature index 57c07085..a3a36ae6 100644 --- a/test/features/config/bindings.feature +++ b/test/features/config/bindings.feature @@ -53,7 +53,7 @@ Feature: Configuration: Bindings (:require [clojure.test :refer :all])) (deftest stacktrace-test - (is (throw (java.lang.Exception.))) + (is (throw (java.lang.Exception.)))) """ When I run `bin/kaocha` @@ -78,7 +78,7 @@ Feature: Configuration: Bindings (:require [clojure.test :refer :all])) (deftest stacktrace-test - (is (throw (java.lang.Exception.))) + (is (throw (java.lang.Exception.)))) """ When I run `bin/kaocha` @@ -99,7 +99,7 @@ Feature: Configuration: Bindings (:require [clojure.test :refer :all])) (deftest stacktrace-test - (is (throw (java.lang.Exception.))) + (is (throw (java.lang.Exception.)))) """ When I run `bin/kaocha` @@ -125,7 +125,7 @@ Feature: Configuration: Bindings (:require [clojure.test :refer :all])) (deftest stacktrace-test - (is (throw (java.lang.Exception.))) + (is (throw (java.lang.Exception.)))) """ When I run `bin/kaocha` From 2e17932cb67aeb6d3f9c37b48d918587a2211e1a Mon Sep 17 00:00:00 2001 From: Ben Lovell Date: Mon, 18 Sep 2023 20:37:11 +0200 Subject: [PATCH 5/7] Remove "at" and use a NS available on CI for shortening "at" is only used on the first line, and the point of this filtering is to prevent it from printing noise further along than the first line to increase readibility of the entire stacktrace. `kaocha.runner` seems to be a good bet to be running both locally and in CI from my observations, and thus better than `kaocha.ns` which doesn't appear to be in the stacktrace when not in CI? --- test/features/config/bindings.feature | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/features/config/bindings.feature b/test/features/config/bindings.feature index a3a36ae6..e5009773 100644 --- a/test/features/config/bindings.feature +++ b/test/features/config/bindings.feature @@ -59,11 +59,11 @@ Feature: Configuration: Bindings When I run `bin/kaocha` Then the output should contain: """ - at clojure.lang + clojure.lang """ And the output should not contain """ - at clojure.core + clojure.core """ Scenario: Stacktrace filtering turned off @@ -84,14 +84,14 @@ Feature: Configuration: Bindings When I run `bin/kaocha` Then the output should contain: """ - at clojure.core + clojure.core """ Scenario: Stacktrace shortening Given a file named "tests.edn" with: """ clojure #kaocha/v1 - {:bindings {kaocha.stacktrace/*stacktrace-stop-list* ["kaocha.ns"]}} + {:bindings {kaocha.stacktrace/*stacktrace-stop-list* ["kaocha.runner"]}} """ And a file named "test/my/erroring_test.clj" with: """ clojure @@ -109,7 +109,7 @@ Feature: Configuration: Bindings """ And the output should not contain """ - at kaocha.ns + kaocha.runner """ Scenario: Disable stacktrace shortening @@ -131,7 +131,7 @@ Feature: Configuration: Bindings When I run `bin/kaocha` Then the output should contain: """ - at kaocha.runner + kaocha.runner """ And the output should not contain """ From 44b445f274fd1229b753b9983e860367cfa3be2b Mon Sep 17 00:00:00 2001 From: Ben Lovell Date: Mon, 18 Sep 2023 20:45:23 +0200 Subject: [PATCH 6/7] Add line to changelog about *stacktrace-stop-list* --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d7ff02d..89d5fbef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +- Added kaocha.stacktrace/*stacktrace-stop-list* binding to stop printing a stacktrace after matching a string + ## Added ## Fixed @@ -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. \ No newline at end of file + tweak. From 8ed006d03790cfc5ae34d54e1ca92160e4220e58 Mon Sep 17 00:00:00 2001 From: Ben Lovell Date: Mon, 18 Sep 2023 20:57:11 +0200 Subject: [PATCH 7/7] Avoid false positive from 'Instrumented blah' line --- doc/config/bindings.md | 12 ++++++------ test/features/config/bindings.feature | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/doc/config/bindings.md b/doc/config/bindings.md index d60c539a..8b06b51a 100644 --- a/doc/config/bindings.md +++ b/doc/config/bindings.md @@ -81,14 +81,14 @@ You can configure dynamic vars from `tests.edn`, these will be bound to the - Then the output should contain: ``` nil -at clojure.lang +clojure.lang ``` - And the output should not contain ``` nil -at clojure.core +clojure.core ``` @@ -120,7 +120,7 @@ at clojure.core - Then the output should contain: ``` nil -at clojure.core +clojure.core ``` @@ -131,7 +131,7 @@ at clojure.core ``` clojure #kaocha/v1 -{:bindings {kaocha.stacktrace/*stacktrace-stop-list* ["kaocha.ns"]}} +{:bindings {kaocha.stacktrace/*stacktrace-stop-list* ["kaocha.runner"]}} ``` @@ -159,7 +159,7 @@ at clojure.core - And the output should not contain ``` nil -at kaocha.ns +kaocha.runner$ ``` @@ -192,7 +192,7 @@ at kaocha.ns - Then the output should contain: ``` nil -at kaocha.runner +kaocha.runner$ ``` diff --git a/test/features/config/bindings.feature b/test/features/config/bindings.feature index e5009773..81e1f399 100644 --- a/test/features/config/bindings.feature +++ b/test/features/config/bindings.feature @@ -109,7 +109,7 @@ Feature: Configuration: Bindings """ And the output should not contain """ - kaocha.runner + kaocha.runner$ """ Scenario: Disable stacktrace shortening @@ -131,7 +131,7 @@ Feature: Configuration: Bindings When I run `bin/kaocha` Then the output should contain: """ - kaocha.runner + kaocha.runner$ """ And the output should not contain """