Created
November 16, 2023 18:43
-
-
Save autonomousapps/75938c892bceb446b6191e2c862414c2 to your computer and use it in GitHub Desktop.
Example usage of gradle-testkit-support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* An example usage of https://github.com/autonomousapps/dependency-analysis-gradle-plugin/tree/main/testkit. | |
* This uses JUnit5 as the test framework. The test is paramaterized as well. The custom task `printVersions`, | |
* in the test fixture, unzips the jar and prints the contents of a resource file in that jar. The resource file | |
* is generated by "my-plugin", which is an internal plugin that bundles the runtime classpath artifact names | |
* (group:artifact:version) into the final jar built by the application. | |
*/ | |
internal class PluginTest { | |
private companion object { | |
@JvmStatic fun gradleVersions() = listOf(GradleVersion.current(), GradleVersion.version("8.4")) | |
} | |
@ParameterizedTest(name = "{0}") | |
@MethodSource("gradleVersions") | |
fun `can record versions`(gradleVersion: GradleVersion) { | |
// Given | |
val project = MyProject().gradleProject | |
// When | |
val result = build(gradleVersion, project.rootDir, ":printVersions") | |
// Then | |
assertThat(result.output).contains( | |
""" | |
com.squareup.moshi:moshi:1.15.0 | |
com.squareup.okio:okio:2.10.0 | |
org.jetbrains.kotlin:kotlin-stdlib-common:1.8.21 | |
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.21 | |
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.21 | |
org.jetbrains.kotlin:kotlin-stdlib:1.8.21 | |
org.jetbrains:annotations:13.0 | |
""".trimIndent() | |
) | |
} | |
} | |
private class MyProject : AbstractProject( | |
rootProjectName = "version-print-test" | |
) { | |
val gradleProject: GradleProject = build() | |
private fun build(): GradleProject { | |
val theVersion = "1.0" | |
val jarName = "$rootProjectName-$theVersion.jar" | |
val taskOutputFilename = "runtime-dependencies.txt" | |
return newGradleProjectBuilder() | |
.withRootProject { | |
withBuildScript { | |
plugins(Plugin.application, Plugin("my-plugin", AbstractGradleProject.PLUGIN_UNDER_TEST_VERSION) | |
dependencies(implementation("com.squareup.moshi:moshi:1.15.0")) | |
group = "test" | |
version = theVersion | |
withGroovy( | |
""" | |
// This unzips the application jar we create and then prints the version list file to stdout. | |
// With this, we verify that our jar contains the expected resource file | |
tasks.register('printVersions', Exec) { | |
dependsOn 'jar' | |
commandLine 'unzip', '-p', 'build/libs/$jarName', '$taskOutputFilename' | |
} | |
""".trimIndent() | |
) | |
} | |
} | |
}.write() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment