Minor: cleanup warnings in codebase-tests

This commit is contained in:
Nikolay Krasko
2023-03-15 11:19:38 +01:00
committed by Space Team
parent c1a1986344
commit 3eeb867f56
5 changed files with 34 additions and 29 deletions
+1
View File
@@ -2,6 +2,7 @@
<dictionary name="Nikolay.Krasko"> <dictionary name="Nikolay.Krasko">
<words> <words>
<w>accessors</w> <w>accessors</w>
<w>codeowners</w>
<w>coroutines</w> <w>coroutines</w>
<w>crossinline</w> <w>crossinline</w>
<w>fqname</w> <w>fqname</w>
@@ -17,7 +17,8 @@ class CodeConformanceTest : TestCase() {
companion object { companion object {
private val JAVA_FILE_PATTERN = Pattern.compile(".+\\.java") private val JAVA_FILE_PATTERN = Pattern.compile(".+\\.java")
private val SOURCES_FILE_PATTERN = Pattern.compile(".+\\.(java|kt|js)") private val SOURCES_FILE_PATTERN = Pattern.compile(".+\\.(java|kt|js)")
private const val MAX_STEPS_COUNT = 100
@Suppress("SpellCheckingInspection")
private val nonSourcesMatcher = FileMatcher( private val nonSourcesMatcher = FileMatcher(
File("."), File("."),
listOf( listOf(
@@ -74,6 +75,7 @@ class CodeConformanceTest : TestCase() {
) )
) )
@Suppress("SpellCheckingInspection")
private val COPYRIGHT_EXCLUDED_FILES_AND_DIRS_MATCHER = FileMatcher( private val COPYRIGHT_EXCLUDED_FILES_AND_DIRS_MATCHER = FileMatcher(
File("."), File("."),
listOf( listOf(
@@ -162,7 +164,7 @@ class CodeConformanceTest : TestCase() {
val atAuthorPattern = Pattern.compile("/\\*.+@author.+\\*/", Pattern.DOTALL) val atAuthorPattern = Pattern.compile("/\\*.+@author.+\\*/", Pattern.DOTALL)
val tests = listOf( @Suppress("SpellCheckingInspection") val tests = listOf(
FileTestCase( FileTestCase(
"%d source files contain @author javadoc tag.\nPlease remove them or exclude in this test:\n%s" "%d source files contain @author javadoc tag.\nPlease remove them or exclude in this test:\n%s"
) { _, source -> ) { _, source ->
@@ -394,7 +396,7 @@ class CodeConformanceTest : TestCase() {
) )
) )
data class RepoOccurance(val repo: String, val file: File) data class RepoOccurrence(val repo: String, val file: File)
data class RepoOccurrences(val repo: String, val files: Collection<File>) data class RepoOccurrences(val repo: String, val files: Collection<File>)
val extensionsPattern = Pattern.compile(".+\\.(java|kt|gradle|kts|xml)(\\.\\w+)?") val extensionsPattern = Pattern.compile(".+\\.(java|kt|gradle|kts|xml)(\\.\\w+)?")
@@ -405,12 +407,12 @@ class CodeConformanceTest : TestCase() {
} }
if (checkers.isNotEmpty()) { if (checkers.isNotEmpty()) {
val occurrences = ArrayList<RepoOccurance>() val occurrences = ArrayList<RepoOccurrence>()
file.useLines { lines -> file.useLines { lines ->
for (line in lines) { for (line in lines) {
for (checker in checkers) { for (checker in checkers) {
if (line.contains(checker.repo) && (checker.exclude == null || !line.contains(checker.exclude))) { if (line.contains(checker.repo) && (checker.exclude == null || !line.contains(checker.exclude))) {
occurrences.add(RepoOccurance(checker.repo, file)) occurrences.add(RepoOccurrence(checker.repo, file))
} }
} }
} }
@@ -17,7 +17,7 @@ import org.junit.Test
import java.io.File import java.io.File
class DepsVerificationMetadataTest { class DependenciesVerificationMetadataTest {
@JacksonXmlRootElement(localName = "verification-metadata") @JacksonXmlRootElement(localName = "verification-metadata")
private data class VerificationMetadata( private data class VerificationMetadata(
@field:JacksonXmlElementWrapper(localName = "components") @field:JacksonXmlElementWrapper(localName = "components")
@@ -27,7 +27,7 @@ class ReflectionCodeSanityTest : TestCase() {
} }
private fun loadClass(name: String): Class<*> = private fun loadClass(name: String): Class<*> =
classLoader.loadClass("kotlin.reflect.jvm.internal.$name") classLoader.loadClass("kotlin.reflect.jvm.internal.$name")
private fun collectClassesWithSupers(vararg names: String): Set<Class<*>> { private fun collectClassesWithSupers(vararg names: String): Set<Class<*>> {
val result = linkedSetOf<Class<*>>() val result = linkedSetOf<Class<*>>()
@@ -47,41 +47,43 @@ class ReflectionCodeSanityTest : TestCase() {
fun testNoDelegatedPropertiesInKClassAndKProperties() { fun testNoDelegatedPropertiesInKClassAndKProperties() {
val badMembers = linkedSetOf<Member>() val badMembers = linkedSetOf<Member>()
for (klass in collectClassesWithSupers( for (klass in collectClassesWithSupers(
"KClassImpl", "KClassImpl",
"KMutableProperty0Impl", "KMutableProperty0Impl",
"KMutableProperty1Impl", "KMutableProperty1Impl",
"KMutableProperty2Impl" "KMutableProperty2Impl"
)) { )) {
badMembers.addAll(klass.declaredFields.filter { it.name.endsWith(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX) }) badMembers.addAll(klass.declaredFields.filter { it.name.endsWith(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX) })
badMembers.addAll(klass.declaredMethods.filter { it.name.endsWith(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX) }) badMembers.addAll(klass.declaredMethods.filter { it.name.endsWith(JvmAbi.DELEGATED_PROPERTY_NAME_SUFFIX) })
} }
if (badMembers.isNotEmpty()) { if (badMembers.isNotEmpty()) {
fail("The members listed below appear to be delegates for properties.\n" + fail(
"It's highly not recommended to use property delegates in reflection.jvm because a KProperty instance\n" + "The members listed below appear to be delegates for properties.\n" +
"is created for each delegated property and that makes the initialization sequence of reflection\n" + "It's highly not recommended to use property delegates in reflection.jvm because a KProperty instance\n" +
"implementation classes unpredictable and leads to a deadlock or ExceptionInInitializerError.\n\n" + "is created for each delegated property and that makes the initialization sequence of reflection\n" +
"Please un-delegate the corresponding properties:\n\n" + "implementation classes unpredictable and leads to a deadlock or ExceptionInInitializerError.\n\n" +
badMembers.joinToString("\n")) "Please un-delegate the corresponding properties:\n\n" +
badMembers.joinToString("\n")
)
} }
} }
fun testMaxAllowedFields() { fun testMaxAllowedFields() {
// The following classes are instantiated a lot in Kotlin applications and thus they should be optimized as best as possible. // The following classes are instantiated a lot in Kotlin applications, and thus they should be optimized as good as possible.
// This test checks that these classes have not more fields than a predefined small number, which can usually be calculated as // This test checks that these classes have not more fields than a predefined small number, which can usually be calculated as
// the number of constructor parameters (number of objects needed to initialize an instance) + 1 for 'data', the reflection cache. // the number of constructor parameters (number of objects needed to initialize an instance) + 1 for 'data', the reflection cache.
val classesWithMaxAllowedFields = linkedMapOf( val classesWithMaxAllowedFields = linkedMapOf(
"KClassImpl" to 2, // jClass, data "KClassImpl" to 2, // jClass, data
"KPackageImpl" to 3 // jClass, moduleName, data "KPackageImpl" to 3 // jClass, moduleName, data
) )
val badClasses = linkedMapOf<Class<*>, Collection<Field>>() val badClasses = linkedMapOf<Class<*>, Collection<Field>>()
for ((className, maxAllowedFields) in classesWithMaxAllowedFields) { for ((className, maxAllowedFields) in classesWithMaxAllowedFields) {
val klass = loadClass(className) val klass = loadClass(className)
val fields = generateSequence(klass) { it.superclass } val fields = generateSequence(klass) { it.superclass }
.flatMap { it.declaredFields.asSequence() } .flatMap { it.declaredFields.asSequence() }
.filterNot { Modifier.isStatic(it.modifiers) } .filterNot { Modifier.isStatic(it.modifiers) }
.toList() .toList()
if (fields.size > maxAllowedFields) { if (fields.size > maxAllowedFields) {
badClasses[klass] = fields badClasses[klass] = fields
} }
@@ -89,11 +91,11 @@ class ReflectionCodeSanityTest : TestCase() {
if (badClasses.isNotEmpty()) { if (badClasses.isNotEmpty()) {
fail("Some classes in reflection.jvm contain more fields than it is allowed. Please optimize storage in these classes:\n\n" + fail("Some classes in reflection.jvm contain more fields than it is allowed. Please optimize storage in these classes:\n\n" +
badClasses.entries.joinToString("\n") { entry -> badClasses.entries.joinToString("\n") { entry ->
val (klass, fields) = entry val (klass, fields) = entry
"$klass has ${fields.size} fields but max allowed = ${classesWithMaxAllowedFields[klass.simpleName]}:\n" + "$klass has ${fields.size} fields but max allowed = ${classesWithMaxAllowedFields[klass.simpleName]}:\n" +
fields.joinToString("\n") { " $it" } fields.joinToString("\n") { " $it" }
}) })
} }
} }
} }
@@ -120,7 +120,7 @@ class SpaceCodeOwnersTest : TestCase() {
// (line = 5, pattern = /some/), // (line = 5, pattern = /some/),
// (line = 1, pattern = *) // (line = 1, pattern = *)
// With input of parent = (line = 5, pattern = /some/) and path = /some/other // With input of parent = (line = 5, pattern = /some/) and path = /some/other
// we only search till our parent pattern line, as other rules has lower specifity // we only search till our parent pattern line, as other rules are less specific
for (use in this) { for (use in this) {
if (parentMatchLine != null && use.item.line < parentMatchLine) break if (parentMatchLine != null && use.item.line < parentMatchLine) break
if (use.rule.isMatch(path, isDirectory)) { if (use.rule.isMatch(path, isDirectory)) {