2010-12-04 85 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)))