/
AbstractGradleFuncTest.groovy
237 lines (199 loc) · 8.12 KB
/
AbstractGradleFuncTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.gradle.fixtures
import org.apache.commons.io.FileUtils
import org.elasticsearch.gradle.internal.test.ConfigurationCacheCompatibleAwareGradleRunner
import org.elasticsearch.gradle.internal.test.InternalAwareGradleRunner
import org.elasticsearch.gradle.internal.test.NormalizeOutputGradleRunner
import org.elasticsearch.gradle.internal.test.TestResultExtension
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import spock.lang.Specification
import spock.lang.TempDir
import java.lang.management.ManagementFactory
import java.util.jar.JarEntry
import java.util.jar.JarOutputStream
import static org.elasticsearch.gradle.internal.test.TestUtils.normalizeString
abstract class AbstractGradleFuncTest extends Specification {
@Rule
TemporaryFolder testProjectDir = new TemporaryFolder()
@TempDir
File gradleUserHome
File settingsFile
File buildFile
File propertiesFile
File projectDir
boolean configurationCacheCompatible = true
def setup() {
projectDir = testProjectDir.root
settingsFile = testProjectDir.newFile('settings.gradle')
settingsFile << "rootProject.name = 'hello-world'\n"
buildFile = testProjectDir.newFile('build.gradle')
propertiesFile = testProjectDir.newFile('gradle.properties')
propertiesFile <<
"org.gradle.java.installations.fromEnv=JAVA_HOME,RUNTIME_JAVA_HOME,JAVA15_HOME,JAVA14_HOME,JAVA13_HOME,JAVA12_HOME,JAVA11_HOME,JAVA8_HOME"
}
def cleanup() {
if (featureFailed()) {
FileUtils.copyDirectory(testProjectDir.root, new File("build/test-debug/" + testProjectDir.root.name))
}
}
File subProject(String subProjectPath) {
def subProjectBuild = file(subProjectPath.replace(":", "/") + "/build.gradle")
if (subProjectBuild.exists() == false) {
settingsFile << "include \"${subProjectPath}\"\n"
}
subProjectBuild
}
File subProject(String subProjectPath, Closure configAction) {
def subProjectBuild = subProject(subProjectPath)
configAction.setDelegate(new ProjectConfigurer(subProjectBuild.parentFile))
configAction.setResolveStrategy(Closure.DELEGATE_ONLY)
configAction.call()
subProjectBuild
}
GradleRunner gradleRunner(Object... arguments) {
return gradleRunner(testProjectDir.root, arguments)
}
GradleRunner gradleRunner(File projectDir, Object... arguments) {
return new NormalizeOutputGradleRunner(
new ConfigurationCacheCompatibleAwareGradleRunner(
new InternalAwareGradleRunner(
GradleRunner.create()
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments()
.toString().indexOf("-agentlib:jdwp") > 0
)
.withProjectDir(projectDir)
.withPluginClasspath()
.forwardOutput()
), configurationCacheCompatible),
).withArguments(arguments.collect { it.toString() })
}
def assertOutputContains(String givenOutput, String expected) {
assert normalized(givenOutput).contains(normalized(expected))
true
}
def assertNoDeprecationWarning(BuildResult result) {
assertOutputMissing(result.getOutput(), "Deprecated Gradle features were used in this build");
}
def assertOutputMissing(String givenOutput, String expected) {
assert normalized(givenOutput).contains(normalized(expected)) == false
true
}
String normalized(String input) {
return normalizeString(input, testProjectDir.root)
}
File file(String path) {
return file(testProjectDir.root, path)
}
File file(File parent, String path) {
File newFile = new File(parent, path)
newFile.getParentFile().mkdirs()
newFile
}
File someJar(String fileName = 'some.jar') {
File jarFolder = new File(testProjectDir.root, "jars");
jarFolder.mkdirs()
File jarFile = new File(jarFolder, fileName)
JarEntry entry = new JarEntry("foo.txt");
jarFile.withOutputStream {
JarOutputStream target = new JarOutputStream(it)
target.putNextEntry(entry);
target.closeEntry();
target.close();
}
return jarFile;
}
File internalBuild(
List<String> extraPlugins = [],
String bugfix = "7.15.2",
String bugfixLucene = "8.9.0",
String staged = "7.16.0",
String stagedLucene = "8.10.0",
String minor = "8.0.0",
String minorLucene = "9.0.0"
) {
buildFile << """plugins {
id 'elasticsearch.global-build-info'
${extraPlugins.collect { p -> "id '$p'" }.join('\n')}
}
import org.elasticsearch.gradle.Architecture
import org.elasticsearch.gradle.internal.info.BuildParams
import org.elasticsearch.gradle.internal.BwcVersions.VersionPair
import org.elasticsearch.gradle.internal.BwcVersions
import org.elasticsearch.gradle.Version
Version currentVersion = Version.fromString("8.1.0")
def versionList = [
new VersionPair(Version.fromString("$bugfix"), Version.fromString("$bugfixLucene")),
new VersionPair(Version.fromString("$staged"), Version.fromString("$stagedLucene")),
new VersionPair(Version.fromString("$minor"), Version.fromString("$minorLucene")),
new VersionPair(currentVersion, Version.fromString("9.0.0"))
]
BwcVersions versions = new BwcVersions(currentVersion, versionList)
BuildParams.init { it.setBwcVersions(provider(() -> versions)) }
"""
}
void setupLocalGitRepo() {
execute("git init")
execute('git config user.email "build-tool@elastic.co"')
execute('git config user.name "Build tool"')
execute("git add .")
execute('git commit -m "Initial"')
}
void execute(String command, File workingDir = testProjectDir.root) {
def proc = command.execute(Collections.emptyList(), workingDir)
proc.waitFor()
if (proc.exitValue()) {
System.err.println("Error running command ${command}:")
System.err.println("Syserr: " + proc.errorStream.text)
}
}
File dir(String path) {
def dir = file(projectDir, path)
dir.mkdirs()
dir
}
void withVersionCatalogue() {
file('build.versions.toml') << '''\
[libraries]
checkstyle = "com.puppycrawl.tools:checkstyle:10.3"
'''
settingsFile << '''
dependencyResolutionManagement {
versionCatalogs {
buildLibs {
from(files("build.versions.toml"))
}
}
}
'''
}
boolean featureFailed() {
specificationContext.currentSpec.listeners
.findAll { it instanceof TestResultExtension.ErrorListener }
.any {
(it as TestResultExtension.ErrorListener).errorInfo != null }
}
static class ProjectConfigurer {
private File projectDir
ProjectConfigurer(File projectDir) {
this.projectDir = projectDir
}
File classFile(String fullQualifiedName) {
File sourceRoot = new File(projectDir, 'src/main/java');
File file = new File(sourceRoot, fullQualifiedName.replace('.', '/') + '.java')
file.getParentFile().mkdirs()
file
}
File getBuildFile() {
return new File(projectDir, 'build.gradle')
};
}
}