Skip to content

Instantly share code, notes, and snippets.

@robstoll
Last active December 29, 2020 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robstoll/dde7adb7a06e420f87a5eb9390b37ce4 to your computer and use it in GitHub Desktop.
Save robstoll/dde7adb7a06e420f87a5eb9390b37ce4 to your computer and use it in GitHub Desktop.
gradle kotlin MPP fail build if test previously failed
fun memoizeTestFile(testTask: Test) =
project.file("${project.buildDir}/test-results/memoize-previous-state-${testTask.name}.txt")
tasks.withType<Test> {
testLogging {
exceptionFormat = TestExceptionFormat.FULL
showExceptions = true
showCauses = true
showStackTraces = true
}
val testTask = this
addTestListener(object : TestListener {
override fun beforeSuite(suite: TestDescriptor) {}
override fun beforeTest(testDescriptor: TestDescriptor) {}
override fun afterTest(testDescriptor: TestDescriptor, result: TestResult) {}
override fun afterSuite(suite: TestDescriptor, result: TestResult) {
if (suite.parent == null) {
if (result.testCount == 0L) {
throw GradleException("No tests executed, most likely the discovery failed.")
}
println("Result: ${result.resultType} (${result.successfulTestCount} succeeded, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)")
memoizeTestFile(testTask).writeText(result.resultType.toString())
}
}
})
}
tasks.withType<Test>().forEach { testTask ->
val failIfTestFailedLastTime =
tasks.register("fail-if-${testTask.name}-failed-last-time") {
doLast {
if (!testTask.didWork) {
val memoizeTestFile = memoizeTestFile(testTask)
if (memoizeTestFile.exists() && memoizeTestFile.readText() == TestResult.ResultType.FAILURE.toString()) {
val allTests = tasks.getByName("allTests") as TestReport
throw GradleException(
"test failed in last run, execute clean${testTask.name} to force its execution\n" +
"See the following report for more information:\nfile://${allTests.destinationDir}/index.html"
)
}
}
}
}
testTask.finalizedBy(failIfTestFailedLastTime)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment