test line reporting doesn't always report test's file & line number

Description

If an Exception is thrown during test execution, the filename and line number are frequently not helpful for finding the problem. For instance, this code:

error_reporting.clj

(require '[clojure.test :refer [deftest test-var]]) (deftest foo (meta)) (test-var #'foo)

Will output an error at AFn.java:429.

ERROR in (foo) (AFn.java:429) Uncaught exception, not in assertion. expected: nil actual: clojure.lang.ArityException: Wrong number of args (0) passed to: core/meta--4144 at clojure.lang.AFn.throwArity (AFn.java:429) clojure.lang.AFn.invoke (AFn.java:28) user/fn (error_reporting.clj:4) clojure.test$test_var$fn__7670.invoke (test.clj:704) clojure.test$test_var.invoke (test.clj:704) user$eval6.invoke (error_reporting.clj:6) clojure.lang.Compiler.eval (Compiler.java:6782) ...etc

Rich's Comment 24016 on CLJ-377 says that he thinks the message should report the test file line rather than where the exception was thrown.

Approach: Filter the stacktrace class prefix clojure.lang.AFn from the top of error stacktraces.

After applying the patch, the above example outputs error_reporting.clj:4:

ERROR in (foo) (error_reporting.clj:4) Uncaught exception, not in assertion. expected: nil actual: clojure.lang.ArityException: Wrong number of args (0) passed to: core/meta--4141 at clojure.lang.AFn.throwArity (AFn.java:429) clojure.lang.AFn.invoke (AFn.java:28) user$fn__3.invokeStatic (error_reporting.clj:4) user/fn (error_reporting.clj:3) clojure.test$test_var$fn__114.invoke (test.clj:705) clojure.test$test_var.invokeStatic (test.clj:705) clojure.test$test_var.invoke (test.clj:-1) user$eval6.invokeStatic (error_reporting.clj:6) user$eval6.invoke (error_reporting.clj:-1) clojure.lang.Compiler.eval (Compiler.java:6939) ...etc

Patch: clj-1811.patch

Environment

None

Attachments

4
  • 03 Dec 2015, 03:57 PM
  • 30 Sep 2015, 09:41 PM
  • 02 Sep 2015, 11:36 PM
  • 02 Sep 2015, 11:34 PM

Activity

Show:

Alex Miller December 3, 2015 at 3:57 PM

This is not fixed by CLJ-1856, but could be if clojure.lang.AFn was filtered out of error stacktraces when finding the location. This is pretty specific - it might make sense to broaden to other calling paths too, not sure.

Attached a new patch that applies this filtering.

Michael Blume December 2, 2015 at 11:55 PM

Patch doesn't apply anymore, but maybe this has been sorted by CLJ-1856?

Ryan Fowler October 1, 2015 at 12:20 AM

While discussing an issue with my coworker James, I realized that this fix helps with shared functions calling (is). Notice how the run of this sample code reports line 7 with LINE_REPORTING_2.patch applied. This test line is generally much more useful than the shared function line.

example_2

ryans-mbp:~/oss/clojure% cat -n error_reporting.clj 1 (require '[clojure.test :refer [deftest test-var is]]) 2 3 (defn shared-code [arg] 4 (is arg)) 5 6 (deftest test-shared-code 7 (shared-code false)) 8 9 (test-var #'test-shared-code) ryans-mbp:~/oss/clojure% java -jar ~/.m2/repository/org/clojure/clojure/1.7.0/clojure-1.7.0.jar ./error_reporting.clj FAIL in (test-shared-code) (error_reporting.clj:4) expected: arg actual: false ryans-mbp:~/oss/clojure% java -jar target/clojure-1.8.0-master-SNAPSHOT.jar ./error_reporting.clj FAIL in (test-shared-code) (error_reporting.clj:7) expected: arg actual: false

Ryan Fowler September 30, 2015 at 9:53 PM

I've filled in some detail in the approach section.

I also added a new patch LINE_REPORTING_2.patch that addresses reflection warnings, restores the old arity of file-and-line and adds protection from people calling file-and-line from outside a testing context.

Ryan Fowler September 30, 2015 at 9:41 PM

Second attempt at a patch to resolve this issue. Corrects issues Alex pointed out

Details

Assignee

Reporter

Approval

Triaged

Patch

Code and Test

Priority

Affects versions

Created September 2, 2015 at 11:34 PM
Updated July 16, 2024 at 3:09 AM

Flag notifications