forked from detekt/detekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForbiddenSuppressSpec.kt
201 lines (176 loc) · 6.4 KB
/
ForbiddenSuppressSpec.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
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
internal class ForbiddenSuppressSpec {
@Nested
inner class `checking for suppressions of rule ARule` {
private val subject = ForbiddenSuppress(
TestConfig("rules" to listOf("ARule"))
)
@Test
fun `supports java suppress annotations`() {
val code = """
package config
@SuppressWarnings("ARule")
class Foo
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(3, 1)
assertThat(findings.first()).hasMessage(
"Cannot @Suppress rule \"ARule\" due to the current configuration."
)
}
@Test
fun `reports file-level suppression of forbidden rule`() {
val code = """
@file:Suppress("ARule")
package config
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(1, 1)
assertThat(findings.first()).hasMessage(
"Cannot @Suppress rule \"ARule\" due to the current configuration."
)
}
@Test
fun `reports top-level suppression of forbidden rule`() {
val code = """
package config
@Suppress("ARule")
fun foo() { }
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(3, 1)
assertThat(findings.first()).hasMessage(
"Cannot @Suppress rule \"ARule\" due to the current configuration."
)
}
@Test
fun `reports line-level suppression of forbidden rule`() {
val code = """
package config
fun foo() {
@Suppress("ARule")
println("bar")
}
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(4, 5)
assertThat(findings.first()).hasMessage(
"Cannot @Suppress rule \"ARule\" due to the current configuration."
)
}
@Test
fun `doesn't report non-forbidden rule`() {
val code = """
package config
@Suppress("UNCHECKED_CAST")
fun foo() { }
"""
val findings = subject.compileAndLint(code)
assertThat(findings).isEmpty()
}
@Test
fun `does not include non-forbidden rule in report`() {
val code = """
package config
@Suppress("UNCHECKED_CAST", "ARule")
fun foo() { }
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings.first()).hasMessage(
"Cannot @Suppress rule \"ARule\" due to the current configuration."
)
}
@Test
fun `runs and does not report suppress without rules`() {
val code = """
@file:Suppress()
package config
@Suppress
fun foo() { }
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(0)
}
}
@Nested
inner class `checking multiple forbidden rules` {
private val subject = ForbiddenSuppress(
TestConfig("rules" to listOf("ARule", "BRule"))
)
@Test
fun `reports suppression of both forbidden rules`() {
val code = """
@file:Suppress("ARule", "BRule")
package config
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(1, 1)
assertThat(findings.first()).hasMessage(
"Cannot @Suppress rules \"ARule\", \"BRule\" " +
"due to the current configuration."
)
}
@Test
fun `reports method-level suppression of one of two forbidden rules`() {
val code = """
package config
@Suppress("BRule")
fun foo() { }
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(3, 1)
assertThat(findings.first()).hasMessage(
"Cannot @Suppress rule \"BRule\" due to the current configuration."
)
}
}
@Nested
inner class `checking suppression of self` {
private val subject = ForbiddenSuppress(
TestConfig("rules" to listOf("ForbiddenSuppress", "ARule"))
)
@Test
fun `does not catch self-suppression`() {
val code = """
@file:Suppress("ForbiddenSuppress")
package config
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(0)
}
@Test
fun `does not catch suppression of any forbidden rule when one of them`() {
val code = """
@file:Suppress("ForbiddenSuppress", "ARule")
package config
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(0)
}
}
@Nested
inner class `checking active with no rules defined` {
private val subject = ForbiddenSuppress(TestConfig())
@Test
fun `will not report issues with no forbidden rules defined`() {
val code = """
@file:Suppress("ARule")
package config
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(0)
}
}
}