diff --git a/.editorconfig b/.editorconfig index 0b364cd..faf302e 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,3 +1,4 @@ [*.kt] ktlint_standard_trailing-comma-on-call-site = disabled -ktlint_standard_trailing-comma-on-declaration-site = disabled \ No newline at end of file +ktlint_standard_trailing-comma-on-declaration-site = disabled +ktlint_function_naming_ignore_when_annotated_with = Test, ParameterizedWithGradleVersions \ No newline at end of file diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index bc74b43..1c84570 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -11,7 +11,7 @@ repositories { tasks.withType { compilerOptions { - jvmTarget = JvmTarget.JVM_11 + jvmTarget = JvmTarget.JVM_17 } } @@ -24,8 +24,8 @@ sourceSets { } java { - sourceCompatibility = JavaVersion.VERSION_11 - targetCompatibility = JavaVersion.VERSION_11 + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 } dependencies { diff --git a/coverage-plugin/src/main/kotlin/com/toasttab/gradle/testkit/FlushJacocoPlugin.kt b/coverage-plugin/src/main/kotlin/com/toasttab/gradle/testkit/FlushJacocoPlugin.kt index 3015e48..e21b567 100644 --- a/coverage-plugin/src/main/kotlin/com/toasttab/gradle/testkit/FlushJacocoPlugin.kt +++ b/coverage-plugin/src/main/kotlin/com/toasttab/gradle/testkit/FlushJacocoPlugin.kt @@ -23,13 +23,15 @@ import org.gradle.api.flow.FlowParameters import org.gradle.api.flow.FlowScope import javax.inject.Inject -class FlushJacocoPlugin @Inject constructor( - private val flowScope: FlowScope -) : Plugin { - override fun apply(target: Project) { - flowScope.always(DumpAction::class.java) { } +class FlushJacocoPlugin + @Inject + constructor( + private val flowScope: FlowScope + ) : Plugin { + override fun apply(target: Project) { + flowScope.always(DumpAction::class.java) { } + } } -} class DumpAction : FlowAction { override fun execute(parameters: FlowParameters.None) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index df33668..c0ed627 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,8 +2,8 @@ kotlin = "2.3.21" nexus = "2.0.0" -ktlint = "0.50.0" -spotless = "6.21.0" +ktlint = "1.8.0" +spotless = "8.4.0" jacoco = "0.8.14" diff --git a/integration-tests/src/test/kotlin/com/toasttab/gradle/testkit/FlushJacocoPluginIntegrationTest.kt b/integration-tests/src/test/kotlin/com/toasttab/gradle/testkit/FlushJacocoPluginIntegrationTest.kt index 5e9a961..a276e8f 100644 --- a/integration-tests/src/test/kotlin/com/toasttab/gradle/testkit/FlushJacocoPluginIntegrationTest.kt +++ b/integration-tests/src/test/kotlin/com/toasttab/gradle/testkit/FlushJacocoPluginIntegrationTest.kt @@ -48,12 +48,13 @@ class FlushJacocoPluginIntegrationTest { """.trimIndent() ) - TestProjectExtension.createProject( - projectDir = projectDir, - gradleVersion = GradleVersionArgument.of("8.7"), - cleanup = true, - coverageRecorder = recorder - ).build("build", "--configuration-cache", "--stacktrace") + TestProjectExtension + .createProject( + projectDir = projectDir, + gradleVersion = GradleVersionArgument.of("8.7"), + cleanup = true, + coverageRecorder = recorder + ).build("build", "--configuration-cache", "--stacktrace") } recorder.close() @@ -61,15 +62,16 @@ class FlushJacocoPluginIntegrationTest { val classes = hashSetOf() file.inputStream().buffered().use { - ExecutionDataReader(it).apply { - setExecutionDataVisitor { data -> - if (data.name.startsWith("com/toasttab")) { - classes.add(data.name) + ExecutionDataReader(it) + .apply { + setExecutionDataVisitor { data -> + if (data.name.startsWith("com/toasttab")) { + classes.add(data.name) + } } - } - setSessionInfoVisitor { } - }.read() + setSessionInfoVisitor { } + }.read() } expectThat(classes).contains( diff --git a/jacoco-reflect/src/main/kotlin/com/toasttab/gradle/testkit/jacoco/JacocoRt.kt b/jacoco-reflect/src/main/kotlin/com/toasttab/gradle/testkit/jacoco/JacocoRt.kt index 2928c2c..415c3a0 100644 --- a/jacoco-reflect/src/main/kotlin/com/toasttab/gradle/testkit/jacoco/JacocoRt.kt +++ b/jacoco-reflect/src/main/kotlin/com/toasttab/gradle/testkit/jacoco/JacocoRt.kt @@ -30,7 +30,10 @@ private class ReflectiveJacocoAgent( ) : JacocoAgent { private val options by lazy { // agent.options - agent.javaClass.getDeclaredField("options").apply { isAccessible = true }.get(agent) + agent.javaClass + .getDeclaredField("options") + .apply { isAccessible = true } + .get(agent) } override val location: String @@ -53,11 +56,12 @@ object JacocoRt { private val agentLookup by lazy { runCatching { - val rt = try { - ClassLoader.getSystemClassLoader().loadClass(RT_CLASS) - } catch (e: ClassNotFoundException) { - JacocoRt::class.java.classLoader.loadClass(RT_CLASS) - } + val rt = + try { + ClassLoader.getSystemClassLoader().loadClass(RT_CLASS) + } catch (e: ClassNotFoundException) { + JacocoRt::class.java.classLoader.loadClass(RT_CLASS) + } ReflectiveJacocoAgent(rt.getMethod("getAgent").invoke(null)) } diff --git a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/CoverageRecorder.kt b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/CoverageRecorder.kt index f26bd56..d21c9ab 100644 --- a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/CoverageRecorder.kt +++ b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/CoverageRecorder.kt @@ -33,34 +33,35 @@ private class ReaderTask( private val readingSession = AtomicBoolean(false) private val running = AtomicBoolean(true) - private val reader = object : RemoteControlReader(sock.getInputStream()) { - init { - setSessionInfoVisitor { s -> - readingSession.set(true) - } + private val reader = + object : RemoteControlReader(sock.getInputStream()) { + init { + setSessionInfoVisitor { s -> + readingSession.set(true) + } - setExecutionDataVisitor { ex -> - synchronized(writer) { - writer.visitClassExecution(ex) + setExecutionDataVisitor { ex -> + synchronized(writer) { + writer.visitClassExecution(ex) + } } } - } - override fun readBlock(blockid: Byte): Boolean { - if (blockid == 32.toByte()) { - // OK message which follows jacoco tcpclient dumping execution data - finishSession() + override fun readBlock(blockid: Byte): Boolean { + if (blockid == 32.toByte()) { + // OK message which follows jacoco tcpclient dumping execution data + finishSession() + } + return super.readBlock(blockid) } - return super.readBlock(blockid) - } - override fun read() = - try { - super.read() - } catch (e: Exception) { - false - } - } + override fun read() = + try { + super.read() + } catch (e: Exception) { + false + } + } init { start() @@ -103,21 +104,23 @@ class CoverageRecorder( private val tasks = mutableListOf() - private val runner = thread { - while (!server.isClosed) { - val sock = try { - server.accept() - } catch (e: Exception) { - break + private val runner = + thread { + while (!server.isClosed) { + val sock = + try { + server.accept() + } catch (e: Exception) { + break + } + + tasks.add(ReaderTask(sock, writer)) } - tasks.add(ReaderTask(sock, writer)) - } - - for (task in tasks) { - task.done() + for (task in tasks) { + task.done() + } } - } val port: Int get() = server.localPort diff --git a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/GradleVersion.kt b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/GradleVersion.kt index dd17231..b2c7866 100644 --- a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/GradleVersion.kt +++ b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/GradleVersion.kt @@ -22,4 +22,7 @@ annotation class GradleVersion( ) @Retention(AnnotationRetention.RUNTIME) -annotation class Property(val key: String, val value: String) +annotation class Property( + val key: String, + val value: String +) diff --git a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/GradleVersionArgument.kt b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/GradleVersionArgument.kt index 97557a6..c400707 100644 --- a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/GradleVersionArgument.kt +++ b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/GradleVersionArgument.kt @@ -21,18 +21,20 @@ class GradleVersionArgument private constructor( ) { fun property(key: String): String? = properties[key] - override fun toString() = when { - version == null -> "default" - properties.isEmpty() -> version - else -> "$version${properties.entries.joinToString(prefix = " [", postfix = "]") { "${it.key}=${it.value}" }}" - } + override fun toString() = + when { + version == null -> "default" + properties.isEmpty() -> version + else -> "$version${properties.entries.joinToString(prefix = " [", postfix = "]") { "${it.key}=${it.value}" }}" + } companion object { - fun of(version: String, properties: Map = emptyMap()) = - GradleVersionArgument(version, properties) + fun of( + version: String, + properties: Map = emptyMap() + ) = GradleVersionArgument(version, properties) - fun of(spec: GradleVersion) = - GradleVersionArgument(spec.version, spec.properties.associate { it.key to it.value }) + fun of(spec: GradleVersion) = GradleVersionArgument(spec.version, spec.properties.associate { it.key to it.value }) val DEFAULT = GradleVersionArgument(null) } diff --git a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/ProjectLocator.kt b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/ProjectLocator.kt index ae730c4..2387f19 100644 --- a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/ProjectLocator.kt +++ b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/ProjectLocator.kt @@ -20,15 +20,22 @@ import java.nio.file.Path import kotlin.io.path.Path interface ProjectLocator { - fun projectPath(root: String, context: ExtensionContext): Path + fun projectPath( + root: String, + context: ExtensionContext + ): Path } class SimpleNameProjectLocator : ProjectLocator { - override fun projectPath(root: String, context: ExtensionContext) = - Path(root, context.requiredTestClass.simpleName, context.requiredTestMethod.name) + override fun projectPath( + root: String, + context: ExtensionContext + ) = Path(root, context.requiredTestClass.simpleName, context.requiredTestMethod.name) } class FullyQualifiedNameProjectLocator : ProjectLocator { - override fun projectPath(root: String, context: ExtensionContext) = - Path(root, context.requiredTestClass.name.replace('.', '/'), context.requiredTestMethod.name) + override fun projectPath( + root: String, + context: ExtensionContext + ) = Path(root, context.requiredTestClass.name.replace('.', '/'), context.requiredTestMethod.name) } diff --git a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestKitDisplayNameGenerator.kt b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestKitDisplayNameGenerator.kt index 7d8f055..09c8690 100644 --- a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestKitDisplayNameGenerator.kt +++ b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestKitDisplayNameGenerator.kt @@ -19,10 +19,14 @@ import org.junit.jupiter.api.DisplayNameGenerator.Standard import java.lang.reflect.Method class TestKitDisplayNameGenerator : Standard() { - override fun generateDisplayNameForMethod(cls: Class<*>, method: Method) = - method.name + method.parameterTypes.filter { - it != TestProject::class.java - }.joinToString(", ", prefix = "(", postfix = ")") { - it.simpleName - } + override fun generateDisplayNameForMethod( + cls: Class<*>, + method: Method + ) = method.name + + method.parameterTypes + .filter { + it != TestProject::class.java + }.joinToString(", ", prefix = "(", postfix = ")") { + it.simpleName + } } diff --git a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestProject.kt b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestProject.kt index 1633877..fada8dd 100644 --- a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestProject.kt +++ b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestProject.kt @@ -51,15 +51,17 @@ class TestProject( private fun createRunner(vararg args: String) = createRunner().withArguments(initArgs + args) - private fun createRunner() = GradleRunner.create() - .withProjectDir(dir.toFile()) - .forwardStdOutput(output) - .forwardStdError(output).apply { - if (gradleVersion.version != null) { - withGradleVersion(gradleVersion.version) - } - } - .withArguments() + private fun createRunner() = + GradleRunner + .create() + .withProjectDir(dir.toFile()) + .forwardStdOutput(output) + .forwardStdError(output) + .apply { + if (gradleVersion.version != null) { + withGradleVersion(gradleVersion.version) + } + }.withArguments() fun logOutputOnce() { if (!outputLogged.getAndSet(true)) { diff --git a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestProjectExtension.kt b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestProjectExtension.kt index fa7ffc6..dcdac03 100644 --- a/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestProjectExtension.kt +++ b/junit5/src/main/kotlin/com/toasttab/gradle/testkit/TestProjectExtension.kt @@ -26,7 +26,7 @@ import org.junit.jupiter.api.extension.ParameterResolver import org.junit.jupiter.params.provider.Arguments import org.junit.jupiter.params.provider.ArgumentsProvider import java.nio.file.Path -import java.util.* +import java.util.Optional import java.util.concurrent.ConcurrentHashMap import java.util.stream.Stream import kotlin.io.path.ExperimentalPathApi @@ -51,7 +51,10 @@ data class ProjectKey( class TestProjects : CloseableResource { private val projects = ConcurrentHashMap() - fun project(key: ProjectKey, create: (ProjectKey) -> TestProject) = projects.computeIfAbsent(key, create) + fun project( + key: ProjectKey, + create: (ProjectKey) -> TestProject + ) = projects.computeIfAbsent(key, create) override fun close() { projects.values.forEach(TestProject::close) @@ -62,7 +65,12 @@ class TestProjects : CloseableResource { } } -class TestProjectExtension : ParameterResolver, BeforeAllCallback, AfterTestExecutionCallback, InvocationInterceptor, ArgumentsProvider { +class TestProjectExtension : + ParameterResolver, + BeforeAllCallback, + AfterTestExecutionCallback, + InvocationInterceptor, + ArgumentsProvider { override fun beforeAll(context: ExtensionContext) { val coverage = CoverageSettings.settings @@ -71,12 +79,17 @@ class TestProjectExtension : ParameterResolver, BeforeAllCallback, AfterTestExec } } - override fun supportsParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext) = - parameterContext.parameter.type == TestProject::class.java && - extensionContext.parent.map { it::class.java.name } != Optional.of("org.junit.jupiter.engine.descriptor.TestTemplateExtensionContext") + override fun supportsParameter( + parameterContext: ParameterContext, + extensionContext: ExtensionContext + ) = parameterContext.parameter.type == TestProject::class.java && + extensionContext.parent.map { it::class.java.name } != + Optional.of("org.junit.jupiter.engine.descriptor.TestTemplateExtensionContext") - override fun resolveParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext) = - extensionContext.project(GradleVersionArgument.DEFAULT) + override fun resolveParameter( + parameterContext: ParameterContext, + extensionContext: ExtensionContext + ) = extensionContext.project(GradleVersionArgument.DEFAULT) override fun afterTestExecution(context: ExtensionContext) { context.executionException.ifPresent { @@ -90,98 +103,114 @@ class TestProjectExtension : ParameterResolver, BeforeAllCallback, AfterTestExec val methodAnn = context.requiredTestMethod.getAnnotation(ParameterizedWithGradleVersions::class.java) val classAnn = context.requiredTestClass.getAnnotation(TestKit::class.java) - val specs: List = when { - methodAnn.versions.isNotEmpty() -> methodAnn.versions.map(GradleVersionArgument::of) - methodAnn.value.isNotEmpty() -> methodAnn.value.map { GradleVersionArgument.of(it) } - classAnn.versions.isNotEmpty() -> classAnn.versions.map(GradleVersionArgument::of) - else -> classAnn.gradleVersions.map { GradleVersionArgument.of(it) } - } + val specs: List = + when { + methodAnn.versions.isNotEmpty() -> methodAnn.versions.map(GradleVersionArgument::of) + methodAnn.value.isNotEmpty() -> methodAnn.value.map { GradleVersionArgument.of(it) } + classAnn.versions.isNotEmpty() -> classAnn.versions.map(GradleVersionArgument::of) + else -> classAnn.gradleVersions.map { GradleVersionArgument.of(it) } + } return specs.map { Arguments.of(context.project(it)) }.stream() } companion object { - private inline fun ExtensionContext.get(namespace: ExtensionContext.Namespace, key: String) = - getStore(namespace).get(key, T::class.java) + private inline fun ExtensionContext.get( + namespace: ExtensionContext.Namespace, + key: String + ) = getStore(namespace).get(key, T::class.java) - private inline fun ExtensionContext.Store.cache(key: K, noinline f: (K) -> V) = - getOrComputeIfAbsent(key, f, V::class.java) + private inline fun ExtensionContext.Store.cache( + key: K, + noinline f: (K) -> V + ) = getOrComputeIfAbsent(key, f, V::class.java) @OptIn(ExperimentalPathApi::class) private fun ExtensionContext.project(gradleVersion: GradleVersionArgument): TestProject { val parameters = requiredTestClass.getAnnotation(TestKit::class.java) ?: TestKit() - val locator = getStore(NAMESPACE).cache(LOCATOR) { - parameters.locator.java.getDeclaredConstructor().newInstance() as ProjectLocator - } + val locator = + getStore(NAMESPACE).cache(LOCATOR) { + parameters.locator.java + .getDeclaredConstructor() + .newInstance() as ProjectLocator + } - return getStore(NAMESPACE).cache(PROJECTS) { - TestProjects() - }.project(ProjectKey(gradleVersion)) { - val tempProjectDir = createTempDirectory("junit-gradlekit") + return getStore(NAMESPACE) + .cache(PROJECTS) { + TestProjects() + }.project(ProjectKey(gradleVersion)) { + val tempProjectDir = createTempDirectory("junit-gradlekit") - val location = locator.projectPath(System.getProperty("testkit-projects"), this) + val location = locator.projectPath(System.getProperty("testkit-projects"), this) - if (!location.exists()) { - error { "expected a test project in $location" } - } + if (!location.exists()) { + error { "expected a test project in $location" } + } - location.copyToRecursively(target = tempProjectDir, followLinks = false, overwrite = false) + location.copyToRecursively(target = tempProjectDir, followLinks = false, overwrite = false) - createProject( - tempProjectDir, - gradleVersion, - parameters.cleanup, - CoverageSettings.settings?.let { get(NAMESPACE, COVERAGE_RECORDER) } - ) - } + createProject( + tempProjectDir, + gradleVersion, + parameters.cleanup, + CoverageSettings.settings?.let { get(NAMESPACE, COVERAGE_RECORDER) } + ) + } } - fun createProject(projectDir: Path, gradleVersion: GradleVersionArgument, cleanup: Boolean = true, coverageRecorder: CoverageRecorder?): TestProject { + fun createProject( + projectDir: Path, + gradleVersion: GradleVersionArgument, + cleanup: Boolean = true, + coverageRecorder: CoverageRecorder? + ): TestProject { val integrationRepo = System.getProperty("testkit-integration-repo") val projectVersion = System.getProperty("testkit-project-version") val pluginVersion = System.getProperty("testkit-plugin-version") - val plugins = System.getProperty("testkit-plugin-ids")?.split(',')?.joinToString(separator = "\n") { - """id("$it") version("$projectVersion")""" - } ?: "" - - val initArgs = if (integrationRepo != null) { - projectDir.appendToFile( - "init.gradle.kts", - """ - - settingsEvaluated { - pluginManagement { - repositories { - maven(url = "file://$integrationRepo") - gradlePluginPortal() - } - - plugins { - id("com.toasttab.testkit.coverage") version("$pluginVersion") - $plugins + val plugins = + System.getProperty("testkit-plugin-ids")?.split(',')?.joinToString(separator = "\n") { + """id("$it") version("$projectVersion")""" + } ?: "" + + val initArgs = + if (integrationRepo != null) { + projectDir.appendToFile( + "init.gradle.kts", + """ + + settingsEvaluated { + pluginManagement { + repositories { + maven(url = "file://$integrationRepo") + gradlePluginPortal() + } + + plugins { + id("com.toasttab.testkit.coverage") version("$pluginVersion") + $plugins + } } } - } - """.trimIndent() - ) + """.trimIndent() + ) - listOf("--init-script", "init.gradle.kts") - } else { - emptyList() - } + listOf("--init-script", "init.gradle.kts") + } else { + emptyList() + } if (coverageRecorder != null) { projectDir.appendToFile( "gradle.properties", """ - - # custom jacoco properties - systemProp.jacoco-agent.output=tcpclient - systemProp.jacoco-agent.port=${coverageRecorder.port} - systemProp.jacoco-agent.sessionid=test - systemProp.jacoco-agent.includes=${coverageRecorder.settings.includes} - systemProp.jacoco-agent.excludes=${coverageRecorder.settings.excludes} + + # custom jacoco properties + systemProp.jacoco-agent.output=tcpclient + systemProp.jacoco-agent.port=${coverageRecorder.port} + systemProp.jacoco-agent.sessionid=test + systemProp.jacoco-agent.includes=${coverageRecorder.settings.includes} + systemProp.jacoco-agent.excludes=${coverageRecorder.settings.excludes} """.trimIndent() ) } @@ -191,7 +220,10 @@ class TestProjectExtension : ParameterResolver, BeforeAllCallback, AfterTestExec } } -private fun Path.appendToFile(fileName: String, text: String) { +private fun Path.appendToFile( + fileName: String, + text: String +) { val file = resolve(fileName) if (!file.exists()) { diff --git a/junit5/src/test/kotlin/com/toasttab/gradle/testkit/GradleVersionPropertiesIntegrationTest.kt b/junit5/src/test/kotlin/com/toasttab/gradle/testkit/GradleVersionPropertiesIntegrationTest.kt index b5270f4..b69b218 100644 --- a/junit5/src/test/kotlin/com/toasttab/gradle/testkit/GradleVersionPropertiesIntegrationTest.kt +++ b/junit5/src/test/kotlin/com/toasttab/gradle/testkit/GradleVersionPropertiesIntegrationTest.kt @@ -33,11 +33,12 @@ import strikt.assertions.isEqualTo class GradleVersionPropertiesIntegrationTest { @ParameterizedWithGradleVersions fun `per-version properties are attached to the test project`(project: TestProject) { - val expectedKotlin = when (project.gradleVersion.version) { - "8.6" -> "1.9.24" - "8.7" -> "2.0.0" - else -> error("unexpected gradle version ${project.gradleVersion.version}") - } + val expectedKotlin = + when (project.gradleVersion.version) { + "8.6" -> "1.9.24" + "8.7" -> "2.0.0" + else -> error("unexpected gradle version ${project.gradleVersion.version}") + } expectThat(project.property("kotlin")).isEqualTo(expectedKotlin) } diff --git a/testkit-plugin/src/main/kotlin/com/toasttab/gradle/testkit/TestkitExtension.kt b/testkit-plugin/src/main/kotlin/com/toasttab/gradle/testkit/TestkitExtension.kt index 38bb1ab..3f21da2 100644 --- a/testkit-plugin/src/main/kotlin/com/toasttab/gradle/testkit/TestkitExtension.kt +++ b/testkit-plugin/src/main/kotlin/com/toasttab/gradle/testkit/TestkitExtension.kt @@ -22,7 +22,10 @@ abstract class TestkitExtension { abstract val testProjectsDir: Property abstract val replaceTokens: MapProperty - fun replaceToken(name: String, value: String) { + fun replaceToken( + name: String, + value: String + ) { replaceTokens.put(name, value) } } diff --git a/testkit-plugin/src/main/kotlin/com/toasttab/gradle/testkit/TestkitPlugin.kt b/testkit-plugin/src/main/kotlin/com/toasttab/gradle/testkit/TestkitPlugin.kt index c1c5162..8c80630 100644 --- a/testkit-plugin/src/main/kotlin/com/toasttab/gradle/testkit/TestkitPlugin.kt +++ b/testkit-plugin/src/main/kotlin/com/toasttab/gradle/testkit/TestkitPlugin.kt @@ -32,69 +32,74 @@ import org.gradle.kotlin.dsl.register import org.gradle.testing.jacoco.tasks.JacocoReportBase import javax.inject.Inject -class TestkitPlugin @Inject constructor( - private val fs: FileSystemOperations -) : Plugin { - override fun apply(project: Project) { - val extension = project.extensions.create("testkitTests") - val destfile = project.layout.buildDirectory.file("jacoco/testkit.exec") - val testProjectDir = project.layout.buildDirectory.dir("test-projects") +class TestkitPlugin + @Inject + constructor( + private val fs: FileSystemOperations + ) : Plugin { + override fun apply(project: Project) { + val extension = project.extensions.create("testkitTests") + val destfile = project.layout.buildDirectory.file("jacoco/testkit.exec") + val testProjectDir = project.layout.buildDirectory.dir("test-projects") - project.tasks.register("copyTestProjects") { - from(extension.testProjectsDir.getOrElse("src/test/projects")) - into(testProjectDir) + project.tasks.register("copyTestProjects") { + from(extension.testProjectsDir.getOrElse("src/test/projects")) + into(testProjectDir) - val tokens = mapOf( - "TESTKIT_PLUGIN_VERSION" to BuildConfig.VERSION, - "TESTKIT_INTEGRATION_REPO" to project.integrationDirectory().path, - "VERSION" to "${project.version}" - ) + extension.replaceTokens.get() + val tokens = + mapOf( + "TESTKIT_PLUGIN_VERSION" to BuildConfig.VERSION, + "TESTKIT_INTEGRATION_REPO" to project.integrationDirectory().path, + "VERSION" to "${project.version}" + ) + extension.replaceTokens.get() - // default up-to-date checks ignore the tokens - inputs.property("tokens", tokens) + // default up-to-date checks ignore the tokens + inputs.property("tokens", tokens) - filter( - mapOf("tokens" to tokens) - ) - } + filter( + mapOf("tokens" to tokens) + ) + } - project.tasks.named("test") { - dependsOn("copyTestProjects") + project.tasks.named("test") { + dependsOn("copyTestProjects") - doFirst(JacocoOutputCleanupTestTaskAction(fs, destfile)) + doFirst(JacocoOutputCleanupTestTaskAction(fs, destfile)) - inputs.dir(testProjectDir).withPropertyName("testkit-projects-input") - .withPathSensitivity(PathSensitivity.RELATIVE) + inputs + .dir(testProjectDir) + .withPropertyName("testkit-projects-input") + .withPathSensitivity(PathSensitivity.RELATIVE) - // declare an additional jacoco output file so that the JUnit JVM and the TestKit JVM - // do not try to write to the same file - outputs.file(destfile).withPropertyName("testkit-coverage-output") + // declare an additional jacoco output file so that the JUnit JVM and the TestKit JVM + // do not try to write to the same file + outputs.file(destfile).withPropertyName("testkit-coverage-output") - systemProperty("testkit-coverage-output", "${destfile.get()}") - systemProperty("testkit-projects", "${testProjectDir.get()}") - systemProperty("testkit-integration-repo", project.integrationDirectory().path) - systemProperty("testkit-plugin-version", BuildConfig.VERSION) - } + systemProperty("testkit-coverage-output", "${destfile.get()}") + systemProperty("testkit-projects", "${testProjectDir.get()}") + systemProperty("testkit-integration-repo", project.integrationDirectory().path) + systemProperty("testkit-plugin-version", BuildConfig.VERSION) + } - project.configureIntegrationPublishing() + project.configureIntegrationPublishing() - project.pluginManager.withPlugin("jacoco") { - project.pluginManager.withPlugin("jvm-test-suite") { - // add the TestKit jacoco file to outgoing artifacts so that it can be aggregated - project.configurations.getAt("coverageDataElementsForTest").outgoing.artifact(destfile) { - type = ArtifactTypeDefinition.BINARY_DATA_TYPE - builtBy("test") + project.pluginManager.withPlugin("jacoco") { + project.pluginManager.withPlugin("jvm-test-suite") { + // add the TestKit jacoco file to outgoing artifacts so that it can be aggregated + project.configurations.getAt("coverageDataElementsForTest").outgoing.artifact(destfile) { + type = ArtifactTypeDefinition.BINARY_DATA_TYPE + builtBy("test") + } } - } - project.tasks.named("jacocoTestReport") { - // add the TestKit jacoco file to the local jacoco report - executionData.from( - project.layout.buildDirectory.dir("jacoco").map { - it.files("test.exec", "testkit.exec") - } - ) + project.tasks.named("jacocoTestReport") { + // add the TestKit jacoco file to the local jacoco report + executionData.from( + project.layout.buildDirectory.dir("jacoco").map { + it.files("test.exec", "testkit.exec") + } + ) + } } } } -} diff --git a/testkit-plugin/src/test/kotlin/com/toasttab/gradle/testkit/TestkitPluginIntegrationTest.kt b/testkit-plugin/src/test/kotlin/com/toasttab/gradle/testkit/TestkitPluginIntegrationTest.kt index 382959f..875b8f0 100644 --- a/testkit-plugin/src/test/kotlin/com/toasttab/gradle/testkit/TestkitPluginIntegrationTest.kt +++ b/testkit-plugin/src/test/kotlin/com/toasttab/gradle/testkit/TestkitPluginIntegrationTest.kt @@ -36,7 +36,8 @@ class TestkitPluginIntegrationTest { val projectDir = dir.resolve("TestkitPluginIntegrationTest/filtering") - GradleRunner.create() + GradleRunner + .create() .withProjectDir(projectDir.toFile()) .withPluginClasspath() .withArguments("test")