Skip to content

Commit

Permalink
Add unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Feb 4, 2023
1 parent 6dc13a5 commit ef45db7
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,29 @@ internal abstract class ConsoleReportWorker : WorkAction<ConsoleReportWorker.Par
val discoveredErrors = parameters.errorsContainer.readKtlintErrors()
val baselineContent = parameters.baselineFile.orNull?.asFile?.readKtlintBaseline().orEmpty()

val errorsFound = discoveredErrors.any()
if (errorsFound) {
discoveredErrors.forEach { (file, errors) ->
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"
Expand All @@ -51,15 +57,6 @@ internal abstract class ConsoleReportWorker : WorkAction<ConsoleReportWorker.Par
}
}

private fun printError(file: File, lintError: LintError, corrected: Boolean) = when (mode) {
KtlintRunMode.Check -> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
"""
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="src/main/kotlin/WithOffence.kt">
<error line="1" column="1" source="filename" />
</file>
<file name="src/main/kotlin/CustomClass.kt">
<error line="2" column="21" source="curly-spacing" />
</file>
</baseline>
""".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(
"""
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="src/main/kotlin/WithOffence.kt">
<error line="1" column="1" source="filename" />
</file>
<file name="src/main/kotlin/CustomClass.kt">
<error line="2" column="22" source="no-multi-spaces" />
</file>
</baseline>
""".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(
"""
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="src/main/kotlin/WithOffence.kt">
<error line="1" column="1" source="filename" />
</file>
<file name="src/main/kotlin/CustomClass.kt">
<error line="2" column="21" source="curly-spacing" />
</file>
</baseline>
""".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(
"""
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="src/main/kotlin/WithOffence.kt">
<error line="1" column="1" source="filename" />
</file>
<file name="src/main/kotlin/CustomClass.kt">
<error line="2" column="22" source="no-multi-spaces" />
</file>
</baseline>
""".trimIndent(),
)
}
build("formatKotlin").apply {
assertThat(output).contains("CustomClass.kt:2:22: Format fixed > [no-multi-spaces]")
}
}
}

0 comments on commit ef45db7

Please sign in to comment.