Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
3flex committed Apr 27, 2024
1 parent cbd3adf commit 8f4483c
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.gitlab.arturbosch.detekt.api

import io.gitlab.arturbosch.detekt.test.fromRelative
import io.gitlab.arturbosch.detekt.test.location
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import kotlin.io.path.Path
import kotlin.io.path.absolute
import kotlin.io.path.absolutePathString

class CodeSmellSpec {

Expand All @@ -19,10 +19,7 @@ class CodeSmellSpec {
source = SourceLocation(1, 1),
endSource = SourceLocation(1, 1),
text = TextLocation(0, 0),
filePath = fromRelative(
Path("/").absolute().resolve("Users/tester/detekt/"),
Path("TestFile.kt"),
),
path = Path("/").absolute().resolve("Users/tester/detekt/TestFile.kt"),
),
ktElement = null
),
Expand All @@ -32,7 +29,7 @@ class CodeSmellSpec {
assertThat(codeSmell.toString()).isEqualTo(
"CodeSmell(entity=Entity(name=TestEntity, signature=TestEntitySignature, " +
"location=Location(source=1:1, endSource=1:1, text=0:0, " +
"filePath=${codeSmell.location.filePath}), ktElement=null), message=TestMessage, " +
"path=${codeSmell.location.path.absolutePathString()}), ktElement=null), message=TestMessage, " +
"references=[])"
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.gitlab.arturbosch.detekt.test.createEntity
import io.gitlab.arturbosch.detekt.test.location
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import kotlin.io.path.absolutePathString

class CorrectableCodeSmellSpec {

Expand All @@ -18,8 +19,9 @@ class CorrectableCodeSmellSpec {
assertThat(codeSmell.toString()).isEqualTo(
"CorrectableCodeSmell(autoCorrectEnabled=true, " +
"entity=Entity(name=TestEntity, signature=TestEntitySignature, " +
"location=Location(source=1:1, endSource=1:1, text=0:0, filePath=${codeSmell.location.filePath}), " +
"ktElement=null), message=TestMessage, references=[])"
"location=Location(source=1:1, endSource=1:1, text=0:0, " +
"path=${codeSmell.location.path.absolutePathString()}), ktElement=null), " +
"message=TestMessage, references=[])"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.io.path.Path
import kotlin.io.path.relativeToOrSelf
import kotlin.io.path.toPath

class EntitySpec {

private val path = Path("src/test/resources/EntitySpecFixture.kt").toAbsolutePath()
private val basePath = EntitySpec::class.java.getResource("/")!!.toURI().toPath()
private val relativePath = path.relativeToOrSelf(basePath)
private val code = compileForTest(path)

@Nested
Expand Down Expand Up @@ -56,7 +52,7 @@ class EntitySpec {
.isEqualTo(
"Entity(name=memberFun, signature=EntitySpecFixture.kt\$C\$private fun memberFun(): Int, " +
"location=Location(source=5:17, endSource=5:26, text=49:58, " +
"filePath=FilePath(absolutePath=$path, basePath=$basePath, relativePath=$relativePath)), " +
"path=$path), " +
"ktElement=FUN)"
)
}
Expand All @@ -83,7 +79,7 @@ class EntitySpec {
.isEqualTo(
"Entity(name=C, signature=EntitySpecFixture.kt\$C : Any, " +
"location=Location(source=3:7, endSource=3:8, text=20:21, " +
"filePath=FilePath(absolutePath=$path, basePath=$basePath, relativePath=$relativePath)), " +
"path=$path), " +
"ktElement=CLASS)"
)
}
Expand Down Expand Up @@ -114,7 +110,7 @@ class EntitySpec {
.isEqualTo(
"Entity(name=EntitySpecFixture.kt, signature=EntitySpecFixture.kt\$test.EntitySpecFixture.kt, " +
"location=Location(source=1:1, endSource=9:1, text=0:109, " +
"filePath=FilePath(absolutePath=$path, basePath=$basePath, relativePath=$relativePath)), " +
"path=$path), " +
"ktElement=KtFile: EntitySpecFixture.kt)"
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class LocationSpec {

assertThat(location.toString()).isEqualTo(
"Location(source=2:5, endSource=2:11, text=22:28, " +
"filePath=FilePath(absolutePath=${location.filePath.absolutePath}, " +
"basePath=${location.filePath.basePath}, relativePath=${location.filePath.relativePath}))"
"path=${location.path})"
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.gitlab.arturbosch.detekt.test

import io.github.detekt.psi.FilePath
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Location
Expand All @@ -10,7 +9,6 @@ import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.api.TextLocation
import org.jetbrains.kotlin.psi.KtElement
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.absolute

Expand Down Expand Up @@ -80,7 +78,7 @@ fun createIssueForRelativePath(
source = SourceLocation(1, 1),
endSource = SourceLocation(1, 1),
text = TextLocation(0, 0),
filePath = fromRelative(Path("/").absolute().resolve(basePath), Path(relativePath))
path = Path("/").absolute().resolve(basePath).resolve(relativePath)
),
ktElement = null
),
Expand Down Expand Up @@ -111,8 +109,7 @@ fun createLocation(
source = SourceLocation(position.first, position.second),
endSource = SourceLocation(endPosition.first, endPosition.second),
text = TextLocation(text.first, text.last),
filePath = basePath?.let { fromRelative(Path("/").absolute().resolve(it), Path(path)) }
?: fromAbsolute(Path("/").absolute().resolve(path)),
path = basePath?.let { Path(it).absolute().resolve(path) } ?: Path(path).absolute(),
)
}

Expand All @@ -130,10 +127,3 @@ private data class IssueImpl(
override val description: String,
) : Issue.RuleInfo
}

fun fromAbsolute(path: Path) = FilePath(absolutePath = path.normalize())
fun fromRelative(basePath: Path, relativePath: Path) = FilePath(
absolutePath = basePath.resolve(relativePath).normalize(),
basePath = basePath.normalize(),
relativePath = relativePath
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.gitlab.arturbosch.detekt.test.createLocation
import io.gitlab.arturbosch.detekt.test.createRuleInfo
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import kotlin.io.path.absolutePathString

class FileBasedIssuesReportSpec {

Expand All @@ -28,10 +29,10 @@ class FileBasedIssuesReportSpec {

assertThat(output).isEqualTo(
"""
${location1.filePath.absolutePath}
${location1.path.absolutePathString()}
TestSmell - [TestMessage] at ${location1.compact()}
TestSmell - [TestMessage] at ${location1.compact()}
${location2.filePath.absolutePath}
${location2.path.absolutePathString()}
TestSmell - [TestMessage] at ${location2.compact()}
""".trimIndent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import kotlin.io.path.Path
import kotlin.io.path.absolutePathString

class FormattingRuleSpec {

Expand Down Expand Up @@ -74,7 +75,7 @@ class FormattingRuleSpec {
val rule = ChainWrapping(Config.empty)
val findings = rule.visitFile(compileForTest(expectedPath))
assertThat(findings).anySatisfy { finding ->
assertThat(finding.location.filePath.absolutePath.toString()).isEqualTo(expectedPath.toString())
assertThat(finding.location.path.absolutePathString()).isEqualTo(expectedPath.toString())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ class HtmlOutputReportSpec {
@Test
fun `renders the right file locations`() {
val result = htmlReport.render(createTestDetektionWithMultipleSmells())
val root = Path("/").absolute().invariantSeparatorsPathString
val root = Path("${System.getProperty("user.dir")}/Users/tester/detekt/").invariantSeparatorsPathString

assertThat(result).contains("<span class=\"location\">${root}src/main/com/sample/Sample1.kt:11:1</span>")
assertThat(result).contains("<span class=\"location\">${root}src/main/com/sample/Sample2.kt:22:2</span>")
assertThat(result).contains("<span class=\"location\">${root}src/main/com/sample/Sample3.kt:33:3</span>")
assertThat(result).contains("<span class=\"location\">$root/src/main/com/sample/Sample1.kt:11:1</span>")
assertThat(result).contains("<span class=\"location\">$root/src/main/com/sample/Sample2.kt:22:2</span>")
assertThat(result).contains("<span class=\"location\">$root/src/main/com/sample/Sample3.kt:33:3</span>")
}

@Test
Expand Down Expand Up @@ -165,7 +165,10 @@ class HtmlOutputReportSpec {
@Test
fun `asserts that the generated HTML is the same as expected`() {
val expectedString = readResourceContent("HtmlOutputFormatTest.html")
.replace("<PREFIX>", Path("/").absolute().invariantSeparatorsPathString)
.replace(
"<PREFIX>",
Path("${System.getProperty("user.dir")}/Users/tester/detekt/").invariantSeparatorsPathString
)
val expected = createTempFileForTest("expected-report", ".html").apply { writeText(expectedString) }

val result = htmlReport.render(createTestDetektionWithMultipleSmells())
Expand Down Expand Up @@ -196,12 +199,17 @@ private fun fakeKtElement(): KtElement {
}

private fun createTestDetektionWithMultipleSmells(): Detektion {
val basePath = "${System.getProperty("user.dir")}/Users/tester/detekt/"
val entity1 = createEntity(
location = createLocation("src/main/com/sample/Sample1.kt", position = 11 to 1, text = 10..14),
location = createLocation("src/main/com/sample/Sample1.kt", basePath, position = 11 to 1, text = 10..14),
ktElement = fakeKtElement()
)
val entity2 = createEntity(location = createLocation("src/main/com/sample/Sample2.kt", position = 22 to 2))
val entity3 = createEntity(location = createLocation("src/main/com/sample/Sample3.kt", position = 33 to 3))
val entity2 = createEntity(
location = createLocation("src/main/com/sample/Sample2.kt", basePath, position = 22 to 2)
)
val entity3 = createEntity(
location = createLocation("src/main/com/sample/Sample3.kt", basePath, position = 33 to 3)
)

return TestDetektion(
createIssue(createRuleInfo("id_a", "RuleSet1"), entity1, "Issue message 1"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ <h3>RuleSet1: 2</h3>
<summary class="rule-container"><span class="rule">id_a: 2 </span><span class="description">Description id_a</span></summary>
<a href="https://detekt.dev/docs/rules/ruleset1#id_a">Documentation</a>
<ul>
<li><span class="location"><PREFIX>src/main/com/sample/Sample1.kt:11:1</span><span class="message">Issue message 1</span>
<li><span class="location"><PREFIX>/src/main/com/sample/Sample1.kt:11:1</span><span class="message">Issue message 1</span>
<pre><code><span class="lineno"> 8 </span>
<span class="lineno"> 9 </span>
<span class="lineno"> 10 </span>
Expand All @@ -183,15 +183,15 @@ <h3>RuleSet1: 2</h3>
<span class="lineno"> 13 </span>
</code></pre>
</li>
<li><span class="location"><PREFIX>src/main/com/sample/Sample2.kt:22:2</span><span class="message">Issue message 2</span></li>
<li><span class="location"><PREFIX>/src/main/com/sample/Sample2.kt:22:2</span><span class="message">Issue message 2</span></li>
</ul>
</details>
<h3>RuleSet2: 1</h3>
<details id="id_b" open="open">
<summary class="rule-container"><span class="rule">id_b: 1 </span><span class="description">Description id_b</span></summary>
<a href="https://detekt.dev/docs/rules/ruleset2#id_b">Documentation</a>
<ul>
<li><span class="location"><PREFIX>src/main/com/sample/Sample3.kt:33:3</span><span class="message">Issue message 3</span></li>
<li><span class="location"><PREFIX>/src/main/com/sample/Sample3.kt:33:3</span><span class="message">Issue message 3</span></li>
</ul>
</details>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SarifOutputReportSpec {
.render(result)

val expectedReport = readResourceContent("vanilla.sarif.json")
.replace("<PREFIX>", Path("/").absolute().toUri().toString())
.replace("<PREFIX>", Path(System.getProperty("user.dir")).toUri().toString())

assertThat(report).isEqualToIgnoringWhitespace(expectedReport)
}
Expand Down Expand Up @@ -83,7 +83,7 @@ class SarifOutputReportSpec {
.render(result)

val expectedReport = readResourceContent("rule_warning.sarif.json")
.replace("<PREFIX>", Path("/").absolute().toUri().toString())
.replace("<PREFIX>", Path(System.getProperty("user.dir")).toUri().toString())

assertThat(report).isEqualToIgnoringWhitespace(expectedReport)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class XmlOutputFormatSpec {
"""
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="4.3">
<file name="${entity1.location.filePath.absolutePath.invariantSeparatorsPathString}">
<file name="${entity1.location.path.absolute().invariantSeparatorsPathString}">
$TAB<error line="11" column="1" severity="error" message="TestMessage" source="detekt.id_a" />
</file>
</checkstyle>
Expand All @@ -81,7 +81,7 @@ class XmlOutputFormatSpec {
"""
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="4.3">
<file name="${entity1.location.filePath.absolutePath.invariantSeparatorsPathString}">
<file name="${entity1.location.path.absolute().invariantSeparatorsPathString}">
$TAB<error line="11" column="1" severity="error" message="TestMessage" source="detekt.id_a" />
$TAB<error line="11" column="1" severity="error" message="TestMessage" source="detekt.id_b" />
</file>
Expand All @@ -101,10 +101,10 @@ class XmlOutputFormatSpec {
"""
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="4.3">
<file name="${entity1.location.filePath.absolutePath.invariantSeparatorsPathString}">
<file name="${entity1.location.path.absolute().invariantSeparatorsPathString}">
$TAB<error line="11" column="1" severity="error" message="TestMessage" source="detekt.id_a" />
</file>
<file name="${entity2.location.filePath.absolutePath.invariantSeparatorsPathString}">
<file name="${entity2.location.path.absolute().invariantSeparatorsPathString}">
$TAB<error line="22" column="2" severity="error" message="TestMessage" source="detekt.id_a" />
</file>
</checkstyle>
Expand Down Expand Up @@ -165,11 +165,11 @@ class XmlOutputFormatSpec {
"""
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="4.3">
<file name="${entity1.location.filePath.absolutePath.invariantSeparatorsPathString}">
<file name="${entity1.location.path.absolute().invariantSeparatorsPathString}">
$TAB<error line="11" column="1" severity="error" message="TestMessage" source="detekt.id_a" />
$TAB<error line="11" column="1" severity="error" message="TestMessage" source="detekt.id_b" />
</file>
<file name="${entity2.location.filePath.absolutePath.invariantSeparatorsPathString}">
<file name="${entity2.location.path.absolute().invariantSeparatorsPathString}">
$TAB<error line="22" column="2" severity="error" message="TestMessage" source="detekt.id_a" />
$TAB<error line="22" column="2" severity="error" message="TestMessage" source="detekt.id_b" />
</file>
Expand All @@ -195,7 +195,7 @@ class XmlOutputFormatSpec {
val expected = """
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="4.3">
<file name="${entity1.location.filePath.absolutePath.invariantSeparatorsPathString}">
<file name="${entity1.location.path.absolute().invariantSeparatorsPathString}">
$TAB<error line="${issue.location.source.line}" column="${issue.location.source.column}" severity="$xmlSeverity" message="${issue.message}" source="detekt.${issue.ruleInfo.id}" />
</file>
</checkstyle>
Expand Down

0 comments on commit 8f4483c

Please sign in to comment.