[mpp, tests] Add infrastructure for *Generated KPM Core tests

This commit is contained in:
Dmitry Savvinov
2022-07-14 12:38:34 +02:00
parent 309b2f569d
commit c1e21f5b8f
9 changed files with 136 additions and 7 deletions
+23
View File
@@ -0,0 +1,23 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Generate MPP Tests" type="GradleRunConfiguration" factoryName="Gradle" folderName="Generators">
<ExternalSystemSettings>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$/libraries/tools/kotlin-project-model-tests-generator" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="" />
<option name="taskDescriptions">
<list />
</option>
<option name="taskNames">
<list>
<option value="generateKpmTests" />
</list>
</option>
<option name="vmOptions" />
</ExternalSystemSettings>
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
<DebugAllEnabled>false</DebugAllEnabled>
<method v="2" />
</configuration>
</component>
@@ -30,7 +30,8 @@ private val METHOD_GENERATORS = listOf(
)
class NewTestGeneratorImpl(
additionalMethodGenerators: List<MethodGenerator<Nothing>>
additionalMethodGenerators: List<MethodGenerator<Nothing>>,
private val generatorName: String = TEST_GENERATOR_NAME,
) : TestGenerator(METHOD_GENERATORS + additionalMethodGenerators) {
private val GENERATED_FILES = HashSet<String>()
@@ -155,7 +156,7 @@ class NewTestGeneratorImpl(
p.println("import java.io.File;")
p.println("import java.util.regex.Pattern;")
p.println()
p.println("/** This class is generated by {@link ", TEST_GENERATOR_NAME, "}. DO NOT MODIFY MANUALLY */")
p.println("/** This class is generated by {@link ", generatorName, "}. DO NOT MODIFY MANUALLY */")
p.generateSuppressAllWarnings()
@@ -10,6 +10,8 @@ pill {
variant = PillExtension.Variant.FULL
}
testsJar()
val kotlinGradlePluginTest = project(":kotlin-gradle-plugin").sourceSets.named("test").map { it.output }
dependencies {
@@ -0,0 +1,29 @@
plugins {
kotlin("jvm")
id("jps-compatible")
id("java-test-fixtures")
}
dependencies {
testImplementation(kotlinStdlib())
testApi(projectTests(":compiler:cli"))
testApi(projectTests(":generators:test-generator"))
testApi(testFixtures(project(":kotlin-project-model")))
testApi(project(":kotlin-project-model"))
testApi(projectTests(":kotlin-gradle-plugin-integration-tests"))
testCompileOnly(project(":kotlin-reflect-api"))
testImplementation(project(":kotlin-reflect"))
testImplementation(projectTests(":compiler:test-infrastructure-utils"))
testImplementation(projectTests(":compiler:test-infrastructure"))
testImplementation(projectTests(":compiler:tests-common-new"))
testImplementation(projectTests(":js:js.tests"))
testApiJUnit5()
if (Ide.IJ()) {
testCompileOnly(jpsBuildTest())
testApi(jpsBuildTest())
}
}
val generateKpmTests by generator("org.jetbrains.kotlin.kpm.GenerateKpmTestsKt") {
}
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2022 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.
*/
package org.jetbrains.kotlin.kpm
import org.jetbrains.kotlin.project.model.infra.generate.generateKpmTestCases
fun main() {
generateKpmTestCases {
// Add generated tests here
}
}
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.project.model.infra.generate
import org.jetbrains.kotlin.generators.model.AnnotationModel
import org.jetbrains.kotlin.generators.model.MethodModel
import org.jetbrains.kotlin.generators.model.TestClassModel
import org.jetbrains.kotlin.project.model.coreCases.KpmCoreCase
import org.jetbrains.kotlin.project.model.coreCases.KpmTestCaseDescriptor
import org.jetbrains.kotlin.project.model.infra.KpmTestCase
import org.jetbrains.kotlin.project.model.infra.generateTemplateCanonicalFileStructure
import org.jetbrains.kotlin.test.util.KtTestUtil
@@ -55,7 +55,7 @@ class KpmCoreCasesTestClassModel(
get() = emptyList() // Don't need to annotate with `@ExtendWith`, because we inherit [KpmCoreCasesTestRunner]
private fun doCollectMethods(): Collection<MethodModel> {
val allCoreCasesNames: Set<String> = KpmCoreCase.allCasesNames
val allCoreCasesNames: Set<String> = KpmTestCaseDescriptor.allCasesNames
val allAdditionalTestdata: Set<File> = additionalTestDataRoot.listFiles().orEmpty().toSet()
val methodModelsForCoreCasesWithExistingTestData = allAdditionalTestdata.map { testDataForCase ->
@@ -83,9 +83,9 @@ class KpmCoreCasesTestClassModel(
val methodModelsForCoreCasesWithoutTestData = coreCasesNamesWithoutTestData.map {
val expectedTestDataPath = additionalTestDataRoot.resolve(it)
val kpmCoreCase = KpmCoreCase.allCasesByNames[it]!!
val kpmTestCaseDescriptor = KpmTestCaseDescriptor.allCaseDescriptorsByNames[it]!!
println("Generating template sources testdata for uncovered KPM Core Case $it at ${additionalTestDataRoot.path}")
kpmCoreCase.case.generateTemplateCanonicalFileStructure(expectedTestDataPath)
kpmTestCaseDescriptor.generateTemplateCanonicalFileStructure(expectedTestDataPath)
KpmCoreCaseTestMethodModel(it, additionalTestDataRoot, expectedTestDataPath)
}
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2022 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.
*/
package org.jetbrains.kotlin.project.model.infra.generate
import org.jetbrains.kotlin.generators.*
import org.jetbrains.kotlin.generators.model.*
import org.jetbrains.kotlin.project.model.infra.KpmCoreCasesTestRunner
import java.io.File
fun generateKpmTestCases(
dryRun: Boolean = false,
init: TestGroupSuite.() -> Unit
) {
val suite = TestGroupSuite(DefaultTargetBackendComputer).apply {
init()
}
for (testGroup in suite.testGroups) {
for (testClass in testGroup.testClasses) {
val (changed, testSourceFilePath) = NewTestGeneratorImpl(
listOf(KpmCoreCaseTestMethodGenerator),
generatorName = "GenerateKpmTests.kt"
).generateAndSave(testClass, dryRun)
if (changed) {
InconsistencyChecker.inconsistencyChecker(dryRun).add(testSourceFilePath)
}
}
}
}
// NB: [testRunnersSourceRoot] is a root-folder against which will be resolved a usual java-like folder structure
// based on packages of T
// E.g.: testRunnersSourceRoot = "foo/bar", T == "org.jetbrains.kotlin.AbstractMyClass"
// Then the resulting file will be created at "foo/bar/org/jetbrains/kotlin"
inline fun <reified T : KpmCoreCasesTestRunner> TestGroupSuite.kpmRunnerWithSources(testRunnersSourceRoot: String, testSourcesPath: String) {
testGroup(testRunnersSourceRoot, testSourcesPath) {
testClass<T> {
testModels += KpmCoreCasesTestClassModel(File(testSourcesPath))
}
}
}
internal fun generateTestMethodsTemplateForCases(cases: Set<String>, generateTestMethodBody: (String) -> String = { "TODO()" }): String {
return buildString {
for (case in cases) {
append(
"""
fun test$case(case: KpmTestCase) {
${generateTestMethodBody(case)}
}
""".trimIndent()
)
}
}
}
@@ -99,7 +99,7 @@ private fun TestKpmFragment.canonicalSourceFolderRelative(): File {
fragmentName
)
return File(pathParts.joinToString(separator = "/"))
return File(pathParts.joinToString(separator = File.separator))
}
private val PLACEHOLDER_SOURCES_TEXT = """
+2
View File
@@ -243,6 +243,7 @@ include ":kotlin-imports-dumper-compiler-plugin",
":generators:test-generator",
":tools:kotlinp",
":kotlin-project-model",
":kotlin-project-model-tests-generator",
":kotlin-gradle-plugin-api",
":kotlin-gradle-plugin-idea",
":kotlin-gradle-plugin-idea-proto",
@@ -733,6 +734,7 @@ project(':kotlin-sam-with-receiver-compiler-plugin.cli').projectDir = "$rootDir/
project(':tools:kotlinp').projectDir = "$rootDir/libraries/tools/kotlinp" as File
project(':kotlin-project-model').projectDir = "$rootDir/libraries/tools/kotlin-project-model" as File
project(':kotlin-project-model-tests-generator').projectDir = "$rootDir/libraries/tools/kotlin-project-model-tests-generator" as File
project(':kotlin-gradle-plugin-api').projectDir = "$rootDir/libraries/tools/kotlin-gradle-plugin-api" as File
project(':kotlin-gradle-plugin-idea').projectDir = "$rootDir/libraries/tools/kotlin-gradle-plugin-idea" as File
project(':kotlin-gradle-plugin-idea-proto').projectDir = "$rootDir/libraries/tools/kotlin-gradle-plugin-idea-proto" as File