Modifying `cake test` to control stack trace depth (Clojure)
I'd like to modify cake test
so that it operates with a different value for *stack-trace-depth*
.
The built-in definition is simply:
(deftask test #{compile}
"Run project tests."
"Specify which tests to run as arguments like: namespace, namespace/function, or :tag"
(run-project-tests))
Ideally I'd like to specify the value with the command-line argument --depth=n
, something to this effect:
(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 responses: Putting the following in tasks.clj
(undeftask test)
(deftask test #{compile}
(.bindRoot #'*stack-trace-depth* 5)
(println "Defining task: *stack-trace-开发者_Go百科depth* is" *stack-trace-depth* "in" (Thread/currentThread))
(run-project-tests))
produces the following output:
Loading
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 is on Gist.)
(Update: threw out the original answer, here's what seems to be a working solution.)
I took the sample project from your Gist and made the following changes:
rm tasks.clj
Added the following code to
project.clj
below thedefproject
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 file,
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)))
At this point, everything seems to work -- cake test
outputs
Testing cake-testing.core-test
Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
----
Finished in 0.033200374 seconds.
Also, adding in a "test" which deliberately throws an exception results in a nice, short stack trace being printed.
Was able to put this in a task.clj file at the same level as a project.clj file and when I run cake test --depth=5
I can see the "Running tests with stack-trace-depth = 5" being printed. 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)))
精彩评论