【发布时间】:2014-09-23 14:42:57
【问题描述】:
我有一个目录,我必须在其中放置一些结果,但用户可以配置它 如果该目录不存在,我必须创建它。但现在我必须为该代码编写测试。
getOutputPath()是getter方法
@Optional
def outputPath = new File('build/sampleDir')
@TaskAction
def generate() {
if (!getOutputPath().exists()) {
project.mkdir(getOutputPath())
}
创建是在 Action 闭包中,所以这是在执行阶段执行的,但如果我尝试:
...
def 'change default outputPath dir'() {
when:
someTask.outputPath = new File(project.rootProject.buildDir, "newPath")
then:
....
expect:
project.afterEvaluate {
generateQRFromTextTask.getOutputQRPath().exists() == true
}
}
令我惊讶的是,这段代码也通过了相同的方式,但使用了 project.gradle.buildFinished {。 没有它们它会失败。为什么测试通过 afterEvaluate ?
什么是最好的方法来做这个检查(目录是否存在?在 constructor 还是?
【问题讨论】:
标签: gradle execution mkdir spock