/
ValidateConfigSpec.kt
250 lines (217 loc) · 8.77 KB
/
ValidateConfigSpec.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package io.gitlab.arturbosch.detekt.core.config
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.internal.CommaSeparatedPattern
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.yamlConfig
import io.gitlab.arturbosch.detekt.test.yamlConfigFromContent
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import org.assertj.core.api.Assertions.assertThatIllegalStateException
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
class ValidateConfigSpec {
private val baseline = yamlConfig("config_validation/baseline.yml")
@Test
fun `passes for same config test`() {
val result = validateConfig(baseline, baseline)
assertThat(result).isEmpty()
}
@Test
fun `passes for properties which may appear on rules and rule sets but may be not present in default config`() {
val result = validateConfig(
yamlConfig("config_validation/default-excluded-properties.yml"),
baseline
)
assertThat(result).isEmpty()
}
@Test
fun `reports different rule set name`() {
val result = validateConfig(
yamlConfig("config_validation/other-ruleset-name.yml"),
baseline
)
assertThat(result).contains(propertyDoesNotExists("code-smell"))
}
@Test
fun `reports different nested property names`() {
val result = validateConfig(
yamlConfig("config_validation/other-nested-property-names.yml"),
baseline
)
assertThat(result).contains(
propertyDoesNotExists("complexity>LongLongMethod"),
propertyDoesNotExists("complexity>LongParameterList>enabled"),
propertyDoesNotExists("complexity>LargeClass>howMany"),
propertyDoesNotExists("complexity>InnerMap>InnerKey"),
propertyDoesNotExists("complexity>InnerMap>Inner2>nestedActive")
)
}
@Test
fun `reports nested configuration expected`() {
val result = validateConfig(
yamlConfig("config_validation/no-nested-config.yml"),
baseline
)
assertThat(result).contains(
nestedConfigurationExpected("complexity"),
nestedConfigurationExpected("style>WildcardImport")
)
}
@Test
fun `reports unexpected nested configs`() {
// note that the baseline config is now the first argument
val result = validateConfig(baseline, yamlConfig("config_validation/no-value.yml"))
assertThat(result).contains(
unexpectedNestedConfiguration("style"),
unexpectedNestedConfiguration("comments")
)
}
@Test
fun `returns an error for an invalid config type`() {
val invalidConfig = TestConfig()
assertThatIllegalStateException().isThrownBy {
validateConfig(invalidConfig, baseline)
}.withMessageStartingWith("Unsupported config type for validation")
}
@Test
fun `returns an error for an invalid baseline`() {
val invalidBaseline = TestConfig()
assertThatIllegalArgumentException().isThrownBy {
validateConfig(Config.empty, invalidBaseline)
}.withMessageStartingWith("Only supported baseline config is the YamlConfig.")
}
@Test
fun `returns an error for an empty baseline`() {
val invalidBaseline = Config.empty
assertThatIllegalArgumentException().isThrownBy {
validateConfig(Config.empty, invalidBaseline)
}.withMessageStartingWith("Cannot validate configuration based on an empty baseline config.")
}
@Nested
inner class `validate composite configurations` {
@Test
fun `passes for same left, right and baseline config`() {
val result = validateConfig(CompositeConfig(baseline, baseline), baseline)
assertThat(result).isEmpty()
}
@Test
fun `passes for empty configs`() {
val result = validateConfig(CompositeConfig(Config.empty, Config.empty), baseline)
assertThat(result).isEmpty()
}
@Test
fun `finds accumulated errors`() {
val result = validateConfig(
CompositeConfig(
yamlConfig("config_validation/other-nested-property-names.yml"),
yamlConfig("config_validation/no-nested-config.yml")
),
baseline
)
assertThat(result).contains(
nestedConfigurationExpected("complexity"),
nestedConfigurationExpected("style>WildcardImport"),
propertyDoesNotExists("complexity>LongLongMethod"),
propertyDoesNotExists("complexity>LongParameterList>enabled"),
propertyDoesNotExists("complexity>LargeClass>howMany"),
propertyDoesNotExists("complexity>InnerMap>InnerKey"),
propertyDoesNotExists("complexity>InnerMap>Inner2>nestedActive")
)
}
}
@Nested
inner class `configure additional exclude paths` {
private fun patterns(str: String) = CommaSeparatedPattern(str).mapToRegex()
@Test
fun `does not report any complexity properties`() {
val result = validateConfig(
yamlConfig("config_validation/other-nested-property-names.yml"),
baseline,
patterns("complexity")
)
assertThat(result).isEmpty()
}
@Test
fun `does not report 'complexity_LargeClass_howMany'`() {
val result = validateConfig(
yamlConfig("config_validation/other-nested-property-names.yml"),
baseline,
patterns(".*>.*>howMany")
)
assertThat(result).contains(
propertyDoesNotExists("complexity>LongLongMethod"),
propertyDoesNotExists("complexity>LongParameterList>enabled"),
propertyDoesNotExists("complexity>InnerMap>InnerKey"),
propertyDoesNotExists("complexity>InnerMap>Inner2>nestedActive")
)
assertThat(result).doesNotContain(
propertyDoesNotExists("complexity>LargeClass>howMany")
)
}
@Test
@DisplayName("does not report .*>InnerMap")
fun `does not report innerMap`() {
val result = validateConfig(
yamlConfig("config_validation/other-nested-property-names.yml"),
baseline,
patterns(".*>InnerMap")
)
assertThat(result).contains(
propertyDoesNotExists("complexity>LargeClass>howMany"),
propertyDoesNotExists("complexity>LongLongMethod"),
propertyDoesNotExists("complexity>LongParameterList>enabled")
)
assertThat(result).doesNotContain(
propertyDoesNotExists("complexity>InnerMap>InnerKey"),
propertyDoesNotExists("complexity>InnerMap>Inner2>nestedActive")
)
}
}
@Nested
inner class `deprecated configuration option` {
@ParameterizedTest
@ValueSource(booleans = [true, false])
fun `reports a deprecated property as a warning`(warningsAsErrors: Boolean) {
val config = yamlConfigFromContent(
"""
config:
warningsAsErrors: $warningsAsErrors
naming:
FunctionParameterNaming:
ignoreOverriddenFunctions: ''
""".trimIndent()
)
val result = validateConfig(config, config)
assertThat(result).contains(
propertyIsDeprecated(
"naming>FunctionParameterNaming>ignoreOverriddenFunctions",
"Use `ignoreOverridden` instead",
reportAsError = warningsAsErrors
)
)
}
}
@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
)
)
}
}