/
SarifOutputReportSpec.kt
200 lines (172 loc) · 6.87 KB
/
SarifOutputReportSpec.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
package io.github.detekt.report.sarif
import io.github.detekt.test.utils.readResourceContent
import io.github.detekt.tooling.api.VersionProvider
import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Location
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.SeverityLevel
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.api.TextLocation
import io.gitlab.arturbosch.detekt.api.UnstableApi
import io.gitlab.arturbosch.detekt.api.internal.whichOS
import io.gitlab.arturbosch.detekt.test.EmptySetupContext
import io.gitlab.arturbosch.detekt.test.TestDetektion
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.createEntity
import io.gitlab.arturbosch.detekt.test.createFindingForRelativePath
import io.gitlab.arturbosch.detekt.test.createIssue
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.psi.KtClassOrObject
import org.junit.jupiter.api.Test
import java.nio.file.Paths
@OptIn(UnstableApi::class)
class SarifOutputReportSpec {
@Test
fun `renders multiple issues`() {
val result = TestDetektion(
createFinding(ruleName = "TestSmellA", severity = SeverityLevel.ERROR),
createFinding(ruleName = "TestSmellB", severity = SeverityLevel.WARNING),
createFinding(ruleName = "TestSmellC", severity = SeverityLevel.INFO)
)
val report = SarifOutputReport()
.apply { init(EmptySetupContext()) }
.render(result)
assertThat(report).isEqualToIgnoringWhitespace(readResourceContent("vanilla.sarif.json"))
}
@Test
fun `renders multiple issues with relative path`() {
val basePath = "/Users/tester/detekt/"
val result = TestDetektion(
createFindingForRelativePath(ruleName = "TestSmellA", basePath = basePath),
createFindingForRelativePath(ruleName = "TestSmellB", basePath = basePath),
createFindingForRelativePath(ruleName = "TestSmellC", basePath = basePath)
)
val report = SarifOutputReport()
.apply {
init(
EmptySetupContext().apply {
register(DETEKT_OUTPUT_REPORT_BASE_PATH_KEY, Paths.get(basePath))
}
)
}
.render(result)
.stripWhitespace()
val expectedReport = readResourceContent("relative_path.sarif.json")
// Note: Github CI uses D: drive, but it could be any drive for local development
val systemAwareExpectedReport = if (whichOS().startsWith("windows", ignoreCase = true)) {
val winRoot = Paths.get("/").toAbsolutePath().toString().replace("\\", "/")
expectedReport.replace("file:///", "file://$winRoot")
} else {
expectedReport
}
assertThat(report).isEqualToIgnoringWhitespace(systemAwareExpectedReport)
}
@Test
fun `region should be bounded with word`() {
/**
* Region constraints in Snippet for word 'TestClass'
* @sample Snippet.code
*/
val startLine = 3
val startColumn = 7
val endLine = 3
val endColumn = 15
val refEntity = TestRule().compileAndLint(Snippet.code).first().entity
val location = Location(
SourceLocation(startLine, startColumn),
TextLocation(
startLine + (startColumn - 1) * Snippet.lineLength,
endColumn + (endLine - 1) * Snippet.lineLength
),
filePath = refEntity.location.filePath
)
val result = TestDetektion(
createFinding(
ruleName = "TestSmellB",
entity = refEntity.copy(location = location),
severity = SeverityLevel.WARNING
)
)
val report = SarifOutputReport()
.apply { init(EmptySetupContext()) }
.render(result)
assertThat(report)
.containsIgnoringWhitespaces(constrainRegion(startLine, startColumn, endLine, endColumn))
}
@Test
fun `region should be bounded with block`() {
/**
* Region constraints in Snippet for curly braces
* @sample Snippet.code
*/
val startLine = 3
val startColumn = 17
val endLine = 5
val endColumn = 1
val refEntity = TestRule().compileAndLint(Snippet.code).first().entity
val location = Location(
SourceLocation(startLine, startColumn),
TextLocation(
startLine + (startColumn - 1) * Snippet.lineLength,
endColumn + (endLine - 1) * Snippet.lineLength
),
filePath = refEntity.location.filePath
)
val result = TestDetektion(
createFinding(
ruleName = "TestSmellB",
entity = refEntity.copy(location = location),
severity = SeverityLevel.WARNING
)
)
val report = SarifOutputReport()
.apply { init(EmptySetupContext()) }
.render(result)
assertThat(report)
.containsIgnoringWhitespaces(constrainRegion(startLine, startColumn, endLine, endColumn))
}
}
private object Snippet {
// Each line of code is 50 chars long
const val lineLength = 50
val code = """
// 4567890123456789012345678901234567890123456789
// 0000001111111111222222222233333333334444444444
class TestClass { ///////////////////////////////
val greeting: String = "Hello, World!"///////
}////////////////////////////////////////////////
""".trimIndent()
}
private fun constrainRegion(startLine: Int, startColumn: Int, endLine: Int, endColumn: Int) = """
"region": {
"endColumn": ${endColumn + 1},
"endLine": $endLine,
"startColumn": $startColumn,
"startLine": $startLine
}
"""
class TestRule : Rule() {
override val issue = Issue(javaClass.simpleName, Severity.Warning, "", Debt.FIVE_MINS)
override fun visitClassOrObject(classOrObject: KtClassOrObject) {
report(CodeSmell(issue, Entity.atName(classOrObject), message = "Error"))
}
}
private fun createFinding(
ruleName: String,
severity: SeverityLevel,
entity: Entity = createEntity("TestFile.kt")
): Finding {
return object : CodeSmell(createIssue(ruleName), entity, "TestMessage") {
override val severity: SeverityLevel
get() = severity
}
}
private fun String.stripWhitespace() = replace(Regex("\\s"), "")
internal class TestVersionProvider : VersionProvider {
override fun current(): String = "1.0.0"
}