Migrate detekt-rules tests to JUnit (#4514)

This commit is contained in:
Matthew Haughton
2022-01-24 17:09:33 +11:00
committed by GitHub
parent ad270ef4f1
commit c74d714f6b
5 changed files with 41 additions and 30 deletions

View File

@@ -24,9 +24,8 @@ dependencies {
testImplementation(projects.detektRulesNaming)
testImplementation(projects.detektRulesPerformance)
testImplementation(projects.detektRulesStyle)
testImplementation(libs.bundles.testImplementation)
testImplementation(libs.assertj)
testImplementation(libs.reflections)
testRuntimeOnly(libs.spek.runner)
}
tasks.build { finalizedBy(":detekt-generator:generateDocumentation") }

View File

@@ -4,15 +4,17 @@ import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.internal.DefaultRuleSetProvider
import io.gitlab.arturbosch.detekt.test.TestConfig
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.reflections.Reflections
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
class RuleProviderConfigSpec : Spek({
class RuleProviderConfigSpec {
describe("RuleProvider config test") {
@Nested
inner class `RuleProvider config test` {
it("should test if the config has been passed to all rules") {
@Test
fun `should test if the config has been passed to all rules`() {
val config = TestConfig()
val reflections = Reflections("io.gitlab.arturbosch.detekt.rules")
val providers = reflections.getSubTypesOf(DefaultRuleSetProvider::class.java)
@@ -31,4 +33,4 @@ class RuleProviderConfigSpec : Spek({
}
}
}
})
}

View File

@@ -16,16 +16,18 @@ import io.gitlab.arturbosch.detekt.rules.naming.NamingProvider
import io.gitlab.arturbosch.detekt.rules.performance.PerformanceProvider
import io.gitlab.arturbosch.detekt.rules.style.StyleGuideProvider
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.reflections.Reflections
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import java.lang.reflect.Modifier
class RuleProviderSpec : Spek({
class RuleProviderSpec {
describe("Rule Provider") {
@Nested
inner class `Rule Provider` {
it("checks whether all rules are called in the corresponding RuleSetProvider") {
@Test
fun `checks whether all rules are called in the corresponding RuleSetProvider`() {
val reflections = Reflections("io.gitlab.arturbosch.detekt.rules")
val providers = reflections.getSubTypesOf(DefaultRuleSetProvider::class.java)
providers.forEach { providerType ->
@@ -42,7 +44,7 @@ class RuleProviderSpec : Spek({
}
}
}
})
}
private val ruleMap = mapOf<Class<*>, String>(
CommentSmellProvider().javaClass to "io.gitlab.arturbosch.detekt.rules.documentation",

View File

@@ -12,14 +12,16 @@ import io.gitlab.arturbosch.detekt.rules.complexity.TooManyFunctions
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class SuppressingSpec : Spek({
class SuppressingSpec {
describe("Rule suppression") {
@Nested
inner class `Rule suppression` {
it("all findings are suppressed on element levels") {
@Test
fun `all findings are suppressed on element levels`() {
val ktFile = compileForTest(resourceAsPath("SuppressedByElementAnnotation.kt"))
val ruleSet = RuleSet("Test", listOf(LongMethod(), LongParameterList(), ComplexCondition()))
@@ -28,7 +30,8 @@ class SuppressingSpec : Spek({
assertThat(findings).isEmpty()
}
it("all findings are suppressed on file levels") {
@Test
fun `all findings are suppressed on file levels`() {
val ktFile = compileForTest(resourceAsPath("SuppressedElementsByFileAnnotation.kt"))
val ruleSet = RuleSet("Test", listOf(LongMethod(), LongParameterList(), ComplexCondition()))
@@ -37,7 +40,8 @@ class SuppressingSpec : Spek({
assertThat(findings).isEmpty()
}
it("all findings are suppressed on class levels") {
@Test
fun `all findings are suppressed on class levels`() {
val ktFile = compileForTest(resourceAsPath("SuppressedElementsByClassAnnotation.kt"))
val ruleSet = RuleSet("Test", listOf(LongMethod(), LongParameterList(), ComplexCondition()))
@@ -46,7 +50,8 @@ class SuppressingSpec : Spek({
assertThat(findings).isEmpty()
}
it("should suppress TooManyFunctionsRule on class level") {
@Test
fun `should suppress TooManyFunctionsRule on class level`() {
val rule = TooManyFunctions(TestConfig(mapOf("thresholdInClass" to "0")))
val findings = rule.lint(resourceAsPath("SuppressedElementsByClassAnnotation.kt"))
@@ -54,10 +59,11 @@ class SuppressingSpec : Spek({
assertThat(findings).isEmpty()
}
it("should suppress StringLiteralDuplication on class level") {
@Test
fun `should suppress StringLiteralDuplication on class level`() {
val path = resourceAsPath("SuppressStringLiteralDuplication.kt")
assertThat(StringLiteralDuplication().lint(path)).isEmpty()
}
}
})
}

View File

@@ -6,17 +6,19 @@ import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.SetupContext
import io.gitlab.arturbosch.detekt.api.UnstableApi
import org.assertj.core.api.Assertions.assertThatCode
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.io.PrintStream
import java.net.URI
@OptIn(UnstableApi::class)
class LicenceHeaderLoaderExtensionSpec : Spek({
class LicenceHeaderLoaderExtensionSpec {
describe("Licence header extension - #2503") {
@Nested
inner class `Licence header extension - #2503` {
it("should not crash when using resources") {
@Test
fun `should not crash when using resources`() {
assertThatCode {
LicenceHeaderLoaderExtension().init(object : SetupContext {
override val configUris: Collection<URI> = listOf(resource("extensions/config.yml"))
@@ -29,4 +31,4 @@ class LicenceHeaderLoaderExtensionSpec : Spek({
}.doesNotThrowAnyException()
}
}
})
}