2013-09-28 32 views
4

我正在開發一個項目,其中運行一些測試的標準方法foo就是要做run foo --bar --baz --qux --quux someDirectory。更糟糕的是,文檔非常薄,並且需要仔細研究以確定測試是如何運行的。你如何使用某些選項使sbt`test`等同於運行命令?

這是這種情況的原因是該項目做了一些代碼生成(它生成C++,然後編譯和運行),測試是運行生成的代碼與模型,也是一個生成的blob由同一個項目生成的代碼。從這個角度來看,你可以看到事情是這樣發生的,但它使運行測試不直觀。

我希望能夠與test foo一起運行測試。是否可以使test foo只執行上述運行命令,如果是這樣,我該怎麼做?

如果這是不可能的,我會添加一些文檔,以便項目的新手可以更容易地解決問題。但是,我傾向於使其他與使用sbt的項目保持一致。

+0

是對'測試foo'有意擴展爲'跑富--bar ...'或者是隻是一個速記,你仍然預計全'測試富--bar ...'? –

+0

前者 - 默認情況下,我想擴展到完整列表。但是,指定選項以允許「高級」用戶選擇除默認值之外的其他選項將會很好。 – dan

回答

4

當前執行此操作的最佳方法是直接實施自定義任務。詳情請參閱Input Tasks

// A custom task is required because `test` doesn't accept input. 
lazy val customTest = inputKey[Unit]("custom test") 

// This custom parser accepts a space separated list of arguments and then appends 
// the fixed arguments to them. To do further parsing based on the user-specified 
// arguments, use `flatMap` and return the next Parser. 
lazy val testParser = 
    Def.spaceDelimited().map((explicitArgs: Seq[String]) => 
     explicitArgs ++ Seq("--bar", "--baz", "--qux", "--quux", "someDirectory") 
    ) 

customTest := { 
    // the result of parsing 
    val args = testParser.parsed 
    // auto-detected main class: can replace with literal string 
    val main = (mainClass in Compile).value getOrElse error("No main class detected.") 
    // main classpath, including compiled classes 
    val classpath = (fullClasspath in Compile).value.files 
    // provides Scala code execution 
    val scalaRun = (runner in (Compile, run)).value 
    val result = scalaRun.run(main, classpath, args, streams.value.log) 
    // handle any error 
    result foreach { errorMsg => sys.error(errorMsg) } 
} 
相關問題