Skip to content

Commit 710c9fc

Browse files
authored
Migrate functional tests to Kotlin and Junit part 4 (#1131)
* 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
1 parent c2100a8 commit 710c9fc

25 files changed

+633
-1000
lines changed

build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ dependencies {
9595
intiTestImplementation(libs.apache.maven.repositoryMetadata)
9696
// TODO: this will be removed after we migrated all functional tests to Kotlin.
9797
intiTestImplementation(sourceSets.main.get().output)
98+
intiTestImplementation(libs.moshi)
99+
intiTestImplementation(libs.moshi.kotlin)
98100

99101
lintChecks(libs.androidx.gradlePluginLints)
100102
lintChecks(libs.assertk.lint)

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[versions]
22
maven = "3.9.9"
3+
moshi = "1.15.2"
34

45
[libraries]
56
apache-ant = "org.apache.ant:ant:1.10.15"
@@ -15,6 +16,8 @@ plexus-utils = "org.codehaus.plexus:plexus-utils:4.0.2"
1516
plexus-xml = "org.codehaus.plexus:plexus-xml:4.0.4"
1617
xmlunit = "org.xmlunit:xmlunit-legacy:2.10.0"
1718
okio = "com.squareup.okio:okio:3.9.1"
19+
moshi = { module = "com.squareup.moshi:moshi", version.ref = "moshi" }
20+
moshi-kotlin = { module = "com.squareup.moshi:moshi-kotlin", version.ref = "moshi" }
1821

1922
pluginPublish = "com.gradle.publish:plugin-publish-plugin:1.3.0"
2023
mavenPublish = "com.vanniktech:gradle-maven-publish-plugin:0.30.0"
Lines changed: 34 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
package com.github.jengelman.gradle.plugins.shadow
22

33
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
4-
import com.github.jengelman.gradle.plugins.shadow.util.AppendableJar
5-
import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository
4+
import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenRepository
65
import org.apache.commons.lang3.StringUtils
7-
import org.codehaus.plexus.util.IOUtil
86
import org.gradle.testkit.runner.BuildResult
97
import org.gradle.testkit.runner.GradleRunner
8+
import spock.lang.Shared
109
import spock.lang.Specification
1110
import spock.lang.TempDir
1211

12+
import java.nio.file.Files
1313
import java.nio.file.Path
1414
import java.nio.file.Paths
15-
import java.util.function.Function
16-
import java.util.jar.JarEntry
1715
import java.util.jar.JarFile
1816

1917
abstract class BasePluginSpecification extends Specification {
2018

2119
@TempDir
2220
Path root
2321

24-
AppendableMavenFileRepository repo
22+
@Shared
23+
static AppendableMavenRepository repo
2524

26-
def setup() {
27-
repo = repo()
28-
repo.module('junit', 'junit', '3.8.2')
29-
.use(Paths.get(this.class.classLoader.getResource('junit-3.8.2.jar').toURI()))
30-
.publish()
25+
def setupSpec() {
26+
repo = new AppendableMavenRepository(
27+
Files.createTempDirectory(null).resolve('local-maven-repo'),
28+
runner,
29+
)
30+
repo.module('junit', 'junit', '3.8.2') { module ->
31+
module.useJar(Paths.get(this.class.classLoader.getResource('junit-3.8.2.jar').toURI()))
32+
}.publish()
33+
}
3134

35+
def setup() {
3236
projectScriptFile << getDefaultProjectBuildScript('java', true, true)
3337
settingsScriptFile << getDefaultSettingsBuildScript()
3438
}
@@ -37,6 +41,10 @@ abstract class BasePluginSpecification extends Specification {
3741
println projectScriptFile.text
3842
}
3943

44+
def cleanupSpec() {
45+
// TODO: Delete repo recursively.
46+
}
47+
4048
String getDefaultProjectBuildScript(
4149
String javaPlugin = 'java',
4250
boolean withGroup = false,
@@ -61,7 +69,7 @@ abstract class BasePluginSpecification extends Specification {
6169
return """
6270
dependencyResolutionManagement {
6371
repositories {
64-
maven { url = "${repo.uri}" }
72+
maven { url = "${repo.root.toUri()}" }
6573
mavenCentral()
6674
}
6775
}
@@ -73,42 +81,29 @@ abstract class BasePluginSpecification extends Specification {
7381
static def shadowJar = "tasks.named('shadowJar', ${ShadowJar.class.name})".trim()
7482

7583
GradleRunner getRunner() {
76-
GradleRunner.create()
77-
.withProjectDir(root.toFile())
84+
def runner = GradleRunner.create()
7885
.forwardOutput()
7986
.withPluginClasspath()
8087
.withTestKitDir(testKitDir)
88+
if (root != null) {
89+
runner.withProjectDir(root.toFile())
90+
}
91+
return runner
8192
}
8293

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

87-
BuildResult run(String... tasks) {
88-
run(tasks.toList())
89-
}
90-
91-
BuildResult run(List<String> tasks, Function<GradleRunner, GradleRunner> runnerFunction = { it }) {
92-
def result = runnerFunction.apply(runner(tasks)).build()
93-
assertNoDeprecationWarnings(result)
94-
return result
95-
}
96-
97-
BuildResult runWithFailure(List<String> tasks, Function<GradleRunner, GradleRunner> runnerFunction = { it }) {
98-
def result = runnerFunction.apply(runner(tasks)).buildAndFail()
99-
assertNoDeprecationWarnings(result)
100-
return result
101-
}
102-
103-
private static void assertNoDeprecationWarnings(BuildResult result) {
104-
result.output.eachLine {
105-
assert !containsDeprecationWarning(it)
98+
BuildResult run(List<String> tasks) {
99+
def result = runner(tasks).build()
100+
result.output.eachLine { output ->
101+
assert !(
102+
output.contains("has been deprecated and is scheduled to be removed in Gradle") ||
103+
output.contains("has been deprecated. This is scheduled to be removed in Gradle")
104+
)
106105
}
107-
}
108-
109-
private static boolean containsDeprecationWarning(String output) {
110-
output.contains("has been deprecated and is scheduled to be removed in Gradle") ||
111-
output.contains("has been deprecated. This is scheduled to be removed in Gradle")
106+
return result
112107
}
113108

114109
File getProjectScriptFile() {
@@ -137,20 +132,6 @@ abstract class BasePluginSpecification extends Specification {
137132
return f
138133
}
139134

140-
AppendableMavenFileRepository repo(String path = 'maven-repo') {
141-
new AppendableMavenFileRepository(root.resolve(path))
142-
}
143-
144-
String getJarFileContents(File f, String path) {
145-
JarFile jf = new JarFile(f)
146-
def is = jf.getInputStream(new JarEntry(path))
147-
StringWriter sw = new StringWriter()
148-
IOUtil.copy(is, sw)
149-
is.close()
150-
jf.close()
151-
return sw.toString()
152-
}
153-
154135
void assertContains(File f, List<String> paths) {
155136
JarFile jar = new JarFile(f)
156137
paths.each { path ->
@@ -167,32 +148,15 @@ abstract class BasePluginSpecification extends Specification {
167148
jar.close()
168149
}
169150

170-
/**
171-
* Helper method to allow scoping variables into a closure in a spock test
172-
* Prevents variable expansion
173-
* When using this you *must* include explicit `assert` statements as Spock will not do it for you
174-
*/
175-
void assertions(Closure closure) {
176-
closure()
177-
}
178-
179-
AppendableJar buildJar(String path) {
180-
return new AppendableJar(file(path).toPath())
181-
}
182-
183151
File getOutputShadowJar() {
184152
file('build/libs/shadow-1.0-all.jar')
185153
}
186154

187-
static File getTestKitDir() {
155+
private static File getTestKitDir() {
188156
def gradleUserHome = System.getenv("GRADLE_USER_HOME")
189157
if (!gradleUserHome) {
190158
gradleUserHome = new File(System.getProperty("user.home"), ".gradle").absolutePath
191159
}
192160
return new File(gradleUserHome, "testkit")
193161
}
194-
195-
static String escapedPath(Path path) {
196-
return path.toString().replaceAll('\\\\', '\\\\\\\\')
197-
}
198162
}

0 commit comments

Comments
 (0)