Skip to content

Commit

Permalink
Migrate functional tests to Kotlin and Junit part 4 (#1131)
Browse files Browse the repository at this point in the history
* Intro Moshi

* Migrate XmlSlurper and JsonSlurper to MavenXpp3Reader and Moshi

* Convert PublishingSpec

* Migrate Moshi to Kotlinx Serialization

* Revert "Migrate Moshi to Kotlinx Serialization"

This reverts commit 321d8df.

* Remove unused things in BasePluginSpecification

* Corrects

* Add documentation reference to GradleModuleMetadata

* Add AppendableMavenRepository to replace AppendableMavenFileRepository

* Merge AppendableJar into JarBuilder

* Remove AppendableMavenRepository in PublishingTest

* Optimize performance by publishing common things in BeforeAll

* Publish A and B for all tests

* Publish C and D for FilteringTest

* Build jars in blocks
  • Loading branch information
Goooler authored Jan 7, 2025
1 parent c2100a8 commit 710c9fc
Show file tree
Hide file tree
Showing 25 changed files with 633 additions and 1,000 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ dependencies {
intiTestImplementation(libs.apache.maven.repositoryMetadata)
// TODO: this will be removed after we migrated all functional tests to Kotlin.
intiTestImplementation(sourceSets.main.get().output)
intiTestImplementation(libs.moshi)
intiTestImplementation(libs.moshi.kotlin)

lintChecks(libs.androidx.gradlePluginLints)
lintChecks(libs.assertk.lint)
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
maven = "3.9.9"
moshi = "1.15.2"

[libraries]
apache-ant = "org.apache.ant:ant:1.10.15"
Expand All @@ -15,6 +16,8 @@ plexus-utils = "org.codehaus.plexus:plexus-utils:4.0.2"
plexus-xml = "org.codehaus.plexus:plexus-xml:4.0.4"
xmlunit = "org.xmlunit:xmlunit-legacy:2.10.0"
okio = "com.squareup.okio:okio:3.9.1"
moshi = { module = "com.squareup.moshi:moshi", version.ref = "moshi" }
moshi-kotlin = { module = "com.squareup.moshi:moshi-kotlin", version.ref = "moshi" }

pluginPublish = "com.gradle.publish:plugin-publish-plugin:1.3.0"
mavenPublish = "com.vanniktech:gradle-maven-publish-plugin:0.30.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
package com.github.jengelman.gradle.plugins.shadow

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.github.jengelman.gradle.plugins.shadow.util.AppendableJar
import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository
import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenRepository
import org.apache.commons.lang3.StringUtils
import org.codehaus.plexus.util.IOUtil
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.TempDir

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.function.Function
import java.util.jar.JarEntry
import java.util.jar.JarFile

abstract class BasePluginSpecification extends Specification {

@TempDir
Path root

AppendableMavenFileRepository repo
@Shared
static AppendableMavenRepository repo

def setup() {
repo = repo()
repo.module('junit', 'junit', '3.8.2')
.use(Paths.get(this.class.classLoader.getResource('junit-3.8.2.jar').toURI()))
.publish()
def setupSpec() {
repo = new AppendableMavenRepository(
Files.createTempDirectory(null).resolve('local-maven-repo'),
runner,
)
repo.module('junit', 'junit', '3.8.2') { module ->
module.useJar(Paths.get(this.class.classLoader.getResource('junit-3.8.2.jar').toURI()))
}.publish()
}

def setup() {
projectScriptFile << getDefaultProjectBuildScript('java', true, true)
settingsScriptFile << getDefaultSettingsBuildScript()
}
Expand All @@ -37,6 +41,10 @@ abstract class BasePluginSpecification extends Specification {
println projectScriptFile.text
}

def cleanupSpec() {
// TODO: Delete repo recursively.
}

String getDefaultProjectBuildScript(
String javaPlugin = 'java',
boolean withGroup = false,
Expand All @@ -61,7 +69,7 @@ abstract class BasePluginSpecification extends Specification {
return """
dependencyResolutionManagement {
repositories {
maven { url = "${repo.uri}" }
maven { url = "${repo.root.toUri()}" }
mavenCentral()
}
}
Expand All @@ -73,42 +81,29 @@ abstract class BasePluginSpecification extends Specification {
static def shadowJar = "tasks.named('shadowJar', ${ShadowJar.class.name})".trim()

GradleRunner getRunner() {
GradleRunner.create()
.withProjectDir(root.toFile())
def runner = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withTestKitDir(testKitDir)
if (root != null) {
runner.withProjectDir(root.toFile())
}
return runner
}

GradleRunner runner(Collection<String> tasks) {
runner.withArguments(["--warning-mode=fail", "--configuration-cache", "--stacktrace"] + tasks.toList())
}

BuildResult run(String... tasks) {
run(tasks.toList())
}

BuildResult run(List<String> tasks, Function<GradleRunner, GradleRunner> runnerFunction = { it }) {
def result = runnerFunction.apply(runner(tasks)).build()
assertNoDeprecationWarnings(result)
return result
}

BuildResult runWithFailure(List<String> tasks, Function<GradleRunner, GradleRunner> runnerFunction = { it }) {
def result = runnerFunction.apply(runner(tasks)).buildAndFail()
assertNoDeprecationWarnings(result)
return result
}

private static void assertNoDeprecationWarnings(BuildResult result) {
result.output.eachLine {
assert !containsDeprecationWarning(it)
BuildResult run(List<String> tasks) {
def result = runner(tasks).build()
result.output.eachLine { output ->
assert !(
output.contains("has been deprecated and is scheduled to be removed in Gradle") ||
output.contains("has been deprecated. This is scheduled to be removed in Gradle")
)
}
}

private static boolean containsDeprecationWarning(String output) {
output.contains("has been deprecated and is scheduled to be removed in Gradle") ||
output.contains("has been deprecated. This is scheduled to be removed in Gradle")
return result
}

File getProjectScriptFile() {
Expand Down Expand Up @@ -137,20 +132,6 @@ abstract class BasePluginSpecification extends Specification {
return f
}

AppendableMavenFileRepository repo(String path = 'maven-repo') {
new AppendableMavenFileRepository(root.resolve(path))
}

String getJarFileContents(File f, String path) {
JarFile jf = new JarFile(f)
def is = jf.getInputStream(new JarEntry(path))
StringWriter sw = new StringWriter()
IOUtil.copy(is, sw)
is.close()
jf.close()
return sw.toString()
}

void assertContains(File f, List<String> paths) {
JarFile jar = new JarFile(f)
paths.each { path ->
Expand All @@ -167,32 +148,15 @@ abstract class BasePluginSpecification extends Specification {
jar.close()
}

/**
* Helper method to allow scoping variables into a closure in a spock test
* Prevents variable expansion
* When using this you *must* include explicit `assert` statements as Spock will not do it for you
*/
void assertions(Closure closure) {
closure()
}

AppendableJar buildJar(String path) {
return new AppendableJar(file(path).toPath())
}

File getOutputShadowJar() {
file('build/libs/shadow-1.0-all.jar')
}

static File getTestKitDir() {
private static File getTestKitDir() {
def gradleUserHome = System.getenv("GRADLE_USER_HOME")
if (!gradleUserHome) {
gradleUserHome = new File(System.getProperty("user.home"), ".gradle").absolutePath
}
return new File(gradleUserHome, "testkit")
}

static String escapedPath(Path path) {
return path.toString().replaceAll('\\\\', '\\\\\\\\')
}
}
Loading

0 comments on commit 710c9fc

Please sign in to comment.