From 85da22d7ef2ad335be65450bcdf288b13cd45823 Mon Sep 17 00:00:00 2001 From: Andy Kruth Date: Thu, 23 May 2024 12:40:47 -0400 Subject: [PATCH 1/2] Reporting compiler style absolute file paths and code lines fixes gantsign/ktlint-maven-plugin#582 --- src/it/check/postbuild.groovy | 2 +- .../maven/plugin/ktlint/MavenLogReporter.kt | 4 +-- .../ktlint/internal/AbstractCheckSupport.kt | 16 ++++----- .../maven/plugin/ktlint/CheckMojoTest.kt | 35 +++++++++++-------- .../maven/plugin/ktlint/KtlintReportTest.kt | 8 +++-- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/it/check/postbuild.groovy b/src/it/check/postbuild.groovy index 34dbf7d4..421c710b 100644 --- a/src/it/check/postbuild.groovy +++ b/src/it/check/postbuild.groovy @@ -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/ diff --git a/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/MavenLogReporter.kt b/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/MavenLogReporter.kt index 080c08c5..99d26911 100644 --- a/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/MavenLogReporter.kt +++ b/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/MavenLogReporter.kt @@ -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) { diff --git a/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/internal/AbstractCheckSupport.kt b/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/internal/AbstractCheckSupport.kt index 4b152e30..c0b58af5 100644 --- a/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/internal/AbstractCheckSupport.kt +++ b/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/internal/AbstractCheckSupport.kt @@ -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 @@ -242,7 +242,7 @@ internal abstract class AbstractCheckSupport( } private fun report( - relativeRoute: String, + absolutePath: String, ktlintCliErrors: List, reporter: ReporterV2, ) { @@ -250,10 +250,10 @@ internal abstract class AbstractCheckSupport( 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.containsErrorThatCanBeAutocorrected() = any { diff --git a/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/CheckMojoTest.kt b/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/CheckMojoTest.kt index a588b7f9..212569e3 100644 --- a/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/CheckMojoTest.kt +++ b/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/CheckMojoTest.kt @@ -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() @@ -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() @@ -120,9 +124,9 @@ 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) @@ -130,7 +134,9 @@ class CheckMojoTest { @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() @@ -153,9 +159,9 @@ 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) } @@ -163,6 +169,7 @@ class CheckMojoTest { @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() @@ -188,7 +195,7 @@ class CheckMojoTest { """ [ { - "file": "src/main/kotlin/example/Example.kt", + "file": "$basepath/src/main/kotlin/example/Example.kt", "errors": [ { "line": 29, @@ -199,7 +206,7 @@ class CheckMojoTest { ] }, { - "file": "src/test/kotlin/example/TestExample.kt", + "file": "$basepath/src/test/kotlin/example/TestExample.kt", "errors": [ { "line": 29, diff --git a/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/KtlintReportTest.kt b/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/KtlintReportTest.kt index 9393b0ef..d8a44dd3 100644 --- a/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/KtlintReportTest.kt +++ b/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/KtlintReportTest.kt @@ -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() @@ -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) } From b15929d0952b1bd1e4ed839f09652ae7d81fc1c9 Mon Sep 17 00:00:00 2001 From: Andy Kruth Date: Thu, 23 May 2024 13:14:00 -0400 Subject: [PATCH 2/2] using paranthese instead of square brackets so line numbers are read properly --- src/it/check/postbuild.groovy | 2 +- .../maven/plugin/ktlint/MavenLogReporter.kt | 4 ++-- .../ktlint/internal/AbstractCheckSupport.kt | 2 +- .../maven/plugin/ktlint/CheckMojoTest.kt | 22 ++++++++++++++----- .../maven/plugin/ktlint/KtlintReportTest.kt | 6 ++++- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/it/check/postbuild.groovy b/src/it/check/postbuild.groovy index 421c710b..6bed23af 100644 --- a/src/it/check/postbuild.groovy +++ b/src/it/check/postbuild.groovy @@ -24,5 +24,5 @@ * #L% */ String buildLog = new File(basedir, 'build.log').text -assert buildLog =~ /\[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/ diff --git a/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/MavenLogReporter.kt b/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/MavenLogReporter.kt index 99d26911..0f624145 100644 --- a/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/MavenLogReporter.kt +++ b/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/MavenLogReporter.kt @@ -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) { diff --git a/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/internal/AbstractCheckSupport.kt b/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/internal/AbstractCheckSupport.kt index c0b58af5..3a937a0c 100644 --- a/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/internal/AbstractCheckSupport.kt +++ b/src/main/kotlin/com/github/gantsign/maven/plugin/ktlint/internal/AbstractCheckSupport.kt @@ -229,7 +229,7 @@ internal abstract class AbstractCheckSupport( report(absolutePath, ktlintCliErrors, reporter) ktlintCliErrors .asSequence() - .map { "$absolutePath:[${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 diff --git a/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/CheckMojoTest.kt b/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/CheckMojoTest.kt index 212569e3..70794b07 100644 --- a/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/CheckMojoTest.kt +++ b/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/CheckMojoTest.kt @@ -81,8 +81,12 @@ class CheckMojoTest { 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: $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.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) } @@ -125,7 +129,11 @@ class CheckMojoTest { ) } 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.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") } @@ -160,8 +168,12 @@ class CheckMojoTest { 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: $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.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) } diff --git a/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/KtlintReportTest.kt b/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/KtlintReportTest.kt index d8a44dd3..43d1b24b 100644 --- a/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/KtlintReportTest.kt +++ b/src/test/kotlin/com/github/gantsign/maven/plugin/ktlint/KtlintReportTest.kt @@ -71,7 +71,11 @@ class KtlintReportTest { verify { log.debug("Discovered reporter 'json'") } verify { log.debug("Discovered reporter 'checkstyle'") } 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.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) }