【问题标题】:Test existance of directory in Gradle?在 Gradle 中测试目录是否存在?
【发布时间】: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


    【解决方案1】:

    该属性应使用@OutputDirectory 注释,在这种情况下将自动创建目录。

    PS:根据您测试的具体性质,afterEvaluate 可能永远不会被触发。

    PPS:具有默认值的属性不需要使用@Optional 进行注释。

    【讨论】:

    • 谢谢彼得的回答和建议。
    猜你喜欢
    • 2014-01-12
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-01
    • 2018-02-19
    • 2011-11-22
    相关资源
    最近更新 更多