2010-12-04 67 views
4

我想修改cake test以便它可以使用a different value for *stack-trace-depth*修改`蛋糕测试`来控制堆栈跟踪深度(Clojure)

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)) 

下面产生以下输出:

加载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. 

(测试代码为on Gist

回答

3

(更新:抛弃了原来的答案,这里是什么似乎是一个有效的解决方案)

我把示例项目从你的Gist做出如下改变:

  1. rm tasks.clj

  2. 下面的代码添加到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))))) 
    
  3. 创建一个新的文件,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. 

此外,在“测试”中添加故意引发异常的结果会打印出一个很好的短堆栈跟踪。

+0

谢谢,但似乎测试仍然没有看到更新的值。我已经为该问题添加了一些测试输出。 – 2010-12-08 16:37:58

+0

上面的新版本(在`test`任务的自定义版本中内嵌'cake.tasks.test/run-project-tests`的`* stack-trace-depth *`设置版本)似乎工作正常为了我。现在是时候挖掘Cake来学习它为什么会这样做了,因为我似乎还没有对代码库进行研究。尽管如此,它看起来很酷。感谢让我感兴趣! :-) – 2010-12-13 06:09:30

2

能够把这个在task.clj文件在同一水平作为project.clj文件,当我运行cake test --depth=5我可以看到正在打印的“ = 5 堆栈跟踪深度运行测试” 。希望这可以帮助。

(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)))