From 8f4483c3bab18cc61a7091992cc173e2081578fb Mon Sep 17 00:00:00 2001 From: Matthew Haughton <3flex@users.noreply.github.com> Date: Sat, 27 Apr 2024 17:05:13 +1000 Subject: [PATCH] Update tests --- .../arturbosch/detekt/api/CodeSmellSpec.kt | 9 +++---- .../detekt/api/CorrectableCodeSmellSpec.kt | 6 +++-- .../arturbosch/detekt/api/EntitySpec.kt | 10 +++----- .../arturbosch/detekt/api/LocationSpec.kt | 3 +-- .../arturbosch/detekt/test/TestFactory.kt | 14 ++--------- .../console/FileBasedIssuesReportSpec.kt | 5 ++-- .../detekt/formatting/FormattingRuleSpec.kt | 3 ++- .../report/html/HtmlOutputReportSpec.kt | 24 ++++++++++++------- .../test/resources/HtmlOutputFormatTest.html | 6 ++--- .../report/sarif/SarifOutputReportSpec.kt | 4 ++-- .../detekt/report/xml/XmlOutputFormatSpec.kt | 14 +++++------ 11 files changed, 46 insertions(+), 52 deletions(-) diff --git a/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/CodeSmellSpec.kt b/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/CodeSmellSpec.kt index ff64dab296c4..082d4339ad4b 100644 --- a/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/CodeSmellSpec.kt +++ b/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/CodeSmellSpec.kt @@ -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 { @@ -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 ), @@ -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=[])" ) } diff --git a/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/CorrectableCodeSmellSpec.kt b/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/CorrectableCodeSmellSpec.kt index 65ab3b1eb629..78a3ef213117 100644 --- a/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/CorrectableCodeSmellSpec.kt +++ b/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/CorrectableCodeSmellSpec.kt @@ -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 { @@ -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=[])" ) } } diff --git a/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/EntitySpec.kt b/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/EntitySpec.kt index 906e1b0294aa..2e3e67a2ff64 100644 --- a/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/EntitySpec.kt +++ b/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/EntitySpec.kt @@ -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 @@ -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)" ) } @@ -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)" ) } @@ -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)" ) } diff --git a/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/LocationSpec.kt b/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/LocationSpec.kt index c31675ecbc47..e9f31eae7dc7 100644 --- a/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/LocationSpec.kt +++ b/detekt-api/src/test/kotlin/io/gitlab/arturbosch/detekt/api/LocationSpec.kt @@ -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})" ) } } diff --git a/detekt-api/src/testFixtures/kotlin/io/gitlab/arturbosch/detekt/test/TestFactory.kt b/detekt-api/src/testFixtures/kotlin/io/gitlab/arturbosch/detekt/test/TestFactory.kt index 85ef42bda274..c0c7164691e2 100644 --- a/detekt-api/src/testFixtures/kotlin/io/gitlab/arturbosch/detekt/test/TestFactory.kt +++ b/detekt-api/src/testFixtures/kotlin/io/gitlab/arturbosch/detekt/test/TestFactory.kt @@ -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 @@ -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 @@ -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 ), @@ -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(), ) } @@ -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 -) diff --git a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/reporting/console/FileBasedIssuesReportSpec.kt b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/reporting/console/FileBasedIssuesReportSpec.kt index 379151959cf1..23170c9b6417 100644 --- a/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/reporting/console/FileBasedIssuesReportSpec.kt +++ b/detekt-core/src/test/kotlin/io/gitlab/arturbosch/detekt/core/reporting/console/FileBasedIssuesReportSpec.kt @@ -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 { @@ -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() diff --git a/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/FormattingRuleSpec.kt b/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/FormattingRuleSpec.kt index 493ff12c808c..f560903d8906 100644 --- a/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/FormattingRuleSpec.kt +++ b/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/FormattingRuleSpec.kt @@ -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 { @@ -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()) } } } diff --git a/detekt-report-html/src/test/kotlin/io/github/detekt/report/html/HtmlOutputReportSpec.kt b/detekt-report-html/src/test/kotlin/io/github/detekt/report/html/HtmlOutputReportSpec.kt index 3189e08c88a3..b3a0f46c34e3 100644 --- a/detekt-report-html/src/test/kotlin/io/github/detekt/report/html/HtmlOutputReportSpec.kt +++ b/detekt-report-html/src/test/kotlin/io/github/detekt/report/html/HtmlOutputReportSpec.kt @@ -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("${root}src/main/com/sample/Sample1.kt:11:1") - assertThat(result).contains("${root}src/main/com/sample/Sample2.kt:22:2") - assertThat(result).contains("${root}src/main/com/sample/Sample3.kt:33:3") + assertThat(result).contains("$root/src/main/com/sample/Sample1.kt:11:1") + assertThat(result).contains("$root/src/main/com/sample/Sample2.kt:22:2") + assertThat(result).contains("$root/src/main/com/sample/Sample3.kt:33:3") } @Test @@ -165,7 +165,10 @@ class HtmlOutputReportSpec { @Test fun `asserts that the generated HTML is the same as expected`() { val expectedString = readResourceContent("HtmlOutputFormatTest.html") - .replace("", Path("/").absolute().invariantSeparatorsPathString) + .replace( + "", + Path("${System.getProperty("user.dir")}/Users/tester/detekt/").invariantSeparatorsPathString + ) val expected = createTempFileForTest("expected-report", ".html").apply { writeText(expectedString) } val result = htmlReport.render(createTestDetektionWithMultipleSmells()) @@ -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"), diff --git a/detekt-report-html/src/test/resources/HtmlOutputFormatTest.html b/detekt-report-html/src/test/resources/HtmlOutputFormatTest.html index fad267100fb2..b8a89b8989c3 100644 --- a/detekt-report-html/src/test/resources/HtmlOutputFormatTest.html +++ b/detekt-report-html/src/test/resources/HtmlOutputFormatTest.html @@ -174,7 +174,7 @@ RuleSet1: 2 id_a: 2 Description id_a Documentation - src/main/com/sample/Sample1.kt:11:1Issue message 1 + /src/main/com/sample/Sample1.kt:11:1Issue message 1 8 9 10 @@ -183,7 +183,7 @@ RuleSet1: 2 13 - src/main/com/sample/Sample2.kt:22:2Issue message 2 + /src/main/com/sample/Sample2.kt:22:2Issue message 2 RuleSet2: 1 @@ -191,7 +191,7 @@ RuleSet2: 1 id_b: 1 Description id_b Documentation - src/main/com/sample/Sample3.kt:33:3Issue message 3 + /src/main/com/sample/Sample3.kt:33:3Issue message 3 diff --git a/detekt-report-sarif/src/test/kotlin/io/github/detekt/report/sarif/SarifOutputReportSpec.kt b/detekt-report-sarif/src/test/kotlin/io/github/detekt/report/sarif/SarifOutputReportSpec.kt index 26878db82c2f..71ef3edaed09 100644 --- a/detekt-report-sarif/src/test/kotlin/io/github/detekt/report/sarif/SarifOutputReportSpec.kt +++ b/detekt-report-sarif/src/test/kotlin/io/github/detekt/report/sarif/SarifOutputReportSpec.kt @@ -52,7 +52,7 @@ class SarifOutputReportSpec { .render(result) val expectedReport = readResourceContent("vanilla.sarif.json") - .replace("", Path("/").absolute().toUri().toString()) + .replace("", Path(System.getProperty("user.dir")).toUri().toString()) assertThat(report).isEqualToIgnoringWhitespace(expectedReport) } @@ -83,7 +83,7 @@ class SarifOutputReportSpec { .render(result) val expectedReport = readResourceContent("rule_warning.sarif.json") - .replace("", Path("/").absolute().toUri().toString()) + .replace("", Path(System.getProperty("user.dir")).toUri().toString()) assertThat(report).isEqualToIgnoringWhitespace(expectedReport) } diff --git a/detekt-report-xml/src/test/kotlin/io/github/detekt/report/xml/XmlOutputFormatSpec.kt b/detekt-report-xml/src/test/kotlin/io/github/detekt/report/xml/XmlOutputFormatSpec.kt index 64037bba7637..01a80461f370 100644 --- a/detekt-report-xml/src/test/kotlin/io/github/detekt/report/xml/XmlOutputFormatSpec.kt +++ b/detekt-report-xml/src/test/kotlin/io/github/detekt/report/xml/XmlOutputFormatSpec.kt @@ -62,7 +62,7 @@ class XmlOutputFormatSpec { """ - + $TAB @@ -81,7 +81,7 @@ class XmlOutputFormatSpec { """ - + $TAB $TAB @@ -101,10 +101,10 @@ class XmlOutputFormatSpec { """ - + $TAB - + $TAB @@ -165,11 +165,11 @@ class XmlOutputFormatSpec { """ - + $TAB $TAB - + $TAB $TAB @@ -195,7 +195,7 @@ class XmlOutputFormatSpec { val expected = """ - + $TAB
8 9 10 @@ -183,7 +183,7 @@ RuleSet1: 2 13