Skip to content

Commit 0a9f0c8

Browse files
authored
Align logic for functional test kits (GradleUp#1122)
* Rename contains and doesNotContain * Binary files should be asserted to exist, text files should be created * Remove unused assertJarFileContentsEqual * Replace runWithDebug with run * Cleanups * Rename getOutput to getOutputShadowJar * Rename getDefaultBuildScript to getProjectBuildScript * Rename dir to root * Rename getBuildFile and getSettingsFile * Fix * Merge getFile logic into file * Align getProjectBuildScript logic * Don't assert existing in path * Cleanup * Rename getProjectBuildScript and getDefaultSettingsBuildScript * Rename getBuildScript and getSettingsScript
1 parent 6c3341c commit 0a9f0c8

File tree

14 files changed

+336
-346
lines changed

14 files changed

+336
-346
lines changed

src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/BasePluginSpecification.groovy

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.jengelman.gradle.plugins.shadow
33
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
44
import com.github.jengelman.gradle.plugins.shadow.util.AppendableJar
55
import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository
6+
import org.apache.commons.lang3.StringUtils
67
import org.codehaus.plexus.util.IOUtil
78
import org.gradle.testkit.runner.BuildResult
89
import org.gradle.testkit.runner.GradleRunner
@@ -18,25 +19,25 @@ import java.util.jar.JarFile
1819
abstract class BasePluginSpecification extends Specification {
1920

2021
@TempDir
21-
Path dir
22+
Path root
2223

2324
AppendableMavenFileRepository repo
2425

2526
def setup() {
2627
repo = repo()
27-
repo.module('junit', 'junit', '3.8.2').use(testJar).publish()
28+
repo.module('junit', 'junit', '3.8.2')
29+
.use(Paths.get(this.class.classLoader.getResource('junit-3.8.2.jar').toURI()))
30+
.publish()
2831

29-
buildFile << getDefaultBuildScript('java', true, true)
30-
buildFile << System.lineSeparator()
31-
settingsFile << settingsBuildScript
32-
settingsFile << System.lineSeparator()
32+
projectScriptFile << getDefaultProjectBuildScript('java', true, true)
33+
settingsScriptFile << getDefaultSettingsBuildScript()
3334
}
3435

3536
def cleanup() {
36-
println buildFile.text
37+
println projectScriptFile.text
3738
}
3839

39-
String getDefaultBuildScript(
40+
String getDefaultProjectBuildScript(
4041
String javaPlugin = 'java',
4142
boolean withGroup = false,
4243
boolean withVersion = false
@@ -52,10 +53,10 @@ abstract class BasePluginSpecification extends Specification {
5253
5354
$groupInfo
5455
$versionInfo
55-
""".stripIndent().trim()
56+
""".stripIndent().trim() + System.lineSeparator()
5657
}
5758

58-
String getSettingsBuildScript(boolean withRootProject = true) {
59+
String getDefaultSettingsBuildScript(boolean withRootProject = true) {
5960
def rootProjectInfo = withRootProject ? "rootProject.name = 'shadow'" : ""
6061
return """
6162
dependencyResolutionManagement {
@@ -66,14 +67,14 @@ abstract class BasePluginSpecification extends Specification {
6667
}
6768
6869
$rootProjectInfo
69-
""".stripIndent().trim()
70+
""".stripIndent().trim() + System.lineSeparator()
7071
}
7172

7273
static def shadowJar = "tasks.named('shadowJar', ${ShadowJar.class.name})".trim()
7374

7475
GradleRunner getRunner() {
7576
GradleRunner.create()
76-
.withProjectDir(dir.toFile())
77+
.withProjectDir(root.toFile())
7778
.forwardOutput()
7879
.withPluginClasspath()
7980
.withTestKitDir(testKitDir)
@@ -93,37 +94,40 @@ abstract class BasePluginSpecification extends Specification {
9394
return result
9495
}
9596

96-
BuildResult runWithDebug(String... tasks) {
97-
return run(tasks.toList(), { it.withDebug(true) })
98-
}
99-
10097
BuildResult runWithFailure(List<String> tasks, Function<GradleRunner, GradleRunner> runnerFunction = { it }) {
10198
def result = runnerFunction.apply(runner(tasks)).buildAndFail()
10299
assertNoDeprecationWarnings(result)
103100
return result
104101
}
105102

106-
static void assertNoDeprecationWarnings(BuildResult result) {
103+
private static void assertNoDeprecationWarnings(BuildResult result) {
107104
result.output.eachLine {
108105
assert !containsDeprecationWarning(it)
109106
}
110107
}
111108

112-
static boolean containsDeprecationWarning(String output) {
109+
private static boolean containsDeprecationWarning(String output) {
113110
output.contains("has been deprecated and is scheduled to be removed in Gradle") ||
114111
output.contains("has been deprecated. This is scheduled to be removed in Gradle")
115112
}
116113

117-
File getBuildFile() {
114+
File getProjectScriptFile() {
118115
file('build.gradle')
119116
}
120117

121-
File getSettingsFile() {
118+
File getSettingsScriptFile() {
122119
file('settings.gradle')
123120
}
124121

125122
File file(String path) {
126-
File f = dir.resolve(path).toFile()
123+
File f = root.resolve(path).toFile()
124+
String extension = StringUtils.substringAfterLast(path, '.')
125+
126+
// Binary files should be asserted to exist, text files should be created.
127+
if (extension == "jar" || extension == "zip") {
128+
return f
129+
}
130+
127131
if (!f.exists()) {
128132
f.parentFile.mkdirs()
129133
if (!f.createNewFile()) {
@@ -133,16 +137,8 @@ abstract class BasePluginSpecification extends Specification {
133137
return f
134138
}
135139

136-
File getFile(String path) {
137-
return dir.resolve(path).toFile()
138-
}
139-
140140
AppendableMavenFileRepository repo(String path = 'maven-repo') {
141-
new AppendableMavenFileRepository(dir.resolve(path))
142-
}
143-
144-
void assertJarFileContentsEqual(File f, String path, String contents) {
145-
assert getJarFileContents(f, path) == contents
141+
new AppendableMavenFileRepository(root.resolve(path))
146142
}
147143

148144
String getJarFileContents(File f, String path) {
@@ -155,15 +151,15 @@ abstract class BasePluginSpecification extends Specification {
155151
return sw.toString()
156152
}
157153

158-
void contains(File f, List<String> paths) {
154+
void assertContains(File f, List<String> paths) {
159155
JarFile jar = new JarFile(f)
160156
paths.each { path ->
161157
assert jar.getJarEntry(path), "${f.path} does not contain [$path]"
162158
}
163159
jar.close()
164160
}
165161

166-
void doesNotContain(File f, List<String> paths) {
162+
void assertDoesNotContain(File f, List<String> paths) {
167163
JarFile jar = new JarFile(f)
168164
paths.each { path ->
169165
assert !jar.getJarEntry(path), "${f.path} contains [$path]"
@@ -184,27 +180,19 @@ abstract class BasePluginSpecification extends Specification {
184180
return new AppendableJar(file(path).toPath())
185181
}
186182

187-
protected File getOutput() {
188-
getFile('build/libs/shadow-1.0-all.jar')
189-
}
190-
191-
protected File output(String name) {
192-
getFile("build/libs/${name}")
193-
}
194-
195-
protected Path getTestJar(String name = 'junit-3.8.2.jar') {
196-
return Paths.get(this.class.classLoader.getResource(name).toURI())
183+
File getOutputShadowJar() {
184+
file('build/libs/shadow-1.0-all.jar')
197185
}
198186

199-
protected static File getTestKitDir() {
187+
static File getTestKitDir() {
200188
def gradleUserHome = System.getenv("GRADLE_USER_HOME")
201189
if (!gradleUserHome) {
202190
gradleUserHome = new File(System.getProperty("user.home"), ".gradle").absolutePath
203191
}
204192
return new File(gradleUserHome, "testkit")
205193
}
206194

207-
protected static String escapedPath(Path path) {
195+
static String escapedPath(Path path) {
208196
return path.toString().replaceAll('\\\\', '\\\\\\\\')
209197
}
210198
}

src/funcTest/groovy/com/github/jengelman/gradle/plugins/shadow/PublishingSpec.groovy

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class PublishingSpec extends BasePluginSpecification {
2626
.insertFile('b.properties', 'b')
2727
.publish()
2828

29-
settingsFile << "rootProject.name = 'maven'"
30-
buildFile << """
29+
settingsScriptFile << "rootProject.name = 'maven'"
30+
projectScriptFile << """
3131
apply plugin: 'maven-publish'
3232
3333
dependencies {
@@ -63,7 +63,7 @@ class PublishingSpec extends BasePluginSpecification {
6363
assert publishedFile.exists()
6464

6565
and:
66-
contains(publishedFile, ['a.properties', 'a2.properties'])
66+
assertContains(publishedFile, ['a.properties', 'a2.properties'])
6767

6868
and:
6969
File pom = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.pom').toFile().canonicalFile
@@ -93,8 +93,8 @@ class PublishingSpec extends BasePluginSpecification {
9393
.insertFile('b.properties', 'b')
9494
.publish()
9595

96-
settingsFile << "rootProject.name = 'maven'"
97-
buildFile << """
96+
settingsScriptFile << "rootProject.name = 'maven'"
97+
projectScriptFile << """
9898
apply plugin: 'maven-publish'
9999
100100
dependencies {
@@ -134,14 +134,14 @@ class PublishingSpec extends BasePluginSpecification {
134134
def "publish multiproject shadow jar with maven-publish plugin"() {
135135
given:
136136

137-
settingsFile << """
137+
settingsScriptFile << """
138138
rootProject.name = 'maven'
139139
include 'a'
140140
include 'b'
141141
include 'c'
142142
""".stripMargin()
143143

144-
buildFile.text = """
144+
projectScriptFile.text = """
145145
subprojects {
146146
apply plugin: 'java'
147147
apply plugin: 'maven-publish'
@@ -211,7 +211,7 @@ class PublishingSpec extends BasePluginSpecification {
211211
assert publishedFile.exists()
212212

213213
and:
214-
contains(publishedFile, ['a.properties', 'a2.properties'])
214+
assertContains(publishedFile, ['a.properties', 'a2.properties'])
215215

216216
and:
217217
File pom = publishingRepo.rootDir.resolve('shadow/maven-all/1.0/maven-all-1.0.pom').toFile().canonicalFile
@@ -237,10 +237,10 @@ class PublishingSpec extends BasePluginSpecification {
237237
.insertFile('b.properties', 'b')
238238
.publish()
239239

240-
settingsFile << """
240+
settingsScriptFile << """
241241
rootProject.name = 'maven'
242242
"""
243-
buildFile << """
243+
projectScriptFile << """
244244
apply plugin: 'maven-publish'
245245
dependencies {
246246
implementation 'shadow:a:1.0'
@@ -277,7 +277,7 @@ class PublishingSpec extends BasePluginSpecification {
277277
assert shadowJar.exists()
278278

279279
and:
280-
contains(shadowJar, ['a.properties', 'a2.properties'])
280+
assertContains(shadowJar, ['a.properties', 'a2.properties'])
281281

282282
and: "publishes both a POM file and a Gradle metadata file"
283283
File pom = publishingRepo.rootDir.resolve('com/acme/maven/1.0/maven-1.0.pom').toFile().canonicalFile
@@ -328,7 +328,7 @@ class PublishingSpec extends BasePluginSpecification {
328328
assertions {
329329
shadowJar = publishingRepo.rootDir.resolve('com/acme/maven-all/1.0/maven-all-1.0-all.jar').toFile().canonicalFile
330330
assert shadowJar.exists()
331-
contains(shadowJar, ['a.properties', 'a2.properties'])
331+
assertContains(shadowJar, ['a.properties', 'a2.properties'])
332332
}
333333

334334
assertions {

0 commit comments

Comments
 (0)