build.gradle:如何将Groovy-ish自定义任务转换为Kotlin-ish?

nzkunb0c  于 2023-08-02  发布在  Kotlin
关注(0)|答案(1)|浏览(107)

我通过Groovy编写了这样一个自定义任务:

task systemTest(type: Test, description: 'Runs the system tests', group: 'Verification'){
    exclude "**/**Smoke.class"
    exclude "**/**Test.class"
    exclude "**/**IT.class"
    include "**/**ST.class"

    useJUnitPlatform()
    testLogging {
        exceptionFormat = 'full'
        events "passed", "skipped", "failed", "standardOut", "standardError"
        showStandardStreams = true
        showCauses true
        showExceptions true
        showStackTraces true
    }
    afterSuite { desc, result ->
        if (!desc.parent) {
            def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
            def startItem = '|  ', endItem = '  |'
            def repeatLength = startItem.length() + output.length() + endItem.length()
            println('\n\n\n' + ('-' * repeatLength) + "\n$startItem $output $endItem\n" + ('-' * repeatLength) + '\n\n\n')
        }
    }
    systemTest.finalizedBy('allureReport')
}

字符串
尝试通过工具转换此代码段,例如this onethis one,然后手动重构脚本:

val systemTest by tasks.registering(Test::class) {
    description = "Runs the system tests"
    group = "Verification"

    exclude("**/**Smoke.class")
    exclude("**/**Test.class")
    exclude("**/**IT.class")
    include("**/**ST.class")

    useJUnitPlatform()
    testLogging {
        exceptionFormat = "full"
        events = setOf("passed", "skipped", "failed", "standardOut", "standardError")
        showStandardStreams = true
        showCauses = true
        showExceptions = true
        showStackTraces = true
    }
    afterSuite { desc, result ->
        if (!desc.parent) {
            val output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
            val startItem = "|  "
            val endItem = "  |"
            val repeatLength = startItem.length() + output.length() + endItem.length()
            println("\n\n\n" + ("-" * repeatLength) + "\n$startItem $output $endItem\n" + ("-" * repeatLength) + "\n\n\n")
        }
    }
    systemTest.finalizedBy("allureReport")
}


但是在./gradlew systemTest上有很多错误:

* What went wrong:
Script compilation errors:

  Line 44:         exceptionFormat = "full"
                                     ^ Type mismatch: inferred type is String but TestExceptionFormat! was expected

  Line 45:         events = setOf("passed", "skipped", "failed", "standardOut", "standardError")
                            ^ Type mismatch: inferred type is String but TestLogEvent! was expected

  Line 45:         events = setOf("passed", "skipped", "failed", "standardOut", "standardError")
                            ^ Type mismatch: inferred type is Set<String> but (Mutable)Set<TestLogEvent!>! was expected

  Line 51:     afterSuite { desc, result ->
                          ^ Type mismatch: inferred type is ([ERROR : <Unknown lambda parameter type>], [ERROR : <Unknown lambda parameter type>]) -> Unit but Closure<(raw) Any!>! was expected

  Line 51:     afterSuite { desc, result ->
                            ^ Cannot infer a type for this parameter. Please specify it explicitly.

  Line 51:     afterSuite { desc, result ->
                                  ^ Cannot infer a type for this parameter. Please specify it explicitly.

  Line 56:             val repeatLength = startItem.length() + output.length() + endItem.length()
                                                    ^ Expression 'length' of type 'Int' cannot be invoked as a function. The function 'invoke()' is not found

  Line 56:             val repeatLength = startItem.length() + output.length() + endItem.length()
                                                                      ^ Expression 'length' of type 'Int' cannot be invoked as a function. The function 'invoke()' is not found

  Line 56:             val repeatLength = startItem.length() + output.length() + endItem.length()
                                                                                         ^ Expression 'length' of type 'Int' cannot be invoked as a function. The function 'invoke()' is not found

  Line 57:             println("\n\n\n" + ("-" * repeatLength) + "\n$startItem $output $endItem\n" + ("-" * repeatLength) + "\n\n\n")
                                               ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
                                                   public inline operator fun BigDecimal.times(other: BigDecimal): BigDecimal defined in kotlin
                                                   public inline operator fun BigInteger.times(other: BigInteger): BigInteger defined in kotlin

  Line 57:             println("\n\n\n" + ("-" * repeatLength) + "\n$startItem $output $endItem\n" + ("-" * repeatLength) + "\n\n\n")
                                                                                                          ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
                                                                                                              public inline operator fun BigDecimal.times(other: BigDecimal): BigDecimal defined in kotlin
                                                                                                              public inline operator fun BigInteger.times(other: BigInteger): BigInteger defined in kotlin

  Line 60:     systemTest.finalizedBy("allureReport")
               ^ Type checking has run into a recursive problem. Easiest workaround: specify types of your declarations explicitly

12 errors


java -version

java -version
openjdk version "11.0.13" 2021-10-19 LTS
OpenJDK Runtime Environment Corretto-11.0.13.8.1 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.13.8.1 (build 11.0.13+8-LTS, mixed mode)


在构建脚本中使用的插件:

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.7.10"
    id("java")
    id("io.qameta.allure") version "2.11.2"
}


任何帮助指出我做错了什么将是高度赞赏。

pkwftd7m

pkwftd7m1#

我相信这就是你要找的

val systemTest by tasks.registering(Test::class) {
    description = "Runs the system tests"
    group = "Verification"

    exclude("**/**Smoke.class")
    exclude("**/**Test.class")
    exclude("**/**IT.class")
    include("**/**ST.class")

    useJUnitPlatform()
    testLogging {
      exceptionFormat = TestExceptionFormat.FULL
      events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED, TestLogEvent.STANDARD_OUT, TestLogEvent.STANDARD_ERROR)
      showStandardStreams = true
      showCauses = true
      showExceptions = true
      showStackTraces = true
    }
    afterSuite(KotlinClosure2<TestDescriptor, TestResult, Any>({ desc, result ->
      if (desc.parent == null) {
        val output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
        val startItem = "|  "
        val endItem = "  |"
        val repeatLength = startItem.length + output.length + endItem.length
        println("\n\n\n" + ("-".repeat(repeatLength)) + "\n$startItem $output $endItem\n" + ("-".repeat(repeatLength)) + "\n\n\n")
      }
    }))
    finalizedBy(tasks.named("allureReport"))
  }

字符串
我也在转gradle.kts。有帮助的是再次启动build.gradle.kts文件,似乎IntelliJ在同步到有效文件(即使它是空的)之前不会给予语法突出显示。我还搜索了GitHub,找到了afterSuite等位。

相关问题