[Native][tests] Move golden data outside of Gradle build file

This commit is contained in:
Dmitriy Dolovov
2021-08-27 22:56:40 +03:00
parent c2e2d3a43e
commit 1a863106e6
488 changed files with 2349 additions and 718 deletions
@@ -10,9 +10,7 @@ import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.*
import org.gradle.language.base.plugins.LifecycleBasePlugin
import org.gradle.process.ExecSpec
import org.jetbrains.kotlin.konan.exec.Command
@@ -222,18 +220,34 @@ open class KonanLocalTest : KonanTest() {
var expectedFail = false
/**
* Used to validate output as a gold value.
* Used to validate output against the golden data.
*/
@Input
@Optional
var goldValue: String? = null
var useGoldenData: Boolean = false
@get:InputFile
open val goldenDataFile: File
get() {
val sourceFile = project.file(source)
return sourceFile.parentFile.resolve(sourceFile.nameWithoutExtension + ".out")
}
private val goldenData: String?
get() = if (useGoldenData) {
check(goldenDataFile.isFile) { "Task $name. Golden data file does not exist: $goldenDataFile" }
goldenDataFile.readText(Charsets.UTF_8)
} else
null
/**
* Checks test's output against gold value and returns true if the output matches the expectation.
*/
@Input
@Optional
var outputChecker: (String) -> Boolean = { str -> goldValue == null || goldValue == str }
var outputChecker: (String) -> Boolean = { output ->
if (useGoldenData) goldenData == output else true
}
/**
* Input test data to be passed to process' stdin.
@@ -307,23 +321,21 @@ open class KonanLocalTest : KonanTest() {
println("Expected failure. $message")
}
val result = stdOut + stdErr
val goldValueMismatch = !outputChecker(result.replace(System.lineSeparator(), "\n"))
if (goldValueMismatch) {
val message = if (goldValue != null)
"Expected output: $goldValue, actual output: $result"
else
"Actual output doesn't match with output checker: $result"
val output = stdOut + stdErr
val outputMismatch = !outputChecker(output.replace(System.lineSeparator(), "\n"))
if (outputMismatch) {
val message = goldenData?.let { goldenData -> "Expected output: $goldenData, actual output: $output" }
?: "Actual output doesn't match with output checker: $output"
check(expectedFail) { "Test failed. $message" }
println("Expected failure. $message")
}
check((exitCodeMismatch || goldValueMismatch) || !expectedFail) {
check((exitCodeMismatch || outputMismatch) || !expectedFail) {
"""
|Unexpected pass:
| * exit code mismatch: $exitCodeMismatch
| * gold value mismatch: $goldValueMismatch
| * gold value mismatch: $outputMismatch
| * expected fail: $expectedFail
""".trimMargin()
}
@@ -463,6 +475,13 @@ open class KonanDynamicTest : KonanStandaloneTest() {
@Optional
var interop: String? = null
override val goldenDataFile: File
get() {
val sourceFile = project.file(source)
val cSourceFile = File(cSource)
return sourceFile.parentFile.resolve(sourceFile.nameWithoutExtension + "-" + cSourceFile.nameWithoutExtension + ".out")
}
// Replace testlib_api.h and all occurrences of the testlib with the actual name of the test
private fun processCSource(): String {
val sourceFile = File(cSource)