[Gradle][KT-53342] Implement simple SingleTargetAndroidSourceSetLayoutTest

This commit is contained in:
sebastian.sellmair
2022-08-02 09:26:25 +02:00
committed by Space
parent f45a073dc0
commit e307b96671
2 changed files with 106 additions and 0 deletions
@@ -0,0 +1,73 @@
/*
* 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.BaseExtension
import com.android.build.gradle.LibraryPlugin
import org.gradle.api.internal.project.ProjectInternal
import org.gradle.testfixtures.ProjectBuilder
import org.jetbrains.kotlin.gradle.addBuildEventsListenerRegistryMock
import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.utils.androidExtension
import org.junit.Test
import kotlin.test.BeforeTest
import kotlin.test.assertEquals
import kotlin.test.assertSame
class SingleTargetAndroidSourceSetLayoutTest {
private val project = ProjectBuilder.builder().build() as ProjectInternal
private val android: BaseExtension = run {
addBuildEventsListenerRegistryMock(project)
project.plugins.apply(LibraryPlugin::class.java)
project.androidExtension
}
@BeforeTest
fun setup() {
project.plugins.apply(KotlinAndroidPluginWrapper::class.java)
android.compileSdkVersion(31)
}
@Test
fun `test - default configuration - AndroidSourceSet has associated KotlinSourceSet`() {
android.sourceSets.all { androidSourceSet -> project.assertSingleKotlinSourceSet(androidSourceSet) }
project.evaluate()
}
@Test
@Suppress("deprecation")
fun `test - default configuration - AndroidSourceSet has KotlinSourceSet as convention`() {
android.sourceSets.all { androidSourceSet ->
assertSame(
project.assertSingleKotlinSourceSet(androidSourceSet),
(androidSourceSet as org.gradle.api.internal.HasConvention).convention.plugins["kotlin"] as? KotlinSourceSet,
"Expected Convention 'kotlin' on AndroidSourceSet: ${androidSourceSet.name}"
)
}
project.evaluate()
}
@Test
fun `test - with flavors - AndroidSourceSet has associated KotlinSourceSet`() {
android.flavorDimensions("price", "market")
android.productFlavors {
it.create("free").dimension = "price"
it.create("paid").dimension = "price"
it.create("german").dimension = "market"
it.create("usa").dimension = "market"
}
android.sourceSets.all { androidSourceSet ->
val kotlinSourceSet = project.assertSingleKotlinSourceSet(androidSourceSet)
assertEquals(androidSourceSet.name, kotlinSourceSet.name)
}
}
}
@@ -0,0 +1,33 @@
/*
* 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("DEPRECATION")
package org.jetbrains.kotlin.gradle.sources.android
import com.android.build.gradle.api.AndroidSourceSet
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.sources.android.androidSourceSetInfoOrNull
import kotlin.test.fail
fun Project.assertSingleKotlinSourceSet(androidSourceSet: AndroidSourceSet): KotlinSourceSet {
val kotlinSourceSets = kotlinExtension.sourceSets
.filter { kotlinSourceSet -> kotlinSourceSet.androidSourceSetInfoOrNull?.androidSourceSetName == androidSourceSet.name }
if (kotlinSourceSets.isEmpty()) {
fail("Missing KotlinSourceSet for AndroidSourceSet: ${androidSourceSet.name}")
}
if (kotlinSourceSets.size > 1) {
fail(
"More than one KotlinSourceSet associated with AndroidSourceSet: ${androidSourceSet.name}. " +
"KotlinSourceSets: ${kotlinSourceSets.map { it.name }}"
)
}
return kotlinSourceSets.single()
}