Skip to content

Commit

Permalink
Enable to use defaultConfig with allRules
Browse files Browse the repository at this point in the history
  • Loading branch information
BraisGabin committed Jan 10, 2024
1 parent 7ed1fc4 commit f445b1f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.gitlab.arturbosch.detekt.api.internal.whichDetekt
import io.gitlab.arturbosch.detekt.api.internal.whichJava
import io.gitlab.arturbosch.detekt.api.internal.whichOS
import io.gitlab.arturbosch.detekt.core.config.AllRulesConfig
import io.gitlab.arturbosch.detekt.core.config.CompositeConfig
import io.gitlab.arturbosch.detekt.core.config.DisabledAutoCorrectConfig
import io.gitlab.arturbosch.detekt.core.config.validation.DeprecatedRule
import io.gitlab.arturbosch.detekt.core.config.validation.loadDeprecations
Expand All @@ -36,7 +37,7 @@ internal class Analyzer(
private val processors: List<FileProcessListener>
) {

private val config: Config = settings.spec.workaroundConfiguration(settings.config)
private val config: Config = settings.spec.workaroundConfiguration(settings.baseConfig)

fun run(
ktFiles: Collection<KtFile>,
Expand Down Expand Up @@ -194,18 +195,17 @@ internal fun ProcessingSpec.workaroundConfiguration(config: Config): Config {
var declaredConfig: Config = config

if (rulesSpec.activateAllRules) {
val defaultConfig = getDefaultConfiguration()
val deprecatedRules = loadDeprecations().filterIsInstance<DeprecatedRule>().toSet()
declaredConfig = AllRulesConfig(
originalConfig = declaredConfig,
defaultConfig = defaultConfig,
deprecatedRules = deprecatedRules
)
declaredConfig = AllRulesConfig(declaredConfig, deprecatedRules)
}

if (!rulesSpec.autoCorrect) {
declaredConfig = DisabledAutoCorrectConfig(declaredConfig)
}

if (configSpec.useDefaultConfig) {
declaredConfig = CompositeConfig(declaredConfig, getDefaultConfiguration())
}

return declaredConfig
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,33 @@ import io.gitlab.arturbosch.detekt.core.config.validation.validateConfig

@Suppress("UNCHECKED_CAST")
internal data class AllRulesConfig(
private val originalConfig: Config,
private val defaultConfig: Config,
private val wrapped: Config,
private val deprecatedRules: Set<DeprecatedRule>,
override val parent: Config? = null,
) : Config, ValidatableConfiguration {

override val parentPath: String?
get() = originalConfig.parentPath ?: defaultConfig.parentPath
get() = wrapped.parentPath

override fun subConfig(key: String) =
AllRulesConfig(originalConfig.subConfig(key), defaultConfig.subConfig(key), deprecatedRules, this)
AllRulesConfig(wrapped.subConfig(key), deprecatedRules, this)

override fun <T : Any> valueOrDefault(key: String, default: T): T {
return when (key) {
Config.ACTIVE_KEY -> if (isDeprecated()) false as T else originalConfig.valueOrDefault(key, true) as T
else -> originalConfig.valueOrDefault(key, defaultConfig.valueOrDefault(key, default))
Config.ACTIVE_KEY -> if (isDeprecated()) false as T else wrapped.valueOrDefault(key, true) as T
else -> wrapped.valueOrDefault(key, default)
}
}

override fun <T : Any> valueOrNull(key: String): T? {
return when (key) {
Config.ACTIVE_KEY -> if (isDeprecated()) false as T else originalConfig.valueOrNull(key) ?: true as? T
else -> originalConfig.valueOrNull(key) ?: defaultConfig.valueOrNull(key)
Config.ACTIVE_KEY -> if (isDeprecated()) false as T else wrapped.valueOrNull(key) ?: true as? T
else -> wrapped.valueOrNull(key)
}
}

override fun validate(baseline: Config, excludePatterns: Set<Regex>) =
validateConfig(originalConfig, baseline, excludePatterns)
validateConfig(wrapped, baseline, excludePatterns)

private fun isDeprecated(): Boolean = deprecatedRules.any { parentPath == it.toPath() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ class WorkaroundConfigurationKtSpec {
inner class `with all rules activated by default` {

private val config = ProcessingSpec {
config { resources = listOf(resourceUrl("/configs/empty.yml")) }
config {
resources = listOf(resourceUrl("/configs/empty.yml"))
useDefaultConfig = true
}
rules { activateAllRules = true }
}.withSettings { spec.workaroundConfiguration(config) }
}.withSettings { spec.workaroundConfiguration(baseConfig) }

@Test
fun `should override active to true by default`() {
Expand All @@ -40,7 +43,7 @@ class WorkaroundConfigurationKtSpec {
private val config = ProcessingSpec {
config { resources = listOf(resourceUrl("/configs/activate-all-rules-will-override-here.yml")) }
rules { activateAllRules = true }
}.withSettings { spec.workaroundConfiguration(config) }
}.withSettings { spec.workaroundConfiguration(baseConfig) }

@Test
fun `should override config when specified`() {
Expand Down Expand Up @@ -68,7 +71,7 @@ class WorkaroundConfigurationKtSpec {
private val config = ProcessingSpec {
config { resources = listOf(resourceUrl("/configs/config-with-auto-correct.yml")) }
rules { autoCorrect = true }
}.withSettings { spec.workaroundConfiguration(config) }
}.withSettings { spec.workaroundConfiguration(baseConfig) }

private val style = config.subConfig("style")
private val comments = config.subConfig("comments")
Expand All @@ -92,7 +95,7 @@ class WorkaroundConfigurationKtSpec {
inner class `when not specified all autoCorrect values are overridden to false` {
private val config = ProcessingSpec {
config { resources = listOf(resourceUrl("/configs/config-with-auto-correct.yml")) }
}.withSettings { spec.workaroundConfiguration(config) }
}.withSettings { spec.workaroundConfiguration(baseConfig) }
private val style = config.subConfig("style")
private val comments = config.subConfig("comments")

Expand All @@ -116,7 +119,7 @@ class WorkaroundConfigurationKtSpec {
private val config = ProcessingSpec {
config { resources = listOf(resourceUrl("/configs/config-with-auto-correct.yml")) }
rules { autoCorrect = false }
}.withSettings { spec.workaroundConfiguration(config) }
}.withSettings { spec.workaroundConfiguration(baseConfig) }
private val style = config.subConfig("style")
private val comments = config.subConfig("comments")

Expand Down Expand Up @@ -146,7 +149,7 @@ class WorkaroundConfigurationKtSpec {
autoCorrect = false
activateAllRules = true
}
}.withSettings { spec.workaroundConfiguration(config) }
}.withSettings { spec.workaroundConfiguration(baseConfig) }

private val style = config.subConfig("style")
private val comments = config.subConfig("comments")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class AllRulesConfigSpec {
@Test
fun `is derived from the original config`() {
val subject = AllRulesConfig(
originalConfig = rulesetConfig,
defaultConfig = emptyYamlConfig,
wrapped = rulesetConfig,
deprecatedRules = emptySet(),
)
val actual = subject.parentPath
Expand All @@ -30,12 +29,11 @@ class AllRulesConfigSpec {
@Test
fun `is derived from the default config if unavailable in original config`() {
val subject = AllRulesConfig(
originalConfig = emptyYamlConfig,
defaultConfig = rulesetConfig,
wrapped = emptyYamlConfig,
deprecatedRules = emptySet(),
)
val actual = subject.parentPath
assertThat(actual).isEqualTo(rulesetId)
assertThat(actual).isEqualTo(null)
}
}

Expand All @@ -46,8 +44,7 @@ class AllRulesConfigSpec {
@Test
fun `is the parent`() {
val subject = AllRulesConfig(
originalConfig = rulesetConfig,
defaultConfig = emptyYamlConfig,
wrapped = rulesetConfig,
deprecatedRules = emptySet(),
)
val actual = subject.subConfig("style").parent
Expand All @@ -61,8 +58,7 @@ class AllRulesConfigSpec {
@Test
fun `rule is active if not deprecated`() {
val subject = AllRulesConfig(
originalConfig = emptyYamlConfig,
defaultConfig = emptyYamlConfig,
wrapped = emptyYamlConfig,
deprecatedRules = emptySet()
)
.subConfig("ruleset")
Expand All @@ -76,9 +72,8 @@ class AllRulesConfigSpec {
@Test
fun `rule is inactive if deprecated`() {
val subject = AllRulesConfig(
originalConfig = emptyYamlConfig,
defaultConfig = emptyYamlConfig,
deprecatedRules = setOf(DeprecatedRule("ruleset", "ARule", ""))
wrapped = emptyYamlConfig,
deprecatedRules = setOf(DeprecatedRule("ruleset", "ARule", "")),
)
.subConfig("ruleset")
.subConfig("ARule")
Expand Down

0 comments on commit f445b1f

Please sign in to comment.