Changing the "cake test" to control the depth of the stack trace (Clojure)

I would like to modify the cake test to work with a different value for *stack-trace-depth* .

the inline definition is simple:

 (deftask test #{compile} "Run project tests." "Specify which tests to run as arguments like: namespace, namespace/function, or :tag" (run-project-tests)) 

Ideally, I would like to specify a value with a command line argument --depth=n , something like that:

 (binding [*stack-trace-depth* (if (*opts* :depth) (read-string (*opts* :depth)))] (run-project-tests)) 

What code do I need to make this work?


Based on the answers: Put the following in tasks.clj

 (undeftask test) (deftask test #{compile} (.bindRoot #'*stack-trace-depth* 5) (println "Defining task: *stack-trace-depth* is" *stack-trace-depth* "in" (Thread/currentThread)) (run-project-tests)) 

outputs the following result:

Download test/cake_test/core.clj :

  Loading tests: *stack-trace-depth* is nil in #<Thread[thread-13,5,main]> 

$ cake test

  Defining task: *stack-trace-depth* is 5 in #<Thread[Thread-18,5,main]> In test: *stack-trace-depth* is nil in #<Thread[Thread-16,5,main]> Testing cake-testing.core FAIL in (test-stack-trace-depth) (core.clj:8) expected: (= *stack-trace-depth* 5) actual: (not (= nil 5)) Ran 1 tests containing 1 assertions. 1 failures, 0 errors. ---- Finished in 0.011865 seconds. 

(Tested code in Gist .)

+4
source share
2 answers

(Update: threw out the original answer, this is what seems like a working solution.)

I took a sample project from your Gist and made the following changes:

  • rm tasks.clj

  • The following code has been added to project.clj below the defproject form:

     (use '[bake.find-namespaces :only [find-namespaces-in-dir]] '[cake.tasks.test :only [test-opts]]) (undeftask test) (deftask test #{compile} (bake (:use bake.test [bake.core :only [with-context]] [clojure.test :only [*stack-trace-depth*]]) [namespaces (find-namespaces-in-dir (java.io.File. "test")) opts (test-opts)] (with-context :test (binding [*stack-trace-depth* 5] (run-project-tests namespaces opts))))) 
  • Created a new test/cake_testing/core_test.clj with the following contents:

     (ns cake-testing.core-test (:use clojure.test)) (deftest correct-stack-trace-depth? (is (= *stack-trace-depth* 5))) 

Everything works at this point - cake test outputs

 Testing cake-testing.core-test Ran 1 tests containing 1 assertions. 0 failures, 0 errors. ---- Finished in 0.033200374 seconds. 

In addition, adding to the β€œtest” that deliberately throws an exception results in a nice short stack trace being printed.

+3
source

You could put this in the task.clj file at the same level as the project.clj file, and when I run cake test --depth=5 , I can see how it prints "Running tests with stack trace-depth = 5 " Hope this helps.

 (ns tasks (:use cake cake.core cake.tasks.test [clojure.test :only [*stack-trace-depth*]])) (undeftask test) (deftask test #{compile} "Run tests" (binding [*stack-trace-depth* (if (*opts* :depth) (read-string (first (*opts* :depth))))] (println "Running tests with *stack-trace-depth* = " *stack-trace-depth*) (run-project-tests))) 
+2
source

All Articles