diff --git a/build.gradle.kts b/build.gradle.kts index 0b16e205..3315745a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,7 +20,7 @@ val githubUrl = "https://github.com/jeremymailen/kotlinter-gradle" val webUrl = "https://github.com/jeremymailen/kotlinter-gradle" val projectDescription = "Lint and formatting for Kotlin using ktlint with configuration-free setup on JVM and Android projects" -version = "4.5.0" +version = "5.0.0-M1" group = "org.jmailen.gradle" description = projectDescription @@ -46,10 +46,13 @@ dependencies { compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin") compileOnly(libs.android.tools.gradle) - implementation(libs.bundles.ktlint.engine) - implementation(libs.bundles.ktlint.reporters) - implementation(libs.bundles.ktlint.rulesets) + compileOnly(libs.bundles.ktlint.engine) + compileOnly(libs.bundles.ktlint.reporters) + compileOnly(libs.bundles.ktlint.rulesets) + testImplementation(libs.bundles.ktlint.engine) + testImplementation(libs.bundles.ktlint.reporters) + testImplementation(libs.bundles.ktlint.rulesets) testImplementation(libs.bundles.junit.jupiter) testImplementation(libs.commons.io) testImplementation(libs.mockito.kotlin) @@ -67,7 +70,12 @@ tasks { outputs.file(propertiesFile) doLast { - propertiesFile.writeText("version = $projectVersion") + propertiesFile.writeText( + """ + version = $projectVersion + ktlintVersion = ${libs.versions.ktlint.get()} + """.trimIndent() + ) } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt index 2c0ce310..fba2bafc 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt @@ -1,15 +1,17 @@ package org.jmailen.gradle.kotlinter import org.jmailen.gradle.kotlinter.support.ReporterType +import org.jmailen.gradle.kotlinter.support.versionProperties open class KotlinterExtension { companion object { - const val DEFAULT_FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT = false - const val DEFAULT_IGNORE_FAILURES = false + const val DEFAULT_IGNORE_FORMAT_FAILURES = true + const val DEFAULT_IGNORE_LINT_FAILURES = false val DEFAULT_REPORTER = ReporterType.checkstyle.name } - var failBuildWhenCannotAutoFormat = DEFAULT_FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT - var ignoreFailures = DEFAULT_IGNORE_FAILURES + var ktlintVersion = versionProperties.ktlintVersion() + var ignoreFormatFailures = DEFAULT_IGNORE_FORMAT_FAILURES + var ignoreLintFailures = DEFAULT_IGNORE_LINT_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 29411cbf..01b3ad61 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt @@ -3,18 +3,22 @@ package org.jmailen.gradle.kotlinter import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.Task +import org.gradle.api.artifacts.Configuration import org.gradle.api.file.RegularFile import org.gradle.api.provider.Provider import org.gradle.api.tasks.TaskProvider import org.jmailen.gradle.kotlinter.pluginapplier.AndroidSourceSetApplier import org.jmailen.gradle.kotlinter.pluginapplier.KotlinSourceSetApplier import org.jmailen.gradle.kotlinter.support.reporterFileExtension +import org.jmailen.gradle.kotlinter.tasks.ConfigurableKtLintTask import org.jmailen.gradle.kotlinter.tasks.FormatTask -import org.jmailen.gradle.kotlinter.tasks.InstallPreCommitHookTask import org.jmailen.gradle.kotlinter.tasks.InstallPrePushHookTask import org.jmailen.gradle.kotlinter.tasks.LintTask class KotlinterPlugin : Plugin { + companion object { + const val KTLINT_CONFIGURATION_NAME = "ktlint" + } private val extendablePlugins = mapOf( "org.jetbrains.kotlin.jvm" to KotlinSourceSetApplier, @@ -35,38 +39,13 @@ class KotlinterPlugin : Plugin { pluginManager.withPlugin(pluginId) { val lintKotlin = registerParentLintTask() val formatKotlin = registerParentFormatTask() + val ktlintConfiguration = createKtLintConfiguration(kotlinterExtension) + + registerSourceSetTasks(kotlinterExtension, sourceResolver, lintKotlin, formatKotlin) - sourceResolver.applyToAll(this) { id, resolvedSources -> - val lintTaskPerSourceSet = tasks.register( - "lintKotlin${id.replaceFirstChar(Char::titlecase)}", - LintTask::class.java, - ) { lintTask -> - lintTask.source(resolvedSources) - lintTask.ignoreFailures.set(provider { kotlinterExtension.ignoreFailures }) - lintTask.reports.set( - provider { - kotlinterExtension.reporters.associateWith { reporter -> - reportFile("$id-lint.${reporterFileExtension(reporter)}").get().asFile - } - }, - ) - } - lintKotlin.configure { lintTask -> - lintTask.dependsOn(lintTaskPerSourceSet) - } - - val formatKotlinPerSourceSet = tasks.register( - "formatKotlin${id.replaceFirstChar(Char::titlecase)}", - FormatTask::class.java, - ) { formatTask -> - formatTask.source(resolvedSources) - formatTask.failBuildWhenCannotAutoFormat.set(provider { kotlinterExtension.failBuildWhenCannotAutoFormat }) - formatTask.ignoreFailures.set(provider { kotlinterExtension.ignoreFailures }) - formatTask.report.set(reportFile("$id-format.txt")) - } - formatKotlin.configure { formatTask -> - formatTask.dependsOn(formatKotlinPerSourceSet) - } + // Configure all tasks including custom user tasks + tasks.withType(ConfigurableKtLintTask::class.java).configureEach { task -> + task.ktlintClasspath.from(ktlintConfiguration) } } } @@ -84,17 +63,67 @@ class KotlinterPlugin : Plugin { it.description = "Formats the Kotlin source files." } + private fun Project.createKtLintConfiguration(kotlinterExtension: KotlinterExtension): Configuration { + val configuration = configurations.maybeCreate(KTLINT_CONFIGURATION_NAME).apply { + isCanBeResolved = true + isCanBeConsumed = false + isVisible = false + + val dependencyProvider = provider { + // Even though we don't use CLI, it bundles all the runtime dependencies we need. + val ktlintVersion = kotlinterExtension.ktlintVersion + this@createKtLintConfiguration.dependencies.create("com.pinterest.ktlint:ktlint-cli:$ktlintVersion") + } + dependencies.addLater(dependencyProvider) + } + return configuration + } + + private fun Project.registerSourceSetTasks( + kotlinterExtension: KotlinterExtension, + sourceResolver: SourceSetApplier, + parentLintTask: TaskProvider, + parentFormatTask: TaskProvider, + ) { + sourceResolver.applyToAll(this) { id, resolvedSources -> + val lintSourceSetTask = tasks.register( + "lintKotlin${id.replaceFirstChar(Char::titlecase)}", + LintTask::class.java, + ) { lintTask -> + lintTask.source(resolvedSources) + lintTask.ignoreLintFailures.set(provider { kotlinterExtension.ignoreLintFailures }) + lintTask.reports.set( + provider { + kotlinterExtension.reporters.associateWith { reporter -> + reportFile("$id-lint.${reporterFileExtension(reporter)}").get().asFile + } + }, + ) + } + parentLintTask.configure { lintTask -> + lintTask.dependsOn(lintSourceSetTask) + } + + val formatSourceSetTask = tasks.register( + "formatKotlin${id.replaceFirstChar(Char::titlecase)}", + FormatTask::class.java, + ) { formatTask -> + formatTask.source(resolvedSources) + formatTask.ignoreFormatFailures.set(provider { kotlinterExtension.ignoreFormatFailures }) + formatTask.ignoreLintFailures.set(provider { kotlinterExtension.ignoreLintFailures }) + formatTask.report.set(reportFile("$id-format.txt")) + } + parentFormatTask.configure { formatTask -> + formatTask.dependsOn(formatSourceSetTask) + } + } + } + private fun Project.registerPrePushHookTask(): TaskProvider = tasks.register("installKotlinterPrePushHook", InstallPrePushHookTask::class.java) { it.group = "build setup" it.description = "Installs Kotlinter Git pre-push hook" } - - private fun Project.registerPreCommitHookTask(): TaskProvider = - tasks.register("installKotlinterPreCommitHook", InstallPreCommitHookTask::class.java) { - it.group = "build setup" - it.description = "Installs Kotlinter Git pre-commit hook" - } } internal val String.id: String diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/VersionProperties.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/VersionProperties.kt index ad42a6e3..e5fe9bf9 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/VersionProperties.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/VersionProperties.kt @@ -2,10 +2,14 @@ package org.jmailen.gradle.kotlinter.support import java.util.Properties +val versionProperties by lazy { VersionProperties() } + class VersionProperties : Properties() { init { load(this.javaClass.getResourceAsStream("/version.properties")) } fun version(): String = getProperty("version") + + fun ktlintVersion(): String = getProperty("ktlintVersion") } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt index 49a29a61..d49dc4b7 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/ConfigurableKtLintTask.kt @@ -1,21 +1,23 @@ package org.jmailen.gradle.kotlinter.tasks +import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.FileCollection import org.gradle.api.file.ProjectLayout import org.gradle.api.model.ObjectFactory import org.gradle.api.provider.ListProperty import org.gradle.api.provider.MapProperty import org.gradle.api.provider.Property +import org.gradle.api.tasks.Classpath import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.SourceTask -import org.gradle.internal.exceptions.MultiCauseException import org.gradle.work.FileChange import org.gradle.work.Incremental import org.gradle.work.InputChanges -import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_IGNORE_FAILURES +import org.gradle.workers.WorkerExecutionException +import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_IGNORE_LINT_FAILURES import org.jmailen.gradle.kotlinter.support.findApplicableEditorConfigFiles abstract class ConfigurableKtLintTask(projectLayout: ProjectLayout, objectFactory: ObjectFactory) : SourceTask() { @@ -28,7 +30,10 @@ abstract class ConfigurableKtLintTask(projectLayout: ProjectLayout, objectFactor } @Input - open val ignoreFailures: Property = objectFactory.property(default = DEFAULT_IGNORE_FAILURES) + open val ignoreLintFailures: Property = objectFactory.property(default = DEFAULT_IGNORE_LINT_FAILURES) + + @Classpath + val ktlintClasspath: ConfigurableFileCollection = objectFactory.fileCollection() protected fun getChangedEditorconfigFiles(inputChanges: InputChanges) = inputChanges.getFileChanges(editorconfigFiles).map(FileChange::getFile) @@ -48,10 +53,8 @@ internal inline fun ObjectFactory.mapProperty(default: Ma set(default) } -inline fun Throwable.workErrorCauses(): List = when (this) { - is MultiCauseException -> this.causes.map { it.cause } - else -> listOf(this.cause) -}.filter { - // class instance comparison doesn't work due to different classloaders - it?.javaClass?.canonicalName == T::class.java.canonicalName -}.filterNotNull() +fun WorkerExecutionException.hasRootCause(type: Class<*>): Boolean { + // this is lame, but serialized across worker boundaries exceptions are not comparable + // and recursive cause checking runs into serialized placeholder exceptions + return this.stackTraceToString().contains(type.canonicalName) +} 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 decea3d7..a1e8f67c 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt @@ -1,6 +1,5 @@ package org.jmailen.gradle.kotlinter.tasks -import org.gradle.api.GradleException import org.gradle.api.file.ProjectLayout import org.gradle.api.file.RegularFileProperty import org.gradle.api.model.ObjectFactory @@ -10,10 +9,9 @@ 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.WorkerExecutionException 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 @@ -32,8 +30,8 @@ open class FormatTask @Inject constructor( val report: RegularFileProperty = objectFactory.fileProperty() @Input - val failBuildWhenCannotAutoFormat: Property = objectFactory.property( - default = KotlinterExtension.DEFAULT_FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT, + val ignoreFormatFailures: Property = objectFactory.property( + default = KotlinterExtension.DEFAULT_IGNORE_FORMAT_FAILURES, ) init { @@ -42,26 +40,25 @@ open class FormatTask @Inject constructor( @TaskAction fun run(inputChanges: InputChanges) { - val result = with(workerExecutor.noIsolation()) { - submit(FormatWorkerAction::class.java) { p -> - p.name.set(name) - p.files.from(source) - p.projectDirectory.set(projectLayout.projectDirectory.asFile) - p.output.set(report) - p.changedEditorConfigFiles.from(getChangedEditorconfigFiles(inputChanges)) - } - runCatching { await() } + val workQueue = workerExecutor.processIsolation { config -> + config.classpath.setFrom(ktlintClasspath) } - - result.exceptionOrNull()?.workErrorCauses()?.ifNotEmpty { - forEach { logger.error(it.message, it.cause) } - throw GradleException("error formatting sources for $name") + workQueue.submit(FormatWorkerAction::class.java) { p -> + p.name.set(name) + p.files.from(source) + p.projectDirectory.set(projectLayout.projectDirectory.asFile) + p.output.set(report) + p.changedEditorConfigFiles.from(getChangedEditorconfigFiles(inputChanges)) } - - if (failBuildWhenCannotAutoFormat.get()) { - val lintFailures = result.exceptionOrNull()?.workErrorCauses() ?: emptyList() - if (lintFailures.isNotEmpty() && !ignoreFailures.get()) { - throw GradleException("$name sources failed lint check") + try { + workQueue.await() + } catch (e: WorkerExecutionException) { + if (e.hasRootCause(LintFailure::class.java)) { + if (!ignoreFormatFailures.get()) { + throw e + } + } else { + throw e } } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt index f17a0713..d829209f 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/GitHookTasks.kt @@ -5,7 +5,7 @@ import org.gradle.api.provider.Property import org.gradle.api.tasks.Input import org.gradle.api.tasks.Internal import org.gradle.api.tasks.TaskAction -import org.jmailen.gradle.kotlinter.support.VersionProperties +import org.jmailen.gradle.kotlinter.support.versionProperties import java.io.File abstract class InstallPreCommitHookTask : InstallHookTask("pre-commit") { @@ -111,11 +111,9 @@ abstract class InstallHookTask(@get:Internal val hookFileName: String) : Default } companion object { - private val version = VersionProperties().version() - internal const val START_HOOK = "\n##### KOTLINTER HOOK START #####" - internal val hookVersion = "##### KOTLINTER $version #####" + internal val hookVersion = "##### KOTLINTER ${versionProperties.version()} #####" internal const val END_HOOK = "##### KOTLINTER HOOK END #####\n" diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt index c6f33886..bd0e9fa2 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt @@ -1,6 +1,5 @@ package org.jmailen.gradle.kotlinter.tasks -import org.gradle.api.GradleException import org.gradle.api.file.FileTree import org.gradle.api.file.ProjectLayout import org.gradle.api.model.ObjectFactory @@ -12,9 +11,8 @@ import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction import org.gradle.work.InputChanges +import org.gradle.workers.WorkerExecutionException import org.gradle.workers.WorkerExecutor -import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty -import org.jmailen.gradle.kotlinter.support.KotlinterError import org.jmailen.gradle.kotlinter.support.LintFailure import org.jmailen.gradle.kotlinter.tasks.lint.LintWorkerAction import java.io.File @@ -39,25 +37,22 @@ open class LintTask @Inject constructor( @TaskAction fun run(inputChanges: InputChanges) { - val result = with(workerExecutor.noIsolation()) { - submit(LintWorkerAction::class.java) { p -> - p.name.set(name) - p.files.from(source) - p.projectDirectory.set(projectLayout.projectDirectory.asFile) - p.reporters.putAll(reports) - p.changedEditorConfigFiles.from(getChangedEditorconfigFiles(inputChanges)) - } - runCatching { await() } + val workQueue = workerExecutor.processIsolation { config -> + config.classpath.setFrom(ktlintClasspath) } - - result.exceptionOrNull()?.workErrorCauses()?.ifNotEmpty { - forEach { logger.error(it.message, it.cause) } - throw GradleException("error linting sources for $name") + workQueue.submit(LintWorkerAction::class.java) { p -> + p.name.set(name) + p.files.from(source) + p.projectDirectory.set(projectLayout.projectDirectory.asFile) + p.reporters.putAll(reports) + p.changedEditorConfigFiles.from(getChangedEditorconfigFiles(inputChanges)) } - - val lintFailures = result.exceptionOrNull()?.workErrorCauses() ?: emptyList() - if (lintFailures.isNotEmpty() && !ignoreFailures.get()) { - throw GradleException("$name sources failed lint check") + try { + workQueue.await() + } catch (e: WorkerExecutionException) { + if (!(ignoreLintFailures.get() && e.hasRootCause(LintFailure::class.java))) { + throw e + } } } } 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..3c7fe984 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 @@ -64,7 +64,7 @@ abstract class FormatWorkerAction : WorkAction { } if (hasError) { - throw LintFailure("kotlin source failed lint check") + throw LintFailure("kotlin source $name failed lint check") } output?.writeText( diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt index 5b8bf8fd..8b4b7195 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/lint/LintWorkerAction.kt @@ -63,7 +63,7 @@ abstract class LintWorkerAction : WorkAction { } if (hasError) { - throw LintFailure("kotlin source failed lint check") + throw LintFailure("kotlin source $name failed lint check") } } } diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/CustomTaskTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/CustomTaskTest.kt index db8d1517..ae046a14 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/CustomTaskTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/CustomTaskTest.kt @@ -3,6 +3,7 @@ package org.jmailen.gradle.kotlinter.functional import org.gradle.testkit.runner.TaskOutcome import org.jmailen.gradle.kotlinter.functional.utils.editorConfig import org.jmailen.gradle.kotlinter.functional.utils.kotlinClass +import org.jmailen.gradle.kotlinter.functional.utils.repositories import org.jmailen.gradle.kotlinter.functional.utils.resolve import org.jmailen.gradle.kotlinter.functional.utils.settingsFile import org.junit.jupiter.api.Assertions.assertArrayEquals @@ -28,7 +29,7 @@ class CustomTaskTest : WithGradleTest.Kotlin() { id 'kotlin' id 'org.jmailen.kotlinter' } - + $repositories """.trimIndent() writeText(buildScript) } @@ -111,7 +112,7 @@ class CustomTaskTest : WithGradleTest.Kotlin() { task customizedLintTask(type: LintTask) { source files('src') reports = ['plain': file('build/lint-report.txt')] - ignoreFailures = true + ignoreLintFailures = true } """.trimIndent() 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 cd3f25e0..3080bbcb 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/EditorConfigTest.kt @@ -4,6 +4,7 @@ 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.repositories import org.jmailen.gradle.kotlinter.functional.utils.resolve import org.jmailen.gradle.kotlinter.functional.utils.settingsFile import org.junit.jupiter.api.Assertions.assertEquals @@ -27,13 +28,15 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { id 'kotlin' id 'org.jmailen.kotlinter' } + $repositories """.trimIndent() - KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> + KotlinterConfig.FAIL_FORMAT_FAILURES -> """ plugins { id 'org.jetbrains.kotlin.js' id 'org.jmailen.kotlinter' } + $repositories kotlin { js { @@ -42,15 +45,16 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { } kotlinter { - failBuildWhenCannotAutoFormat = true + ignoreFormatFailures = false } """.trimIndent() - KotlinterConfig.IGNORE_FAILURES -> + KotlinterConfig.IGNORE_LINT_FAILURES -> """ plugins { id 'org.jetbrains.kotlin.js' id 'org.jmailen.kotlinter' } + $repositories kotlin { js { @@ -59,8 +63,7 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { } kotlinter { - ignoreFailures = true - failBuildWhenCannotAutoFormat = true + ignoreLintFailures = true } """.trimIndent() } @@ -246,8 +249,8 @@ internal class EditorConfigTest : WithGradleTest.Kotlin() { } @Test - fun `editorconfig changes are taken for format task re-runs when failBuildWhenCannotAutoFormat configured`() { - setup(KotlinterConfig.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT) + fun `editorconfig changes are taken for format task re-runs when ignoreFormatFailures false`() { + setup(KotlinterConfig.FAIL_FORMAT_FAILURES) projectRoot.resolve(".editorconfig") { writeText(editorConfig) } diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/ExtensionTest.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/ExtensionTest.kt index b5039e97..e7e946b5 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/ExtensionTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/ExtensionTest.kt @@ -2,6 +2,7 @@ package org.jmailen.gradle.kotlinter.functional import org.gradle.testkit.runner.TaskOutcome import org.jmailen.gradle.kotlinter.functional.utils.kotlinClass +import org.jmailen.gradle.kotlinter.functional.utils.repositories import org.jmailen.gradle.kotlinter.functional.utils.resolve import org.jmailen.gradle.kotlinter.functional.utils.settingsFile import org.junit.jupiter.api.Assertions.assertEquals @@ -22,11 +23,11 @@ internal class ExtensionTest : WithGradleTest.Kotlin() { // language=groovy val buildScript = """ - plugins { - id 'kotlin' - id 'org.jmailen.kotlinter' - } - + plugins { + id 'kotlin' + id 'org.jmailen.kotlinter' + } + $repositories """.trimIndent() writeText(buildScript) } @@ -34,13 +35,13 @@ internal class ExtensionTest : WithGradleTest.Kotlin() { } @Test - fun `extension configures ignoreFailures`() { + fun `extension configures ignoreLintFailures`() { projectRoot.resolve("build.gradle") { // language=groovy val script = """ kotlinter { - ignoreFailures = true + ignoreLintFailures = true } """.trimIndent() appendText(script) @@ -87,13 +88,14 @@ internal class ExtensionTest : WithGradleTest.Kotlin() { id 'kotlin' id 'org.jmailen.kotlinter' } + $repositories tasks.whenTaskAdded { // configure all tasks eagerly } kotlinter { - ignoreFailures = true + ignoreLintFailures = 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 c2df7247..3c7c4253 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.FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT -> + KotlinterConfig.FAIL_FORMAT_FAILURES -> """ plugins { id 'org.jetbrains.kotlin.js' @@ -50,10 +50,10 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { } kotlinter { - failBuildWhenCannotAutoFormat = true + ignoreFormatFailures = false } """.trimIndent() - KotlinterConfig.IGNORE_FAILURES -> + KotlinterConfig.IGNORE_LINT_FAILURES -> """ plugins { id 'org.jetbrains.kotlin.js' @@ -70,8 +70,7 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { } kotlinter { - ignoreFailures = true - failBuildWhenCannotAutoFormat = true + ignoreLintFailures = true } """.trimIndent() } @@ -160,8 +159,8 @@ 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) + fun `formatKotlin fails when lint errors not automatically fixed and ignoreFormatFailures false`() { + setup(KotlinterConfig.FAIL_FORMAT_FAILURES) projectRoot.resolve("src/main/kotlin/FixtureClass.kt") { // language=kotlin val kotlinClass = @@ -199,45 +198,4 @@ class KotlinJsProjectTest : WithGradleTest.Kotlin() { assertTrue(output.contains("FixtureTestClass.kt:1:1: Format could not fix > [standard:no-wildcard-imports] Wildcard import")) } } - - @Test - 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 - 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")) - } - } } 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 95037c6b..5373b4c3 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/KotlinProjectTest.kt @@ -103,9 +103,9 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } @Test - fun `formatKotlin fails when lint errors not automatically fixed and failBuildWhenCannotAutoFormat enabled`() { + fun `formatKotlin fails when lint errors not automatically fixed and ignoreFormatFailures false`() { settingsFile() - buildFileFailBuildWhenCannotAutoFormat() + buildFileIgnoreFormatFailuresFalse() // language=kotlin val kotlinClass = """ @@ -128,32 +128,6 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } } - @Test - fun `formatKotlin reports formatted and unformatted files when failBuildWhenCannotAutoFormat and ignoreFailures 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 `check task runs lintFormat`() { settingsFile() @@ -262,27 +236,7 @@ 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 { - failBuildWhenCannotAutoFormat = true - } - """.trimIndent() - writeText(buildscript) - } - - private fun buildFileIgnoreFailures() = buildFile.apply { + private fun buildFileIgnoreFormatFailuresFalse() = buildFile.apply { // language=groovy val buildscript = """ @@ -296,8 +250,7 @@ internal class KotlinProjectTest : WithGradleTest.Kotlin() { } kotlinter { - ignoreFailures = true - failBuildWhenCannotAutoFormat = true + ignoreFormatFailures = false } """.trimIndent() writeText(buildscript) diff --git a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/Factories.kt b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/Factories.kt index b940a93a..344dd56e 100644 --- a/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/Factories.kt +++ b/src/test/kotlin/org/jmailen/gradle/kotlinter/functional/utils/Factories.kt @@ -25,3 +25,11 @@ internal val editorConfig = [*.kt] """ + +internal val repositories = + """ + repositories { + mavenCentral() + } + + """.trimIndent() 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 b7ea0305..98e00187 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, - FAIL_BUILD_WHEN_CANNOT_AUTO_FORMAT, - IGNORE_FAILURES, + FAIL_FORMAT_FAILURES, + IGNORE_LINT_FAILURES, }