Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Report as warnings the Strings that should be an array #4793

Merged
merged 2 commits into from May 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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