From 91d6203e5d874116157746ec91b8d4f820840854 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Thu, 5 Dec 2024 03:00:27 +0000 Subject: [PATCH] chore(deps): update plugin kotlin-qa to v0.75.0 --- build.gradle.kts | 1 + gradle/libs.versions.toml | 2 +- .../gradle/git/hooks/AbstractScriptContext.kt | 13 ++-- .../git/hooks/CommitMsgScriptContext.kt | 21 +++--- .../gradle/git/hooks/CommonScriptContext.kt | 42 +++++++----- .../git/hooks/ConventionalCommitsContext.kt | 26 +++---- .../gradle/git/hooks/GitHooksExtension.kt | 21 +++--- .../gradle/git/hooks/GradleGitHooksPlugin.kt | 2 +- .../gradle/git/hooks/ScriptContext.kt | 33 ++++++--- .../gradle/git/hooks/test/TestingDSL.kt | 51 +++++++------- .../gradle/git/hooks/test/Tests.kt | 68 +++++++++++-------- 11 files changed, 165 insertions(+), 115 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index e36b6d96..3734c0e3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,6 +19,7 @@ plugins { */ group = "org.danilopianini" description = "A Gradle plugin enforcing pre-commit and commit-msg Git hooks configuration. Conventional-commits-ready." + inner class ProjectInfo { val longName = "Gradle pre-commit Git Hooks" val website = "https://github.com/DanySK/$name" diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e2690ded..b413a45e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -19,7 +19,7 @@ dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" } gitSemVer = { id = "org.danilopianini.git-sensitive-semantic-versioning", version = "3.1.7" } gradlePluginPublish = { id = "com.gradle.plugin-publish", version = "1.3.0" } kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } -kotlin-qa = { id = "org.danilopianini.gradle-kotlin-qa", version = "0.70.2" } +kotlin-qa = { id = "org.danilopianini.gradle-kotlin-qa", version = "0.75.0" } multiJvmTesting = { id = "org.danilopianini.multi-jvm-test-plugin", version = "1.3.2" } publishOnCentral = { id = "org.danilopianini.publish-on-central", version = "6.0.0" } taskTree = { id = "com.dorongold.task-tree", version = "4.0.0" } diff --git a/src/main/kotlin/org/danilopianini/gradle/git/hooks/AbstractScriptContext.kt b/src/main/kotlin/org/danilopianini/gradle/git/hooks/AbstractScriptContext.kt index d539ae45..55bf1c37 100644 --- a/src/main/kotlin/org/danilopianini/gradle/git/hooks/AbstractScriptContext.kt +++ b/src/main/kotlin/org/danilopianini/gradle/git/hooks/AbstractScriptContext.kt @@ -4,9 +4,14 @@ package org.danilopianini.gradle.git.hooks * Pre-implements [tasks]. */ abstract class AbstractScriptContext : ScriptContext { + final override fun tasks( + first: Any, + vararg others: Any, + requireSuccess: Boolean, + ) = processTasks(first, *others, requireSuccess = requireSuccess) - final override fun tasks(first: Any, vararg others: Any, requireSuccess: Boolean) = - processTasks(first, *others, requireSuccess = requireSuccess) - - protected abstract fun processTasks(vararg tasks: Any, requireSuccess: Boolean) + protected abstract fun processTasks( + vararg tasks: Any, + requireSuccess: Boolean, + ) } diff --git a/src/main/kotlin/org/danilopianini/gradle/git/hooks/CommitMsgScriptContext.kt b/src/main/kotlin/org/danilopianini/gradle/git/hooks/CommitMsgScriptContext.kt index a1d80afa..6455b286 100644 --- a/src/main/kotlin/org/danilopianini/gradle/git/hooks/CommitMsgScriptContext.kt +++ b/src/main/kotlin/org/danilopianini/gradle/git/hooks/CommitMsgScriptContext.kt @@ -6,24 +6,25 @@ import java.net.URL * Specialized hook context for commit-msg. */ class CommitMsgScriptContext : CommonScriptContext("commit-msg") { - /** * Pre-configures the commit-msg script to check for a valid * [conventional commit](https://www.conventionalcommits.org/) * message. */ fun conventionalCommits(configuration: ConventionalCommitsContext.() -> Unit = { defaultTypes() }) { - val types = object : ConventionalCommitsContext { - override var types = super.types + val types = + object : ConventionalCommitsContext { + override var types = super.types - override fun types(otherTypes: Set) { - types = types + otherTypes - } - }.apply(configuration).types + override fun types(otherTypes: Set) { + types = types + otherTypes + } + }.apply(configuration).types from("") { - val script: URL = requireNotNull(Thread.currentThread().contextClassLoader.getResource(SCRIPT_PATH)) { - "Unable to load $SCRIPT_PATH, this is likely a bug in the ${GitHooksExtension.name} plugin" - } + val script: URL = + requireNotNull(Thread.currentThread().contextClassLoader.getResource(SCRIPT_PATH)) { + "Unable to load $SCRIPT_PATH, this is likely a bug in the ${GitHooksExtension.NAME} plugin" + } script.readText().replace("# INJECT_TYPES", types.joinToString(separator = " ")) } } diff --git a/src/main/kotlin/org/danilopianini/gradle/git/hooks/CommonScriptContext.kt b/src/main/kotlin/org/danilopianini/gradle/git/hooks/CommonScriptContext.kt index f6fe4081..50abb245 100644 --- a/src/main/kotlin/org/danilopianini/gradle/git/hooks/CommonScriptContext.kt +++ b/src/main/kotlin/org/danilopianini/gradle/git/hooks/CommonScriptContext.kt @@ -13,21 +13,24 @@ open class CommonScriptContext(override val name: String) : AbstractScriptContex override fun appendScript(script: () -> String) { require(this.script.isNotEmpty()) { """ - An append was requested to an uninitialized script $name. Configure as follows, instead: - gitHooks { - { - from { - - } - + An append was requested to an uninitialized script $name. Configure as follows, instead: + gitHooks { + { + from { + } + } + } """.trimIndent() } this.script += script() + '\n' } - final override fun from(shebang: String?, script: () -> String) { + final override fun from( + shebang: String?, + script: () -> String, + ) { require(this.script.isEmpty()) { """ The $name git hook is being defined twice, and this is likely an error. Formerly: @@ -45,15 +48,22 @@ open class CommonScriptContext(override val name: String) : AbstractScriptContex this.script = (shebang?.takeIf { it.isNotBlank() }?.let { "$it\n" }.orEmpty()) + script() } - final override fun processTasks(vararg tasks: Any, requireSuccess: Boolean) { - val names = tasks.map { task -> - when (task) { - is String -> task - is Task -> task.name - is TaskProvider<*> -> task.name - else -> error("Object '$task' with type '${task::class.simpleName}' cannot produce valid task names") + final override fun processTasks( + vararg tasks: Any, + requireSuccess: Boolean, + ) { + val names = + tasks.map { task -> + when (task) { + is String -> task + is Task -> task.name + is TaskProvider<*> -> task.name + else -> + error( + "Object '$task' with type '${task::class.simpleName}' cannot produce valid task names", + ) + } } - } if (script.isEmpty()) { from { "" } } diff --git a/src/main/kotlin/org/danilopianini/gradle/git/hooks/ConventionalCommitsContext.kt b/src/main/kotlin/org/danilopianini/gradle/git/hooks/ConventionalCommitsContext.kt index 16fde2c1..22ccc2b3 100644 --- a/src/main/kotlin/org/danilopianini/gradle/git/hooks/ConventionalCommitsContext.kt +++ b/src/main/kotlin/org/danilopianini/gradle/git/hooks/ConventionalCommitsContext.kt @@ -4,7 +4,6 @@ package org.danilopianini.gradle.git.hooks * Specialized DSL element for dealing with [conventional commits](https://www.conventionalcommits.org/). */ interface ConventionalCommitsContext { - /** * The selected valid commit types. */ @@ -29,7 +28,6 @@ interface ConventionalCommitsContext { * Container for the default types. */ companion object { - /** * Base types: `fix` and `feat`. */ @@ -39,16 +37,18 @@ interface ConventionalCommitsContext { * Additional types, as listed in the [conventional commits](https://www.conventionalcommits.org/) webpage, * plus `refactor`, which is commonly used. */ - val defaultTypes: Set = baseTypes + setOf( - "build", - "chore", - "ci", - "docs", - "perf", - "refactor", - "revert", - "style", - "test", - ) + val defaultTypes: Set = + baseTypes + + setOf( + "build", + "chore", + "ci", + "docs", + "perf", + "refactor", + "revert", + "style", + "test", + ) } } diff --git a/src/main/kotlin/org/danilopianini/gradle/git/hooks/GitHooksExtension.kt b/src/main/kotlin/org/danilopianini/gradle/git/hooks/GitHooksExtension.kt index 372a42fe..2265f453 100644 --- a/src/main/kotlin/org/danilopianini/gradle/git/hooks/GitHooksExtension.kt +++ b/src/main/kotlin/org/danilopianini/gradle/git/hooks/GitHooksExtension.kt @@ -9,7 +9,6 @@ import java.io.Serializable * DSL entry point, to be applied to [settings].gradle.kts. */ open class GitHooksExtension(val settings: Settings) : Serializable { - private var hooks: Map = emptyMap() private var pathHasBeenManuallySet = false @@ -40,7 +39,10 @@ open class GitHooksExtension(val settings: Settings) : Serializable { private val gitDir get() = File(repoRoot, ".git") - private inline fun hook(context: H, configuration: H.() -> Unit) { + private inline fun hook( + context: H, + configuration: H.() -> Unit, + ) { require(!hooks.containsKey(context.name)) { "it looks like the hook ${context.name} is being defined twice" } @@ -50,8 +52,10 @@ open class GitHooksExtension(val settings: Settings) : Serializable { /** * Defines a new hook with an arbitrary name. */ - fun hook(hookName: String, configuration: ScriptContext.() -> Unit) = - hook(CommonScriptContext(hookName), configuration) + fun hook( + hookName: String, + configuration: ScriptContext.() -> Unit, + ) = hook(CommonScriptContext(hookName), configuration) /** * Pre-commit hook. @@ -146,7 +150,7 @@ open class GitHooksExtension(val settings: Settings) : Serializable { /** * Extension name. */ - internal const val name: String = "gitHooks" + internal const val NAME: String = "gitHooks" private fun String.withMargins() = lines().joinToString(separator = "\n|", prefix = "|") @@ -155,8 +159,9 @@ open class GitHooksExtension(val settings: Settings) : Serializable { setExecutable(true) } - private fun File.isGitRoot(): Boolean = listFiles() - ?.any { file -> file.name == ".git" } - ?: false + private fun File.isGitRoot(): Boolean = + listFiles() + ?.any { file -> file.name == ".git" } + ?: false } } diff --git a/src/main/kotlin/org/danilopianini/gradle/git/hooks/GradleGitHooksPlugin.kt b/src/main/kotlin/org/danilopianini/gradle/git/hooks/GradleGitHooksPlugin.kt index c7637a83..943d03c4 100644 --- a/src/main/kotlin/org/danilopianini/gradle/git/hooks/GradleGitHooksPlugin.kt +++ b/src/main/kotlin/org/danilopianini/gradle/git/hooks/GradleGitHooksPlugin.kt @@ -21,6 +21,6 @@ open class GradleGitHooksPlugin : Plugin { } """.trimIndent() } - settings.extensions.create(GitHooksExtension.name, settings) + settings.extensions.create(GitHooksExtension.NAME, settings) } } diff --git a/src/main/kotlin/org/danilopianini/gradle/git/hooks/ScriptContext.kt b/src/main/kotlin/org/danilopianini/gradle/git/hooks/ScriptContext.kt index c1d2b6de..0e900101 100644 --- a/src/main/kotlin/org/danilopianini/gradle/git/hooks/ScriptContext.kt +++ b/src/main/kotlin/org/danilopianini/gradle/git/hooks/ScriptContext.kt @@ -9,7 +9,6 @@ import java.net.URL * Collects the DSL elements for defining scripts. */ interface ScriptContext { - /** * Hook name. */ @@ -44,36 +43,52 @@ interface ScriptContext { * Generates a script with either the provided [shebang] or with a shebang invoking bash, * and with the result of the provided function. */ - fun from(shebang: String? = "#!/usr/bin/env bash", script: () -> String) + fun from( + shebang: String? = "#!/usr/bin/env bash", + script: () -> String, + ) /** * Adds the provided tasks to the script, by invoking `./gradlew `. * By default, a failure of the task implies a failure of the commit. * To run a task without considering the failure critical, pass `[requireSuccess] = false`. */ - fun tasks(name: String, vararg otherNames: String, requireSuccess: Boolean = true) = - tasks(name as Any, *otherNames, requireSuccess = requireSuccess) + fun tasks( + name: String, + vararg otherNames: String, + requireSuccess: Boolean = true, + ) = tasks(name as Any, *otherNames, requireSuccess = requireSuccess) /** * Adds the provided tasks to the script, by invoking `./gradlew `. * By default, a failure of the task implies a failure of the commit. * To run a task without considering the failure critical, pass `[requireSuccess] = false`. */ - fun tasks(task: Task, vararg otherTasks: Task, requireSuccess: Boolean = true) = - tasks(task as Any, *otherTasks, requireSuccess = requireSuccess) + fun tasks( + task: Task, + vararg otherTasks: Task, + requireSuccess: Boolean = true, + ) = tasks(task as Any, *otherTasks, requireSuccess = requireSuccess) /** * Adds the provided tasks to the script, by invoking `./gradlew `. * By default, a failure of the task implies a failure of the commit. * To run a task without considering the failure critical, pass `[requireSuccess] = false`. */ - fun tasks(task: TaskProvider<*>, vararg otherTasks: TaskProvider<*>, requireSuccess: Boolean = true) = - tasks(task as Any, *otherTasks, requireSuccess = requireSuccess) + fun tasks( + task: TaskProvider<*>, + vararg otherTasks: TaskProvider<*>, + requireSuccess: Boolean = true, + ) = tasks(task as Any, *otherTasks, requireSuccess = requireSuccess) /** * Adds the provided tasks to the script, by invoking `./gradlew `. * By default, a failure of the task implies a failure of the commit. * To run a task without considering the failure critical, pass `[requireSuccess] = false`. */ - fun tasks(first: Any, vararg others: Any, requireSuccess: Boolean = true) + fun tasks( + first: Any, + vararg others: Any, + requireSuccess: Boolean = true, + ) } diff --git a/src/test/kotlin/org/danilopianini/gradle/git/hooks/test/TestingDSL.kt b/src/test/kotlin/org/danilopianini/gradle/git/hooks/test/TestingDSL.kt index f0dfc201..0485e5ea 100644 --- a/src/test/kotlin/org/danilopianini/gradle/git/hooks/test/TestingDSL.kt +++ b/src/test/kotlin/org/danilopianini/gradle/git/hooks/test/TestingDSL.kt @@ -26,11 +26,15 @@ data class Expectation( ) enum class Permission(private val hasPermission: File.() -> Boolean) { - R(File::canRead), W(File::canWrite), X(File::canExecute); + R(File::canRead), + W(File::canWrite), + X(File::canExecute), + ; - fun requireOnFile(file: File) = require(file.hasPermission()) { - "File ${file.absolutePath} must have permission $name, but it does not." - } + fun requireOnFile(file: File) = + require(file.hasPermission()) { + "File ${file.absolutePath} must have permission $name, but it does not." + } } data class ExistingFile( @@ -40,14 +44,15 @@ data class ExistingFile( val trim: Boolean = false, val permissions: List = emptyList(), ) { - fun validate(actualFile: File): Unit = with(actualFile) { - require(exists()) { - "File $name does not exist." - } - if (content != null) { - val text = readText() - require(text.trim() == content.trim()) { - """ + fun validate(actualFile: File): Unit = + with(actualFile) { + require(exists()) { + "File $name does not exist." + } + if (content != null) { + val text = readText() + require(text.trim() == content.trim()) { + """ |Content of $name does not match expectations. | |Expected: @@ -58,18 +63,18 @@ data class ExistingFile( | |Difference starts at index ${StringUtils.indexOfDifference(content, text)}: |${StringUtils.difference(content, text).replace(Regex("\\R"), "\n|")} - """.trimMargin() + """.trimMargin() + } } - } - if (findRegex != null) { - val regex = Regex(findRegex) - requireNotNull(readLines().find { regex.matches(it) }) { - """ - None of the lines in $name matches the regular expression $findRegex. File content: - ${readText()} - """.trimIndent() + if (findRegex != null) { + val regex = Regex(findRegex) + requireNotNull(readLines().find { regex.matches(it) }) { + """ + None of the lines in $name matches the regular expression $findRegex. File content: + ${readText()} + """.trimIndent() + } } + permissions.forEach { it.requireOnFile(this) } } - permissions.forEach { it.requireOnFile(this) } - } } diff --git a/src/test/kotlin/org/danilopianini/gradle/git/hooks/test/Tests.kt b/src/test/kotlin/org/danilopianini/gradle/git/hooks/test/Tests.kt index ac29b59c..d00d529f 100644 --- a/src/test/kotlin/org/danilopianini/gradle/git/hooks/test/Tests.kt +++ b/src/test/kotlin/org/danilopianini/gradle/git/hooks/test/Tests.kt @@ -21,31 +21,35 @@ import java.util.regex.Pattern class Tests : StringSpec( { - val scan = ClassGraph() - .enableAllInfo() - .acceptPackages(Tests::class.java.`package`.name) - .scan() + val scan = + ClassGraph() + .enableAllInfo() + .acceptPackages(Tests::class.java.`package`.name) + .scan() scan.getResourcesWithLeafName("test.yaml") .flatMap { resource -> log.debug("Found test list in {}", resource) val yamlFile = File(resource.classpathElementFile.absolutePath + "/" + resource.path) - val testConfiguration = Config { - addSpec(Root) - }.from.yaml.inputStream(resource.open()) + val testConfiguration = + Config { + addSpec(Root) + }.from.yaml.inputStream(resource.open()) testConfiguration[Root.tests].map { it to yamlFile.parentFile } } .forEach { (test, location) -> log.debug("Test to be executed: {} from {}", test, location) - val testFolder = folder { - location.copyRecursively(this.root) - } + val testFolder = + folder { + location.copyRecursively(this.root) + } log.debug("Test has been copied into {} and is ready to get executed", testFolder) test.description { - val result = GradleRunner.create() - .withProjectDir(testFolder.root) - .withArguments(test.configuration.tasks + test.configuration.options) - .withPluginClasspath() - .run { if (test.expectation.failure.isEmpty()) build() else buildAndFail() } + val result = + GradleRunner.create() + .withProjectDir(testFolder.root) + .withArguments(test.configuration.tasks + test.configuration.options) + .withPluginClasspath() + .run { if (test.expectation.failure.isEmpty()) build() else buildAndFail() } println(result.tasks) println(result.output) test.expectation.output_contains.forEach { @@ -58,10 +62,11 @@ class Tests : StringSpec( result.outcomeOf(it) shouldBe TaskOutcome.FAILED } test.expectation.file_exists.forEach { - val file = File("${testFolder.root.absolutePath}/${it.name}").apply { - shouldExist() - shouldBeAFile() - } + val file = + File("${testFolder.root.absolutePath}/${it.name}").apply { + shouldExist() + shouldBeAFile() + } it.validate(file) } test.expectation.post_run_script.takeIf { it.isNotEmpty() }?.also { postRuns -> @@ -73,10 +78,11 @@ class Tests : StringSpec( writeText(postRun) setExecutable(true) } - val process = ProcessBuilder() - .directory(testFolder.root) - .command(shell, fileName) - .start() + val process = + ProcessBuilder() + .directory(testFolder.root) + .command(shell, fileName) + .start() val finished = process.waitFor(10, TimeUnit.SECONDS) finished shouldBe true log.debug(process.inputStream.bufferedReader().use { it.readText() }) @@ -94,14 +100,16 @@ class Tests : StringSpec( val log: Logger = LoggerFactory.getLogger(Tests::class.java) private val shells = listOf("sh", "bash", "zsh", "fish", "csh", "ksh") - private fun BuildResult.outcomeOf(name: String) = requireNotNull(task(":$name")) { - "Task $name was not present among the executed tasks" - }.outcome + private fun BuildResult.outcomeOf(name: String) = + requireNotNull(task(":$name")) { + "Task $name was not present among the executed tasks" + }.outcome - private fun folder(closure: TemporaryFolder.() -> Unit) = TemporaryFolder().apply { - create() - closure() - } + private fun folder(closure: TemporaryFolder.() -> Unit) = + TemporaryFolder().apply { + create() + closure() + } private fun findShell(): String? { val paths = System.getenv("PATH").split(Regex(Pattern.quote(File.pathSeparator)))