Skip to content

Commit

Permalink
Report as warnings the Strings that should be an array (#4793)
Browse files Browse the repository at this point in the history
* Report as warnings the Strings that should be an array

* Update detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt

Co-authored-by: Nicola Corti <corti.nico@gmail.com>

Co-authored-by: Nicola Corti <corti.nico@gmail.com>
  • Loading branch information
BraisGabin and cortinico committed May 12, 2022
1 parent 5af7184 commit 045928b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Expand Up @@ -99,6 +99,8 @@ internal fun validateConfig(

if (!base.contains(prop)) {
notifications.add(propertyDoesNotExists(propertyPath))
} else if (current[prop] is String && base[prop] is List<*>) {
notifications.add(propertyShouldBeAnArray(propertyPath, warningsAsErrors))
}

val next = current[prop] as? Map<String, Any>
Expand Down Expand Up @@ -140,3 +142,12 @@ internal fun propertyIsDeprecated(
"Property '$prop' is deprecated. $deprecationDescription.",
if (reportAsError) Notification.Level.Error else Notification.Level.Warning,
)

internal fun propertyShouldBeAnArray(
prop: String,
reportAsError: Boolean,
): Notification =
SimpleNotification(
"Property '$prop' should be a YAML array instead of a comma-separated String.",
if (reportAsError) Notification.Level.Error else Notification.Level.Warning,
)
Expand Up @@ -224,4 +224,27 @@ class ValidateConfigSpec {
)
}
}

@ParameterizedTest
@ValueSource(booleans = [true, false])
fun `reports a string that should be an array as a warning`(warningsAsErrors: Boolean) {
val config = yamlConfigFromContent(
"""
config:
warningsAsErrors: $warningsAsErrors
style:
MagicNumber:
ignoreNumbers: '-1,0,1,2'
""".trimIndent()
)

val result = validateConfig(config, baseline)

assertThat(result).contains(
propertyShouldBeAnArray(
"style>MagicNumber>ignoreNumbers",
reportAsError = warningsAsErrors
)
)
}
}
2 changes: 2 additions & 0 deletions detekt-core/src/test/resources/config_validation/baseline.yml
@@ -1,3 +1,5 @@
config:
warningsAsErrors: true
complexity:
LongMethod:
active: true
Expand Down

0 comments on commit 045928b

Please sign in to comment.