diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1d265d75..c5fe14ff 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -21,6 +21,7 @@ kaml = "com.charleskorn.kaml:kaml:0.61.0" junit5 = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" } junit5-params = { module = "org.junit.jupiter:junit-jupiter-params", version.ref = "junit" } assertj = "org.assertj:assertj-core:3.26.3" +konsist = "com.lemonappdev:konsist:0.16.1" reflections = "org.reflections:reflections:0.10.2" [plugins] diff --git a/rules/detekt/build.gradle.kts b/rules/detekt/build.gradle.kts index 820546d8..d1b64658 100644 --- a/rules/detekt/build.gradle.kts +++ b/rules/detekt/build.gradle.kts @@ -33,4 +33,5 @@ dependencies { testImplementation(libs.assertj) testImplementation(libs.reflections) testImplementation(libs.kaml) + testImplementation(libs.konsist) } diff --git a/rules/detekt/src/test/kotlin/io/nlopez/compose/rules/detekt/ComposeRuleSetProviderTest.kt b/rules/detekt/src/test/kotlin/io/nlopez/compose/rules/detekt/ComposeRuleSetProviderTest.kt index 94432c68..98197d95 100644 --- a/rules/detekt/src/test/kotlin/io/nlopez/compose/rules/detekt/ComposeRuleSetProviderTest.kt +++ b/rules/detekt/src/test/kotlin/io/nlopez/compose/rules/detekt/ComposeRuleSetProviderTest.kt @@ -6,11 +6,17 @@ import com.charleskorn.kaml.Yaml import com.charleskorn.kaml.YamlMap import com.charleskorn.kaml.YamlScalar import com.charleskorn.kaml.yamlMap +import com.lemonappdev.konsist.api.Konsist +import com.lemonappdev.konsist.api.ext.list.withAllParentsOf +import com.lemonappdev.konsist.api.verify.assertTrue import io.gitlab.arturbosch.detekt.api.Config +import io.nlopez.compose.core.ComposeKtVisitor import io.nlopez.compose.rules.DetektRule import org.assertj.core.api.AssertionsForInterfaceTypes.assertThat import org.junit.jupiter.api.Test import org.reflections.Reflections +import org.reflections.scanners.Scanners +import org.reflections.util.ConfigurationBuilder import java.io.File class ComposeRuleSetProviderTest { @@ -90,4 +96,33 @@ class ComposeRuleSetProviderTest { .isEqualTo(shouldBeActive) } } + + @Test + fun `ensure all available rules have a detekt rule`() { + val detektRulesReflections = Reflections(ruleSetProvider.javaClass.packageName) + val detektRuleNames = detektRulesReflections.getSubTypesOf(DetektRule::class.java).map { it.simpleName } + + val commonRulesReflections = Reflections( + ConfigurationBuilder() + .setClassLoaders(arrayOf(ComposeKtVisitor::class.java.classLoader)) + .setScanners(Scanners.SubTypes), + ) + val ruleNames = commonRulesReflections.getSubTypesOf(ComposeKtVisitor::class.java).map { it.simpleName } + + for (ruleName in ruleNames) { + assertThat(detektRuleNames) + .describedAs { "$ruleName should have a detekt rule named ${ruleName}Check" } + .contains("${ruleName}Check") + } + } + + @Test + fun `ensure all detekt rules have a unit test`() { + Konsist.scopeFromProduction() + .classes() + .withAllParentsOf(DetektRule::class) + .assertTrue { clazz -> + clazz.testClasses { it.hasNameContaining(clazz.name) }.isNotEmpty() + } + } } diff --git a/rules/ktlint/build.gradle.kts b/rules/ktlint/build.gradle.kts index 8ad1a1d1..8ccb2b87 100644 --- a/rules/ktlint/build.gradle.kts +++ b/rules/ktlint/build.gradle.kts @@ -32,4 +32,5 @@ dependencies { testImplementation(libs.junit5.params) testImplementation(libs.assertj) testImplementation(libs.reflections) + testImplementation(libs.konsist) } diff --git a/rules/ktlint/src/test/kotlin/io/nlopez/compose/rules/ktlint/ComposeRuleSetProviderTest.kt b/rules/ktlint/src/test/kotlin/io/nlopez/compose/rules/ktlint/ComposeRuleSetProviderTest.kt index c9cb1601..bb591217 100644 --- a/rules/ktlint/src/test/kotlin/io/nlopez/compose/rules/ktlint/ComposeRuleSetProviderTest.kt +++ b/rules/ktlint/src/test/kotlin/io/nlopez/compose/rules/ktlint/ComposeRuleSetProviderTest.kt @@ -2,10 +2,16 @@ // SPDX-License-Identifier: Apache-2.0 package io.nlopez.compose.rules.ktlint +import com.lemonappdev.konsist.api.Konsist +import com.lemonappdev.konsist.api.ext.list.withAllParentsOf +import com.lemonappdev.konsist.api.verify.assertTrue +import io.nlopez.compose.core.ComposeKtVisitor import io.nlopez.compose.rules.KtlintRule import org.assertj.core.api.AssertionsForInterfaceTypes.assertThat import org.junit.jupiter.api.Test import org.reflections.Reflections +import org.reflections.scanners.Scanners +import org.reflections.util.ConfigurationBuilder class ComposeRuleSetProviderTest { @@ -35,4 +41,32 @@ class ComposeRuleSetProviderTest { .describedAs("ComposeRuleSetProvider should have the rules in alphabetical order") .isTrue() } + + @Test + fun `ensure all available rules have a ktlint rule`() { + val ktlintRuleNames = ruleClassesInPackage.map { it.simpleName } + + val commonRulesReflections = Reflections( + ConfigurationBuilder() + .setClassLoaders(arrayOf(ComposeKtVisitor::class.java.classLoader)) + .setScanners(Scanners.SubTypes), + ) + val ruleNames = commonRulesReflections.getSubTypesOf(ComposeKtVisitor::class.java).map { it.simpleName } + + for (ruleName in ruleNames) { + assertThat(ktlintRuleNames) + .describedAs { "$ruleName should have a ktlint rule named ${ruleName}Check" } + .contains("${ruleName}Check") + } + } + + @Test + fun `ensure all ktlint rules have a unit test`() { + Konsist.scopeFromProduction() + .classes() + .withAllParentsOf(KtlintRule::class) + .assertTrue { clazz -> + clazz.testClasses { it.hasNameContaining(clazz.name) }.isNotEmpty() + } + } }