-
-
Notifications
You must be signed in to change notification settings - Fork 755
/
AnalyzerSpec.kt
162 lines (139 loc) · 6.74 KB
/
AnalyzerSpec.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
package io.gitlab.arturbosch.detekt.core
import io.github.detekt.test.utils.compileForTest
import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.RuleSet
import io.gitlab.arturbosch.detekt.api.RuleSetProvider
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.test.yamlConfig
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.jetbrains.kotlin.psi.KtFile
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.util.concurrent.CompletionException
class AnalyzerSpec {
@Nested
inner class `exceptions during analyze()` {
@Test
fun `throw error explicitly when config has wrong value type in config`() {
val testFile = path.resolve("Test.kt")
val settings = createProcessingSettings(testFile, yamlConfig("configs/config-value-type-wrong.yml"))
val analyzer = Analyzer(settings, listOf(StyleRuleSetProvider()), emptyList())
assertThatThrownBy {
settings.use { analyzer.run(listOf(compileForTest(testFile))) }
}.isInstanceOf(IllegalStateException::class.java)
}
@Test
fun `throw error explicitly in parallel when config has wrong value in config`() {
val testFile = path.resolve("Test.kt")
val settings = createProcessingSettings(
inputPath = testFile,
config = yamlConfig("configs/config-value-type-wrong.yml"),
spec = createNullLoggingSpec {
execution {
parallelParsing = true
parallelAnalysis = true
}
}
)
val analyzer = Analyzer(settings, listOf(StyleRuleSetProvider()), emptyList())
assertThatThrownBy { settings.use { analyzer.run(listOf(compileForTest(testFile))) } }
.isInstanceOf(CompletionException::class.java)
.hasCauseInstanceOf(IllegalStateException::class.java)
}
}
@Nested
inner class `analyze successfully when config has correct value type in config` {
@Test
fun `no findings`() {
val testFile = path.resolve("Test.kt")
val settings = createProcessingSettings(testFile, yamlConfig("configs/config-value-type-correct.yml"))
val analyzer = Analyzer(settings, listOf(StyleRuleSetProvider()), emptyList())
assertThat(settings.use { analyzer.run(listOf(compileForTest(testFile))) }).isEmpty()
}
@Test
fun `with findings`() {
val testFile = path.resolve("Test.kt")
val settings = createProcessingSettings(testFile, yamlConfig("configs/config-value-type-correct.yml"))
val analyzer = Analyzer(settings, listOf(StyleRuleSetProvider(18)), emptyList())
assertThat(settings.use { analyzer.run(listOf(compileForTest(testFile))) }).hasSize(1)
}
@Test
fun `with findings but ignored`() {
val testFile = path.resolve("Test.kt")
val settings = createProcessingSettings(
testFile,
yamlConfig("configs/config-value-type-correct-ignore-annotated.yml")
)
val analyzer = Analyzer(settings, listOf(StyleRuleSetProvider(18)), emptyList())
assertThat(settings.use { analyzer.run(listOf(compileForTest(testFile))) }).isEmpty()
}
@Test
fun `with faulty rule`() {
val testFile = path.resolve("Test.kt")
val settings = createProcessingSettings(
testFile,
yamlConfig("configs/config-value-type-correct.yml")
)
val analyzer = Analyzer(settings, listOf(FaultyRuleSetProvider()), emptyList())
assertThatThrownBy { settings.use { analyzer.run(listOf(compileForTest(testFile))) } }
.hasCauseInstanceOf(IllegalStateException::class.java)
.hasMessageContaining("Location: ${FaultyRule::class.java.name}")
}
@Test
fun `with faulty rule without stack trace`() {
val testFile = path.resolve("Test.kt")
val settings = createProcessingSettings(
testFile,
yamlConfig("configs/config-value-type-correct.yml")
)
val analyzer = Analyzer(settings, listOf(FaultyRuleNoStackTraceSetProvider()), emptyList())
assertThatThrownBy { settings.use { analyzer.run(listOf(compileForTest(testFile))) } }
.hasCauseInstanceOf(IllegalStateException::class.java)
.hasMessageContaining("Location: ${null}")
}
}
}
private class StyleRuleSetProvider(private val threshold: Int? = null) : RuleSetProvider {
override val ruleSetId: String = "style"
override fun instance(config: Config) = RuleSet(ruleSetId, listOf(MaxLineLength(config, threshold)))
}
private class MaxLineLength(config: Config, threshold: Int?) : Rule(config) {
override val issue = Issue(this::class.java.simpleName, Severity.Style, "", Debt.FIVE_MINS)
private val lengthThreshold: Int = threshold ?: valueOrDefault("maxLineLength", 100)
override fun visitKtFile(file: KtFile) {
super.visitKtFile(file)
for (line in file.text.lineSequence()) {
if (line.length > lengthThreshold) {
report(CodeSmell(issue, Entity.atPackageOrFirstDecl(file), issue.description))
}
}
}
}
private class FaultyRuleSetProvider : RuleSetProvider {
override val ruleSetId: String = "style"
override fun instance(config: Config) = RuleSet(ruleSetId, listOf(FaultyRule(config)))
}
private class FaultyRule(config: Config) : Rule(config) {
override val issue = Issue(this::class.java.simpleName, Severity.Style, "", Debt.FIVE_MINS)
override fun visitKtFile(file: KtFile) {
throw object : IllegalStateException("Deliberately triggered error.") {}
}
}
private class FaultyRuleNoStackTraceSetProvider : RuleSetProvider {
override val ruleSetId: String = "style"
override fun instance(config: Config) = RuleSet(ruleSetId, listOf(FaultyRuleNoStackTrace(config)))
}
private class FaultyRuleNoStackTrace(config: Config) : Rule(config) {
override val issue = Issue(this::class.java.simpleName, Severity.Style, "", Debt.FIVE_MINS)
override fun visitKtFile(file: KtFile) {
throw object : IllegalStateException("Deliberately triggered error without stack trace.") {
init { stackTrace = emptyArray() }
}
}
}