@@ -3,6 +3,7 @@ package com.github.jengelman.gradle.plugins.shadow
3
3
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
4
4
import com.github.jengelman.gradle.plugins.shadow.util.AppendableJar
5
5
import com.github.jengelman.gradle.plugins.shadow.util.AppendableMavenFileRepository
6
+ import org.apache.commons.lang3.StringUtils
6
7
import org.codehaus.plexus.util.IOUtil
7
8
import org.gradle.testkit.runner.BuildResult
8
9
import org.gradle.testkit.runner.GradleRunner
@@ -18,25 +19,25 @@ import java.util.jar.JarFile
18
19
abstract class BasePluginSpecification extends Specification {
19
20
20
21
@TempDir
21
- Path dir
22
+ Path root
22
23
23
24
AppendableMavenFileRepository repo
24
25
25
26
def setup () {
26
27
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()
28
31
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()
33
34
}
34
35
35
36
def cleanup () {
36
- println buildFile . text
37
+ println projectScriptFile . text
37
38
}
38
39
39
- String getDefaultBuildScript (
40
+ String getDefaultProjectBuildScript (
40
41
String javaPlugin = ' java' ,
41
42
boolean withGroup = false ,
42
43
boolean withVersion = false
@@ -52,10 +53,10 @@ abstract class BasePluginSpecification extends Specification {
52
53
53
54
$groupInfo
54
55
$versionInfo
55
- """ . stripIndent(). trim()
56
+ """ . stripIndent(). trim() + System . lineSeparator()
56
57
}
57
58
58
- String getSettingsBuildScript (boolean withRootProject = true ) {
59
+ String getDefaultSettingsBuildScript (boolean withRootProject = true ) {
59
60
def rootProjectInfo = withRootProject ? " rootProject.name = 'shadow'" : " "
60
61
return """
61
62
dependencyResolutionManagement {
@@ -66,14 +67,14 @@ abstract class BasePluginSpecification extends Specification {
66
67
}
67
68
68
69
$rootProjectInfo
69
- """ . stripIndent(). trim()
70
+ """ . stripIndent(). trim() + System . lineSeparator()
70
71
}
71
72
72
73
static def shadowJar = " tasks.named('shadowJar', ${ ShadowJar.class.name} )" . trim()
73
74
74
75
GradleRunner getRunner () {
75
76
GradleRunner . create()
76
- .withProjectDir(dir . toFile())
77
+ .withProjectDir(root . toFile())
77
78
.forwardOutput()
78
79
.withPluginClasspath()
79
80
.withTestKitDir(testKitDir)
@@ -93,37 +94,40 @@ abstract class BasePluginSpecification extends Specification {
93
94
return result
94
95
}
95
96
96
- BuildResult runWithDebug (String ... tasks ) {
97
- return run(tasks. toList(), { it. withDebug(true ) })
98
- }
99
-
100
97
BuildResult runWithFailure (List<String > tasks , Function<GradleRunner , GradleRunner > runnerFunction = { it }) {
101
98
def result = runnerFunction. apply(runner(tasks)). buildAndFail()
102
99
assertNoDeprecationWarnings(result)
103
100
return result
104
101
}
105
102
106
- static void assertNoDeprecationWarnings (BuildResult result ) {
103
+ private static void assertNoDeprecationWarnings (BuildResult result ) {
107
104
result. output. eachLine {
108
105
assert ! containsDeprecationWarning(it)
109
106
}
110
107
}
111
108
112
- static boolean containsDeprecationWarning (String output ) {
109
+ private static boolean containsDeprecationWarning (String output ) {
113
110
output. contains(" has been deprecated and is scheduled to be removed in Gradle" ) ||
114
111
output. contains(" has been deprecated. This is scheduled to be removed in Gradle" )
115
112
}
116
113
117
- File getBuildFile () {
114
+ File getProjectScriptFile () {
118
115
file(' build.gradle' )
119
116
}
120
117
121
- File getSettingsFile () {
118
+ File getSettingsScriptFile () {
122
119
file(' settings.gradle' )
123
120
}
124
121
125
122
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
+
127
131
if (! f. exists()) {
128
132
f. parentFile. mkdirs()
129
133
if (! f. createNewFile()) {
@@ -133,16 +137,8 @@ abstract class BasePluginSpecification extends Specification {
133
137
return f
134
138
}
135
139
136
- File getFile (String path ) {
137
- return dir. resolve(path). toFile()
138
- }
139
-
140
140
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))
146
142
}
147
143
148
144
String getJarFileContents (File f , String path ) {
@@ -155,15 +151,15 @@ abstract class BasePluginSpecification extends Specification {
155
151
return sw. toString()
156
152
}
157
153
158
- void contains (File f , List<String > paths ) {
154
+ void assertContains (File f , List<String > paths ) {
159
155
JarFile jar = new JarFile (f)
160
156
paths. each { path ->
161
157
assert jar. getJarEntry(path), " ${ f.path} does not contain [$path ]"
162
158
}
163
159
jar. close()
164
160
}
165
161
166
- void doesNotContain (File f , List<String > paths ) {
162
+ void assertDoesNotContain (File f , List<String > paths ) {
167
163
JarFile jar = new JarFile (f)
168
164
paths. each { path ->
169
165
assert ! jar. getJarEntry(path), " ${ f.path} contains [$path ]"
@@ -184,27 +180,19 @@ abstract class BasePluginSpecification extends Specification {
184
180
return new AppendableJar (file(path). toPath())
185
181
}
186
182
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' )
197
185
}
198
186
199
- protected static File getTestKitDir () {
187
+ static File getTestKitDir () {
200
188
def gradleUserHome = System . getenv(" GRADLE_USER_HOME" )
201
189
if (! gradleUserHome) {
202
190
gradleUserHome = new File (System . getProperty(" user.home" ), " .gradle" ). absolutePath
203
191
}
204
192
return new File (gradleUserHome, " testkit" )
205
193
}
206
194
207
- protected static String escapedPath (Path path ) {
195
+ static String escapedPath (Path path ) {
208
196
return path. toString(). replaceAll(' \\\\ ' , ' \\\\\\\\ ' )
209
197
}
210
198
}
0 commit comments