Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reporting compiler style absolute file paths and code lines #622

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,22 @@ 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 +128,23 @@ 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 +167,21 @@ 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 +207,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 +218,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,12 @@ 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