[Gradle] Fix loosing compiler plugin options with the same plugin ID

The most common use case is to add one CompilerPluginOptions to another
while both may have options with the same plugin ID.

^KT-54160 Fixed
This commit is contained in:
Yahor Berdnikau
2023-08-21 11:31:08 +02:00
committed by Space Team
parent df55283148
commit e8905ea849
2 changed files with 60 additions and 2 deletions
@@ -54,8 +54,13 @@ class CompilerPluginOptions() : CompilerPluginConfig() {
}
private fun copyOptionsFrom(options: CompilerPluginConfig) {
options.allOptions().forEach { entry ->
optionsByPluginId[entry.key] = entry.value.toMutableList()
options.allOptions().forEach { (pluginId, pluginOptions) ->
val existingValue = optionsByPluginId[pluginId]
optionsByPluginId[pluginId] = if (existingValue != null) {
(existingValue + pluginOptions).toMutableList()
} else {
pluginOptions.toMutableList()
}
}
}
}
@@ -0,0 +1,53 @@
/*
* Copyright 2010-2023 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.tasks
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
import org.junit.Test
import kotlin.test.assertEquals
class CompilerPluginOptionsTest {
@Test
fun shouldCreateNewOneWithoutLoosingExistingOptions() {
val compilerOptions1 = CompilerPluginOptions()
compilerOptions1.addPluginArgument(EXAMPLE_PLUGIN_ID, subpluginOption1)
val compilerOptions2 = CompilerPluginOptions(compilerOptions1)
assertEquals(setOf(EXAMPLE_PLUGIN_ID), compilerOptions2.allOptions().keys)
assertEquals(listOf(subpluginOption1), compilerOptions2.allOptions()[EXAMPLE_PLUGIN_ID])
}
@Test
fun addingNewArgumentDoNotLooseExistingOptions() {
val compilerOptions = CompilerPluginOptions()
compilerOptions.addPluginArgument(EXAMPLE_PLUGIN_ID, subpluginOption1)
compilerOptions.addPluginArgument(EXAMPLE_PLUGIN_ID, subpluginOption2)
assertEquals(setOf(EXAMPLE_PLUGIN_ID), compilerOptions.allOptions().keys)
assertEquals(listOf(subpluginOption1, subpluginOption2), compilerOptions.allOptions()[EXAMPLE_PLUGIN_ID])
}
@Test
fun combiningTwoCompilerOptionsCombinesOptionsWithTheSamePluginId() {
val compilerOptions1 = CompilerPluginOptions()
compilerOptions1.addPluginArgument(EXAMPLE_PLUGIN_ID, subpluginOption1)
val compilerOptions2 = CompilerPluginOptions()
compilerOptions2.addPluginArgument(EXAMPLE_PLUGIN_ID, subpluginOption2)
val compilerOptions3 = compilerOptions1 + compilerOptions2
assertEquals(setOf(EXAMPLE_PLUGIN_ID), compilerOptions3.allOptions().keys)
assertEquals(listOf(subpluginOption1, subpluginOption2), compilerOptions3.allOptions()[EXAMPLE_PLUGIN_ID])
}
companion object {
private const val EXAMPLE_PLUGIN_ID = "com.example"
private val subpluginOption1 = SubpluginOption("option1", "value1")
private val subpluginOption2 = SubpluginOption("option2", "value2")
}
}