From bcc7f87a9ac372798efd21b1c2f02ef817f5ce20 Mon Sep 17 00:00:00 2001 From: AtulyaReddy Date: Wed, 11 Oct 2023 15:46:01 -0500 Subject: [PATCH 01/14] changed lint fix to fail build when it can't fix lint errors, updated expected behavior in KotlinProjectTest --- .../jmailen/gradle/kotlinter/tasks/FormatTask.kt | 12 ++++++++++++ .../kotlinter/tasks/format/FormatWorkerAction.kt | 13 ++++++++++++- .../kotlinter/functional/KotlinProjectTest.kt | 4 ++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt index fd2d6815..365a22dc 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt @@ -4,13 +4,17 @@ import org.gradle.api.GradleException import org.gradle.api.file.ProjectLayout import org.gradle.api.file.RegularFileProperty import org.gradle.api.model.ObjectFactory +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input import org.gradle.api.tasks.Optional import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.TaskAction import org.gradle.work.InputChanges import org.gradle.workers.WorkerExecutor import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty +import org.jmailen.gradle.kotlinter.KotlinterExtension import org.jmailen.gradle.kotlinter.support.KotlinterError +import org.jmailen.gradle.kotlinter.support.LintFailure import org.jmailen.gradle.kotlinter.tasks.format.FormatWorkerAction import javax.inject.Inject @@ -27,6 +31,9 @@ open class FormatTask @Inject constructor( @Optional val report: RegularFileProperty = objectFactory.fileProperty() + @Input + val ignoreFailures: Property = objectFactory.property(default = KotlinterExtension.DEFAULT_IGNORE_FAILURES) + init { outputs.upToDateWhen { false } } @@ -48,5 +55,10 @@ open class FormatTask @Inject constructor( forEach { logger.error(it.message, it.cause) } throw GradleException("error formatting sources for $name") } + + val lintFailures = result.exceptionOrNull()?.workErrorCauses() ?: emptyList() + if (lintFailures.isNotEmpty() && !ignoreFailures.get()) { + throw GradleException("$name sources failed lint check") + } } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt index ae88f570..b94d8188 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt @@ -7,6 +7,7 @@ import org.gradle.api.logging.Logging import org.gradle.internal.logging.slf4j.DefaultContextAwareTaskLogger import org.gradle.workers.WorkAction import org.jmailen.gradle.kotlinter.support.KotlinterError +import org.jmailen.gradle.kotlinter.support.LintFailure import org.jmailen.gradle.kotlinter.support.ktlintEngine import org.jmailen.gradle.kotlinter.support.resetEditorconfigCacheIfNeeded import org.jmailen.gradle.kotlinter.tasks.FormatTask @@ -26,6 +27,7 @@ abstract class FormatWorkerAction : WorkAction { ) val fixes = mutableListOf() + var hasError = false try { files.forEach { file -> @@ -44,7 +46,12 @@ abstract class FormatWorkerAction : WorkAction { true -> "${file.path}:${error.line}:${error.col}: Format fixed > [${error.ruleId.value}] ${error.detail}" false -> "${file.path}:${error.line}:${error.col}: Format could not fix > [${error.ruleId.value}] ${error.detail}" } - logger.warn(msg) + if (corrected) { + logger.warn(msg) + } else { + hasError = true + logger.error(msg) // TODO: is this needed? + } fixes.add(msg) } if (!formattedText.contentEquals(sourceText)) { @@ -56,6 +63,10 @@ abstract class FormatWorkerAction : WorkAction { throw KotlinterError("format worker execution error", t) } + if (hasError) { + throw LintFailure("kotlin source failed lint check") + } + output?.writeText( when (fixes.isEmpty()) { true -> "ok" diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt index 1530ed68..7121dfa5 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt @@ -93,8 +93,8 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { """.trimIndent() kotlinSourceFile("KotlinClass.kt", kotlinClass) - build("formatKotlin").apply { - assertEquals(SUCCESS, task(":formatKotlinMain")?.outcome) + buildAndFail("formatKotlin").apply { + assertEquals(FAILED, task(":formatKotlinMain")?.outcome) output.lines().filter { it.contains("Format could not fix") }.forEach { line -> val filePath = pathPattern.find(line)?.groups?.get(1)?.value.orEmpty() assertTrue(File(filePath).exists()) From 79d5cc99b5782311efa76a9473ea6addabc8d28e Mon Sep 17 00:00:00 2001 From: AtulyaReddy Date: Wed, 11 Oct 2023 15:48:18 -0500 Subject: [PATCH 02/14] updated expected behavior in KotlinJsProjectTest --- .../gradle/kotlinter/functional/KotlinJsProjectTest.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt index 61a7908a..32f5af08 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt @@ -107,11 +107,11 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { """.trimIndent() writeText(kotlinClass) } - build("formatKotlin").apply { - assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinMain")?.outcome) + buildAndFail("formatKotlin").apply { + assertEquals(TaskOutcome.FAILED, task(":formatKotlinMain")?.outcome) assertTrue(output.contains("FixtureClass.kt:3:19: Format fixed > [standard:curly-spacing] Missing spacing before \"{\"")) assertTrue(output.contains("FixtureClass.kt:1:1: Format could not fix > [standard:no-wildcard-imports] Wildcard import")) - assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinTest")?.outcome) + assertEquals(TaskOutcome.FAILED, task(":formatKotlinTest")?.outcome) assertTrue(output.contains("FixtureTestClass.kt:3:23: Format fixed > [standard:curly-spacing] Missing spacing before \"{\"")) assertTrue(output.contains("FixtureTestClass.kt:1:1: Format could not fix > [standard:no-wildcard-imports] Wildcard import")) } From 45f0592a645fc674368d4e718e48f8811473ed45 Mon Sep 17 00:00:00 2001 From: AtulyaReddy Date: Wed, 11 Oct 2023 15:50:49 -0500 Subject: [PATCH 03/14] updated expected behavior in EditorConfigTest --- .../gradle/kotlinter/functional/EditorConfigTest.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt index 8b93fa3e..8cd13d7a 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt @@ -155,8 +155,8 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { projectRoot.resolve("src/main/kotlin/FileName.kt") { writeText(kotlinClass("DifferentClassName")) } - build("formatKotlin").apply { - assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinMain")?.outcome) + buildAndFail("formatKotlin").apply { + assertEquals(TaskOutcome.FAILED, task(":formatKotlinMain")?.outcome) assertTrue( output.contains("Format could not fix > [standard:filename] File 'FileName.kt' contains a single top level declaration"), ) @@ -171,8 +171,8 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { """.trimIndent(), ) } - build("formatKotlin", "--info").apply { - assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinMain")?.outcome) + buildAndFail("formatKotlin", "--info").apply { + assertEquals(TaskOutcome.FAILED, task(":formatKotlinMain")?.outcome) assertTrue(output.contains("Format could not fix")) assertFalse(output.contains("resetting KtLint caches")) } From bb1eb177bb98412f7107e404636fee931d2acf9b Mon Sep 17 00:00:00 2001 From: JuliaKispert Date: Mon, 23 Oct 2023 13:54:17 -0500 Subject: [PATCH 04/14] remove todo --- .../jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt index b94d8188..09f90a07 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt @@ -50,7 +50,7 @@ abstract class FormatWorkerAction : WorkAction { logger.warn(msg) } else { hasError = true - logger.error(msg) // TODO: is this needed? + logger.error(msg) } fixes.add(msg) } From f7390cd8ac7225ff558eff333e92a64f3a8a0bf1 Mon Sep 17 00:00:00 2001 From: JuliaKispert Date: Mon, 23 Oct 2023 13:54:52 -0500 Subject: [PATCH 05/14] add todo back --- .../jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt index 09f90a07..b94d8188 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt @@ -50,7 +50,7 @@ abstract class FormatWorkerAction : WorkAction { logger.warn(msg) } else { hasError = true - logger.error(msg) + logger.error(msg) // TODO: is this needed? } fixes.add(msg) } From b272e41a007124da0836c1593849caa17db0b513 Mon Sep 17 00:00:00 2001 From: AtulyaReddy Date: Tue, 17 Oct 2023 12:40:12 -0500 Subject: [PATCH 06/14] made failBuildWhenCannotAutoFormat an optional configuration that is overrided by ignoreFailures, updated tests and readme --- README.md | 3 + .../gradle/kotlinter/KotlinterExtension.kt | 3 +- .../gradle/kotlinter/KotlinterPlugin.kt | 2 + .../gradle/kotlinter/tasks/FormatTask.kt | 11 ++- .../tasks/format/FormatWorkerAction.kt | 2 +- .../kotlinter/functional/KotlinProjectTest.kt | 98 ++++++++++++++++++- 6 files changed, 113 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3c059165..9b6953c8 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,7 @@ Options are configured in the `kotlinter` extension. Defaults shown (you may omi ```kotlin kotlinter { ignoreFailures = false + failBuildWhenCannotAutoFormat = false reporters = arrayOf("checkstyle", "plain") } ``` @@ -168,11 +169,13 @@ kotlinter { ```groovy kotlinter { ignoreFailures = false + failBuildWhenCannotAutoFormat = false reporters = ['checkstyle', 'plain'] } ``` +Setting `failBuildWhenCannotAutoFormat` to `true` will configure the `formatKotlin` task to fail the build when auto-format is not able to fix a lint error. Options for `reporters`: `checkstyle`, `html`, `json`, `plain`, `sarif` diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt index 26189337..7236d4b8 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt @@ -5,10 +5,11 @@ import org.jmailen.gradle.kotlinter.support.ReporterType open class KotlinterExtension { companion object { const val DEFAULT_IGNORE_FAILURES = false + const val DEFAULT_FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT = false val DEFAULT_REPORTER = ReporterType.checkstyle.name } var ignoreFailures = DEFAULT_IGNORE_FAILURES - + var failBuildWhenCannotAutoFormat = DEFAULT_FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT var reporters = arrayOf(DEFAULT_REPORTER) } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt index 9fc3f124..36c11da1 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt @@ -59,6 +59,8 @@ class KotlinterPlugin : Plugin { FormatTask::class.java, ) { formatTask -> formatTask.source(resolvedSources) + formatTask.ignoreFailures.set(provider { kotlinterExtension.ignoreFailures }) + formatTask.failBuildWhenCannotAutoFormat.set(provider { kotlinterExtension.failBuildWhenCannotAutoFormat }) formatTask.report.set(reportFile("$id-format.txt")) } formatKotlin.configure { formatTask -> diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt index 365a22dc..ca58aa94 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt @@ -34,6 +34,9 @@ open class FormatTask @Inject constructor( @Input val ignoreFailures: Property = objectFactory.property(default = KotlinterExtension.DEFAULT_IGNORE_FAILURES) + @Input + val failBuildWhenCannotAutoFormat: Property = objectFactory.property(default = KotlinterExtension.DEFAULT_FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT) + init { outputs.upToDateWhen { false } } @@ -56,9 +59,11 @@ open class FormatTask @Inject constructor( throw GradleException("error formatting sources for $name") } - val lintFailures = result.exceptionOrNull()?.workErrorCauses() ?: emptyList() - if (lintFailures.isNotEmpty() && !ignoreFailures.get()) { - throw GradleException("$name sources failed lint check") + if (failBuildWhenCannotAutoFormat.get()) { + val lintFailures = result.exceptionOrNull()?.workErrorCauses() ?: emptyList() + if (lintFailures.isNotEmpty() && !ignoreFailures.get()) { + throw GradleException("$name sources failed lint check") + } } } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt index b94d8188..09f90a07 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/format/FormatWorkerAction.kt @@ -50,7 +50,7 @@ abstract class FormatWorkerAction : WorkAction { logger.warn(msg) } else { hasError = true - logger.error(msg) // TODO: is this needed? + logger.error(msg) } fixes.add(msg) } diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt index 7121dfa5..af7b9ca3 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt @@ -77,7 +77,33 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } @Test - fun `formatKotlin reports formatted and unformatted files`() { + fun `formatKotlin fails reports formatted and unformatted files when ignoreFailures and failBuildWhenCannotAutoFormat enabled`() { + settingsFile() + buildFileIgnoreFailures() + // language=kotlin + val kotlinClass = + """ + import System.* + + class KotlinClass{ + private fun hi() { + out.println("Hello") + } + } + """.trimIndent() + kotlinSourceFile("KotlinClass.kt", kotlinClass) + + build("formatKotlin").apply { + assertEquals(SUCCESS, task(":formatKotlinMain")?.outcome) + output.lines().filter { it.contains("Format could not fix") }.forEach { line -> + val filePath = pathPattern.find(line)?.groups?.get(1)?.value.orEmpty() + assertTrue(File(filePath).exists()) + } + } + } + + @Test + fun `formatKotlin reports formatted and unformatted files when ignoreFailures and failBuildWhenCannotAutoFormat disabled`() { settingsFile() buildFile() // language=kotlin @@ -93,6 +119,32 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { """.trimIndent() kotlinSourceFile("KotlinClass.kt", kotlinClass) + build("formatKotlin").apply { + assertEquals(SUCCESS, task(":formatKotlinMain")?.outcome) + output.lines().filter { it.contains("Format could not fix") }.forEach { line -> + val filePath = pathPattern.find(line)?.groups?.get(1)?.value.orEmpty() + assertTrue(File(filePath).exists()) + } + } + } + + @Test + fun `formatKotlin fails when lint errors not automatically fixed when ignoreFailures disabled and failBuildWhenCannotAutoFormat enabled`() { + settingsFile() + buildFileFailBuildWhenCannotAutoFormat() + // language=kotlin + val kotlinClass = + """ + import System.* + + class KotlinClass{ + private fun hi() { + out.println("Hello") + } + } + """.trimIndent() + kotlinSourceFile("KotlinClass.kt", kotlinClass) + buildAndFail("formatKotlin").apply { assertEquals(FAILED, task(":formatKotlinMain")?.outcome) output.lines().filter { it.contains("Format could not fix") }.forEach { line -> @@ -102,6 +154,8 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } } + + @Test fun `check task runs lintFormat`() { settingsFile() @@ -210,6 +264,48 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { writeText(buildscript) } + private fun buildFileFailBuildWhenCannotAutoFormat() = buildFile.apply { + // language=groovy + val buildscript = + """ + plugins { + id 'org.jetbrains.kotlin.jvm' + id 'org.jmailen.kotlinter' + } + + repositories { + mavenCentral() + } + + kotlinter { + ignoreFailures = false + failBuildWhenCannotAutoFormat = true + } + """.trimIndent() + writeText(buildscript) + } + + private fun buildFileIgnoreFailures() = buildFile.apply { + // language=groovy + val buildscript = + """ + plugins { + id 'org.jetbrains.kotlin.jvm' + id 'org.jmailen.kotlinter' + } + + repositories { + mavenCentral() + } + + kotlinter { + ignoreFailures = true + failBuildWhenCannotAutoFormat = true + } + """.trimIndent() + writeText(buildscript) + } + private fun kotlinSourceFile(name: String, content: String) = File(sourceDir, name).apply { writeText(content) } From 9e3d0573f653bea3c27c540b70c6b0ef00396594 Mon Sep 17 00:00:00 2001 From: AtulyaReddy Date: Tue, 17 Oct 2023 13:06:59 -0500 Subject: [PATCH 07/14] finished updated test methods, still need to update test setups --- .../kotlinter/functional/EditorConfigTest.kt | 45 ++++++++++++++++--- .../functional/KotlinJsProjectTest.kt | 40 +++++++++++++++++ .../kotlinter/functional/KotlinProjectTest.kt | 23 +++++----- 3 files changed, 90 insertions(+), 18 deletions(-) diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt index 8cd13d7a..8e0c6a8f 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt @@ -24,11 +24,14 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { // language=groovy val buildScript = """ - plugins { - id 'kotlin' - id 'org.jmailen.kotlinter' - } - + plugins { + id 'kotlin' + id 'org.jmailen.kotlinter' + } + + kotlinter { + failBuildWhenCannotAutoFormat = true + } """.trimIndent() writeText(buildScript) } @@ -152,6 +155,38 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { writeText(editorConfig) } + projectRoot.resolve("src/main/kotlin/FileName.kt") { + writeText(kotlinClass("DifferentClassName")) + } + build("formatKotlin").apply { + assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinMain")?.outcome) + assertTrue( + output.contains("Format could not fix > [standard:filename] File 'FileName.kt' contains a single top level declaration"), + ) + } + + projectRoot.resolve(".editorconfig") { + writeText( + // language=editorconfig + """ + [*.{kt,kts}] + ktlint_standard_filename = disabled + """.trimIndent(), + ) + } + build("formatKotlin", "--info").apply { + assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinMain")?.outcome) + assertTrue(output.contains("Format could not fix")) + assertFalse(output.contains("resetting KtLint caches")) + } + } + + @Test + fun `editorconfig changes are ignored for format task re-runs when failBuildWhenCannotAutoFormat enabled`() { + projectRoot.resolve(".editorconfig") { + writeText(editorConfig) + } + projectRoot.resolve("src/main/kotlin/FileName.kt") { writeText(kotlinClass("DifferentClassName")) } diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt index 32f5af08..51bca939 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt @@ -79,6 +79,46 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { @Test fun `formatKotlin reports formatted and unformatted files`() { + projectRoot.resolve("src/main/kotlin/FixtureClass.kt") { + // language=kotlin + val kotlinClass = + """ + import System.* + + class FixtureClass{ + private fun hi() { + out.println("Hello") + } + } + """.trimIndent() + writeText(kotlinClass) + } + projectRoot.resolve("src/test/kotlin/FixtureTestClass.kt") { + // language=kotlin + val kotlinClass = + """ + import System.* + + class FixtureTestClass{ + private fun hi() { + out.println("Hello") + } + } + """.trimIndent() + writeText(kotlinClass) + } + build("formatKotlin").apply { + assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinMain")?.outcome) + assertTrue(output.contains("FixtureClass.kt:3:19: Format fixed > [standard:curly-spacing] Missing spacing before \"{\"")) + assertTrue(output.contains("FixtureClass.kt:1:1: Format could not fix > [standard:no-wildcard-imports] Wildcard import")) + assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinTest")?.outcome) + assertTrue(output.contains("FixtureTestClass.kt:3:23: Format fixed > [standard:curly-spacing] Missing spacing before \"{\"")) + assertTrue(output.contains("FixtureTestClass.kt:1:1: Format could not fix > [standard:no-wildcard-imports] Wildcard import")) + } + } + + @Test + fun `formatKotlin fails when lint errors not automatically fixed and failBuildWhenCannotAutoFormat enabled`() { projectRoot.resolve("src/main/kotlin/FixtureClass.kt") { // language=kotlin val kotlinClass = diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt index af7b9ca3..495cbde1 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt @@ -77,9 +77,9 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } @Test - fun `formatKotlin fails reports formatted and unformatted files when ignoreFailures and failBuildWhenCannotAutoFormat enabled`() { + fun `formatKotlin reports formatted and unformatted files`() { settingsFile() - buildFileIgnoreFailures() + buildFile() // language=kotlin val kotlinClass = """ @@ -103,9 +103,9 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } @Test - fun `formatKotlin reports formatted and unformatted files when ignoreFailures and failBuildWhenCannotAutoFormat disabled`() { + fun `formatKotlin fails when lint errors not automatically fixed and failBuildWhenCannotAutoFormat enabled`() { settingsFile() - buildFile() + buildFileFailBuildWhenCannotAutoFormat() // language=kotlin val kotlinClass = """ @@ -119,8 +119,8 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { """.trimIndent() kotlinSourceFile("KotlinClass.kt", kotlinClass) - build("formatKotlin").apply { - assertEquals(SUCCESS, task(":formatKotlinMain")?.outcome) + buildAndFail("formatKotlin").apply { + assertEquals(FAILED, task(":formatKotlinMain")?.outcome) output.lines().filter { it.contains("Format could not fix") }.forEach { line -> val filePath = pathPattern.find(line)?.groups?.get(1)?.value.orEmpty() assertTrue(File(filePath).exists()) @@ -129,9 +129,9 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } @Test - fun `formatKotlin fails when lint errors not automatically fixed when ignoreFailures disabled and failBuildWhenCannotAutoFormat enabled`() { + fun `formatKotlin fails reports formatted and unformatted files when ignoreFailures and failBuildWhenCannotAutoFormat enabled`() { settingsFile() - buildFileFailBuildWhenCannotAutoFormat() + buildFileIgnoreFailures() // language=kotlin val kotlinClass = """ @@ -145,8 +145,8 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { """.trimIndent() kotlinSourceFile("KotlinClass.kt", kotlinClass) - buildAndFail("formatKotlin").apply { - assertEquals(FAILED, task(":formatKotlinMain")?.outcome) + build("formatKotlin").apply { + assertEquals(SUCCESS, task(":formatKotlinMain")?.outcome) output.lines().filter { it.contains("Format could not fix") }.forEach { line -> val filePath = pathPattern.find(line)?.groups?.get(1)?.value.orEmpty() assertTrue(File(filePath).exists()) @@ -154,8 +154,6 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } } - - @Test fun `check task runs lintFormat`() { settingsFile() @@ -278,7 +276,6 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } kotlinter { - ignoreFailures = false failBuildWhenCannotAutoFormat = true } """.trimIndent() From f72e9e2d420996bc040f8f3d82254dc50e9afc17 Mon Sep 17 00:00:00 2001 From: JuliaKispert Date: Tue, 17 Oct 2023 14:37:42 -0500 Subject: [PATCH 08/14] tests fixed --- build.gradle.kts | 1 + .../functional/KotlinJsProjectTest.kt | 73 ++++++++++++++++--- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 48401b6e..dfcb3f67 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -69,6 +69,7 @@ dependencies { testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}") testImplementation("org.junit.jupiter:junit-jupiter-api:${Versions.junit}") + testImplementation("org.junit.jupiter:junit-jupiter-params:${Versions.junit}") testImplementation("commons-io:commons-io:2.12.0") testImplementation("org.mockito.kotlin:mockito-kotlin:${Versions.mockitoKotlin}") } diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt index 51bca939..d112c3fd 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt @@ -6,43 +6,94 @@ import org.jmailen.gradle.kotlinter.functional.utils.resolve import org.jmailen.gradle.kotlinter.functional.utils.settingsFile import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import java.io.File + class KotlinJsProjectTest : WithGradleTest.Kotlin() { - lateinit var projectRoot: File - @BeforeEach - fun setup() { + enum class KotlinterConfig { + DEFAULT, + IGNORE_FAILURES, + FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT + } + + + lateinit var projectRoot: File + fun setup(kotlinterConfig: KotlinterConfig) { projectRoot = testProjectDir.apply { resolve("settings.gradle") { writeText(settingsFile) } resolve("build.gradle") { - // language=groovy - writeText( - """ + when(kotlinterConfig){ + KotlinterConfig.DEFAULT -> writeText( + """ plugins { id 'org.jetbrains.kotlin.js' id 'org.jmailen.kotlinter' } - + + repositories.mavenCentral() + + kotlin { + js(IR) { + browser() + binaries.executable() + } + } + """.trimIndent(), + ) + KotlinterConfig.IGNORE_FAILURES -> writeText( + """ + plugins { + id 'org.jetbrains.kotlin.js' + id 'org.jmailen.kotlinter' + } + repositories.mavenCentral() + + kotlin { + js(IR) { + browser() + binaries.executable() + } + } + kotlinter { + ignoreFailures = true + failBuildWhenCannotAutoFormat = true + } + """.trimIndent(), + ) + KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> writeText( + """ + plugins { + id 'org.jetbrains.kotlin.js' + id 'org.jmailen.kotlinter' + } + + repositories.mavenCentral() + kotlin { js(IR) { browser() binaries.executable() } } + + kotlinter { + failBuildWhenCannotAutoFormat = true + } """.trimIndent(), - ) + ) + } } } } @Test fun `lintKotlin passes when on valid kotlin files`() { + setup(KotlinterConfig.DEFAULT) projectRoot.resolve("src/main/kotlin/FixtureFileName.kt") { writeText(kotlinClass("FixtureFileName")) } @@ -58,6 +109,7 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { @Test fun `lintKotlin fails when lint errors detected`() { + setup(KotlinterConfig.DEFAULT) projectRoot.resolve("src/main/kotlin/FixtureFileName.kt") { writeText(kotlinClass("DifferentClassName")) } @@ -79,6 +131,7 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { @Test fun `formatKotlin reports formatted and unformatted files`() { + setup(KotlinterConfig.DEFAULT) projectRoot.resolve("src/main/kotlin/FixtureClass.kt") { // language=kotlin val kotlinClass = @@ -119,6 +172,7 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { @Test fun `formatKotlin fails when lint errors not automatically fixed and failBuildWhenCannotAutoFormat enabled`() { + setup(KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT) projectRoot.resolve("src/main/kotlin/FixtureClass.kt") { // language=kotlin val kotlinClass = @@ -156,4 +210,5 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { assertTrue(output.contains("FixtureTestClass.kt:1:1: Format could not fix > [standard:no-wildcard-imports] Wildcard import")) } } + } From 837f4b7138e8ccb7ef74450c49b6bb9534362d11 Mon Sep 17 00:00:00 2001 From: JuliaKispert Date: Wed, 18 Oct 2023 10:33:07 -0500 Subject: [PATCH 09/14] added new test and finished editing tests to account for making autoformat fail build fail --- .../functional/KotlinJsProjectTest.kt | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt index d112c3fd..6c9acdb5 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt @@ -11,15 +11,12 @@ import java.io.File class KotlinJsProjectTest : WithGradleTest.Kotlin() { - - enum class KotlinterConfig { DEFAULT, IGNORE_FAILURES, FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT } - lateinit var projectRoot: File fun setup(kotlinterConfig: KotlinterConfig) { projectRoot = testProjectDir.apply { @@ -211,4 +208,45 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { } } + @Test + fun `formatKotlin fails reports formatted and unformatted files when ignoreFailures and failBuildWhenCannotAutoFormat enabled`() { + setup(KotlinterConfig.IGNORE_FAILURES) + projectRoot.resolve("src/main/kotlin/FixtureClass.kt") { + // language=kotlin + val kotlinClass = + """ + import System.* + + class FixtureClass{ + private fun hi() { + out.println("Hello") + } + } + """.trimIndent() + writeText(kotlinClass) + } + projectRoot.resolve("src/test/kotlin/FixtureTestClass.kt") { + // language=kotlin + val kotlinClass = + """ + import System.* + + class FixtureTestClass{ + private fun hi() { + out.println("Hello") + } + } + """.trimIndent() + writeText(kotlinClass) + } + build("formatKotlin").apply { + assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinMain")?.outcome) + assertTrue(output.contains("FixtureClass.kt:3:19: Format fixed > [standard:curly-spacing] Missing spacing before \"{\"")) + assertTrue(output.contains("FixtureClass.kt:1:1: Format could not fix > [standard:no-wildcard-imports] Wildcard import")) + assertEquals(TaskOutcome.SUCCESS, task(":formatKotlinTest")?.outcome) + assertTrue(output.contains("FixtureTestClass.kt:3:23: Format fixed > [standard:curly-spacing] Missing spacing before \"{\"")) + assertTrue(output.contains("FixtureTestClass.kt:1:1: Format could not fix > [standard:no-wildcard-imports] Wildcard import")) + } + } + } From 2186d5c6ac81d7e6c1bed6d6a2565c6026091f28 Mon Sep 17 00:00:00 2001 From: JuliaKispert Date: Wed, 18 Oct 2023 16:06:57 -0500 Subject: [PATCH 10/14] now committing to switch off for pair programming --- .../gradle/kotlinter/tasks/FormatTask.kt | 4 +- .../functional/KotlinJsProjectTest.kt | 105 +++++++++--------- 2 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt index ca58aa94..0169bb99 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt @@ -35,7 +35,9 @@ open class FormatTask @Inject constructor( val ignoreFailures: Property = objectFactory.property(default = KotlinterExtension.DEFAULT_IGNORE_FAILURES) @Input - val failBuildWhenCannotAutoFormat: Property = objectFactory.property(default = KotlinterExtension.DEFAULT_FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT) + val failBuildWhenCannotAutoFormat: Property = objectFactory.property( + default = KotlinterExtension.DEFAULT_FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT, + ) init { outputs.upToDateWhen { false } diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt index 6c9acdb5..5cbf02f0 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt @@ -8,13 +8,11 @@ import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import java.io.File - - class KotlinJsProjectTest : WithGradleTest.Kotlin() { enum class KotlinterConfig { DEFAULT, IGNORE_FAILURES, - FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT + FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT, } lateinit var projectRoot: File @@ -22,66 +20,66 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { projectRoot = testProjectDir.apply { resolve("settings.gradle") { writeText(settingsFile) } resolve("build.gradle") { - when(kotlinterConfig){ + when (kotlinterConfig) { KotlinterConfig.DEFAULT -> writeText( """ - plugins { - id 'org.jetbrains.kotlin.js' - id 'org.jmailen.kotlinter' - } - - repositories.mavenCentral() - - kotlin { - js(IR) { - browser() - binaries.executable() + plugins { + id 'org.jetbrains.kotlin.js' + id 'org.jmailen.kotlinter' } - } - """.trimIndent(), + + repositories.mavenCentral() + + kotlin { + js(IR) { + browser() + binaries.executable() + } + } + """.trimIndent(), ) KotlinterConfig.IGNORE_FAILURES -> writeText( """ - plugins { - id 'org.jetbrains.kotlin.js' - id 'org.jmailen.kotlinter' - } - - repositories.mavenCentral() - - kotlin { - js(IR) { - browser() - binaries.executable() + plugins { + id 'org.jetbrains.kotlin.js' + id 'org.jmailen.kotlinter' } - } - - kotlinter { - ignoreFailures = true - failBuildWhenCannotAutoFormat = true - } - """.trimIndent(), + + repositories.mavenCentral() + + kotlin { + js(IR) { + browser() + binaries.executable() + } + } + + kotlinter { + ignoreFailures = true + failBuildWhenCannotAutoFormat = true + } + """.trimIndent(), ) KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> writeText( """ - plugins { - id 'org.jetbrains.kotlin.js' - id 'org.jmailen.kotlinter' - } - - repositories.mavenCentral() - - kotlin { - js(IR) { - browser() - binaries.executable() + plugins { + id 'org.jetbrains.kotlin.js' + id 'org.jmailen.kotlinter' } - } - - kotlinter { - failBuildWhenCannotAutoFormat = true - } - """.trimIndent(), + + repositories.mavenCentral() + + kotlin { + js(IR) { + browser() + binaries.executable() + } + } + + kotlinter { + failBuildWhenCannotAutoFormat = true + } + """.trimIndent(), ) } } @@ -209,7 +207,7 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { } @Test - fun `formatKotlin fails reports formatted and unformatted files when ignoreFailures and failBuildWhenCannotAutoFormat enabled`() { + fun `formatKotlin fails when lint errors not automatically fixed and failBuildWhenCannotAutoFormat enabled `() { setup(KotlinterConfig.IGNORE_FAILURES) projectRoot.resolve("src/main/kotlin/FixtureClass.kt") { // language=kotlin @@ -248,5 +246,4 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { assertTrue(output.contains("FixtureTestClass.kt:1:1: Format could not fix > [standard:no-wildcard-imports] Wildcard import")) } } - } From 827a4a1e8d895f6b81c067f63f1fb5d248bc664a Mon Sep 17 00:00:00 2001 From: AtulyaReddy Date: Wed, 18 Oct 2023 16:30:47 -0500 Subject: [PATCH 11/14] finished updating test setup --- .../kotlinter/functional/EditorConfigTest.kt | 57 +++++++++++++------ .../functional/KotlinJsProjectTest.kt | 26 ++++----- .../functional/utils/KotlinterConfig.kt | 7 +++ 3 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt index 8e0c6a8f..6bcb21e9 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt @@ -1,6 +1,7 @@ package org.jmailen.gradle.kotlinter.functional import org.gradle.testkit.runner.TaskOutcome +import org.jmailen.gradle.kotlinter.functional.utils.KotlinterConfig import org.jmailen.gradle.kotlinter.functional.utils.editorConfig import org.jmailen.gradle.kotlinter.functional.utils.kotlinClass import org.jmailen.gradle.kotlinter.functional.utils.resolve @@ -8,7 +9,6 @@ import org.jmailen.gradle.kotlinter.functional.utils.settingsFile import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertFalse import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import java.io.File @@ -16,30 +16,50 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { lateinit var projectRoot: File - @BeforeEach - fun setUp() { + private fun setup(kotlinterConfig: KotlinterConfig) { projectRoot = testProjectDir.apply { resolve("settings.gradle") { writeText(settingsFile) } resolve("build.gradle") { - // language=groovy - val buildScript = - """ - plugins { - id 'kotlin' - id 'org.jmailen.kotlinter' - } - - kotlinter { - failBuildWhenCannotAutoFormat = true - } - """.trimIndent() - writeText(buildScript) + val buildscript = when (kotlinterConfig) { + KotlinterConfig.DEFAULT -> + """ + plugins { + id 'kotlin' + id 'org.jmailen.kotlinter' + } + """.trimIndent() + KotlinterConfig.IGNORE_FAILURES -> + """ + plugins { + id 'org.jetbrains.kotlin.js' + id 'org.jmailen.kotlinter' + } + + kotlinter { + ignoreFailures = true + failBuildWhenCannotAutoFormat = true + } + """.trimIndent() + KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> + """ + plugins { + id 'org.jetbrains.kotlin.js' + id 'org.jmailen.kotlinter' + } + + kotlinter { + failBuildWhenCannotAutoFormat = true + } + """.trimIndent() + } + writeText(buildscript) } } } @Test fun `lintTask uses default indentation if editorconfig absent`() { + setup(KotlinterConfig.DEFAULT) projectRoot.resolve("src/main/kotlin/FourSpacesByDefault.kt") { writeText( """ |package com.example @@ -59,6 +79,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { @Test fun `plugin respects disabled_rules set in editorconfig`() { + setup(KotlinterConfig.DEFAULT) projectRoot.resolve(".editorconfig") { appendText( // language=editorconfig @@ -79,6 +100,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { @Test fun `plugin respects 'indent_size' set in editorconfig`() { + setup(KotlinterConfig.DEFAULT) projectRoot.resolve(".editorconfig") { appendText( // language=editorconfig @@ -110,6 +132,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { @Test fun `editorconfig changes are taken into account on lint task re-runs`() { + setup(KotlinterConfig.DEFAULT) projectRoot.resolve(".editorconfig") { writeText( // language=editorconfig @@ -151,6 +174,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { @Test fun `editorconfig changes are ignored for format task re-runs`() { + setup(KotlinterConfig.DEFAULT) projectRoot.resolve(".editorconfig") { writeText(editorConfig) } @@ -183,6 +207,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { @Test fun `editorconfig changes are ignored for format task re-runs when failBuildWhenCannotAutoFormat enabled`() { + setup(KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT) projectRoot.resolve(".editorconfig") { writeText(editorConfig) } diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt index 5cbf02f0..52acb81f 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt @@ -1,6 +1,7 @@ package org.jmailen.gradle.kotlinter.functional import org.gradle.testkit.runner.TaskOutcome +import org.jmailen.gradle.kotlinter.functional.utils.KotlinterConfig import org.jmailen.gradle.kotlinter.functional.utils.kotlinClass import org.jmailen.gradle.kotlinter.functional.utils.resolve import org.jmailen.gradle.kotlinter.functional.utils.settingsFile @@ -9,19 +10,14 @@ import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import java.io.File class KotlinJsProjectTest : WithGradleTest.Kotlin() { - enum class KotlinterConfig { - DEFAULT, - IGNORE_FAILURES, - FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT, - } lateinit var projectRoot: File - fun setup(kotlinterConfig: KotlinterConfig) { + private fun setup(kotlinterConfig: KotlinterConfig) { projectRoot = testProjectDir.apply { resolve("settings.gradle") { writeText(settingsFile) } resolve("build.gradle") { - when (kotlinterConfig) { - KotlinterConfig.DEFAULT -> writeText( + val buildscript = when (kotlinterConfig) { + KotlinterConfig.DEFAULT -> """ plugins { id 'org.jetbrains.kotlin.js' @@ -36,9 +32,8 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { binaries.executable() } } - """.trimIndent(), - ) - KotlinterConfig.IGNORE_FAILURES -> writeText( + """.trimIndent() + KotlinterConfig.IGNORE_FAILURES -> """ plugins { id 'org.jetbrains.kotlin.js' @@ -58,9 +53,8 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { ignoreFailures = true failBuildWhenCannotAutoFormat = true } - """.trimIndent(), - ) - KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> writeText( + """.trimIndent() + KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> """ plugins { id 'org.jetbrains.kotlin.js' @@ -79,9 +73,9 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { kotlinter { failBuildWhenCannotAutoFormat = true } - """.trimIndent(), - ) + """.trimIndent() } + writeText(buildscript) } } } diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt new file mode 100644 index 00000000..390e5c7a --- /dev/null +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt @@ -0,0 +1,7 @@ +package org.jmailen.gradle.kotlinter.functional.utils + +enum class KotlinterConfig { + DEFAULT, + IGNORE_FAILURES, + FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT, +} From 439a47a64a2b440e194fe53a95107de206ea446e Mon Sep 17 00:00:00 2001 From: AtulyaReddy Date: Fri, 20 Oct 2023 11:00:38 -0500 Subject: [PATCH 12/14] fixed test names and added consistency to order of configuration cases --- README.md | 4 ++-- .../gradle/kotlinter/functional/EditorConfigTest.kt | 6 +++--- .../gradle/kotlinter/functional/KotlinJsProjectTest.kt | 8 ++++---- .../gradle/kotlinter/functional/KotlinProjectTest.kt | 2 +- .../gradle/kotlinter/functional/utils/KotlinterConfig.kt | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9b6953c8..de46eb75 100644 --- a/README.md +++ b/README.md @@ -155,8 +155,8 @@ Options are configured in the `kotlinter` extension. Defaults shown (you may omi ```kotlin kotlinter { - ignoreFailures = false failBuildWhenCannotAutoFormat = false + ignoreFailures = false reporters = arrayOf("checkstyle", "plain") } ``` @@ -168,8 +168,8 @@ kotlinter { ```groovy kotlinter { - ignoreFailures = false failBuildWhenCannotAutoFormat = false + ignoreFailures = false reporters = ['checkstyle', 'plain'] } ``` diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt index 6bcb21e9..2b19e0d6 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt @@ -28,7 +28,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { id 'org.jmailen.kotlinter' } """.trimIndent() - KotlinterConfig.IGNORE_FAILURES -> + KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> """ plugins { id 'org.jetbrains.kotlin.js' @@ -36,11 +36,10 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { } kotlinter { - ignoreFailures = true failBuildWhenCannotAutoFormat = true } """.trimIndent() - KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> + KotlinterConfig.IGNORE_FAILURES -> """ plugins { id 'org.jetbrains.kotlin.js' @@ -48,6 +47,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { } kotlinter { + ignoreFailures = true failBuildWhenCannotAutoFormat = true } """.trimIndent() diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt index 52acb81f..c2df7247 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinJsProjectTest.kt @@ -33,7 +33,7 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { } } """.trimIndent() - KotlinterConfig.IGNORE_FAILURES -> + KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> """ plugins { id 'org.jetbrains.kotlin.js' @@ -50,11 +50,10 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { } kotlinter { - ignoreFailures = true failBuildWhenCannotAutoFormat = true } """.trimIndent() - KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> + KotlinterConfig.IGNORE_FAILURES -> """ plugins { id 'org.jetbrains.kotlin.js' @@ -71,6 +70,7 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { } kotlinter { + ignoreFailures = true failBuildWhenCannotAutoFormat = true } """.trimIndent() @@ -201,7 +201,7 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { } @Test - fun `formatKotlin fails when lint errors not automatically fixed and failBuildWhenCannotAutoFormat enabled `() { + fun `formatKotlin reports formatted and unformatted files when failBuildWhenCannotAutoFormat and ignoreFailures enabled`() { setup(KotlinterConfig.IGNORE_FAILURES) projectRoot.resolve("src/main/kotlin/FixtureClass.kt") { // language=kotlin diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt index 495cbde1..95037c6b 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt @@ -129,7 +129,7 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } @Test - fun `formatKotlin fails reports formatted and unformatted files when ignoreFailures and failBuildWhenCannotAutoFormat enabled`() { + fun `formatKotlin reports formatted and unformatted files when failBuildWhenCannotAutoFormat and ignoreFailures enabled`() { settingsFile() buildFileIgnoreFailures() // language=kotlin diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt index 390e5c7a..d1e5dab7 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt @@ -2,6 +2,6 @@ package org.jmailen.gradle.kotlinter.functional.utils enum class KotlinterConfig { DEFAULT, - IGNORE_FAILURES, FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT, + IGNORE_FAILURES } From a729a3165309f68005ed950af6362e7b8484d16e Mon Sep 17 00:00:00 2001 From: AtulyaReddy Date: Fri, 20 Oct 2023 11:07:26 -0500 Subject: [PATCH 13/14] fixed readme, KotlinterExtension, KotlinterPlugin --- README.md | 3 ++- .../kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt | 4 ++-- .../kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index de46eb75..e16ca131 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,8 @@ kotlinter { ``` -Setting `failBuildWhenCannotAutoFormat` to `true` will configure the `formatKotlin` task to fail the build when auto-format is not able to fix a lint error. + +Setting `failBuildWhenCannotAutoFormat` to `true` will configure the `formatKotlin` task to fail the build when auto-format is not able to fix a lint error. This is overrided by setting `ignoreFailures` to `true`. Options for `reporters`: `checkstyle`, `html`, `json`, `plain`, `sarif` diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt index 7236d4b8..2c0ce310 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt @@ -4,12 +4,12 @@ import org.jmailen.gradle.kotlinter.support.ReporterType open class KotlinterExtension { companion object { - const val DEFAULT_IGNORE_FAILURES = false const val DEFAULT_FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT = false + const val DEFAULT_IGNORE_FAILURES = false val DEFAULT_REPORTER = ReporterType.checkstyle.name } - var ignoreFailures = DEFAULT_IGNORE_FAILURES var failBuildWhenCannotAutoFormat = DEFAULT_FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT + var ignoreFailures = DEFAULT_IGNORE_FAILURES var reporters = arrayOf(DEFAULT_REPORTER) } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt index 36c11da1..9d1af7a1 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt @@ -59,8 +59,8 @@ class KotlinterPlugin : Plugin { FormatTask::class.java, ) { formatTask -> formatTask.source(resolvedSources) - formatTask.ignoreFailures.set(provider { kotlinterExtension.ignoreFailures }) formatTask.failBuildWhenCannotAutoFormat.set(provider { kotlinterExtension.failBuildWhenCannotAutoFormat }) + formatTask.ignoreFailures.set(provider { kotlinterExtension.ignoreFailures }) formatTask.report.set(reportFile("$id-format.txt")) } formatKotlin.configure { formatTask -> From 41bd6aafc3ccb72758c489edcf9a98243609dbba Mon Sep 17 00:00:00 2001 From: AtulyaReddy Date: Thu, 26 Oct 2023 10:54:35 -0500 Subject: [PATCH 14/14] added trailing comma --- .../gradle/kotlinter/functional/utils/KotlinterConfig.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt index d1e5dab7..b7ea0305 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/KotlinterConfig.kt @@ -3,5 +3,5 @@ package org.jmailen.gradle.kotlinter.functional.utils enum class KotlinterConfig { DEFAULT, FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT, - IGNORE_FAILURES + IGNORE_FAILURES, }