From e850d761205eb56ec4ff513daa24530f893684d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brais=20Gab=C3=ADn?= Date: Sat, 30 Apr 2022 13:41:07 +0200 Subject: [PATCH 1/2] Report as warnings the Strings that should be an array --- .../detekt/core/config/ValidateConfig.kt | 11 +++++++++ .../detekt/core/config/ValidateConfigSpec.kt | 23 +++++++++++++++++++ .../resources/config_validation/baseline.yml | 2 ++ 3 files changed, 36 insertions(+) diff --git a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt index 331dbaf5bb1..002c4ada1b3 100644 --- a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt +++ b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt @@ -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 @@ -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 an array instead of a String.", + if (reportAsError) Notification.Level.Error else Notification.Level.Warning, + ) diff --git a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfigSpec.kt b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfigSpec.kt index 81b41bb41d1..a67f6d93e70 100644 --- a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfigSpec.kt +++ b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfigSpec.kt @@ -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 + ) + ) + } } diff --git a/detekt-core/src/test/resources/config_validation/baseline.yml b/detekt-core/src/test/resources/config_validation/baseline.yml index 222505f1294..dc814e5a41a 100644 --- a/detekt-core/src/test/resources/config_validation/baseline.yml +++ b/detekt-core/src/test/resources/config_validation/baseline.yml @@ -1,3 +1,5 @@ +config: + warningsAsErrors: true complexity: LongMethod: active: true From b0a63123caea388e435166fc7a30968c393f1265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brais=20Gab=C3=ADn?= Date: Sat, 30 Apr 2022 20:36:57 +0200 Subject: [PATCH 2/2] Update detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt Co-authored-by: Nicola Corti --- .../io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt index 002c4ada1b3..4f6dfe62d52 100644 --- a/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt +++ b/detekt-core/src/main/kotlin/io/gitlab/arturbosch/detekt/core/config/ValidateConfig.kt @@ -148,6 +148,6 @@ internal fun propertyShouldBeAnArray( reportAsError: Boolean, ): Notification = SimpleNotification( - "Property '$prop' should be an array instead of a String.", + "Property '$prop' should be a YAML array instead of a comma-separated String.", if (reportAsError) Notification.Level.Error else Notification.Level.Warning, )