Skip to content

Commit

Permalink
Make tests file system agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
joshafeinberg committed Feb 28, 2020
1 parent c48f9c7 commit 513a605
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class BaselineReporter(val out: PrintStream) : Reporter {
Paths.get("").toAbsolutePath().relativize(File(file).toPath()).toString().replace('\\', '/')
} catch (e: IllegalArgumentException) {
file
}.let { name ->
if (name[0] != '/') {
"/$name"
} else {
name
}
}
out.println(""" <file name="${fileName.escapeXMLAttrValue()}">""")
for ((line, col, ruleId, _) in errList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ package com.pinterest.ktlint.reporter.baseline
import com.pinterest.ktlint.core.LintError
import java.io.ByteArrayOutputStream
import java.io.PrintStream
import java.nio.file.Paths
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class BaselineReporterTest {

@Test
fun testReportGeneration() {
val basePath = Paths.get("").toAbsolutePath()
val out = ByteArrayOutputStream()
val reporter = BaselineReporter(PrintStream(out, true))
reporter.onLintError(
"/one-fixed-and-one-not.kt",
"$basePath/one-fixed-and-one-not.kt",
LintError(
1, 1, "rule-1",
"<\"&'>"
),
false
)
reporter.onLintError(
"/one-fixed-and-one-not.kt",
"$basePath/one-fixed-and-one-not.kt",
LintError(
2, 1, "rule-2",
"And if you see my friend"
Expand All @@ -30,15 +32,15 @@ class BaselineReporterTest {
)

reporter.onLintError(
"/two-not-fixed.kt",
"$basePath/two-not-fixed.kt",
LintError(
1, 10, "rule-1",
"I thought I would again"
),
false
)
reporter.onLintError(
"/two-not-fixed.kt",
"$basePath/two-not-fixed.kt",
LintError(
2, 20, "rule-2",
"A single thin straight line"
Expand All @@ -47,7 +49,7 @@ class BaselineReporterTest {
)

reporter.onLintError(
"/all-corrected.kt",
"$basePath/all-corrected.kt",
LintError(
1, 1, "rule-1",
"I thought we had more time"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,10 @@ internal fun List<LintError>.containsLintError(error: LintError): Boolean {
* Also adjusts the slashes for uniformity between file systems
*/
internal val File.relativeRoute: String
get() = Paths.get("").toAbsolutePath().relativize(this.toPath()).toString().replace('\\', '/')
get() = Paths.get("").toAbsolutePath().relativize(this.toPath()).toString().replace('\\', '/').let { name ->
if (name[0] != '/') {
"/$name"
} else {
name
}
}
4 changes: 2 additions & 2 deletions ktlint/src/test/resources/test-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="src/test/resources/TestBaselineFile.kt">
<file name="/src/test/resources/TestBaselineFile.kt">
<error line="1" column="24" source="no-empty-class-body" />
<error line="2" column="1" source="no-blank-line-before-rbrace" />
</file>
<file name="src/test/resources/TestBaselineExtraErrorFile.kt">
<file name="/src/test/resources/TestBaselineExtraErrorFile.kt">
<error line="1" column="24" source="no-empty-class-body" />
</file>
</baseline>

0 comments on commit 513a605

Please sign in to comment.