1
1
package com.github.jengelman.gradle.plugins.shadow
2
2
3
3
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
6
5
import org.apache.commons.lang3.StringUtils
7
- import org.codehaus.plexus.util.IOUtil
8
6
import org.gradle.testkit.runner.BuildResult
9
7
import org.gradle.testkit.runner.GradleRunner
8
+ import spock.lang.Shared
10
9
import spock.lang.Specification
11
10
import spock.lang.TempDir
12
11
12
+ import java.nio.file.Files
13
13
import java.nio.file.Path
14
14
import java.nio.file.Paths
15
- import java.util.function.Function
16
- import java.util.jar.JarEntry
17
15
import java.util.jar.JarFile
18
16
19
17
abstract class BasePluginSpecification extends Specification {
20
18
21
19
@TempDir
22
20
Path root
23
21
24
- AppendableMavenFileRepository repo
22
+ @Shared
23
+ static AppendableMavenRepository repo
25
24
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
+ }
31
34
35
+ def setup () {
32
36
projectScriptFile << getDefaultProjectBuildScript(' java' , true , true )
33
37
settingsScriptFile << getDefaultSettingsBuildScript()
34
38
}
@@ -37,6 +41,10 @@ abstract class BasePluginSpecification extends Specification {
37
41
println projectScriptFile. text
38
42
}
39
43
44
+ def cleanupSpec () {
45
+ // TODO: Delete repo recursively.
46
+ }
47
+
40
48
String getDefaultProjectBuildScript (
41
49
String javaPlugin = ' java' ,
42
50
boolean withGroup = false ,
@@ -61,7 +69,7 @@ abstract class BasePluginSpecification extends Specification {
61
69
return """
62
70
dependencyResolutionManagement {
63
71
repositories {
64
- maven { url = "${ repo.uri } " }
72
+ maven { url = "${ repo.root.toUri() } " }
65
73
mavenCentral()
66
74
}
67
75
}
@@ -73,42 +81,29 @@ abstract class BasePluginSpecification extends Specification {
73
81
static def shadowJar = " tasks.named('shadowJar', ${ ShadowJar.class.name} )" . trim()
74
82
75
83
GradleRunner getRunner () {
76
- GradleRunner . create()
77
- .withProjectDir(root. toFile())
84
+ def runner = GradleRunner . create()
78
85
.forwardOutput()
79
86
.withPluginClasspath()
80
87
.withTestKitDir(testKitDir)
88
+ if (root != null ) {
89
+ runner. withProjectDir(root. toFile())
90
+ }
91
+ return runner
81
92
}
82
93
83
94
GradleRunner runner (Collection<String > tasks ) {
84
95
runner. withArguments([" --warning-mode=fail" , " --configuration-cache" , " --stacktrace" ] + tasks. toList())
85
96
}
86
97
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
+ )
106
105
}
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
112
107
}
113
108
114
109
File getProjectScriptFile () {
@@ -137,20 +132,6 @@ abstract class BasePluginSpecification extends Specification {
137
132
return f
138
133
}
139
134
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
-
154
135
void assertContains (File f , List<String > paths ) {
155
136
JarFile jar = new JarFile (f)
156
137
paths. each { path ->
@@ -167,32 +148,15 @@ abstract class BasePluginSpecification extends Specification {
167
148
jar. close()
168
149
}
169
150
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
-
183
151
File getOutputShadowJar () {
184
152
file(' build/libs/shadow-1.0-all.jar' )
185
153
}
186
154
187
- static File getTestKitDir () {
155
+ private static File getTestKitDir () {
188
156
def gradleUserHome = System . getenv(" GRADLE_USER_HOME" )
189
157
if (! gradleUserHome) {
190
158
gradleUserHome = new File (System . getProperty(" user.home" ), " .gradle" ). absolutePath
191
159
}
192
160
return new File (gradleUserHome, " testkit" )
193
161
}
194
-
195
- static String escapedPath (Path path ) {
196
- return path. toString(). replaceAll(' \\\\ ' , ' \\\\\\\\ ' )
197
- }
198
162
}
0 commit comments