[Gradle][MPP] Implement initial MultiplatformAndroidSourceSetLayoutV2DiagnosticsTest
... to cover KT-53709
This commit is contained in:
committed by
Space
parent
d4bded85cd
commit
10878e5967
+1
-1
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.gradle.plugin.sources.android.KotlinAndroidSourceSet
|
|||||||
import org.jetbrains.kotlin.gradle.utils.SingleWarningPerBuild
|
import org.jetbrains.kotlin.gradle.utils.SingleWarningPerBuild
|
||||||
|
|
||||||
internal interface KotlinAndroidSourceSetLayoutChecker {
|
internal interface KotlinAndroidSourceSetLayoutChecker {
|
||||||
class ProjectMisconfiguredException(message: String) : Exception(message)
|
open class ProjectMisconfiguredException(message: String) : Exception(message)
|
||||||
|
|
||||||
interface DiagnosticReporter {
|
interface DiagnosticReporter {
|
||||||
fun error(diagnostic: Diagnostic): Nothing
|
fun error(diagnostic: Diagnostic): Nothing
|
||||||
|
|||||||
+5
@@ -90,4 +90,9 @@ fun Project.assertNotContainsDependencies(configurationName: String, vararg depe
|
|||||||
foundUnexpectedDependencies.isEmpty(),
|
foundUnexpectedDependencies.isEmpty(),
|
||||||
"Unexpected dependencies '$foundUnexpectedDependencies' found in configuration '$configurationName'"
|
"Unexpected dependencies '$foundUnexpectedDependencies' found in configuration '$configurationName'"
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <reified T> assertIsInstance(value: Any?): T {
|
||||||
|
if (value is T) return value
|
||||||
|
fail("Expected $value to implement ${T::class.java}")
|
||||||
}
|
}
|
||||||
+69
@@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@file:Suppress("FunctionName")
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.gradle.sources.android
|
||||||
|
|
||||||
|
import com.android.build.gradle.LibraryPlugin
|
||||||
|
import org.gradle.api.Project
|
||||||
|
import org.gradle.api.internal.project.ProjectInternal
|
||||||
|
import org.jetbrains.kotlin.gradle.*
|
||||||
|
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.sources.android.checker.MultiplatformLayoutV2AndroidStyleSourceDirUsageChecker.AndroidStyleSourceDirUsageDiagnostic
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.sources.android.findAndroidSourceSet
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.sources.android.multiplatformAndroidSourceSetLayoutV2
|
||||||
|
import org.junit.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
class MultiplatformAndroidSourceSetLayoutV2DiagnosticsTest {
|
||||||
|
|
||||||
|
private val diagnosticsReporter = TestDiagnosticsReporter()
|
||||||
|
|
||||||
|
private fun buildMinimalAndroidMultiplatformProject(): ProjectInternal = buildProjectWithMPP {
|
||||||
|
setMultiplatformAndroidSourceSetLayoutVersion(2)
|
||||||
|
plugins.apply(LibraryPlugin::class.java)
|
||||||
|
androidLibrary {
|
||||||
|
compileSdk = 31
|
||||||
|
}
|
||||||
|
kotlin {
|
||||||
|
android()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun Project.checkCreatedSourceSets(
|
||||||
|
diagnosticsReporter: TestDiagnosticsReporter = this@MultiplatformAndroidSourceSetLayoutV2DiagnosticsTest.diagnosticsReporter
|
||||||
|
) {
|
||||||
|
/* Invoke checkers on all source sets */
|
||||||
|
project.multiplatformExtension.sourceSets.forEach { kotlinSourceSet ->
|
||||||
|
val androidSourceSet = project.findAndroidSourceSet(kotlinSourceSet) ?: return@forEach
|
||||||
|
multiplatformAndroidSourceSetLayoutV2.checker.checkCreatedSourceSet(
|
||||||
|
diagnosticReporter = diagnosticsReporter,
|
||||||
|
target = project.multiplatformExtension.android(),
|
||||||
|
layout = multiplatformAndroidSourceSetLayoutV2,
|
||||||
|
kotlinSourceSet = kotlinSourceSet,
|
||||||
|
androidSourceSet = androidSourceSet
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test - KT-53709 - androidTest_kotlin in use`() {
|
||||||
|
|
||||||
|
val project = buildMinimalAndroidMultiplatformProject()
|
||||||
|
|
||||||
|
/* Ensure that the problematic androidTest/kotlin source dir is 'in use' */
|
||||||
|
val androidTestKotlinSourceDir = project.file("src/androidTest/kotlin")
|
||||||
|
androidTestKotlinSourceDir.mkdirs()
|
||||||
|
project.evaluate()
|
||||||
|
|
||||||
|
/* Invoke checkers on all source sets */
|
||||||
|
project.checkCreatedSourceSets()
|
||||||
|
|
||||||
|
val diagnostic = assertIsInstance<AndroidStyleSourceDirUsageDiagnostic>(diagnosticsReporter.assertSingleWarning())
|
||||||
|
assertEquals(androidTestKotlinSourceDir, diagnostic.androidStyleSourceDirInUse)
|
||||||
|
assertEquals(project.file("src/androidInstrumentedTest/kotlin"), diagnostic.kotlinStyleSourceDirToUse)
|
||||||
|
}
|
||||||
|
}
|
||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* 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.gradle.sources.android
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.sources.android.checker.KotlinAndroidSourceSetLayoutChecker
|
||||||
|
import kotlin.test.fail
|
||||||
|
|
||||||
|
internal class TestDiagnosticsReporter : KotlinAndroidSourceSetLayoutChecker.DiagnosticReporter {
|
||||||
|
|
||||||
|
class ErrorDiagnosticException(val diagnostic: KotlinAndroidSourceSetLayoutChecker.Diagnostic) :
|
||||||
|
KotlinAndroidSourceSetLayoutChecker.ProjectMisconfiguredException(diagnostic.message)
|
||||||
|
|
||||||
|
private val _errors = mutableListOf<KotlinAndroidSourceSetLayoutChecker.Diagnostic>()
|
||||||
|
|
||||||
|
private val _warnings = mutableListOf<KotlinAndroidSourceSetLayoutChecker.Diagnostic>()
|
||||||
|
|
||||||
|
val errors get() = _errors.toList()
|
||||||
|
|
||||||
|
val warnings get() = _warnings.toList()
|
||||||
|
|
||||||
|
override fun error(diagnostic: KotlinAndroidSourceSetLayoutChecker.Diagnostic): Nothing {
|
||||||
|
_errors.add(diagnostic)
|
||||||
|
throw ErrorDiagnosticException(diagnostic)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun warning(diagnostic: KotlinAndroidSourceSetLayoutChecker.Diagnostic) {
|
||||||
|
_warnings.add(diagnostic)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal fun TestDiagnosticsReporter.assertSingleWarning(): KotlinAndroidSourceSetLayoutChecker.Diagnostic {
|
||||||
|
if (errors.isNotEmpty()) fail("Expected just a single warning, but found errors: $errors")
|
||||||
|
if (warnings.isEmpty()) fail("Expected a single warning, but found none!")
|
||||||
|
if (warnings.size > 1) fail("Expected a single warning, but found multiple: $warnings")
|
||||||
|
return warnings.first()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user