-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
CliProjectGradleTest.java
444 lines (348 loc) · 21.7 KB
/
CliProjectGradleTest.java
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
package io.quarkus.cli;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.quarkus.cli.build.ExecuteUtil;
import io.quarkus.cli.build.GradleRunner;
import io.quarkus.devtools.commands.CreateProjectHelper;
import io.quarkus.devtools.testing.RegistryClientTestHelper;
import picocli.CommandLine;
/**
* Similar to CliProjectMavenTest ..
*/
public class CliProjectGradleTest {
static final Path testProjectRoot = Paths.get(System.getProperty("user.dir")).toAbsolutePath()
.resolve("target/test-project/");
static final Path workspaceRoot = testProjectRoot.resolve("CliProjectGradleTest");
static final Path wrapperRoot = testProjectRoot.resolve("gradle-wrapper");
Path project;
static File gradle;
@BeforeAll
public static void setupTestRegistry() {
RegistryClientTestHelper.enableRegistryClientTestConfig();
}
@AfterAll
public static void cleanupTestRegistry() {
RegistryClientTestHelper.disableRegistryClientTestConfig();
}
@BeforeEach
public void setupTestDirectories() throws Exception {
CliDriver.deleteDir(workspaceRoot);
project = workspaceRoot.resolve("code-with-quarkus");
}
@BeforeAll
static void startGradleDaemon() throws Exception {
CliDriver.deleteDir(wrapperRoot);
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle", "--verbose", "-e", "-B",
"--no-code",
"-o", testProjectRoot.toString(),
"gradle-wrapper");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
Assertions.assertTrue(result.stdout.contains("SUCCESS"),
"Expected confirmation that the project has been created." + result);
gradle = ExecuteUtil.findWrapper(wrapperRoot, GradleRunner.windowsWrapper, GradleRunner.otherWrapper);
List<String> args = new ArrayList<>();
args.add(gradle.getAbsolutePath());
args.add("--daemon");
CliDriver.preserveLocalRepoSettings(args);
result = CliDriver.executeArbitraryCommand(wrapperRoot, args.toArray(new String[0]));
Assertions.assertEquals(0, result.exitCode, "Gradle daemon should start properly");
}
@AfterAll
static void stopGradleDaemon() throws Exception {
if (gradle != null) {
List<String> args = new ArrayList<>();
args.add(gradle.getAbsolutePath());
args.add("--stop");
CliDriver.preserveLocalRepoSettings(args);
CliDriver.Result result = CliDriver.executeArbitraryCommand(wrapperRoot, args.toArray(new String[0]));
Assertions.assertEquals(0, result.exitCode, "Gradle daemon should stop properly");
}
}
@Test
public void testNoCode() throws Exception {
// Inspect the no-code project created to hold the gradle wrapper
Assertions.assertTrue(gradle.exists(), "Wrapper should exist");
Path packagePath = wrapperRoot.resolve("src/main/java/");
Assertions.assertTrue(packagePath.toFile().isDirectory(),
"Source directory should exist: " + packagePath.toAbsolutePath());
String[] files = packagePath.toFile().list();
Assertions.assertEquals(0, files.length,
"Source directory should be empty: " + Arrays.toString(files));
}
@Test
public void testCreateAppDefaults() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle", "--verbose", "-e", "-B");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
Assertions.assertTrue(result.stdout.contains("SUCCESS"),
"Expected confirmation that the project has been created." + result);
Assertions.assertTrue(project.resolve("gradlew").toFile().exists(),
"Wrapper should exist by default");
String buildGradleContent = validateBasicGradleGroovyIdentifiers(project, CreateProjectHelper.DEFAULT_GROUP_ID,
CreateProjectHelper.DEFAULT_ARTIFACT_ID,
CreateProjectHelper.DEFAULT_VERSION);
Assertions.assertTrue(buildGradleContent.contains("quarkus-resteasy"),
"build/gradle should contain quarkus-resteasy:\n" + buildGradleContent);
CliDriver.valdiateGeneratedSourcePackage(project, "org/acme");
CliDriver.invokeValidateBuild(project);
}
@Test
public void testCreateAppDefaultsWithKotlinDSL() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle-kotlin-dsl", "--verbose", "-e",
"-B");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
Assertions.assertTrue(result.stdout.contains("SUCCESS"),
"Expected confirmation that the project has been created." + result);
Assertions.assertTrue(project.resolve("gradlew").toFile().exists(),
"Wrapper should exist by default");
String buildGradleContent = validateBasicGradleKotlinIdentifiers(project, CreateProjectHelper.DEFAULT_GROUP_ID,
CreateProjectHelper.DEFAULT_ARTIFACT_ID,
CreateProjectHelper.DEFAULT_VERSION);
Assertions.assertTrue(buildGradleContent.contains("quarkus-resteasy"),
"build/gradle should contain quarkus-resteasy:\n" + buildGradleContent);
Path packagePath = wrapperRoot.resolve("src/main/java/");
Assertions.assertTrue(packagePath.toFile().isDirectory(),
"Java Source directory should exist: " + packagePath.toAbsolutePath());
CliDriver.valdiateGeneratedSourcePackage(project, "org/acme");
CliDriver.invokeValidateBuild(project);
}
@Test
public void testCreateAppOverrides() throws Exception {
Path nested = workspaceRoot.resolve("cli-nested");
project = nested.resolve("my-project");
List<String> configs = Arrays.asList("custom.app.config1=val1",
"custom.app.config2=val2", "lib.config=val3");
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle", "--verbose", "-e", "-B",
"--package-name=custom.pkg",
"--output-directory=" + nested,
"--app-config=" + String.join(",", configs),
"-x resteasy-reactive",
"silly:my-project:0.1.0");
// TODO: would love a test that doesn't use a wrapper, but CI path..
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
Assertions.assertTrue(result.stdout.contains("SUCCESS"),
"Expected confirmation that the project has been created." + result);
Assertions.assertTrue(project.resolve("gradlew").toFile().exists(),
"Wrapper should exist by default");
String buildGradleContent = validateBasicGradleGroovyIdentifiers(project, "silly", "my-project", "0.1.0");
Assertions.assertTrue(buildGradleContent.contains("quarkus-resteasy-reactive"),
"build.gradle should contain quarkus-resteasy-reactive:\n" + buildGradleContent);
CliDriver.valdiateGeneratedSourcePackage(project, "custom/pkg");
CliDriver.validateApplicationProperties(project, configs);
result = CliDriver.invokeValidateDryRunBuild(project);
Assertions.assertTrue(result.stdout.contains("-Dproperty=value1 -Dproperty2=value2"),
"result should contain '-Dproperty=value1 -Dproperty2=value2':\n" + result.stdout);
CliDriver.invokeValidateBuild(project);
}
@Test
public void testCreateCliDefaults() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "cli", "--gradle", "--verbose", "-e", "-B");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
Assertions.assertTrue(result.stdout.contains("SUCCESS"),
"Expected confirmation that the project has been created." + result);
Assertions.assertTrue(project.resolve("gradlew").toFile().exists(),
"Wrapper should exist by default");
String buildGradleContent = validateBasicGradleGroovyIdentifiers(project, CreateProjectHelper.DEFAULT_GROUP_ID,
CreateProjectHelper.DEFAULT_ARTIFACT_ID,
CreateProjectHelper.DEFAULT_VERSION);
Assertions.assertFalse(buildGradleContent.contains("quarkus-resteasy"),
"build/gradle should not contain quarkus-resteasy:\n" + buildGradleContent);
Assertions.assertTrue(buildGradleContent.contains("quarkus-picocli"),
"build/gradle should contain quarkus-picocli:\n" + buildGradleContent);
CliDriver.valdiateGeneratedSourcePackage(project, "org/acme");
CliDriver.invokeValidateBuild(project);
}
@Test
public void testExtensionList() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle", "--verbose", "-e", "-B");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
Path buildGradle = project.resolve("build.gradle");
String buildGradleContent = CliDriver.readFileAsString(project, buildGradle);
Assertions.assertFalse(buildGradleContent.contains("quarkus-qute"),
"Dependencies should not contain qute extension by default. Found:\n" + buildGradleContent);
CliDriver.invokeExtensionAddQute(project, buildGradle);
CliDriver.invokeExtensionAddRedundantQute(project);
CliDriver.invokeExtensionListInstallable(project);
CliDriver.invokeExtensionAddMultiple(project, buildGradle);
CliDriver.invokeExtensionRemoveQute(project, buildGradle);
CliDriver.invokeExtensionRemoveMultiple(project, buildGradle);
CliDriver.invokeExtensionListInstallableSearch(project);
CliDriver.invokeExtensionListFormatting(project);
// TODO: Maven and Gradle give different return codes
result = CliDriver.invokeExtensionRemoveNonexistent(project);
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code. Result:\n" + result);
}
@Test
public void testBuildOptions() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle", "-e", "-B", "--verbose");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
// 1 --clean --tests --native --offline
result = CliDriver.execute(project, "build", "-e", "-B", "--dry-run",
"--clean", "--tests", "--native", "--offline");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code. Result:\n" + result);
Assertions.assertTrue(result.stdout.contains(" clean"),
"gradle command should specify 'clean'\n" + result);
Assertions.assertFalse(result.stdout.contains("-x test"),
"gradle command should not specify '-x test'\n" + result);
Assertions.assertTrue(result.stdout.contains("-Dquarkus.package.type=native"),
"gradle command should specify -Dquarkus.package.type=native\n" + result);
Assertions.assertTrue(result.stdout.contains("--offline"),
"gradle command should specify --offline\n" + result);
// 2 --no-clean --no-tests
result = CliDriver.execute(project, "build", "-e", "-B", "--dry-run",
"--no-clean", "--no-tests");
Assertions.assertFalse(result.stdout.contains(" clean"),
"gradle command should not specify 'clean'\n" + result);
Assertions.assertTrue(result.stdout.contains("-x test"),
"gradle command should specify '-x test'\n" + result);
Assertions.assertFalse(result.stdout.contains("native"),
"gradle command should not specify native\n" + result);
Assertions.assertFalse(result.stdout.contains("offline"),
"gradle command should not specify offline\n" + result);
}
@Test
public void testDevOptions() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle", "-e", "-B", "--verbose");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
// 1 --clean --tests --suspend --offline
result = CliDriver.execute(project, "dev", "-e", "--dry-run",
"--clean", "--tests", "--debug", "--suspend", "--debug-mode=listen", "--offline");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code. Result:\n" + result);
Assertions.assertTrue(result.stdout.contains("GRADLE"),
"gradle command should specify 'GRADLE'\n" + result);
Assertions.assertTrue(result.stdout.contains(" clean"),
"gradle command should specify 'clean'\n" + result);
Assertions.assertFalse(result.stdout.contains("-x test"),
"gradle command should not specify '-x test'\n" + result);
Assertions.assertFalse(result.stdout.contains("-Ddebug"),
"gradle command should not specify '-Ddebug'\n" + result);
Assertions.assertTrue(result.stdout.contains("-Dsuspend"),
"gradle command should specify '-Dsuspend'\n" + result);
Assertions.assertTrue(result.stdout.contains("--offline"),
"gradle command should specify --offline\n" + result);
// 2 --no-clean --no-tests --no-debug
result = CliDriver.execute(project, "dev", "-e", "--dry-run",
"--no-clean", "--no-tests", "--no-debug");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code. Result:\n" + result);
Assertions.assertTrue(result.stdout.contains("GRADLE"),
"gradle command should specify 'GRADLE'\n" + result);
Assertions.assertFalse(result.stdout.contains(" clean"),
"gradle command should not specify 'clean'\n" + result);
Assertions.assertTrue(result.stdout.contains("-x test"),
"gradle command should specify '-x test'\n" + result);
Assertions.assertTrue(result.stdout.contains("-Ddebug=false"),
"gradle command should specify '-Ddebug=false'\n" + result);
Assertions.assertFalse(result.stdout.contains("-Dsuspend"),
"gradle command should not specify '-Dsuspend'\n" + result);
// 3 --no-suspend --debug-host=0.0.0.0 --debug-port=8008 --debug-mode=connect -- arg1 arg2
result = CliDriver.execute(project, "dev", "-e", "--dry-run",
"--no-suspend", "--debug-host=0.0.0.0", "--debug-port=8008", "--debug-mode=connect", "--", "arg1", "arg2");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode,
"Expected OK return code. Result:\n" + result);
Assertions.assertTrue(result.stdout.contains("GRADLE"),
"gradle command should specify 'GRADLE'\n" + result);
Assertions.assertTrue(
result.stdout.contains("-DdebugHost=0.0.0.0 -Ddebug=client -DdebugPort=8008"),
"gradle command should specify -DdebugHost=0.0.0.0 -Ddebug=client -DdebugPort=8008\n" + result);
Assertions.assertFalse(result.stdout.contains("-Dsuspend"),
"gradle command should not specify '-Dsuspend'\n" + result);
Assertions.assertTrue(result.stdout.contains("-Dquarkus.args='arg1 arg2'"),
"gradle command should not specify -Dquarkus.args='arg1 arg2'\n" + result);
}
@Test
public void testCreateArgPassthrough() throws Exception {
Path nested = workspaceRoot.resolve("cli-nested");
project = nested.resolve("my-project");
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "--gradle",
"--verbose", "-e", "-B",
"--dryrun", "--no-wrapper", "--package-name=custom.pkg",
"--output-directory=" + nested,
"silly:my-project:0.1.0");
// We don't need to retest this, just need to make sure all the arguments were passed through
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
Assertions.assertTrue(result.stdout.contains("Creating an app"),
"Should contain 'Creating an app', found: " + result.stdout);
Assertions.assertTrue(result.stdout.contains("GRADLE"),
"Should contain MAVEN, found: " + result.stdout);
// strip spaces to avoid fighting with column whitespace
String noSpaces = result.stdout.replaceAll("[\\s\\p{Z}]", "");
Assertions.assertTrue(noSpaces.contains("Omitbuildtoolwrappertrue"),
"Should contain 'Omit build tool wrapper true', found: " + result.stdout);
Assertions.assertTrue(noSpaces.contains("PackageNamecustom.pkg"),
"Should contain 'Package Name custom.pkg', found: " + result.stdout);
Assertions.assertTrue(noSpaces.contains("ProjectArtifactIdmy-project"),
"Output should contain 'Project ArtifactId my-project', found: " + result.stdout);
Assertions.assertTrue(noSpaces.contains("ProjectGroupIdsilly"),
"Output should contain 'Project GroupId silly', found: " + result.stdout);
}
@Test
public void testCreateArgJava11() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle",
"-e", "-B", "--verbose",
"--java", "11");
// We don't need to retest this, just need to make sure all the arguments were passed through
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
Path buildGradle = project.resolve("build.gradle");
String buildGradleContent = CliDriver.readFileAsString(project, buildGradle);
Assertions.assertTrue(buildGradleContent.contains("sourceCompatibility = JavaVersion.VERSION_11"),
"Java 11 should be used when specified. Found:\n" + buildGradle);
}
@Test
public void testCreateArgJava17() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "--gradle",
"-e", "-B", "--verbose",
"--java", "17");
// We don't need to retest this, just need to make sure all the arguments were passed through
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);
Path buildGradle = project.resolve("build.gradle");
String buildGradleContent = CliDriver.readFileAsString(project, buildGradle);
Assertions.assertTrue(buildGradleContent.contains("sourceCompatibility = JavaVersion.VERSION_17"),
"Java 17 should be used when specified. Found:\n" + buildGradleContent);
}
String validateBasicGradleGroovyIdentifiers(Path project, String group, String artifact, String version) throws Exception {
Path buildGradle = project.resolve("build.gradle");
Assertions.assertTrue(buildGradle.toFile().exists(),
"build.gradle should exist: " + buildGradle.toAbsolutePath().toString());
String buildContent = CliDriver.readFileAsString(project, buildGradle);
Assertions.assertTrue(buildContent.contains("group '" + group + "'"),
"build.gradle should include the group id:\n" + buildContent);
Assertions.assertTrue(buildContent.contains("version '" + version + "'"),
"build.gradle should include the version:\n" + buildContent);
Path settings = project.resolve("settings.gradle");
Assertions.assertTrue(settings.toFile().exists(),
"settings.gradle should exist: " + settings.toAbsolutePath().toString());
String settingsContent = CliDriver.readFileAsString(project, settings);
Assertions.assertTrue(settingsContent.contains(artifact),
"settings.gradle should include the artifact id:\n" + settingsContent);
return buildContent;
}
String validateBasicGradleKotlinIdentifiers(Path project, String group, String artifact, String version) throws Exception {
Path buildGradle = project.resolve("build.gradle.kts");
Assertions.assertTrue(buildGradle.toFile().exists(),
"build.gradle.kts should exist: " + buildGradle.toAbsolutePath().toString());
String buildContent = CliDriver.readFileAsString(project, buildGradle);
Assertions.assertTrue(buildContent.contains("group = \"" + group + "\""),
"build.gradle.kts should include the group id:\n" + buildContent);
Assertions.assertTrue(buildContent.contains("version = \"" + version + "\""),
"build.gradle.kts should include the version:\n" + buildContent);
Path settings = project.resolve("settings.gradle.kts");
Assertions.assertTrue(settings.toFile().exists(),
"settings.gradle.kts should exist: " + settings.toAbsolutePath().toString());
String settingsContent = CliDriver.readFileAsString(project, settings);
Assertions.assertTrue(settingsContent.contains(artifact),
"settings.gradle.kts should include the artifact id:\n" + settingsContent);
return buildContent;
}
}