DryRun mode for GenerateTests is added

Relates to #KTI-17
This commit is contained in:
Vladimir Dolzhenko
2020-06-08 19:05:10 +00:00
parent cd9273028b
commit 9319c4c96e
13 changed files with 5139 additions and 5018 deletions
@@ -6,6 +6,8 @@
package org.jetbrains.kotlin.generators.tests.generator
import junit.framework.TestCase
import org.jetbrains.kotlin.generators.tests.generator.InconsistencyChecker.Companion.hasDryRunArg
import org.jetbrains.kotlin.generators.tests.generator.InconsistencyChecker.Companion.inconsistencyChecker
import org.jetbrains.kotlin.test.TargetBackend
import java.io.File
import java.util.*
@@ -16,7 +18,8 @@ class TestGroup(
val testDataRoot: String,
val testRunnerMethodName: String,
val additionalRunnerArguments: List<String> = emptyList(),
val annotations: List<AnnotationModel> = emptyList()
val annotations: List<AnnotationModel> = emptyList(),
private val dryRun: Boolean = false
) {
inline fun <reified T : TestCase> testClass(
suiteTestClassName: String = getDefaultSuiteTestClassName(T::class.java.simpleName),
@@ -34,13 +37,16 @@ class TestGroup(
annotations: List<AnnotationModel> = emptyList(),
init: TestClass.() -> Unit
) {
TestGenerator(
val testGenerator = TestGenerator(
testsRoot,
suiteTestClassName,
baseTestClassName,
TestClass(annotations).apply(init).testModels,
useJunit4
).generateAndSave()
)
if (testGenerator.generateAndSave(dryRun)) {
inconsistencyChecker(dryRun).add(testGenerator.testSourceFilePath)
}
}
inner class TestClass(val annotations: List<AnnotationModel>) {
@@ -85,14 +91,67 @@ class TestGroup(
}
}
fun testGroup(
testsRoot: String,
testDataRoot: String,
testRunnerMethodName: String = RunTestMethodModel.METHOD_NAME,
additionalRunnerArguments: List<String> = emptyList(),
init: TestGroup.() -> Unit
fun testGroupSuite(
args: Array<String>,
init: TestGroupSuite.() -> Unit
) {
TestGroup(testsRoot, testDataRoot, testRunnerMethodName, additionalRunnerArguments).init()
testGroupSuite(hasDryRunArg(args), init)
}
fun testGroupSuite(
dryRun: Boolean = false,
init: TestGroupSuite.() -> Unit
) {
TestGroupSuite(dryRun).init()
}
class TestGroupSuite(private val dryRun: Boolean) {
fun testGroup(
testsRoot: String,
testDataRoot: String,
testRunnerMethodName: String = RunTestMethodModel.METHOD_NAME,
additionalRunnerArguments: List<String> = emptyList(),
init: TestGroup.() -> Unit
) {
TestGroup(
testsRoot,
testDataRoot,
testRunnerMethodName,
additionalRunnerArguments,
dryRun = dryRun
).init()
}
}
interface InconsistencyChecker {
fun add(affectedFile: String)
val affectedFiles: List<String>
companion object {
fun hasDryRunArg(args: Array<String>) = args.any { it == "dryRun" }
fun inconsistencyChecker(dryRun: Boolean) = if (dryRun) DefaultInconsistencyChecker else EmptyInconsistencyChecker
}
}
object DefaultInconsistencyChecker : InconsistencyChecker {
private val files = mutableListOf<String>()
override fun add(affectedFile: String) {
files.add(affectedFile)
}
override val affectedFiles: List<String>
get() = files
}
object EmptyInconsistencyChecker : InconsistencyChecker {
override fun add(affectedFile: String) {
}
override val affectedFiles: List<String>
get() = emptyList()
}
fun getDefaultSuiteTestClassName(baseTestClassName: String): String {
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.generators.tests.generator
import org.jetbrains.kotlin.generators.util.GeneratorsFileUtil
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.test.TestMetadata
import org.jetbrains.kotlin.utils.Printer
import org.junit.Test
@@ -31,7 +30,7 @@ class TestGenerator(
private val baseTestClassName: String
private val testClassModels: Collection<TestClassModel>
private val useJunit4: Boolean
private val testSourceFilePath: String
internal val testSourceFilePath: String
init {
this.baseTestClassPackage = baseTestClassFqName.substringBeforeLast('.', "")
@@ -48,18 +47,33 @@ class TestGenerator(
}
}
/**
* @return true if a new file is generated
*/
@Throws(IOException::class)
fun generateAndSave() {
fun generateAndSave(dryRun: Boolean): Boolean {
val generatedCode = generate()
val testSourceFile = File(testSourceFilePath)
val changed =
GeneratorsFileUtil.isFileContentChangedIgnoringLineSeparators(testSourceFile, generatedCode)
if (!dryRun) {
GeneratorsFileUtil.writeFileIfContentChanged(testSourceFile, generatedCode, false)
}
return changed
}
private fun generate(): String {
val out = StringBuilder()
val p = Printer(out)
val year = GregorianCalendar()[Calendar.YEAR]
p.println(
"""/*
| * Copyright 2010-$year JetBrains s.r.o. and Kotlin Programming Language contributors.
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
| */
|""".trimMargin()
| * Copyright 2010-$year JetBrains s.r.o. and Kotlin Programming Language contributors.
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
| */
|""".trimMargin()
)
p.println("package ", suiteClassPackage, ";")
p.println()
@@ -126,9 +140,7 @@ class TestGenerator(
}
generateTestClass(p, model, false)
val testSourceFile = File(testSourceFilePath)
GeneratorsFileUtil.writeFileIfContentChanged(testSourceFile, out.toString(), false)
return out.toString()
}
private fun generateTestClass(p: Printer, testClassModel: TestClassModel, isStatic: Boolean) {