4

我正在做一个项目,其中运行一些测试的标准方法foo是做run foo --bar --baz --qux --quux someDirectory. 更糟糕的是,文档非常单薄,需要进行一些挖掘才能弄清楚测试是如何运行的。

出现这种情况的原因是该项目进行了一些代码生成(它生成 c++,然后编译并运行),并且测试是运行生成的代码与模型的对比,该模型也是生成的代码块,由同一个项目。从这个角度来看,你可以看到事情是如何变成这样的,但这会让运行测试变得不直观。

我希望能够使用test foo. 是否可以test foo简单地执行上述运行命令,如果可以,我该怎么做?

如果不可能,我将添加一些文档,以便项目的新手可以更轻松地解决问题。但是,我更愿意使事情与使用 sbt 的其他项目保持一致。

4

1 回答 1

4

目前最好的方法是直接实现自定义任务。有关详细信息,请参阅输入任务

// 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) }
}
于 2013-10-01T15:04:35.227 回答