-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
BuildInitPluginIntegrationTest.groovy
456 lines (367 loc) · 15.9 KB
/
BuildInitPluginIntegrationTest.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* 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.buildinit.plugins
import org.gradle.buildinit.plugins.fixtures.ScriptDslFixture
import org.gradle.buildinit.plugins.internal.BuildScriptBuilder
import org.gradle.buildinit.plugins.internal.modifiers.BuildInitDsl
import org.gradle.integtests.fixtures.executer.ExecutionResult
import org.hamcrest.Matcher
import static org.gradle.buildinit.plugins.internal.modifiers.BuildInitDsl.GROOVY
import static org.hamcrest.CoreMatchers.allOf
import static org.hamcrest.CoreMatchers.containsString
import static org.hamcrest.CoreMatchers.not
class BuildInitPluginIntegrationTest extends AbstractInitIntegrationSpec {
@Override
String subprojectName() { 'app' }
def "init shows up on tasks overview "() {
given:
targetDir.file("settings.gradle").touch()
when:
run 'tasks'
then:
outputContains "init - Initializes a new Gradle build."
}
def "creates a simple project with #scriptDsl build scripts when no pom file present and no type specified"() {
given:
useTestDirectoryThatIsNotEmbeddedInAnotherBuild()
def dslFixture = ScriptDslFixture.of(scriptDsl, targetDir, null)
when:
runInitWith scriptDsl
then:
commonFilesGenerated(scriptDsl, dslFixture)
and:
dslFixture.buildFile.assertContents(
allOf(
containsString("This is a general purpose Gradle build"),
containsString("Learn more about Gradle by exploring our samples at")))
expect:
succeeds 'properties'
where:
scriptDsl << ScriptDslFixture.SCRIPT_DSLS
}
def "creates a simple project with #scriptDsl build scripts when no pom file present and no type specified which uses @Incubating APIs"() {
given:
useTestDirectoryThatIsNotEmbeddedInAnotherBuild()
def dslFixture = ScriptDslFixture.of(scriptDsl, targetDir, null)
when:
runInitWith scriptDsl, '--incubating'
then:
commonFilesGenerated(scriptDsl, dslFixture)
and:
dslFixture.buildFile.assertContents(
allOf(
containsString("This is a general purpose Gradle build"),
containsString("Learn more about Gradle by exploring our samples at"),
containsString(BuildScriptBuilder.getIncubatingApisWarning())))
expect:
succeeds 'properties'
where:
scriptDsl << ScriptDslFixture.SCRIPT_DSLS
}
def "#targetScriptDsl build file generation is skipped when #existingScriptDsl build file already exists"() {
given:
def existingDslFixture = rootProjectDslFixtureFor(existingScriptDsl as BuildInitDsl)
def targetDslFixture = dslFixtureFor(targetScriptDsl as BuildInitDsl)
and:
existingDslFixture.buildFile.createFile()
when:
runInitWith targetScriptDsl as BuildInitDsl
then:
result.assertTasksExecuted(":init")
outputContains("The build file '${existingDslFixture.buildFileName}' already exists. Skipping build initialization.")
and:
!targetDslFixture.settingsFile.exists()
targetDslFixture.assertWrapperNotGenerated()
where:
[existingScriptDsl, targetScriptDsl] << ScriptDslFixture.scriptDslCombinationsFor(2)
}
def "#targetScriptDsl build file generation is skipped when #existingScriptDsl settings file already exists"() {
given:
def existingDslFixture = dslFixtureFor(existingScriptDsl as BuildInitDsl)
def targetDslFixture = dslFixtureFor(targetScriptDsl as BuildInitDsl)
and:
existingDslFixture.settingsFile.createFile()
when:
runInitWith targetScriptDsl as BuildInitDsl
then:
result.assertTasksExecuted(":init")
outputContains("The settings file '${existingDslFixture.settingsFileName}' already exists. Skipping build initialization.")
and:
!targetDslFixture.buildFile.exists()
targetDslFixture.assertWrapperNotGenerated()
where:
[existingScriptDsl, targetScriptDsl] << ScriptDslFixture.scriptDslCombinationsFor(2)
}
def "#targetScriptDsl build file generation is skipped when custom #existingScriptDsl build file exists"() {
given:
def existingDslFixture = dslFixtureFor(existingScriptDsl as BuildInitDsl)
def targetDslFixture = dslFixtureFor(targetScriptDsl as BuildInitDsl)
and:
def customBuildScript = existingDslFixture.scriptFile("build").createFile()
when:
runInitWith targetScriptDsl as BuildInitDsl
then:
result.assertTasksExecuted(":init")
outputContains("The build file '${customBuildScript.name}' already exists. Skipping build initialization.")
and:
!targetDslFixture.buildFile.exists()
!targetDslFixture.settingsFile.exists()
targetDslFixture.assertWrapperNotGenerated()
where:
[existingScriptDsl, targetScriptDsl] << ScriptDslFixture.scriptDslCombinationsFor(2)
}
def "#targetScriptDsl build file generation is skipped when part of a multi-project build with non-standard #existingScriptDsl settings file location"() {
given:
def existingDslFixture = dslFixtureFor(existingScriptDsl as BuildInitDsl)
def targetDslFixture = dslFixtureFor(targetScriptDsl as BuildInitDsl)
and:
def customSettings = existingDslFixture.scriptFile("customSettings")
customSettings << """
include("child")
"""
when:
executer.usingSettingsFile(customSettings)
executer.expectDocumentedDeprecationWarning("Specifying custom settings file location has been deprecated. This is scheduled to be removed in Gradle 9.0. Consult the upgrading guide for further information: https://docs.gradle.org/current/userguide/upgrading_version_7.html#configuring_custom_build_layout")
runInitWith targetScriptDsl as BuildInitDsl
then:
result.assertTasksExecuted(":init")
outputContains("The settings file '${customSettings.name}' already exists. Skipping build initialization.")
and:
!targetDslFixture.buildFile.exists()
!targetDslFixture.settingsFile.exists()
targetDslFixture.assertWrapperNotGenerated()
where:
[existingScriptDsl, targetScriptDsl] << ScriptDslFixture.scriptDslCombinationsFor(2)
}
def "pom conversion to groovy build scripts is triggered when pom and no gradle file found"() {
given:
pom()
when:
run('init')
then:
pomValuesUsed(rootProjectDslFixtureFor(GROOVY))
}
def "pom conversion to #scriptDsl build scripts not triggered when build type is specified"() {
given:
pom()
when:
succeeds('init', '--type', 'java-application', '--dsl', scriptDsl.id)
then:
pomValuesNotUsed(dslFixtureFor(scriptDsl))
where:
scriptDsl << ScriptDslFixture.SCRIPT_DSLS
}
def "gives decent error message when triggered with unknown init-type"() {
when:
fails('init', '--type', 'some-unknown-library')
then:
failure.assertHasCause("""The requested build type 'some-unknown-library' is not supported. Supported types:
- 'basic'
- 'cpp-application'
- 'cpp-library'
- 'groovy-application'
- 'groovy-gradle-plugin'
- 'groovy-library'
- 'java-application'
- 'java-gradle-plugin'
- 'java-library'
- 'kotlin-application'
- 'kotlin-gradle-plugin'
- 'kotlin-library'
- 'pom'
- 'scala-application'
- 'scala-library'""")
}
def "gives decent error message when triggered with unknown dsl"() {
when:
fails('init', '--dsl', 'some-unknown-dsl')
then:
failure.assertHasCause("""The requested build script DSL 'some-unknown-dsl' is not supported. Supported DSLs:
- 'groovy'
- 'kotlin'""")
}
def "gives decent error message when using unknown test framework"() {
when:
fails('init', '--type', 'basic', '--test-framework', 'fake')
then:
failure.assertHasCause("""The requested test framework 'fake' is not supported for 'basic' build type. Supported frameworks:
- 'none'""")
}
def "gives decent error message when test framework is not supported by specific type"() {
when:
fails('init', '--type', 'basic', '--test-framework', 'spock')
then:
failure.assertHasCause("""The requested test framework 'spock' is not supported for 'basic' build type. Supported frameworks:
- 'none'""")
}
def "gives decent error message when project name option is not supported by specific type"() {
when:
fails('init', '--type', 'pom', '--project-name', 'thing')
then:
failure.assertHasCause("Project name is not supported for 'pom' build type.")
}
def "gives decent error message when package name option is not supported by specific type"() {
when:
fails('init', '--type', 'basic', '--package', 'thing')
then:
failure.assertHasCause("Package name is not supported for 'basic' build type.")
}
def "displays all build types and modifiers in help command output"() {
when:
targetDir.file("settings.gradle").touch()
run('help', '--task', 'init')
then:
outputContains("""Options
--dsl Set the build script DSL to be used in generated scripts.
Available values are:
groovy
kotlin
--incubating Allow the generated build to use new features and APIs
--insecure-protocol How to handle insecure URLs used for Maven Repositories.
Available values are:
ALLOW
FAIL
UPGRADE
WARN
--package Set the package for source files.
--project-name Set the project name.
--split-project Split functionality across multiple subprojects?
--test-framework Set the test framework to be used.
Available values are:
junit
junit-jupiter
kotlintest
scalatest
spock
testng
--type Set the type of project to generate.
Available values are:
basic
cpp-application
cpp-library
groovy-application
groovy-gradle-plugin
groovy-library
java-application
java-gradle-plugin
java-library
kotlin-application
kotlin-gradle-plugin
kotlin-library
pom
scala-application
scala-library""")
}
def "can initialize in a directory that is under another build's root directory"() {
when:
containerDir.file("settings.gradle") << "rootProject.name = 'root'"
then:
succeeds "init"
targetDir.file("settings.gradle").assertIsFile()
targetDir.file("build.gradle").assertIsFile()
}
def "fails when initializing in a project directory of another build that contains a build script"() {
when:
containerDir.file("settings.gradle") << """
rootProject.name = 'root'
include('${targetDir.name}')
"""
targetDir.file("build.gradle") << """
// empty
"""
then:
fails "init"
failure.assertHasDescription("Task 'init' not found in project ':some-thing'.")
targetDir.assertHasDescendants("build.gradle")
}
def "fails when initializing in a project directory of another build that does not contain a build script"() {
when:
containerDir.file("settings.gradle") << """
rootProject.name = 'root'
include('${targetDir.name}')
"""
then:
fails "init"
failure.assertHasDescription("Task 'init' not found in project ':some-thing'.")
targetDir.listFiles().size() == 0 // Is still empty
}
def "warns when initializing in root project directory of another single project build that does not contain a build script"() {
when:
containerDir.file("settings.gradle") << """
rootProject.name = 'root'
rootProject.projectDir = file('${targetDir.name}')
"""
then:
succeeds "init"
outputContains("The settings file '..${File.separatorChar}settings.gradle' already exists. Skipping build initialization.")
targetDir.list().size() == 0 // ensure nothing generated
}
def "can create build in user home directory"() {
when:
useTestDirectoryThatIsNotEmbeddedInAnotherBuild()
def dotGradleDir = targetDir.file('.gradle')
dotGradleDir.mkdirs()
def gradlePropertiesFile = dotGradleDir.file("gradle.properties").touch()
gradlePropertiesFile << """
foo=bar
"""
def snapshot = gradlePropertiesFile.snapshot()
executer.withGradleUserHomeDir(dotGradleDir)
executer.withArguments("--project-cache-dir", dotGradleDir.path)
then:
succeeds "init"
targetDir.file("gradlew").assertIsFile()
targetDir.file("settings.gradle").assertIsFile()
targetDir.file("build.gradle").assertIsFile()
targetDir.file(".gradle/gradle.properties").assertHasNotChangedSince(snapshot)
}
def "can create build in user home directory when user home directory has custom name"() {
when:
useTestDirectoryThatIsNotEmbeddedInAnotherBuild()
def dotGradleDir = targetDir.file('.guh')
dotGradleDir.mkdirs()
executer.withGradleUserHomeDir(dotGradleDir)
then:
succeeds "init"
targetDir.file("gradlew").assertIsFile()
targetDir.file("settings.gradle").assertIsFile()
targetDir.file("build.gradle").assertIsFile()
}
private ExecutionResult runInitWith(BuildInitDsl dsl, String... initOptions) {
def tasks = ['init', '--dsl', dsl.id]
tasks.addAll(initOptions)
run tasks
}
private static pomValuesUsed(ScriptDslFixture dslFixture) {
dslFixture.buildFile.assertContents(containsPomGroup(dslFixture))
dslFixture.buildFile.assertContents(containsPomVersion(dslFixture))
dslFixture.settingsFile.assertContents(containsPomArtifactId(dslFixture))
}
private static pomValuesNotUsed(ScriptDslFixture dslFixture) {
dslFixture.buildFile.assertContents(not(containsPomGroup(dslFixture)))
dslFixture.buildFile.assertContents(not(containsPomVersion(dslFixture)))
dslFixture.settingsFile.assertContents(not(containsPomArtifactId(dslFixture)))
}
private static Matcher<String> containsPomGroup(ScriptDslFixture dslFixture) {
dslFixture.containsStringAssignment('group', 'util')
}
private static Matcher<String> containsPomVersion(ScriptDslFixture dslFixture) {
dslFixture.containsStringAssignment('version', '2.5')
}
private static Matcher<String> containsPomArtifactId(ScriptDslFixture dslFixture) {
dslFixture.containsStringAssignment('rootProject.name', 'util')
}
}