Fix casting of extraProperties in PropertiesBuildService

`project.extraProperties` may contain values that are not `String`s. To
avoid failing on those values, we should cast the value with
`as? String` instead of `as String?`.

Test: New PropertiesBuildServiceTest
^KT-62496 Fixed
This commit is contained in:
Hung Nguyen
2023-10-20 15:27:32 +01:00
committed by Space Cloud
parent 3941b05909
commit 19104f73bf
2 changed files with 46 additions and 1 deletions
@@ -75,7 +75,7 @@ private class PropertiesManager(private val project: Project, private val localP
}
project.provider {
// FIXME(KT-62684): We currently don't memoize extraProperties as they may still change, we'll fix this later.
project.extraProperties.getOrNull(propertyName) as String?
project.extraProperties.getOrNull(propertyName) as? String
?: valueFromGradleAndLocalProperties.call()
}
}
@@ -0,0 +1,45 @@
/*
* 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.unitTests
import org.jetbrains.kotlin.gradle.plugin.PropertiesBuildService
import org.jetbrains.kotlin.gradle.plugin.extraProperties
import org.jetbrains.kotlin.gradle.util.buildProject
import org.jetbrains.kotlin.gradle.util.registerMinimalVariantImplementationFactoriesForTests
import org.junit.Assert.assertEquals
import org.junit.Test
class PropertiesBuildServiceTest {
@Test
fun `testPrecedenceOrder`() {
val project = buildProject()
project.extraProperties.apply {
set("a", "extra")
set("b", "extra")
set("x", 1) // Test the case where extra property is not a String
}
// TODO: Find a way to test Gradle properties (it seems that more setup is needed for Gradle to pick up the contents of
// `gradle.properties` in this test).
project.projectDir.resolve("local.properties").writeText(
"""
a = local
c = local
x = "local"
""".trimIndent()
)
project.gradle.registerMinimalVariantImplementationFactoriesForTests()
val properties = PropertiesBuildService.registerIfAbsent(project).get()
assertEquals("extra", properties.get("a", project))
assertEquals("extra", properties.get("b", project))
assertEquals("local", properties.get("c", project))
assertEquals(null, properties.get("d", project))
assertEquals("\"local\"", properties.get("x", project))
}
}