### Gradle Probe Dependency Installation (Kotlin) Source: https://github.com/gw-kit/gradle-probe/blob/main/README.md Adds the gradle-probe library and Gradle TestKit as test dependencies to your project's build.gradle.kts file. ```kotlin dependencies { testImplementation("io.github.gwkit.gradleprobe:gradle-probe:0.0.1") testImplementation(gradleTestKit()) } ``` -------------------------------- ### Gradle Plugin Test Class Setup (Kotlin) Source: https://github.com/gw-kit/gradle-probe/blob/main/README.md Demonstrates setting up a test class for Gradle plugin testing using the @GradlePluginTest annotation. It injects the project directory and GradleRunner instance. ```kotlin @GradlePluginTest("my-test-project") class MyPluginTest { @RootProjectDir lateinit var projectDir: File @GradleRunnerInstance lateinit var gradleRunner: GradleRunner @Test fun `plugin applies successfully`() { gradleRunner.runTask("tasks") .assertOutputContainsStrings("BUILD SUCCESSFUL") } } ``` -------------------------------- ### Resource Utilities for File Operations (Kotlin) Source: https://github.com/gw-kit/gradle-probe/blob/main/README.md Provides utility functions for copying directories from resources, getting resource files, and converting file paths to Unix-style absolute paths. ```kotlin // Copy directory from resources val targetDir = tempDir.copyDirFromResources("test-project") // Get file from resources val resourceFile = getResourceFile("test-project/build.gradle.kts") // Convert to Unix path val unixPath = file.toUnixAbsolutePath() ``` -------------------------------- ### Functional Testing with Gradle Probe Source: https://context7.com/gw-kit/gradle-probe/llms.txt A comprehensive example of a functional test class using Gradle Probe annotations. It demonstrates task execution, file system verification, configuration modification, and cacheability checks within a JUnit 5 environment. ```kotlin import io.github.gwkit.gradleprobe.junit.GradlePluginTest import io.github.gwkit.gradleprobe.junit.GradleRunnerInstance import io.github.gwkit.gradleprobe.junit.ProjectFile import io.github.gwkit.gradleprobe.junit.RootProjectDir import io.github.gwkit.gradleprobe.RestorableFile import io.github.gwkit.gradleprobe.gradlerunner.runTask import io.github.gwkit.gradleprobe.gradlerunner.runTaskAndFail import io.github.gwkit.gradleprobe.assertion.assertOutputContainsStrings import org.gradle.testkit.runner.GradleRunner import org.gradle.testkit.runner.TaskOutcome import org.junit.jupiter.api.Test import java.io.File @GradlePluginTest(resourceProjectDir = "code-generator-plugin-test", kts = true) class CodeGeneratorPluginFunctionalTest { @RootProjectDir lateinit var projectDir: File @GradleRunnerInstance lateinit var gradleRunner: GradleRunner @ProjectFile("build.gradle.kts") lateinit var buildFile: RestorableFile @ProjectFile("src/main/resources/schema.json") lateinit var schemaFile: File @Test fun `plugin registers generateCode task`() { gradleRunner.runTask("tasks", "--all") .assertOutputContainsStrings( "generateCode", "Code generation tasks" ) } @Test fun `generateCode produces output files`() { gradleRunner.runTask("generateCode") .assertOutputContainsStrings("BUILD SUCCESSFUL") val generatedDir = projectDir.resolve("build/generated/sources") assert(generatedDir.exists()) { "Generated directory should exist" } val generatedFiles = generatedDir.walkTopDown() .filter { it.extension == "java" } .toList() assert(generatedFiles.isNotEmpty()) { "Should generate Java files" } val modelFile = generatedDir.resolve("com/example/Model.java") assert(modelFile.exists()) assert(modelFile.readText().contains("public class Model")) } @Test fun `plugin integrates with compileJava`() { val result = gradleRunner.runTask("compileJava") result.assertOutputContainsStrings("BUILD SUCCESSFUL") val generateTask = result.task(":generateCode") assert(generateTask?.outcome == TaskOutcome.SUCCESS) } @Test fun `plugin fails with invalid schema`() { schemaFile.writeText("{ invalid json }") gradleRunner.runTaskAndFail("generateCode") .assertOutputContainsStrings( "BUILD FAILED", "Invalid schema format" ) } @Test fun `plugin respects custom output directory configuration`() { buildFile.file.appendText(""" codeGenerator { outputDir = file("src/generated") } """.trimIndent()) gradleRunner.runTask("generateCode") .assertOutputContainsStrings("BUILD SUCCESSFUL") val customOutputDir = projectDir.resolve("src/generated") assert(customOutputDir.exists()) buildFile.restoreOriginContent() } @Test fun `plugin is cacheable and up-to-date checks work`() { val firstRun = gradleRunner.runTask("generateCode") assert(firstRun.task(":generateCode")?.outcome == TaskOutcome.SUCCESS) val secondRun = gradleRunner.runTask("generateCode") assert(secondRun.task(":generateCode")?.outcome == TaskOutcome.UP_TO_DATE) } } ``` -------------------------------- ### GradleRunner Extensions for Task Execution (Kotlin) Source: https://github.com/gw-kit/gradle-probe/blob/main/README.md Illustrates using GradleRunner extensions to run tasks, including running tasks that are expected to fail. ```kotlin // Run tasks (adds -si flags automatically) gradleRunner.runTask("build", "test") // Run tasks expecting failure gradleRunner.runTaskAndFail("build") ``` -------------------------------- ### GradlePluginTest Annotation Configuration (Kotlin) Source: https://github.com/gw-kit/gradle-probe/blob/main/README.md Shows how to configure the @GradlePluginTest annotation with resourceProjectDir and kts parameters to specify the test project location and DSL type. ```kotlin @GradlePluginTest( resourceProjectDir = "my-test-project", // Path in src/test/resources kts = true // true: keep .kts files, false: keep .gradle files ) class MyTest { ... } ``` -------------------------------- ### Inject Project Files with @ProjectFile Source: https://context7.com/gw-kit/gradle-probe/llms.txt Shows how to use the @ProjectFile annotation to resolve and inject project resources. Supports injection as File objects, Unix-style path strings, or RestorableFile objects for safe test modifications. ```kotlin import io.github.gwkit.gradleprobe.junit.GradlePluginTest import io.github.gwkit.gradleprobe.junit.ProjectFile import io.github.gwkit.gradleprobe.RestorableFile import org.junit.jupiter.api.Test import java.io.File @GradlePluginTest("test-project") class ProjectFileTest { @ProjectFile("build.gradle.kts") lateinit var buildFile: File @ProjectFile("settings.gradle.kts") lateinit var settingsPath: String @ProjectFile("gradle.properties") lateinit var gradleProperties: RestorableFile @ProjectFile("src/main/java/Sample.java") lateinit var sampleJava: File @Test fun `access build file as File`() { assert(buildFile.exists()) val content = buildFile.readText() assert(content.contains("plugins")) } @Test fun `access file path as String`() { assert(settingsPath.contains("/")) assert(settingsPath.endsWith("settings.gradle.kts")) println("Settings file path: $settingsPath") } @Test fun `access nested project files`() { assert(sampleJava.exists()) val javaContent = sampleJava.readText() assert(javaContent.contains("class Sample")) } } ``` -------------------------------- ### Verify Gradle build output using assertOutputContainsStrings Source: https://context7.com/gw-kit/gradle-probe/llms.txt Demonstrates how to use the assertOutputContainsStrings extension function to verify that specific strings appear in the Gradle build output. It utilizes soft assertions, allowing multiple validation failures to be reported simultaneously. ```kotlin import io.github.gwkit.gradleprobe.junit.GradlePluginTest import io.github.gwkit.gradleprobe.junit.GradleRunnerInstance import io.github.gwkit.gradleprobe.gradlerunner.runTask import io.github.gwkit.gradleprobe.assertion.assertOutputContainsStrings import org.gradle.testkit.runner.GradleRunner import org.junit.jupiter.api.Test @GradlePluginTest("my-plugin-project") class BuildResultAssertionsTest { @GradleRunnerInstance lateinit var gradleRunner: GradleRunner @Test fun `assert single string in output`() { gradleRunner.runTask("build") .assertOutputContainsStrings("BUILD SUCCESSFUL") } @Test fun `assert multiple strings in output`() { // All strings must be present - uses soft assertions gradleRunner.runTask("build") .assertOutputContainsStrings( "BUILD SUCCESSFUL", "compileJava", "processResources", "classes" ) } @Test fun `chain assertions with other checks`() { val result = gradleRunner.runTask("tasks", "--all") .assertOutputContainsStrings( "Build tasks", "Help tasks" ) // Can continue using result after assertion assert(result.output.lines().size > 10) } @Test fun `verify plugin output messages`() { gradleRunner.runTask("myPluginTask") .assertOutputContainsStrings( "Starting plugin task", "Processing files", "Plugin task completed", "BUILD SUCCESSFUL" ) } } ``` -------------------------------- ### Inject GradleRunner with @GradleRunnerInstance Source: https://context7.com/gw-kit/gradle-probe/llms.txt Demonstrates how to use the @GradleRunnerInstance annotation to inject a pre-configured GradleRunner into a test class. It supports executing tasks, verifying build output, and asserting build failures using provided helper methods. ```kotlin import io.github.gwkit.gradleprobe.junit.GradlePluginTest import io.github.gwkit.gradleprobe.junit.GradleRunnerInstance import io.github.gwkit.gradleprobe.gradlerunner.runTask import io.github.gwkit.gradleprobe.gradlerunner.runTaskAndFail import io.github.gwkit.gradleprobe.assertion.assertOutputContainsStrings import org.gradle.testkit.runner.GradleRunner import org.junit.jupiter.api.Test @GradlePluginTest("my-plugin-project") class GradleRunnerTest { @GradleRunnerInstance lateinit var gradleRunner: GradleRunner @Test fun `plugin applies and tasks are available`() { gradleRunner.runTask("tasks", "--all") .assertOutputContainsStrings( "BUILD SUCCESSFUL", "myPluginTask" ) } @Test fun `build completes successfully`() { val result = gradleRunner.runTask("build") println(result.output) result.assertOutputContainsStrings("BUILD SUCCESSFUL") } @Test fun `invalid configuration fails build`() { gradleRunner.runTaskAndFail("validateConfig") .assertOutputContainsStrings( "BUILD FAILED", "Configuration error" ) } } ``` -------------------------------- ### Implement Functional Tests for Gradle Plugins in Kotlin Source: https://github.com/gw-kit/gradle-probe/blob/main/README.md A functional test class demonstrating how to use Gradle Probe annotations to manage project directories, Gradle runners, and build files. It covers verifying task registration, handling configuration failures, and asserting generated file content. ```kotlin @GradlePluginTest("sample-project", kts = true) class MyGradlePluginFunctionalTest { @RootProjectDir lateinit var projectDir: File @GradleRunnerInstance lateinit var gradleRunner: GradleRunner @ProjectFile("build.gradle.kts") lateinit var buildFile: RestorableFile @ProjectFile("settings.gradle.kts") lateinit var settingsFile: File @Test fun `plugin registers expected tasks`() { gradleRunner.runTask("tasks", "--all") .assertOutputContainsStrings( "myCustomTask", "BUILD SUCCESSFUL" ) } @Test fun `plugin fails with invalid configuration`() { buildFile.file.appendText(""" myPlugin { invalidOption = true } """.trimIndent()) gradleRunner.runTaskAndFail("build") .assertOutputContainsStrings("Invalid configuration") buildFile.restoreOriginContent() } @Test fun `plugin generates expected output`() { gradleRunner.runTask("generateFiles") val generatedFile = projectDir.resolve("build/generated/output.txt") assertThat(generatedFile).exists() assertThat(generatedFile.readText()).contains("Expected content") } } ``` -------------------------------- ### GradleRunner BuildResult Assertions (Kotlin) Source: https://github.com/gw-kit/gradle-probe/blob/main/README.md Demonstrates asserting the output of Gradle tasks using the assertOutputContainsStrings method on the BuildResult object. ```kotlin gradleRunner.runTask("build") .assertOutputContainsStrings( "BUILD SUCCESSFUL", "Task :compileKotlin" ) ``` -------------------------------- ### RestorableFile for Modifying and Restoring Files (Kotlin) Source: https://github.com/gw-kit/gradle-probe/blob/main/README.md Shows how to use RestorableFile to modify a file within a test and then restore its original content using restoreOriginContent(). ```kotlin @GradlePluginTest("my-test-project") class ModifyBuildFileTest { @ProjectFile("build.gradle.kts") lateinit var buildFile: RestorableFile @Test fun `test with modified build file`() { // Modify the file buildFile.file.appendText(""" tasks.register("customTask") { doLast { println("Custom!") } } """.trimIndent()) // Run test... // Restore original content buildFile.restoreOriginContent() } } ``` -------------------------------- ### GradlePluginTest Annotation for Kotlin DSL Tests Source: https://context7.com/gw-kit/gradle-probe/llms.txt Demonstrates the use of the @GradlePluginTest annotation for testing Gradle plugins with Kotlin DSL. It configures the test environment to use `.gradle.kts` build files and removes `.gradle` files. The @RootProjectDir annotation injects the project directory. ```kotlin import io.github.gwkit.gradleprobe.junit.GradlePluginTest import io.github.gwkit.gradleprobe.junit.RootProjectDir import org.junit.jupiter.api.Test import java.io.File // Use Kotlin DSL (kts = true, default) - deletes .gradle files, keeps .gradle.kts @GradlePluginTest(resourceProjectDir = "my-plugin-test-project", kts = true) class MyPluginKotlinDslTest { @RootProjectDir lateinit var projectDir: File @Test fun `project is set up correctly`() { assert(projectDir.exists()) assert(projectDir.resolve("build.gradle.kts").exists()) assert(!projectDir.resolve("build.gradle").exists()) // Groovy file removed } } ``` -------------------------------- ### GradlePluginTest Annotation for Groovy DSL Tests Source: https://context7.com/gw-kit/gradle-probe/llms.txt Illustrates using the @GradlePluginTest annotation for testing Gradle plugins with Groovy DSL. This configuration prioritizes `.gradle` files and removes `.gradle.kts` files. The @RootProjectDir annotation injects the project directory. ```kotlin import io.github.gwkit.gradleprobe.junit.GradlePluginTest import io.github.gwkit.gradleprobe.junit.RootProjectDir import org.junit.jupiter.api.Test import java.io.File // Use Groovy DSL (kts = false) - deletes .gradle.kts files, keeps .gradle @GradlePluginTest(resourceProjectDir = "my-plugin-test-project", kts = false) class MyPluginGroovyDslTest { @RootProjectDir lateinit var projectDir: File @Test fun `project uses groovy dsl`() { assert(projectDir.resolve("build.gradle").exists()) assert(!projectDir.resolve("build.gradle.kts").exists()) // Kotlin file removed } } ``` -------------------------------- ### RootProjectDir Annotation for Project Directory Injection Source: https://context7.com/gw-kit/gradle-probe/llms.txt Shows how to use the @RootProjectDir annotation to inject the root project directory as a `File` object into a test class property. This allows direct access and manipulation of the test project's file structure within JUnit tests. ```kotlin import io.github.gwkit.gradleprobe.junit.GradlePluginTest import io.github.gwkit.gradleprobe.junit.RootProjectDir import org.junit.jupiter.api.Test import java.io.File @GradlePluginTest("test-project") class ProjectDirectoryTest { @RootProjectDir lateinit var rootProjectDir: File @Test fun `can access project structure`() { // Access the temporary project directory println("Project root: ${rootProjectDir.absolutePath}") // Navigate project structure val srcDir = rootProjectDir.resolve("src/main/java") val buildDir = rootProjectDir.resolve("build") // Verify expected files exist assert(rootProjectDir.resolve("settings.gradle.kts").exists()) // List all files in project rootProjectDir.walkTopDown().forEach { file -> println(file.relativeTo(rootProjectDir)) } } } ``` -------------------------------- ### Add Gradle Probe Dependency to build.gradle.kts Source: https://context7.com/gw-kit/gradle-probe/llms.txt This snippet shows how to add the Gradle Probe library and Gradle TestKit as test dependencies in a Kotlin DSL build script. Ensure you have the correct version of Gradle Probe. ```kotlin // build.gradle.kts dependencies { testImplementation("io.github.gwkit.gradleprobe:gradle-probe:0.0.2") testImplementation(gradleTestKit()) } ``` -------------------------------- ### GradleRunner Extensions: Simplify Task Execution in Tests Source: https://context7.com/gw-kit/gradle-probe/llms.txt These extension functions for GradleRunner simplify the process of executing Gradle tasks within tests. They automatically include essential flags like `-si` for stack traces and info-level logging, reducing boilerplate code. The functions `runTask` and `runTaskAndFail` are key utilities for interacting with the Gradle build process during testing. ```kotlin import io.github.gwkit.gradleprobe.junit.GradlePluginTest import io.github.gwkit.gradleprobe.junit.GradleRunnerInstance import io.github.gwkit.gradleprobe.gradlerunner.runTask import io.github.gwkit.gradleprobe.gradlerunner.runTaskAndFail import org.gradle.testkit.runner.GradleRunner import org.gradle.testkit.runner.BuildResult import org.junit.jupiter.api.Test @GradlePluginTest("sample-project") class RunTaskExtensionsTest { @GradleRunnerInstance lateinit var gradleRunner: GradleRunner @Test fun `runTask executes single task`() { // Run a single task val result: BuildResult = gradleRunner.runTask("help") assert(result.output.contains("BUILD SUCCESSFUL")) } @Test fun `runTask executes multiple tasks`() { // Run multiple tasks in sequence val result = gradleRunner.runTask("clean", "build", "test") // All tasks should complete assert(result.output.contains("BUILD SUCCESSFUL")) } @Test fun `runTask with additional arguments`() { // Tasks with Gradle arguments val result = gradleRunner.runTask("tasks", "--all", "--group=build") assert(result.output.contains("Build tasks")) } @Test fun `runTaskAndFail expects build failure`() { // Use when the build is expected to fail val result = gradleRunner.runTaskAndFail("nonExistentTask") assert(result.output.contains("BUILD FAILED")) } @Test fun `access task outcomes from result`() { val result = gradleRunner.runTask("compileJava") // Check specific task outcome val compileTask = result.task(":compileJava") println("compileJava outcome: ${compileTask?.outcome}") } } ``` -------------------------------- ### RestorableFile: Modify and Restore Files in Gradle Tests Source: https://context7.com/gw-kit/gradle-probe/llms.txt The RestorableFile class provides a mechanism to temporarily modify files within Gradle tests and reliably restore them to their original state afterward. This is crucial for maintaining test isolation and preventing side effects between test cases. It requires no external dependencies beyond standard JUnit and Gradle test kit. ```kotlin import io.github.gwkit.gradleprobe.junit.GradlePluginTest import io.github.gwkit.gradleprobe.junit.GradleRunnerInstance import io.github.gwkit.gradleprobe.junit.ProjectFile import io.github.gwkit.gradleprobe.RestorableFile import io.github.gwkit.gradleprobe.gradlerunner.runTask import io.github.gwkit.gradleprobe.gradlerunner.runTaskAndFail import io.github.gwkit.gradleprobe.assertion.assertOutputContainsStrings import org.gradle.testkit.runner.GradleRunner import org.junit.jupiter.api.Test @GradlePluginTest("my-plugin-project") class RestorableFileTest { @GradleRunnerInstance lateinit var gradleRunner: GradleRunner @ProjectFile("build.gradle.kts") lateinit var buildFile: RestorableFile @Test fun `test with dynamically added task`() { // Modify the build file - add a custom task buildFile.file.appendText( """ tasks.register("customTask") { doLast { println("Custom task executed!") } } """.trimIndent() ) // Run the dynamically added task gradleRunner.runTask("customTask") .assertOutputContainsStrings("Custom task executed!") // Restore original content for test isolation buildFile.restoreOriginContent() } @Test fun `test with modified plugin configuration`() { val originalContent = buildFile.file.readText() // Add invalid configuration to test error handling buildFile.file.appendText( """ myPlugin { invalidOption = "bad value" } """.trimIndent() ) // Verify plugin reports configuration error gradleRunner.runTaskAndFail("build") .assertOutputContainsStrings("Invalid configuration") // Restore for next test buildFile.restoreOriginContent() // Verify restoration worked assert(buildFile.file.readText() == originalContent) } @Test fun `test multiple modifications with restore`() { // First modification buildFile.file.writeText("// First version\nplugins { }") // Second modification buildFile.file.writeText("// Second version\nplugins { id(\"java\") }") // Restore back to original (not first or second modification) buildFile.restoreOriginContent() // Original content is restored regardless of how many modifications were made assert(!buildFile.file.readText().startsWith("// First")) assert(!buildFile.file.readText().startsWith("// Second")) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.