我想修改 cake test
以便它与 a different value for *stack-trace-depth*
一起运行。
built-in definition 很简单:
(deftask test #{compile}
"Run project tests."
"Specify which tests to run as arguments like: namespace, namespace/function, or :tag"
(run-project-tests))
理想情况下,我想用命令行参数
--depth=n
指定值,这样的效果:(binding [*stack-trace-depth* (if (*opts* :depth)
(read-string (*opts* :depth)))]
(run-project-tests))
我需要什么代码才能完成这项工作?
基于响应:将以下内容放入
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))
产生以下输出:
(测试代码是 on Gist 。)
最佳答案
(更新:抛弃了原来的答案,这似乎是一个可行的解决方案。)
我从您的 Gist 中获取了示例项目并进行了以下更改:
rm tasks.clj
project.clj
表单下方的defproject
中添加如下代码:(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)))))
test/cake_testing/core_test.clj
,内容如下:(ns cake-testing.core-test
(:use clojure.test))
(deftest correct-stack-trace-depth?
(is (= *stack-trace-depth* 5)))
此时,似乎一切正常——
cake test
输出Testing cake-testing.core-test
Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
----
Finished in 0.033200374 seconds.
此外,添加故意抛出异常的“测试”会导致打印出漂亮的短堆栈跟踪。
关于clojure - 修改 `cake test` 以控制堆栈跟踪深度 (Clojure),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4356444/