forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add baseline functionality to ktlint (pinterest#707)
* Add baseline functionality to ktlint * Fix up to use add reporter so that no other reporters are overwritten * Fix ktlint issues * Add baseline reporter * Add relative layout comparison for easier to use baseline file on VCS and add in tests for baseline functionality * Make baseline platform agnostic * Make tests file system agnostic * Add baseline * Cleanup path routing * Fix indentation issues * Fix after rebase
- Loading branch information
1 parent
48a420e
commit dbfbfc7
Showing
18 changed files
with
462 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<baseline version="1.0"> | ||
<file name="ktlint/src/test/resources/TestBaselineExtraErrorFile.kt"> | ||
<error line="1" column="34" source="no-empty-class-body" /> | ||
<error line="2" column="1" source="no-blank-line-before-rbrace" /> | ||
</file> | ||
<file name="ktlint/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> | ||
</baseline> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
plugins { | ||
id 'org.jetbrains.kotlin.jvm' | ||
id 'ktlint-publication' | ||
} | ||
|
||
dependencies { | ||
implementation project(':ktlint-core') | ||
implementation deps.kotlin.stdlib | ||
|
||
testImplementation deps.junit | ||
testImplementation deps.assertj | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
GROUP=com.pinterest.ktlint | ||
POM_NAME=ktlint-reporter-baseline | ||
POM_ARTIFACT_ID=ktlint-reporter-baseline | ||
POM_PACKAGING=jar |
46 changes: 46 additions & 0 deletions
46
...orter-baseline/src/main/kotlin/com/pinterest/ktlint/reporter/baseline/BaselineReporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.pinterest.ktlint.reporter.baseline | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import com.pinterest.ktlint.core.Reporter | ||
import java.io.File | ||
import java.io.PrintStream | ||
import java.nio.file.Paths | ||
import java.util.ArrayList | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
class BaselineReporter(val out: PrintStream) : Reporter { | ||
|
||
private val acc = ConcurrentHashMap<String, MutableList<LintError>>() | ||
|
||
override fun onLintError(file: String, err: LintError, corrected: Boolean) { | ||
if (!corrected) { | ||
acc.getOrPut(file) { ArrayList<LintError>() }.add(err) | ||
} | ||
} | ||
|
||
override fun afterAll() { | ||
out.println("""<?xml version="1.0" encoding="utf-8"?>""") | ||
out.println("""<baseline version="1.0">""") | ||
for ((file, errList) in acc.entries.sortedBy { it.key }) { | ||
val fileName = try { | ||
val rootPath = Paths.get("").toAbsolutePath() | ||
val filePath = Paths.get(file) | ||
rootPath.relativize(filePath).toString().replace(File.separatorChar, '/') | ||
} catch (e: IllegalArgumentException) { | ||
file | ||
} | ||
out.println(""" <file name="${fileName.escapeXMLAttrValue()}">""") | ||
for ((line, col, ruleId, _) in errList) { | ||
out.println( | ||
""" <error line="$line" column="$col" source="$ruleId" />""" | ||
) | ||
} | ||
out.println(""" </file>""") | ||
} | ||
out.println("""</baseline>""") | ||
} | ||
|
||
private fun String.escapeXMLAttrValue() = | ||
this.replace("&", "&").replace("\"", """).replace("'", "'") | ||
.replace("<", "<").replace(">", ">") | ||
} |
10 changes: 10 additions & 0 deletions
10
...seline/src/main/kotlin/com/pinterest/ktlint/reporter/baseline/BaselineReporterProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.pinterest.ktlint.reporter.baseline | ||
|
||
import com.pinterest.ktlint.core.Reporter | ||
import com.pinterest.ktlint.core.ReporterProvider | ||
import java.io.PrintStream | ||
|
||
class BaselineReporterProvider : ReporterProvider { | ||
override val id: String = "baseline" | ||
override fun get(out: PrintStream, opt: Map<String, String>): Reporter = BaselineReporter(out) | ||
} |
1 change: 1 addition & 0 deletions
1
...-baseline/src/main/resources/META-INF/services/com.pinterest.ktlint.core.ReporterProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.pinterest.ktlint.reporter.baseline.BaselineReporterProvider |
75 changes: 75 additions & 0 deletions
75
...r-baseline/src/test/kotlin/com/pinterest/ktlint/reporter/baseline/BaselineReporterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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( | ||
"$basePath/one-fixed-and-one-not.kt", | ||
LintError( | ||
1, 1, "rule-1", | ||
"<\"&'>" | ||
), | ||
false | ||
) | ||
reporter.onLintError( | ||
"$basePath/one-fixed-and-one-not.kt", | ||
LintError( | ||
2, 1, "rule-2", | ||
"And if you see my friend" | ||
), | ||
true | ||
) | ||
|
||
reporter.onLintError( | ||
"$basePath/two-not-fixed.kt", | ||
LintError( | ||
1, 10, "rule-1", | ||
"I thought I would again" | ||
), | ||
false | ||
) | ||
reporter.onLintError( | ||
"$basePath/two-not-fixed.kt", | ||
LintError( | ||
2, 20, "rule-2", | ||
"A single thin straight line" | ||
), | ||
false | ||
) | ||
|
||
reporter.onLintError( | ||
"$basePath/all-corrected.kt", | ||
LintError( | ||
1, 1, "rule-1", | ||
"I thought we had more time" | ||
), | ||
true | ||
) | ||
reporter.afterAll() | ||
assertThat(String(out.toByteArray())).isEqualTo( | ||
""" | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<baseline version="1.0"> | ||
<file name="one-fixed-and-one-not.kt"> | ||
<error line="1" column="1" source="rule-1" /> | ||
</file> | ||
<file name="two-not-fixed.kt"> | ||
<error line="1" column="10" source="rule-1" /> | ||
<error line="2" column="20" source="rule-2" /> | ||
</file> | ||
</baseline> | ||
""".trimStart().replace("\n", System.lineSeparator()) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.