-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
ApplyPluginIntegSpec.groovy
227 lines (188 loc) · 7.7 KB
/
ApplyPluginIntegSpec.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
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.api
import org.gradle.integtests.fixtures.AbstractIntegrationSpec
import org.gradle.integtests.fixtures.executer.GradleContextualExecuter
import org.gradle.internal.os.OperatingSystem
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.util.GradleVersion
import spock.lang.IgnoreIf
import spock.lang.Issue
import static org.gradle.util.internal.TextUtil.normaliseFileSeparators
// TODO: This needs a better home - Possibly in the test kit package in the future
@Issue("https://github.com/gradle/gradle-private/issues/3247")
@IgnoreIf({ OperatingSystem.current().macOsX && JavaVersion.current() == JavaVersion.VERSION_1_8})
class ApplyPluginIntegSpec extends AbstractIntegrationSpec {
def testProjectPath
def setup() {
testProjectPath = normalisedPathOf(file("test-project-dir"))
}
@Issue("GRADLE-2358")
def "can reference plugin by id in unit test"() {
given:
file("src/main/groovy/org/acme/TestPlugin.groovy") << """
package com.acme
import org.gradle.api.*
class TestPlugin implements Plugin<Project> {
void apply(Project project) {
println "testplugin applied"
}
}
"""
file("src/main/resources/META-INF/gradle-plugins/testplugin.properties") << "implementation-class=org.acme.TestPlugin"
file("src/test/groovy/org/acme/TestPluginSpec.groovy") << """
import spock.lang.Specification
import ${ProjectBuilder.name}
import ${Project.name}
import com.acme.TestPlugin
class TestPluginSpec extends Specification {
def "can apply plugin by id"() {
when:
def projectDir = new File('${testProjectPath}')
Project project = ProjectBuilder.builder().withProjectDir(projectDir).build()
project.apply(plugin: "testplugin")
then:
project.plugins.withType(TestPlugin).size() == 1
}
}
"""
and:
buildFile << spockBasedBuildScript()
expect:
succeeds("test")
}
@Issue("GRADLE-3068")
@IgnoreIf({ GradleContextualExecuter.embedded }) // Requires a Gradle distribution on the test-under-test classpath, but gradleApi() does not offer the full distribution
def "can use gradleApi in test"() {
given:
file("src/test/groovy/org/acme/ProjectBuilderTest.groovy") << """
package org.acme
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Test
class ProjectBuilderTest {
@Test
void "can evaluate ProjectBuilder"() {
def projectDir = new File('${testProjectPath}')
def userHome = new File('${gradleUserHome}')
def project = ProjectBuilder.builder()
.withProjectDir(projectDir)
.withGradleUserHomeDir(userHome)
.build()
project.apply(plugin: 'groovy')
project.evaluate()
}
}
"""
and:
buildFile << junitBasedBuildScript()
buildFile << """
// Needed when using ProjectBuilder
class AddOpensArgProvider implements CommandLineArgumentProvider {
private final Test test;
public AddOpensArgProvider(Test test) {
this.test = test;
}
@Override
Iterable<String> asArguments() {
return test.javaVersion.isCompatibleWith(JavaVersion.VERSION_1_9)
? ["--add-opens=java.base/java.lang=ALL-UNNAMED"]
: []
}
}
tasks.withType(Test).configureEach {
jvmArgumentProviders.add(new AddOpensArgProvider(it))
}
"""
expect:
executer.withArgument("--info")
succeeds("test")
}
@IgnoreIf({ GradleContextualExecuter.embedded }) // Gradle API JAR is not generated when running embedded
def "generated Gradle API JAR in custom Gradle user home is reused across multiple invocations"() {
requireOwnGradleUserHomeDir()
given:
file("src/test/groovy/org/acme/ProjectBuilderTest.groovy") << """
package org.acme
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import spock.lang.Specification
class ProjectBuilderTest extends Specification {
def "generates Gradle API JAR and reuses it"() {
given:
def gradleVersion = '${GradleVersion.current().getVersion()}'
def gradleUserHome = new File('${gradleUserHome}')
def generatedGradleJarCacheDir = new File(gradleUserHome, "caches/\$gradleVersion/generated-gradle-jars")
def gradleApiJar = new File(generatedGradleJarCacheDir, "gradle-api-\${gradleVersion}.jar")
when:
def project = createProject(gradleUserHome)
then:
gradleApiJar.exists()
long lastModified = gradleApiJar.lastModified()
when:
project = createProject(gradleUserHome)
then:
gradleApiJar.exists()
lastModified == gradleApiJar.lastModified()
}
static Project createProject(File gradleUserHome) {
def project = ProjectBuilder.builder().withGradleUserHomeDir(gradleUserHome).build()
project.plugins.apply('java')
project.dependencies.add('implementation', project.dependencies.gradleApi())
project
}
}
"""
and:
buildFile << spockBasedBuildScript()
expect:
succeeds('test')
}
static String junitBasedBuildScript() {
"""
${basicBuildScript()}
dependencies {
testImplementation 'junit:junit:4.13'
}
"""
}
static String spockBasedBuildScript() {
"""
${basicBuildScript()}
dependencies {
testImplementation ('org.spockframework:spock-core:2.1-groovy-3.0') {
exclude group: 'org.codehaus.groovy'
}
}
"""
}
static String basicBuildScript() {
"""
apply plugin: 'groovy'
${mavenCentralRepository()}
dependencies {
implementation gradleApi()
implementation localGroovy()
}
"""
}
private String getGradleUserHome() {
normalisedPathOf(executer.gradleUserHomeDir)
}
static String normalisedPathOf(File file) {
normaliseFileSeparators(file.absolutePath)
}
}