Skip to content

Commit

Permalink
Return the unmodified filename from the baseline (#1972)
Browse files Browse the repository at this point in the history
Closes #1962
  • Loading branch information
paul-dingemans authored Apr 23, 2023
1 parent e62e569 commit d195ffd
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import java.nio.file.Path
import java.nio.file.Paths
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.ParserConfigurationException
import kotlin.io.path.absolutePathString
import kotlin.io.path.pathString
import kotlin.io.path.relativeToOrSelf

Expand Down Expand Up @@ -121,12 +120,7 @@ private class BaselineLoader(private val path: String) {
with(parseDocument().getElementsByTagName("file")) {
for (i in 0 until length) {
with(item(i) as Element) {
// Use relative path to file starting from baseline path
val fileName =
Paths
.get(getAttribute("name"))
.absolutePathString()
.removePrefix(path)
val fileName = getAttribute("name")
lintErrorsPerFile[fileName] = parseBaselineFileElement()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ import java.util.concurrent.atomic.AtomicInteger
import kotlin.collections.ArrayList
import kotlin.collections.LinkedHashSet
import kotlin.concurrent.thread
import kotlin.io.path.absolutePathString
import kotlin.io.path.pathString
import kotlin.io.path.relativeToOrSelf
import kotlin.system.exitProcess
Expand Down Expand Up @@ -410,7 +409,7 @@ internal class KtlintCommandLine {
.map { it.toFile() }
.takeWhile { errorNumber.get() < limit }
.map { file ->
val fileName = file.toPath().absolutePathString()
val fileName = file.toPath().relativeRoute
Callable {
fileName to
process(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.pinterest.ktlint.cli.CommandLineTestRunner
import com.pinterest.ktlint.cli.containsLineMatching
import com.pinterest.ktlint.cli.doesNotContainLineMatching
import org.assertj.core.api.SoftAssertions
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path
Expand Down Expand Up @@ -33,38 +34,110 @@ class BaselineCLITest {
}
}

@Test
fun `Given files containing lint errors which are all registered in the baseline file and as of that are all ignored`(
@TempDir
tempDir: Path,
) {
val baselinePath = "test-baseline.xml"
@Nested
inner class `Given files containing lint errors which are all registered in the baseline file and as of that are all ignored` {
@Test
fun `Given a baseline file in the root of the working directory`(
@TempDir
tempDir: Path,
) {
val baselinePath = "test-baseline.xml"

CommandLineTestRunner(tempDir)
.run(
"baseline",
listOf(
"--baseline=$baselinePath",
"TestBaselineFile.kt.test",
"some/path/to/TestBaselineFile2.kt.test",
),
) {
SoftAssertions().apply {
assertNormalExitCode()
assertThat(normalOutput)
.doesNotContainLineMatching(Regex("^TestBaselineFile.kt.test:1:24: Unnecessary block.*"))
.doesNotContainLineMatching(Regex("^TestBaselineFile.kt.test:2:1: Unexpected blank line.*"))
.doesNotContainLineMatching(Regex("^some/path/to/TestBaselineFile.kt.test:1:24: Unnecessary block.*"))
.doesNotContainLineMatching(Regex("^some/path/to/TestBaselineFile.kt.test:2:1: Unexpected blank line.*"))
.containsLineMatching(
Regex(
".*Baseline file '$baselinePath' contains 6 reference\\(s\\) to rule ids without a rule set id. For " +
"those references the rule set id 'standard' is assumed. It is advised to regenerate this baseline " +
"file.*",
),
)
}.assertAll()
}
CommandLineTestRunner(tempDir)
.run(
"baseline",
listOf(
"--baseline=$baselinePath",
"TestBaselineFile.kt.test",
"some/path/to/TestBaselineFile2.kt.test",
),
) {
SoftAssertions().apply {
assertNormalExitCode()
assertThat(normalOutput)
.doesNotContainLineMatching(Regex("^TestBaselineFile.kt.test:1:24: Unnecessary block.*"))
.doesNotContainLineMatching(Regex("^TestBaselineFile.kt.test:2:1: Unexpected blank line.*"))
.doesNotContainLineMatching(Regex("^some/path/to/TestBaselineFile.kt.test:1:24: Unnecessary block.*"))
.doesNotContainLineMatching(Regex("^some/path/to/TestBaselineFile.kt.test:2:1: Unexpected blank line.*"))
.containsLineMatching(
Regex(
".*Baseline file '$baselinePath' contains 6 reference\\(s\\) to rule ids without a rule set id. For " +
"those references the rule set id 'standard' is assumed. It is advised to regenerate this " +
"baseline file.*",
),
)
}.assertAll()
}
}

@Test
fun `Given a baseline file in a subdirectory of the working directory`(
@TempDir
tempDir: Path,
) {
val baselinePath = "config/test-baseline.xml"

CommandLineTestRunner(tempDir)
.run(
"baseline",
listOf(
"--baseline=$baselinePath",
"TestBaselineFile.kt.test",
"some/path/to/TestBaselineFile2.kt.test",
),
) {
SoftAssertions().apply {
assertNormalExitCode()
assertThat(normalOutput)
.doesNotContainLineMatching(Regex("^TestBaselineFile.kt.test:1:24: Unnecessary block.*"))
.doesNotContainLineMatching(Regex("^TestBaselineFile.kt.test:2:1: Unexpected blank line.*"))
.doesNotContainLineMatching(Regex("^some/path/to/TestBaselineFile.kt.test:1:24: Unnecessary block.*"))
.doesNotContainLineMatching(Regex("^some/path/to/TestBaselineFile.kt.test:2:1: Unexpected blank line.*"))
.containsLineMatching(
Regex(
".*Baseline file '$baselinePath' contains 6 reference\\(s\\) to rule ids without a rule set id. For " +
"those references the rule set id 'standard' is assumed. It is advised to regenerate this " +
"baseline file.*",
),
)
}.assertAll()
}
}

@Test
fun `Given a baseline file with an absolute path, not necessarily in the working directory or one of its subdirectories`(
@TempDir
tempDir: Path,
) {
val baselinePath = "$tempDir/baseline/config/test-baseline.xml"

CommandLineTestRunner(tempDir)
.run(
"baseline",
listOf(
"--baseline=$baselinePath",
"TestBaselineFile.kt.test",
"some/path/to/TestBaselineFile2.kt.test",
),
) {
SoftAssertions().apply {
assertNormalExitCode()
assertThat(normalOutput)
.doesNotContainLineMatching(Regex("^TestBaselineFile.kt.test:1:24: Unnecessary block.*"))
.doesNotContainLineMatching(Regex("^TestBaselineFile.kt.test:2:1: Unexpected blank line.*"))
.doesNotContainLineMatching(Regex("^some/path/to/TestBaselineFile.kt.test:1:24: Unnecessary block.*"))
.doesNotContainLineMatching(Regex("^some/path/to/TestBaselineFile.kt.test:2:1: Unexpected blank line.*"))
.containsLineMatching(
Regex(
// Escape "\" in baseline path for Windows
".*Baseline file '${baselinePath.replace("\\", "\\\\")}' contains 6 " +
"reference\\(s\\) to rule ids without a rule set id. For those references the rule set id " +
"'standard' is assumed. It is advised to regenerate this baseline file.*",
),
)
}.assertAll()
}
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="TestBaselineFile.kt.test">
<error line="1" column="24" source="no-empty-class-body" />
<error line="2" column="1" source="no-blank-line-before-rbrace" />
</file>
<file name="TestBaselineExtraErrorFile.kt.test">
<error line="1" column="34" source="no-empty-class-body" />
</file>
<file name="some/path/to/TestBaselineFile2.kt.test">
<error line="1" column="25" source="no-empty-class-body" />
<error line="2" column="1" source="no-blank-line-before-rbrace" />
</file>
<file name="some/path/to/TestBaselineExtraErrorFile2.kt.test">
<error line="1" column="35" source="no-empty-class-body" />
</file>
</baseline>

0 comments on commit d195ffd

Please sign in to comment.