-
-
Notifications
You must be signed in to change notification settings - Fork 755
/
YamlConfigSpec.kt
205 lines (178 loc) · 7.25 KB
/
YamlConfigSpec.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
@file:Suppress("detekt.MaxLineLength")
package io.gitlab.arturbosch.detekt.core.config
import io.github.detekt.test.utils.resourceAsPath
import io.github.detekt.utils.getSafeResourceAsStream
import io.gitlab.arturbosch.detekt.api.Config
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.assertThatCode
import org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import org.assertj.core.api.Assertions.assertThatIllegalStateException
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.yaml.snakeyaml.parser.ParserException
import java.nio.file.Paths
class YamlConfigSpec {
@Nested
inner class `load yaml config` {
private val config = yamlConfig("detekt.yml")
@Test
fun `should create a sub config`() {
val subConfig = config.subConfig("style")
assertThat(subConfig.valueOrDefault("WildcardImport", emptyMap<String, Any>())).isNotEmpty
assertThat(
subConfig.valueOrDefault(
"WildcardImport",
emptyMap<String, Any>()
)["active"].toString()
).isEqualTo("true")
assertThat(
subConfig.valueOrDefault(
"WildcardImport",
emptyMap<String, Any>()
)["active"] as Boolean
).isTrue()
assertThat(subConfig.valueOrDefault("NotFound", emptyMap<String, Any>())).isEmpty()
assertThat(subConfig.valueOrDefault("NotFound", "")).isEmpty()
}
@Test
fun `should create a sub sub config`() {
val subConfig = config.subConfig("style")
val subSubConfig = subConfig.subConfig("WildcardImport")
assertThat(subSubConfig.valueOrDefault("active", false)).isTrue()
assertThat(subSubConfig.valueOrDefault("NotFound", true)).isTrue()
}
@Test
fun `tests wrong sub config conversion`() {
assertThatIllegalStateException().isThrownBy {
@Suppress("UNUSED_VARIABLE")
val ignored = config.valueOrDefault("style", "")
}
.withMessage(
"Value \"{WildcardImport={active=true}, NoElseInWhenExpression={active=true}, MagicNumber={active=true, ignoreNumbers=[-1, 0, 1, 2]}}\" set for config parameter \"style\" is not of required type String."
)
}
}
@Nested
inner class `loading empty configurations` {
@Test
fun `empty yaml file is equivalent to empty config`() {
javaClass.getSafeResourceAsStream("/empty.yml")!!.reader().use(YamlConfig::load)
}
@Test
fun `single item in yaml file is valid`() {
javaClass.getSafeResourceAsStream("/oneitem.yml")!!.reader().use(YamlConfig::load)
}
}
@Nested
inner class `meaningful error messages` {
private val config = yamlConfig("wrong-property-type.yml")
@Test
fun `only accepts true and false boolean values`() {
assertThatIllegalStateException()
.isThrownBy { config.valueOrDefault("bool", false) }
.withMessage("""Value "fasle" set for config parameter "bool" is not of required type Boolean.""")
}
@Test
fun `prints whole config-key path for NumberFormatException`() {
assertThatIllegalStateException().isThrownBy {
config.subConfig("RuleSet")
.subConfig("Rule")
.valueOrDefault("threshold", 6)
}
.withMessage(
"Value \"v5.7\" set for config parameter \"RuleSet > Rule > threshold\" is not of required type Int."
)
}
@Test
fun `prints whole config-key path for ClassCastException`() {
assertThatIllegalStateException().isThrownBy {
@Suppress("UNUSED_VARIABLE")
val bool: Int = config.subConfig("RuleSet")
.subConfig("Rule")
.valueOrDefault("active", 1)
}
.withMessage(
"Value \"[]\" set for config parameter \"RuleSet > Rule > active\" is not of required type Int."
)
}
}
@Nested
inner class `yaml config` {
@Test
fun `loads the config from a given yaml file`() {
val path = resourceAsPath("detekt.yml")
val config = YamlConfig.load(path)
assertThat(config).isNotNull
}
@Test
fun `loads the config from a given text file`() {
val path = resourceAsPath("detekt.txt")
val config = YamlConfig.load(path)
assertThat(config).isNotNull
}
@Nested
inner class `Values with reason` {
private val config = YamlConfig.load(resourceAsPath("values-with-reason.yml"))
@Test
fun `can be parsed`() {
assertThat(config).isNotNull
}
@Test
fun `supports lists`() {
val actualAsList: List<*>? = config
.subConfig("style")
.subConfig("AsList")
.valueOrNull("values")
assertThat(actualAsList).hasSize(3)
}
@Test
fun `supports dictionaries`() {
val actualAsMap: List<Map<*, *>>? = config
.subConfig("style")
.subConfig("AsListOfMaps")
.valueOrNull("values")
assertThat(actualAsMap)
.hasSize(3)
}
@Test
fun `supports empty dictionaries`() {
val actualAsMap: List<Map<*, *>>? = config
.subConfig("style")
.subConfig("EmptyListOfMaps")
.valueOrNull("values")
assertThat(actualAsMap)
.isNotNull
.isEmpty()
}
}
@Test
fun `throws an exception on an non-existing file`() {
val path = Paths.get("doesNotExist.yml")
assertThatIllegalArgumentException()
.isThrownBy { YamlConfig.load(path) }
.withMessageStartingWith("Configuration does not exist")
}
@Test
fun `throws an exception on a directory`() {
val path = resourceAsPath("/config_validation")
assertThatIllegalArgumentException()
.isThrownBy { YamlConfig.load(path) }
.withMessageStartingWith("Configuration must be a file")
}
@Test
fun `throws InvalidConfigurationError on invalid structured yaml files`() {
assertThatCode {
yamlConfigFromContent(
"""
map:
{}map
""".trimIndent()
)
}.isInstanceOf(Config.InvalidConfigurationError::class.java)
.hasMessageContaining("Provided configuration file is invalid")
.hasCauseInstanceOf(ParserException::class.java)
}
}
}