From ef45db7a76b128c01d31eaa0d3601f32a4df400e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Kwiecin=CC=81ski?= Date: Sat, 4 Feb 2023 18:55:42 +0100 Subject: [PATCH] Add unit-tests --- .../tasks/workers/ConsoleReportWorker.kt | 39 ++-- .../usefulness/functional/BaselineTest.kt | 195 ++++++++++++++++++ 2 files changed, 213 insertions(+), 21 deletions(-) create mode 100644 ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/BaselineTest.kt diff --git a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/tasks/workers/ConsoleReportWorker.kt b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/tasks/workers/ConsoleReportWorker.kt index f94f0c1..c1e6420 100644 --- a/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/tasks/workers/ConsoleReportWorker.kt +++ b/ktlint-gradle-plugin/src/main/kotlin/io/github/usefulness/tasks/workers/ConsoleReportWorker.kt @@ -26,23 +26,29 @@ internal abstract class ConsoleReportWorker : WorkAction - val baselineErrors = baselineContent[file.getBaselineKey(projectDir)].orEmpty() - errors.forEach { (lintError, corrected) -> - if (baselineErrors.doesNotContain(lintError)) { - printError( - file = file, - lintError = lintError, - corrected = corrected, - ) + var hasUncoveredErrors = false + discoveredErrors.forEach { (file, errors) -> + val baselineErrors = baselineContent[file.getBaselineKey(projectDir)].orEmpty() + errors.forEach { (lintError, corrected) -> + when (mode) { + KtlintRunMode.Check -> + if (baselineErrors.doesNotContain(lintError)) { + logger.warn(lintError.generateMessage(file = file, message = "Lint error")) + hasUncoveredErrors = true + } + + KtlintRunMode.Format -> { + when (corrected) { + true -> logger.quiet(lintError.generateMessage(file = file, message = "Format fixed")) + false -> logger.warn(lintError.generateMessage(file = file, message = "Format could not fix")) + } + hasUncoveredErrors = true } } } } - if (!parameters.ignoreFailures.get() && errorsFound) { + if (!parameters.ignoreFailures.get() && hasUncoveredErrors) { val message = when (mode) { KtlintRunMode.Check -> "ktlint check failed" KtlintRunMode.Format -> "Format failed to autocorrect" @@ -51,15 +57,6 @@ internal abstract class ConsoleReportWorker : WorkAction logger.warn(lintError.generateMessage(file, message = "Lint error")) - - KtlintRunMode.Format -> when (corrected) { - true -> logger.quiet(lintError.generateMessage(file, message = "Format fixed")) - false -> logger.warn(lintError.generateMessage(file, message = "Format could not fix")) - } - } - private fun LintError.generateMessage(file: File, message: String) = "${file.path}:$line:$col: $message > [$ruleId] $detail" interface Parameters : WorkParameters { diff --git a/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/BaselineTest.kt b/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/BaselineTest.kt new file mode 100644 index 0000000..3753cae --- /dev/null +++ b/ktlint-gradle-plugin/src/test/kotlin/io/github/usefulness/functional/BaselineTest.kt @@ -0,0 +1,195 @@ +package io.github.usefulness.functional + +import io.github.usefulness.functional.utils.kotlinClass +import io.github.usefulness.functional.utils.resolve +import io.github.usefulness.functional.utils.settingsFile +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import java.io.File + +class BaselineTest : WithGradleTest.Kotlin() { + lateinit var projectRoot: File + + @BeforeEach + fun setup() { + projectRoot = testProjectDir.apply { + resolve("settings.gradle") { writeText(settingsFile) } + resolve("build.gradle") { + // language=groovy + writeText( + """ + plugins { + id 'org.jetbrains.kotlin.jvm' + id 'io.github.usefulness.ktlint-gradle-plugin' + } + + ktlint { + baselineFile.set(file('config/baseline.xml')) + } + + repositories.mavenCentral() + + """.trimIndent(), + ) + } + resolve("src/main/kotlin/ClassOne.kt") { + writeText(kotlinClass("ClassOne")) + } + } + } + + @Test + fun `lintKotlin respects baseline`() { + projectRoot.resolve("src/main/kotlin/WithOffence.kt") { + writeText(kotlinClass("InvalidName")) + } + projectRoot.resolve("src/main/kotlin/CustomClass.kt") { + // language=kotlin + val validClass = + """ + class CustomClass { + private fun go(){ + println("go") + } + } + + """.trimIndent() + writeText(validClass) + } + projectRoot.resolve("config/baseline.xml") { + // language=xml + writeText( + """ + + + + + + + + + + + """.trimIndent(), + ) + } + + build("lintKotlin").apply { + assertThat(output).doesNotContain("Lint error >") + } + + projectRoot.resolve("src/main/kotlin/CustomClass.kt") { + // language=kotlin + val validClass = + """ + class CustomClass { + private fun go() { + println("go") + } + } + + """.trimIndent() + writeText(validClass) + } + buildAndFail("lintKotlin").apply { + assertThat(output).contains("CustomClass.kt:2:22: Lint error > [no-multi-spaces]") + } + + projectRoot.resolve("config/baseline.xml") { + // language=xml + writeText( + """ + + + + + + + + + + + """.trimIndent(), + ) + } + build("lintKotlin").apply { + assertThat(output).doesNotContain("Lint error") + } + } + + @Test + fun `formatKotlin doesn't respect baseline`() { + projectRoot.resolve("src/main/kotlin/WithOffence.kt") { + writeText(kotlinClass("InvalidName")) + } + projectRoot.resolve("src/main/kotlin/CustomClass.kt") { + // language=kotlin + val validClass = + """ + class CustomClass { + private fun go(){ + println("go") + } + } + + """.trimIndent() + writeText(validClass) + } + projectRoot.resolve("config/baseline.xml") { + // language=xml + writeText( + """ + + + + + + + + + + + """.trimIndent(), + ) + } + build("formatKotlin").apply { + assertThat(output).contains("CustomClass.kt:2:21: Format fixed > [curly-spacing]") + } + + projectRoot.resolve("src/main/kotlin/CustomClass.kt") { + // language=kotlin + val validClass = + """ + class CustomClass { + private fun go() { + println("go") + } + } + + """.trimIndent() + writeText(validClass) + } + + projectRoot.resolve("config/baseline.xml") { + // language=xml + writeText( + """ + + + + + + + + + + + """.trimIndent(), + ) + } + build("formatKotlin").apply { + assertThat(output).contains("CustomClass.kt:2:22: Format fixed > [no-multi-spaces]") + } + } +}