Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add tests to make sure rules have tests #349

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions rules/detekt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ dependencies {
testImplementation(libs.assertj)
testImplementation(libs.reflections)
testImplementation(libs.kaml)
testImplementation(libs.konsist)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
}
}
1 change: 1 addition & 0 deletions rules/ktlint/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ dependencies {
testImplementation(libs.junit5.params)
testImplementation(libs.assertj)
testImplementation(libs.reflections)
testImplementation(libs.konsist)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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()
}
}
}