[Build, IGS] Ignore non-existing keys during setup file parsing

#KTI-1223 In Progress
This commit is contained in:
Alexander.Likhachev
2023-05-10 19:28:31 +02:00
committed by Space Team
parent ea38cfebf2
commit cf03863b65
3 changed files with 32 additions and 8 deletions
@@ -18,6 +18,10 @@ internal data class SetupFile(
val properties: Map<String, String>,
)
private val json = Json { ignoreUnknownKeys = true }
// can't use decodeFromStream: https://github.com/Kotlin/kotlinx.serialization/issues/2218
internal fun parseSetupFile(inputStream: InputStream): SetupFile =
Json.decodeFromString(BufferedReader(InputStreamReader(inputStream)).lines().asSequence().joinToString("\n"))
json.decodeFromString(
BufferedReader(InputStreamReader(inputStream)).lines().asSequence().joinToString("\n")
)
@@ -8,15 +8,11 @@ package org.jetbrains.kotlin.build
import kotlin.test.*
class SetupFileParseTest {
private fun openPropertiesJsonStream() =
SetupFileParseTest::class.java.classLoader.getResourceAsStream("properties.json")
private fun openPropertiesJsonStream(name: String) =
SetupFileParseTest::class.java.classLoader.getResourceAsStream("$name.json")
?: error("No properties.json file found in test resources")
@Test
fun testSimpleParsing() {
val setupFile = openPropertiesJsonStream().use {
parseSetupFile(it)
}
private fun assertSampleSetupFileIsParsedCorrectly(setupFile: SetupFile) {
assertEquals(
mapOf(
"newProperty1" to "someValue",
@@ -26,4 +22,20 @@ class SetupFileParseTest {
setupFile.properties
)
}
@Test
fun testSimpleParsing() {
val setupFile = openPropertiesJsonStream("properties").use {
parseSetupFile(it)
}
assertSampleSetupFileIsParsedCorrectly(setupFile)
}
@Test
fun testUnknownFieldsDoNotBreakParsing() {
val setupFile = openPropertiesJsonStream("properties-with-unknown-fields").use {
parseSetupFile(it)
}
assertSampleSetupFileIsParsedCorrectly(setupFile)
}
}
@@ -0,0 +1,8 @@
{
"properties": {
"newProperty1": "someValue",
"newProperty2": "someOtherValue",
"alreadySetProperty": "newValue"
},
"unknownField": "someValue"
}