From baad1c9ab972c5078b9fdf86de9563f13e2e543c Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Thu, 10 Aug 2023 14:45:01 +0200 Subject: [PATCH 1/2] CLI: test command - add an option to run the test suite and exit - resolves #35290 --- .../src/main/java/io/quarkus/cli/Test.java | 26 ++++++++++++++++--- .../quarkus/cli/build/BuildSystemRunner.java | 2 ++ .../io/quarkus/cli/build/GradleRunner.java | 5 ++++ .../io/quarkus/cli/build/JBangRunner.java | 5 ++++ .../io/quarkus/cli/build/MavenRunner.java | 5 ++++ .../io/quarkus/cli/CliProjectGradleTest.java | 14 ++++++++++ .../io/quarkus/cli/CliProjectMavenTest.java | 8 ++++++ 7 files changed, 62 insertions(+), 3 deletions(-) diff --git a/devtools/cli/src/main/java/io/quarkus/cli/Test.java b/devtools/cli/src/main/java/io/quarkus/cli/Test.java index 4b4ce81792038..f8ef2bc24e532 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/Test.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/Test.java @@ -9,8 +9,11 @@ import io.quarkus.cli.build.BaseBuildCommand; import io.quarkus.cli.build.BuildSystemRunner; +import io.quarkus.cli.build.BuildSystemRunner.BuildCommandArgs; +import io.quarkus.cli.common.BuildOptions; import io.quarkus.cli.common.DebugOptions; import io.quarkus.cli.common.DevOptions; +import io.quarkus.cli.common.RunModeOption; import io.quarkus.devtools.project.BuildTool; import picocli.CommandLine; import picocli.CommandLine.Parameters; @@ -24,6 +27,9 @@ public class Test extends BaseBuildCommand implements Callable { @CommandLine.ArgGroup(order = 3, exclusive = false, validate = true, heading = "%nDebug options:%n") DebugOptions debugOptions = new DebugOptions(); + @CommandLine.Option(names = "--once", description = "Run the test suite with continuous mode disabled.") + boolean runOnce = false; + @Parameters(description = "Parameters passed to the application.") List params = new ArrayList<>(); @@ -34,11 +40,25 @@ public Integer call() { output.throwIfUnmatchedArguments(spec.commandLine()); BuildSystemRunner runner = getRunner(); + + if (runOnce) { + BuildOptions buildOptions = new BuildOptions(); + buildOptions.clean = testOptions.clean; + buildOptions.offline = testOptions.offline; + buildOptions.skipTests = !testOptions.runTests; + BuildCommandArgs commandArgs = runner.prepareTest(buildOptions, new RunModeOption(), params); + if (testOptions.isDryRun()) { + dryRunTest(spec.commandLine().getHelp(), runner.getBuildTool(), commandArgs, false); + return CommandLine.ExitCode.OK; + } + return runner.run(commandArgs); + } + List> commandArgs = runner.prepareDevTestMode( false, testOptions, debugOptions, params); if (testOptions.isDryRun()) { - dryRunTest(spec.commandLine().getHelp(), runner.getBuildTool(), commandArgs.iterator().next().get()); + dryRunTest(spec.commandLine().getHelp(), runner.getBuildTool(), commandArgs.iterator().next().get(), true); return CommandLine.ExitCode.OK; } int ret = 1; @@ -55,8 +75,8 @@ public Integer call() { } } - void dryRunTest(CommandLine.Help help, BuildTool buildTool, BuildSystemRunner.BuildCommandArgs args) { - output.printText("\nRun current project in test mode\n", + void dryRunTest(CommandLine.Help help, BuildTool buildTool, BuildSystemRunner.BuildCommandArgs args, boolean isContinuous) { + output.printText("\nRun current project in" + (isContinuous ? " continuous" : "") + " test mode\n", "\t" + projectRoot().toString()); Map dryRunOutput = new TreeMap<>(); dryRunOutput.put("Build tool", buildTool.name()); diff --git a/devtools/cli/src/main/java/io/quarkus/cli/build/BuildSystemRunner.java b/devtools/cli/src/main/java/io/quarkus/cli/build/BuildSystemRunner.java index 278698d6b7f6a..c5d02826af1fd 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/build/BuildSystemRunner.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/build/BuildSystemRunner.java @@ -111,6 +111,8 @@ Integer updateProject(TargetQuarkusVersionGroup targetQuarkusVersion, RewriteGro BuildCommandArgs prepareBuild(BuildOptions buildOptions, RunModeOption runMode, List params); + BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params); + List> prepareDevTestMode(boolean devMode, DevOptions commonOptions, DebugOptions debugOptions, List params); diff --git a/devtools/cli/src/main/java/io/quarkus/cli/build/GradleRunner.java b/devtools/cli/src/main/java/io/quarkus/cli/build/GradleRunner.java index 9611986e102a0..f5a1f21ce47eb 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/build/GradleRunner.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/build/GradleRunner.java @@ -217,6 +217,11 @@ public BuildCommandArgs prepareAction(String action, BuildOptions buildOptions, return prependExecutable(args); } + @Override + public BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params) { + return prepareAction("test", buildOptions, runMode, params); + } + @Override public List> prepareDevTestMode(boolean devMode, DevOptions commonOptions, DebugOptions debugOptions, List params) { diff --git a/devtools/cli/src/main/java/io/quarkus/cli/build/JBangRunner.java b/devtools/cli/src/main/java/io/quarkus/cli/build/JBangRunner.java index 704d49204812e..95531c738d1f8 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/build/JBangRunner.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/build/JBangRunner.java @@ -113,6 +113,11 @@ public BuildCommandArgs prepareBuild(BuildOptions buildOptions, RunModeOption ru return prepareAction("build", buildOptions, runMode, params); } + @Override + public BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params) { + throw new UnsupportedOperationException("Not there yet. ;)"); + } + @Override public List> prepareDevTestMode(boolean devMode, DevOptions commonOptions, DebugOptions debugOptions, diff --git a/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java b/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java index e0b511b6d99d0..d8420e2a6bf61 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java @@ -223,6 +223,11 @@ public BuildCommandArgs prepareAction(String action, BuildOptions buildOptions, return prependExecutable(args); } + @Override + public BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params) { + return prepareAction("test", buildOptions, runMode, params); + } + @Override public List> prepareDevTestMode(boolean devMode, DevOptions commonOptions, DebugOptions debugOptions, List params) { diff --git a/devtools/cli/src/test/java/io/quarkus/cli/CliProjectGradleTest.java b/devtools/cli/src/test/java/io/quarkus/cli/CliProjectGradleTest.java index 0afd91e2f14d6..6d8fa3566543a 100644 --- a/devtools/cli/src/test/java/io/quarkus/cli/CliProjectGradleTest.java +++ b/devtools/cli/src/test/java/io/quarkus/cli/CliProjectGradleTest.java @@ -350,6 +350,20 @@ public void testDevOptions() throws Exception { Assertions.assertTrue(result.stdout.contains("-Dquarkus.args='arg1 arg2'"), "gradle command should not specify -Dquarkus.args='arg1 arg2'\n" + result); + + // 4 TEST MODE: test --clean --debug --suspend --offline + result = CliDriver.execute(project, "test", "-e", "--dry-run", + "--clean", "--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("Run current project in continuous test mode"), result.toString()); + + // 5 TEST MODE - run once: test --once --offline + result = CliDriver.execute(project, "test", "-e", "--dry-run", + "--once", "--offline"); + Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, + "Expected OK return code. Result:\n" + result); + Assertions.assertTrue(result.stdout.contains("Run current project in test mode"), result.toString()); } @Test diff --git a/devtools/cli/src/test/java/io/quarkus/cli/CliProjectMavenTest.java b/devtools/cli/src/test/java/io/quarkus/cli/CliProjectMavenTest.java index ba57e5ff2a1d8..c55280aa314f2 100644 --- a/devtools/cli/src/test/java/io/quarkus/cli/CliProjectMavenTest.java +++ b/devtools/cli/src/test/java/io/quarkus/cli/CliProjectMavenTest.java @@ -279,6 +279,14 @@ public void testDevTestOptions() throws Exception { "--clean", "--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("Run current project in continuous test mode"), result.toString()); + + // 5 TEST MODE - run once: test --once --offline + result = CliDriver.execute(project, "test", "-e", "--dry-run", + "--once", "--offline"); + Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, + "Expected OK return code. Result:\n" + result); + Assertions.assertTrue(result.stdout.contains("Run current project in test mode"), result.toString()); } @Test From 43abc6ca129ed119f36b084f7070b8216608944e Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Thu, 10 Aug 2023 15:26:34 +0200 Subject: [PATCH 2/2] CLI: test command - add an option to filter the tests - resolves #35292 Co-authored-by: Erin Schnabel --- devtools/cli/src/main/java/io/quarkus/cli/Test.java | 10 +++++++++- .../java/io/quarkus/cli/build/BuildSystemRunner.java | 2 +- .../main/java/io/quarkus/cli/build/GradleRunner.java | 5 ++++- .../main/java/io/quarkus/cli/build/JBangRunner.java | 2 +- .../main/java/io/quarkus/cli/build/MavenRunner.java | 5 ++++- .../test/java/io/quarkus/cli/CliProjectGradleTest.java | 6 ++++-- .../test/java/io/quarkus/cli/CliProjectMavenTest.java | 6 ++++-- 7 files changed, 27 insertions(+), 9 deletions(-) diff --git a/devtools/cli/src/main/java/io/quarkus/cli/Test.java b/devtools/cli/src/main/java/io/quarkus/cli/Test.java index f8ef2bc24e532..ac11a7b36d554 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/Test.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/Test.java @@ -30,6 +30,11 @@ public class Test extends BaseBuildCommand implements Callable { @CommandLine.Option(names = "--once", description = "Run the test suite with continuous mode disabled.") boolean runOnce = false; + @CommandLine.Option(names = "--filter", description = { "Run a subset of the test suite that matches the given filter.", + "If continuous testing is enabled then the value is a regular expression that is matched against the test class name.", + "If continuous testing is disabled then the value is passed as-is to the underlying build tool." }) + String filter; + @Parameters(description = "Parameters passed to the application.") List params = new ArrayList<>(); @@ -46,7 +51,7 @@ public Integer call() { buildOptions.clean = testOptions.clean; buildOptions.offline = testOptions.offline; buildOptions.skipTests = !testOptions.runTests; - BuildCommandArgs commandArgs = runner.prepareTest(buildOptions, new RunModeOption(), params); + BuildCommandArgs commandArgs = runner.prepareTest(buildOptions, new RunModeOption(), params, filter); if (testOptions.isDryRun()) { dryRunTest(spec.commandLine().getHelp(), runner.getBuildTool(), commandArgs, false); return CommandLine.ExitCode.OK; @@ -54,6 +59,9 @@ public Integer call() { return runner.run(commandArgs); } + if (filter != null) { + params.add("-Dquarkus.test.include-pattern=" + filter); + } List> commandArgs = runner.prepareDevTestMode( false, testOptions, debugOptions, params); diff --git a/devtools/cli/src/main/java/io/quarkus/cli/build/BuildSystemRunner.java b/devtools/cli/src/main/java/io/quarkus/cli/build/BuildSystemRunner.java index c5d02826af1fd..86fb31863deab 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/build/BuildSystemRunner.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/build/BuildSystemRunner.java @@ -111,7 +111,7 @@ Integer updateProject(TargetQuarkusVersionGroup targetQuarkusVersion, RewriteGro BuildCommandArgs prepareBuild(BuildOptions buildOptions, RunModeOption runMode, List params); - BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params); + BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params, String filter); List> prepareDevTestMode(boolean devMode, DevOptions commonOptions, DebugOptions debugOptions, List params); diff --git a/devtools/cli/src/main/java/io/quarkus/cli/build/GradleRunner.java b/devtools/cli/src/main/java/io/quarkus/cli/build/GradleRunner.java index f5a1f21ce47eb..cd7aff6f5b4c0 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/build/GradleRunner.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/build/GradleRunner.java @@ -218,7 +218,10 @@ public BuildCommandArgs prepareAction(String action, BuildOptions buildOptions, } @Override - public BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params) { + public BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params, String filter) { + if (filter != null) { + params.add("--tests " + filter); + } return prepareAction("test", buildOptions, runMode, params); } diff --git a/devtools/cli/src/main/java/io/quarkus/cli/build/JBangRunner.java b/devtools/cli/src/main/java/io/quarkus/cli/build/JBangRunner.java index 95531c738d1f8..b647c3adb8370 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/build/JBangRunner.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/build/JBangRunner.java @@ -114,7 +114,7 @@ public BuildCommandArgs prepareBuild(BuildOptions buildOptions, RunModeOption ru } @Override - public BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params) { + public BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params, String filter) { throw new UnsupportedOperationException("Not there yet. ;)"); } diff --git a/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java b/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java index d8420e2a6bf61..632c68c8135ff 100644 --- a/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java +++ b/devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java @@ -224,7 +224,10 @@ public BuildCommandArgs prepareAction(String action, BuildOptions buildOptions, } @Override - public BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params) { + public BuildCommandArgs prepareTest(BuildOptions buildOptions, RunModeOption runMode, List params, String filter) { + if (filter != null) { + params.add("-Dtest=" + filter); + } return prepareAction("test", buildOptions, runMode, params); } diff --git a/devtools/cli/src/test/java/io/quarkus/cli/CliProjectGradleTest.java b/devtools/cli/src/test/java/io/quarkus/cli/CliProjectGradleTest.java index 6d8fa3566543a..a0ae6007a5de8 100644 --- a/devtools/cli/src/test/java/io/quarkus/cli/CliProjectGradleTest.java +++ b/devtools/cli/src/test/java/io/quarkus/cli/CliProjectGradleTest.java @@ -353,17 +353,19 @@ public void testDevOptions() throws Exception { // 4 TEST MODE: test --clean --debug --suspend --offline result = CliDriver.execute(project, "test", "-e", "--dry-run", - "--clean", "--debug", "--suspend", "--debug-mode=listen", "--offline"); + "--clean", "--debug", "--suspend", "--debug-mode=listen", "--offline", "--filter=FooTest"); Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code. Result:\n" + result); Assertions.assertTrue(result.stdout.contains("Run current project in continuous test mode"), result.toString()); + Assertions.assertTrue(result.stdout.contains("-Dquarkus.test.include-pattern=FooTest"), result.toString()); // 5 TEST MODE - run once: test --once --offline result = CliDriver.execute(project, "test", "-e", "--dry-run", - "--once", "--offline"); + "--once", "--offline", "--filter=FooTest"); Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code. Result:\n" + result); Assertions.assertTrue(result.stdout.contains("Run current project in test mode"), result.toString()); + Assertions.assertTrue(result.stdout.contains("--tests FooTest"), result.toString()); } @Test diff --git a/devtools/cli/src/test/java/io/quarkus/cli/CliProjectMavenTest.java b/devtools/cli/src/test/java/io/quarkus/cli/CliProjectMavenTest.java index c55280aa314f2..d8782c4f1b0d2 100644 --- a/devtools/cli/src/test/java/io/quarkus/cli/CliProjectMavenTest.java +++ b/devtools/cli/src/test/java/io/quarkus/cli/CliProjectMavenTest.java @@ -276,17 +276,19 @@ public void testDevTestOptions() throws Exception { // 4 TEST MODE: test --clean --debug --suspend --offline result = CliDriver.execute(project, "test", "-e", "--dry-run", - "--clean", "--debug", "--suspend", "--debug-mode=listen", "--offline"); + "--clean", "--debug", "--suspend", "--debug-mode=listen", "--offline", "--filter=FooTest"); Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code. Result:\n" + result); Assertions.assertTrue(result.stdout.contains("Run current project in continuous test mode"), result.toString()); + Assertions.assertTrue(result.stdout.contains("-Dquarkus.test.include-pattern=FooTest"), result.toString()); // 5 TEST MODE - run once: test --once --offline result = CliDriver.execute(project, "test", "-e", "--dry-run", - "--once", "--offline"); + "--once", "--offline", "--filter=FooTest"); Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code. Result:\n" + result); Assertions.assertTrue(result.stdout.contains("Run current project in test mode"), result.toString()); + Assertions.assertTrue(result.stdout.contains("-Dtest=FooTest"), result.toString()); } @Test