Skip to content

Commit

Permalink
Reporting compiler style absolute file paths and code lines
Browse files Browse the repository at this point in the history
  • Loading branch information
kruthar committed May 23, 2024
1 parent 3b8184c commit 85da22d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/it/check/postbuild.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
* #L%
*/
String buildLog = new File(basedir, 'build.log').text
assert buildLog.contains('[ERROR] src/main/kotlin/example/Example.kt:29:39: Unnecessary semicolon')
assert buildLog =~ /\[ERROR] .*\/src\/main\/kotlin\/example\/Example.kt:\[29,39] Unnecessary semicolon/
assert buildLog =~ /\Q[ERROR] Failed to execute goal com.github.gantsign.maven:ktlint-maven-plugin:\E[0-9.]+(-SNAPSHOT)?\Q:check (check) on project test-project: Kotlin source failed ktlint check. -> [Help 1]\E/
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class MavenLogReporter(
val buf = MessageUtils.buffer()
.a(file.dir())
.strong(file.name())
.a(":")
.a(":[")
.strong(line)
.a(":$col:".pad(4))
.a(",$col]".pad(4))
.a(" ")
.failure(detail)
if (verbose) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,17 @@ internal abstract class AbstractCheckSupport(
return@forEach
}

val baseRelativePath = file.toRelativeString(basedir)
log.debug("checking: $baseRelativePath")
val absolutePath = file.absolutePath
log.debug("checking: $absolutePath")

val ktlintCliErrors = lint(
ktLintRuleEngine = ktLintRuleEngine,
code = Code.fromFile(file),
)
report(baseRelativePath, ktlintCliErrors, reporter)
report(absolutePath, ktlintCliErrors, reporter)
ktlintCliErrors
.asSequence()
.map { "$baseRelativePath:${it.line}:${it.col}: ${it.detail}" }
.map { "$absolutePath:[${it.line},${it.col}] ${it.detail}" }
.forEach { log.debug("Style error > $it") }
if (!ktlintCliErrors.isEmpty()) {
hasErrors = true
Expand All @@ -242,18 +242,18 @@ internal abstract class AbstractCheckSupport(
}

private fun report(
relativeRoute: String,
absolutePath: String,
ktlintCliErrors: List<KtlintCliError>,
reporter: ReporterV2,
) {
if (!adviseToUseFormat.get() && ktlintCliErrors.containsErrorThatCanBeAutocorrected()) {
adviseToUseFormat.set(true)
}

reporter.before(relativeRoute)
reporter.before(absolutePath)
ktlintCliErrors
.forEach { reporter.onLintError(relativeRoute, it) }
reporter.after(relativeRoute)
.forEach { reporter.onLintError(absolutePath, it) }
reporter.after(absolutePath)
}

private fun List<KtlintCliError>.containsErrorThatCanBeAutocorrected() = any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class CheckMojoTest {

@Test
fun hasErrors() {
val pom = File("target/test-scenarios/check-with-errors/pom.xml")
val basedir = File("target/test-scenarios/check-with-errors")
val basepath = basedir.absolutePath
val pom = File(basedir, "pom.xml")

assertThat(pom.isFile).isTrue()

Expand Down Expand Up @@ -78,16 +80,18 @@ class CheckMojoTest {
verify { log.debug("Discovered reporter 'json'") }
verify { log.debug("Discovered reporter 'checkstyle'") }
verify { log.debug("Initializing 'maven' reporter with {verbose=false, color=false, color_name=DARK_GRAY}") }
verify { log.debug("checking: src/main/kotlin/example/Example.kt") }
verify { log.debug("Style error > src/main/kotlin/example/Example.kt:29:39: Unnecessary semicolon") }
verify { log.error("src/main/kotlin/example/Example.kt:29:39: Unnecessary semicolon") }
verify { log.debug("checking: $basepath/src/main/kotlin/example/Example.kt") }
verify { log.debug("Style error > $basepath/src/main/kotlin/example/Example.kt:[29,39] Unnecessary semicolon") }
verify { log.error("$basepath/src/main/kotlin/example/Example.kt:[29,39] Unnecessary semicolon") }
verify { log.warn("Source root doesn't exist: src/test/kotlin") }
confirmVerified(log)
}

@Test
fun groupByFile() {
val pom = File("target/test-scenarios/check-group-by-file/pom.xml")
val basedir = File("target/test-scenarios/check-group-by-file")
val basepath = basedir.absolutePath
val pom = File(basedir, "pom.xml")

assertThat(pom.isFile).isTrue()

Expand Down Expand Up @@ -120,17 +124,19 @@ class CheckMojoTest {
"group_by_file=true}",
)
}
verify { log.debug("checking: src/main/kotlin/example/Example.kt") }
verify { log.debug("Style error > src/main/kotlin/example/Example.kt:29:39: Unnecessary semicolon") }
verify { log.error("src/main/kotlin/example/Example.kt") }
verify { log.debug("checking: $basepath/src/main/kotlin/example/Example.kt") }
verify { log.debug("Style error > $basepath/src/main/kotlin/example/Example.kt:[29,39] Unnecessary semicolon") }
verify { log.error("$basepath/src/main/kotlin/example/Example.kt") }
verify { log.error(" 29:39 Unnecessary semicolon (standard:no-semi)") }
verify { log.warn("Source root doesn't exist: src/test/kotlin") }
confirmVerified(log)
}

@Test
fun proceedWithErrors() {
val pom = File("target/test-scenarios/check-proceed-with-errors/pom.xml")
val basedir = File("target/test-scenarios/check-proceed-with-errors")
val basepath = basedir.absolutePath
val pom = File(basedir, "pom.xml")

assertThat(pom.isFile).isTrue()

Expand All @@ -153,16 +159,17 @@ class CheckMojoTest {
verify { log.debug("Discovered reporter 'json'") }
verify { log.debug("Discovered reporter 'checkstyle'") }
verify { log.debug("Initializing 'maven' reporter with {verbose=false, color=false, color_name=DARK_GRAY}") }
verify { log.debug("checking: src/main/kotlin/example/Example.kt") }
verify { log.debug("Style error > src/main/kotlin/example/Example.kt:29:39: Unnecessary semicolon") }
verify { log.error("src/main/kotlin/example/Example.kt:29:39: Unnecessary semicolon") }
verify { log.debug("checking: $basepath/src/main/kotlin/example/Example.kt") }
verify { log.debug("Style error > $basepath/src/main/kotlin/example/Example.kt:[29,39] Unnecessary semicolon") }
verify { log.error("$basepath/src/main/kotlin/example/Example.kt:[29,39] Unnecessary semicolon") }
verify { log.warn("Source root doesn't exist: src/test/kotlin") }
confirmVerified(log)
}

@Test
fun outputFile() {
val basedir = File("target/test-scenarios/check-output-file")
val basepath = basedir.absolutePath
val pom = File(basedir, "pom.xml")

assertThat(pom.isFile).isTrue()
Expand All @@ -188,7 +195,7 @@ class CheckMojoTest {
"""
[
{
"file": "src/main/kotlin/example/Example.kt",
"file": "$basepath/src/main/kotlin/example/Example.kt",
"errors": [
{
"line": 29,
Expand All @@ -199,7 +206,7 @@ class CheckMojoTest {
]
},
{
"file": "src/test/kotlin/example/TestExample.kt",
"file": "$basepath/src/test/kotlin/example/TestExample.kt",
"errors": [
{
"line": 29,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class KtlintReportTest {

@Test
fun hasErrors() {
val pom = File("target/test-scenarios/check-with-errors/pom.xml")
val basedir = File("target/test-scenarios/check-with-errors")
val basepath = basedir.absolutePath
val pom = File(basedir, "pom.xml")

assertThat(pom.isFile).isTrue()

Expand All @@ -68,8 +70,8 @@ class KtlintReportTest {
verify { log.debug("Discovered reporter 'plain'") }
verify { log.debug("Discovered reporter 'json'") }
verify { log.debug("Discovered reporter 'checkstyle'") }
verify { log.debug("checking: src/main/kotlin/example/Example.kt") }
verify { log.debug("Style error > src/main/kotlin/example/Example.kt:29:39: Unnecessary semicolon") }
verify { log.debug("checking: $basepath/src/main/kotlin/example/Example.kt") }
verify { log.debug("Style error > $basepath/src/main/kotlin/example/Example.kt:[29,39] Unnecessary semicolon") }
verify { log.warn("Source root doesn't exist: src/test/kotlin") }
confirmVerified(log)
}
Expand Down

0 comments on commit 85da22d

Please sign in to comment.