/
ForbiddenImportSpec.kt
137 lines (119 loc) · 5.27 KB
/
ForbiddenImportSpec.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
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.ValueWithReason
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.lint
import io.gitlab.arturbosch.detekt.test.toConfig
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
private const val IMPORTS = "imports"
private const val FORBIDDEN_PATTERNS = "forbiddenPatterns"
class ForbiddenImportSpec {
val code = """
package foo
import kotlin.jvm.JvmField
import kotlin.SinceKotlin
import com.example.R.string
import net.example.R.dimen
import net.example.R.dimension
"""
@Test
fun `should report nothing by default`() {
val findings = ForbiddenImport().lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should report nothing when imports are blank`() {
val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to listOf(" ")))).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should report nothing when imports do not match`() {
val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to listOf("org.*")))).lint(code)
assertThat(findings).isEmpty()
}
@Test
@DisplayName("should report kotlin.* when imports are kotlin.*")
fun reportKotlinWildcardImports() {
val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to listOf("kotlin.*")))).lint(code)
assertThat(findings).hasSize(2)
assertThat(findings[0].message)
.isEqualTo("The import `kotlin.jvm.JvmField` has been forbidden in the Detekt config.")
assertThat(findings[1].message)
.isEqualTo("The import `kotlin.SinceKotlin` has been forbidden in the Detekt config.")
}
@Test
@DisplayName("should report kotlin.* when imports are kotlin.* with reasons")
fun reportKotlinWildcardImports2() {
val config = TestConfig(mapOf(IMPORTS to listOf(ValueWithReason("kotlin.*", "I'm just joking!").toConfig())))
val findings = ForbiddenImport(config).lint(code)
assertThat(findings).hasSize(2)
assertThat(findings[0].message)
.isEqualTo("The import `kotlin.jvm.JvmField` has been forbidden: I'm just joking!")
assertThat(findings[1].message)
.isEqualTo("The import `kotlin.SinceKotlin` has been forbidden: I'm just joking!")
}
@Test
@DisplayName("should report kotlin.SinceKotlin when specified via fully qualified name")
fun reportKotlinSinceKotlinWhenFqdnSpecified() {
val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to listOf("kotlin.SinceKotlin")))).lint(code)
assertThat(findings)
.hasSize(1)
}
@Test
@DisplayName("should report kotlin.SinceKotlin and kotlin.jvm.JvmField when specified via fully qualified names")
fun reportMultipleConfiguredImportsCommaSeparated() {
val findings =
ForbiddenImport(TestConfig(mapOf(IMPORTS to listOf("kotlin.SinceKotlin", "kotlin.jvm.JvmField"))))
.lint(code)
assertThat(findings).hasSize(2)
}
@Test
@DisplayName("should report kotlin.SinceKotlin and kotlin.jvm.JvmField when specified via fully qualified names list")
fun reportMultipleConfiguredImportsInList() {
val findings =
ForbiddenImport(
TestConfig(
mapOf(
IMPORTS to listOf("kotlin.SinceKotlin", "kotlin.jvm.JvmField")
)
)
).lint(code)
assertThat(findings).hasSize(2)
}
@Test
@DisplayName("should report kotlin.SinceKotlin when specified via kotlin.Since*")
fun reportsKotlinSinceKotlinWhenSpecifiedWithWildcard() {
val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to listOf("kotlin.Since*")))).lint(code)
assertThat(findings).hasSize(1)
}
@Test
@DisplayName("should report all of com.example.R.string, net.example.R.dimen, and net.example.R.dimension")
fun preAndPostWildcard() {
val findings = ForbiddenImport(TestConfig(mapOf(IMPORTS to listOf("*.R.*")))).lint(code)
assertThat(findings).hasSize(3)
}
@Test
@DisplayName("should report net.example.R.dimen but not net.example.R.dimension")
fun doNotReportSubstringOfFqdn() {
val findings =
ForbiddenImport(TestConfig(mapOf(IMPORTS to listOf("net.example.R.dimen")))).lint(code)
assertThat(findings).hasSize(1)
}
@Test
fun `should not report import when it does not match any pattern`() {
val findings =
ForbiddenImport(TestConfig(mapOf(FORBIDDEN_PATTERNS to "nets.*R"))).lint(code)
assertThat(findings).isEmpty()
}
@Test
fun `should report import when it matches the forbidden pattern`() {
val findings =
ForbiddenImport(TestConfig(mapOf(FORBIDDEN_PATTERNS to "net.*R|com.*expiremental"))).lint(code)
assertThat(findings).hasSize(2)
assertThat(findings[0].message)
.isEqualTo("The import `net.example.R.dimen` has been forbidden in the Detekt config.")
assertThat(findings[1].message)
.isEqualTo("The import `net.example.R.dimension` has been forbidden in the Detekt config.")
}
}