From c433bab0a8d06e94b988a2cffbc42449ffd01cd2 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 26 Nov 2024 10:15:53 +0100 Subject: [PATCH] Ensure `Sealed interface used in Composable MUST be Immutable or Stable` is detecting error by adding a failing case. --- ...mposableWithNonImmutableSealedInterface.kt | 20 +++++++++++++++++++ .../tests/konsist/KonsistArchitectureTest.kt | 12 +++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 tests/konsist/src/main/kotlin/io/element/android/tests/konsist/failures/FailingComposableWithNonImmutableSealedInterface.kt diff --git a/tests/konsist/src/main/kotlin/io/element/android/tests/konsist/failures/FailingComposableWithNonImmutableSealedInterface.kt b/tests/konsist/src/main/kotlin/io/element/android/tests/konsist/failures/FailingComposableWithNonImmutableSealedInterface.kt new file mode 100644 index 0000000000..1e90577ca6 --- /dev/null +++ b/tests/konsist/src/main/kotlin/io/element/android/tests/konsist/failures/FailingComposableWithNonImmutableSealedInterface.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2024 New Vector Ltd. + * + * SPDX-License-Identifier: AGPL-3.0-only + * Please see LICENSE in the repository root for full details. + */ + +package io.element.android.tests.konsist.failures + +import androidx.compose.runtime.Composable + +// Make test `Sealed interface used in Composable MUST be Immutable or Stable` fails + +sealed interface SealedInterface + +@Composable +fun FailingComposableWithNonImmutableSealedInterface( + sealedInterface: SealedInterface +) { +} diff --git a/tests/konsist/src/test/kotlin/io/element/android/tests/konsist/KonsistArchitectureTest.kt b/tests/konsist/src/test/kotlin/io/element/android/tests/konsist/KonsistArchitectureTest.kt index 7cc848d825..365449a91d 100644 --- a/tests/konsist/src/test/kotlin/io/element/android/tests/konsist/KonsistArchitectureTest.kt +++ b/tests/konsist/src/test/kotlin/io/element/android/tests/konsist/KonsistArchitectureTest.kt @@ -22,6 +22,7 @@ import com.lemonappdev.konsist.api.ext.list.withoutName import com.lemonappdev.konsist.api.ext.list.withoutParents import com.lemonappdev.konsist.api.verify.assertEmpty import com.lemonappdev.konsist.api.verify.assertTrue +import org.junit.Assert.assertTrue import org.junit.Test class KonsistArchitectureTest { @@ -66,18 +67,18 @@ class KonsistArchitectureTest { @Test fun `Sealed interface used in Composable MUST be Immutable or Stable`() { + var failingTestFound = false // List all sealed interface without Immutable nor Stable annotation in the project val forbiddenInterfacesForComposableParameter = Konsist.scopeFromProject() .interfaces() .withSealedModifier() .withoutAnnotationOf(Immutable::class, Stable::class) .map { it.fullyQualifiedName } - Konsist.scopeFromProject() .functions() .withAnnotationOf(Composable::class) .assertTrue(additionalMessage = "Consider adding the @Immutable or @Stable annotation to the sealed interface") { - it.parameters.all { param -> + val result = it.parameters.all { param -> val type = param.type.text return@all if (type.startsWith("@") || type.startsWith("(") || type.startsWith("suspend")) { true @@ -94,6 +95,13 @@ class KonsistArchitectureTest { fullyQualifiedName !in forbiddenInterfacesForComposableParameter } } + if (!result && !failingTestFound && it.name == "FailingComposableWithNonImmutableSealedInterface") { + failingTestFound = true + true + } else { + result + } } + assertTrue("FailingComposableWithNonImmutableSealedInterface should make this test fail.", failingTestFound) } }