Implement 'CommonizerTarget.fileName' and limit fileName length

^KT-46856 Fixed
This commit is contained in:
sebastian.sellmair
2021-05-21 12:03:44 +02:00
committed by Space
parent 0749443f7e
commit 14eca72913
3 changed files with 95 additions and 16 deletions
@@ -8,6 +8,8 @@ package org.jetbrains.kotlin.commonizer
import org.jetbrains.kotlin.konan.library.KONAN_DISTRIBUTION_COMMON_LIBS_DIR
import org.jetbrains.kotlin.konan.library.KONAN_DISTRIBUTION_PLATFORM_LIBS_DIR
import java.io.File
import java.security.MessageDigest
import java.util.*
public fun interface CommonizerOutputLayout {
@@ -24,7 +26,27 @@ public object NativeDistributionCommonizerOutputLayout : CommonizerOutputLayout
}
public object HierarchicalCommonizerOutputLayout : CommonizerOutputLayout {
internal const val maxFileNameLength = 150
override fun getTargetDirectory(root: File, target: CommonizerTarget): File {
return root.resolve(target.identityString)
return root.resolve(target.fileName)
}
public val CommonizerTarget.fileName: String
get() {
val identityString = identityString
return if (identityString.length <= maxFileNameLength) identityString
else {
val hashSuffix = "[--$identityStringHash]"
return identityString.take(maxFileNameLength - hashSuffix.length) + hashSuffix
}
}
private val CommonizerTarget.identityStringHash: String
get() {
val sha = MessageDigest.getInstance("SHA-1")
val base64 = Base64.getUrlEncoder()
return base64.encode(sha.digest(identityString.encodeToByteArray())).decodeToString()
}
}
@@ -0,0 +1,67 @@
/*
* Copyright 2010-2021 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.commonizer
import org.jetbrains.kotlin.commonizer.HierarchicalCommonizerOutputLayout.fileName
import org.jetbrains.kotlin.commonizer.HierarchicalCommonizerOutputLayout.maxFileNameLength
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class CommonizerTargetFileNameTest {
private val longCommonizerTarget = parseCommonizerTarget(
buildString {
append("(")
append(
sequence {
var i = 0
while (true) {
yield(i.toString())
i++
}
}.take(maxFileNameLength).joinToString(", ")
)
append(")")
}
)
@get:Rule
val temporaryFolder = TemporaryFolder()
@Test
fun `small targets will use identityString`() {
val target = parseCommonizerTarget("((a, b), c)")
assertEquals(target.identityString, target.fileName)
}
@OptIn(ExperimentalStdlibApi::class)
@Test
fun `longCommonizerTarget respect maximum fileName length`() {
assertTrue(
longCommonizerTarget.identityString.length > maxFileNameLength,
"Expected test target's identityString to exceed maxFileNameLength"
)
assertEquals(
longCommonizerTarget.fileName.length, maxFileNameLength,
"Expected test target's fileName to be exactly match the maximum"
)
}
@Test
fun `longCommonizerTarget fileName can create new file`() {
val longCommonizerTargetFile = temporaryFolder.root.resolve(longCommonizerTarget.fileName)
assertTrue(longCommonizerTargetFile.createNewFile(), "Expected being able to create file $longCommonizerTargetFile")
longCommonizerTargetFile.writeText(longCommonizerTarget.identityString)
assertEquals(
longCommonizerTarget, parseCommonizerTarget(longCommonizerTargetFile.readText()),
"Expected being able to read and write to $longCommonizerTargetFile"
)
}
}