Skip to content

Commit

Permalink
Merge pull request quarkusio#28838 from ebullient/cli-test
Browse files Browse the repository at this point in the history
Support continuous test in cli (test command)
  • Loading branch information
geoand authored Oct 26, 2022
2 parents 6b60931 + 12e179b commit 5614878
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 59 deletions.
4 changes: 2 additions & 2 deletions devtools/cli/src/main/java/io/quarkus/cli/Dev.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public Integer call() {
output.throwIfUnmatchedArguments(spec.commandLine());

BuildSystemRunner runner = getRunner();
List<Supplier<BuildSystemRunner.BuildCommandArgs>> commandArgs = runner.prepareDevMode(devOptions, debugOptions,
params);
List<Supplier<BuildSystemRunner.BuildCommandArgs>> commandArgs = runner.prepareDevTestMode(
true, devOptions, debugOptions, params);

if (devOptions.isDryRun()) {
dryRunDev(spec.commandLine().getHelp(), runner.getBuildTool(), commandArgs.iterator().next().get());
Expand Down
3 changes: 2 additions & 1 deletion devtools/cli/src/main/java/io/quarkus/cli/QuarkusCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
import picocli.CommandLine.UnmatchedArgumentException;

@CommandLine.Command(name = "quarkus", subcommands = {
Create.class, Build.class, Dev.class, ProjectExtensions.class, Registry.class, Info.class, Update.class, Version.class,
Create.class, Build.class, Dev.class, Test.class, ProjectExtensions.class, Registry.class, Info.class, Update.class,
Version.class,
Completion.class }, scope = ScopeType.INHERIT, sortOptions = false, showDefaultValues = true, versionProvider = Version.class, subcommandsRepeatable = false, mixinStandardHelpOptions = false, commandListHeading = "%nCommands:%n", synopsisHeading = "%nUsage: ", optionListHeading = "Options:%n", headerHeading = "%n", parameterListHeading = "%n")
public class QuarkusCli implements QuarkusApplication, Callable<Integer> {
static {
Expand Down
77 changes: 77 additions & 0 deletions devtools/cli/src/main/java/io/quarkus/cli/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.quarkus.cli;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import java.util.function.Supplier;

import io.quarkus.cli.build.BaseBuildCommand;
import io.quarkus.cli.build.BuildSystemRunner;
import io.quarkus.cli.common.DebugOptions;
import io.quarkus.cli.common.DevOptions;
import io.quarkus.devtools.project.BuildTool;
import picocli.CommandLine;
import picocli.CommandLine.Parameters;

@CommandLine.Command(name = "test", showEndOfOptionsDelimiterInUsageHelp = true, header = "Run the current project in continuous testing mode.")
public class Test extends BaseBuildCommand implements Callable<Integer> {

@CommandLine.ArgGroup(order = 1, exclusive = false, heading = "%nContinuous Test Mode options:%n")
DevOptions testOptions = new DevOptions();

@CommandLine.ArgGroup(order = 3, exclusive = false, validate = true, heading = "%nDebug options:%n")
DebugOptions debugOptions = new DebugOptions();

@Parameters(description = "Parameters passed to the application.")
List<String> params = new ArrayList<>();

@Override
public Integer call() {
try {
output.debug("Run project in test mode with initial parameters: %s", this);
output.throwIfUnmatchedArguments(spec.commandLine());

BuildSystemRunner runner = getRunner();
List<Supplier<BuildSystemRunner.BuildCommandArgs>> commandArgs = runner.prepareDevTestMode(
false, testOptions, debugOptions, params);

if (testOptions.isDryRun()) {
dryRunTest(spec.commandLine().getHelp(), runner.getBuildTool(), commandArgs.iterator().next().get());
return CommandLine.ExitCode.OK;
}
int ret = 1;
for (Supplier<BuildSystemRunner.BuildCommandArgs> i : commandArgs) {
ret = runner.run(i.get());
if (ret != 0) {
return ret;
}
}
return ret;
} catch (Exception e) {
return output.handleCommandException(e,
"Unable to launch project in test mode: " + e.getMessage());
}
}

void dryRunTest(CommandLine.Help help, BuildTool buildTool, BuildSystemRunner.BuildCommandArgs args) {
output.printText("\nRun current project in test mode\n",
"\t" + projectRoot().toString());
Map<String, String> dryRunOutput = new TreeMap<>();
dryRunOutput.put("Build tool", buildTool.name());
output.info(help.createTextTable(dryRunOutput).toString());

output.printText("\nCommand line:\n",
args.showCommand());
}

@Override
public String toString() {
return "Test [debugOptions=" + debugOptions
+ ", testOptions=" + testOptions
+ ", properties=" + propertiesOptions.properties
+ ", output=" + output
+ ", params=" + params + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ Integer listExtensions(RunModeOption runMode, ListFormatOptions format, boolean

BuildCommandArgs prepareBuild(BuildOptions buildOptions, RunModeOption runMode, List<String> params);

List<Supplier<BuildCommandArgs>> prepareDevMode(DevOptions devOptions, DebugOptions debugOptions,
List<String> params);
List<Supplier<BuildCommandArgs>> prepareDevTestMode(boolean devMode, DevOptions commonOptions,
DebugOptions debugOptions, List<String> params);

Path getProjectRoot();

Expand Down
44 changes: 16 additions & 28 deletions devtools/cli/src/main/java/io/quarkus/cli/build/GradleRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import io.quarkus.cli.common.BuildOptions;
import io.quarkus.cli.common.CategoryListFormatOptions;
Expand Down Expand Up @@ -178,23 +177,20 @@ public BuildCommandArgs prepareBuild(BuildOptions buildOptions, RunModeOption ru
}

@Override
public List<Supplier<BuildCommandArgs>> prepareDevMode(DevOptions devOptions, DebugOptions debugOptions,
List<String> params) {
public List<Supplier<BuildCommandArgs>> prepareDevTestMode(boolean devMode, DevOptions commonOptions,
DebugOptions debugOptions, List<String> params) {
ArrayDeque<String> args = new ArrayDeque<>();
List<String> jvmArgs = new ArrayList<>();

setGradleProperties(args, false);

if (devOptions.clean) {
if (commonOptions.clean) {
args.add("clean");
}
args.add("quarkusDev");

if (devOptions.skipTests()) { // TODO: does this make sense for dev mode?
setSkipTests(args);
}
args.add(devMode ? "quarkusDev" : "quarkusTest");

if (devOptions.offline) {
if (commonOptions.offline) {
args.add("--offline");
}

Expand All @@ -206,27 +202,19 @@ public List<Supplier<BuildCommandArgs>> prepareDevMode(DevOptions devOptions, De

try {
Path outputFile = Files.createTempFile("quarkus-dev", ".txt");
args.add("-Dio.quarkus.devmode-args=" + outputFile.toAbsolutePath().toString());
if (devMode) {
args.add("-Dio.quarkus.devmode-args=" + outputFile.toAbsolutePath());
}

BuildCommandArgs buildCommandArgs = prependExecutable(args);
return Arrays.asList(new Supplier<BuildCommandArgs>() {
@Override
public BuildCommandArgs get() {
return buildCommandArgs;
}
}, new Supplier<BuildCommandArgs>() {
@Override
public BuildCommandArgs get() {
try {
List<String> lines = Files.readAllLines(outputFile).stream().filter(s -> !s.isBlank())
.collect(Collectors.toList());
BuildCommandArgs cmd = new BuildCommandArgs();
cmd.arguments = lines.toArray(new String[0]);
cmd.targetDirectory = buildCommandArgs.targetDirectory;
return cmd;
} catch (IOException e) {
throw new RuntimeException(e);
}
return Arrays.asList(() -> buildCommandArgs, () -> {
try {
BuildCommandArgs cmd = new BuildCommandArgs();
cmd.arguments = Files.readAllLines(outputFile).stream().filter(s -> !s.isBlank()).toArray(String[]::new);
cmd.targetDirectory = buildCommandArgs.targetDirectory;
return cmd;
} catch (IOException e) {
throw new RuntimeException(e);
}
});
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public BuildCommandArgs prepareBuild(BuildOptions buildOptions, RunModeOption ru
}

@Override
public List<Supplier<BuildCommandArgs>> prepareDevMode(DevOptions devOptions, DebugOptions debugOptions,
public List<Supplier<BuildCommandArgs>> prepareDevTestMode(boolean devMode, DevOptions commonOptions,
DebugOptions debugOptions,
List<String> params) {
throw new UnsupportedOperationException("Not there yet. ;)");
}
Expand Down
21 changes: 6 additions & 15 deletions devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,23 +191,19 @@ public BuildCommandArgs prepareBuild(BuildOptions buildOptions, RunModeOption ru
}

@Override
public List<Supplier<BuildCommandArgs>> prepareDevMode(DevOptions devOptions, DebugOptions debugOptions,
List<String> params) {
public List<Supplier<BuildCommandArgs>> prepareDevTestMode(boolean devMode, DevOptions commonOptions,
DebugOptions debugOptions, List<String> params) {
ArrayDeque<String> args = new ArrayDeque<>();
List<String> jvmArgs = new ArrayList<>();

setMavenProperties(args, false);

if (devOptions.clean) {
if (commonOptions.clean) {
args.add("clean");
}
args.add("quarkus:dev");
args.add(devMode ? "quarkus:dev" : "quarkus:test");

if (devOptions.skipTests()) { // TODO: does this make sense?
setSkipTests(args);
}

if (devOptions.offline) {
if (commonOptions.offline) {
args.add("--offline");
}

Expand All @@ -218,12 +214,7 @@ public List<Supplier<BuildCommandArgs>> prepareDevMode(DevOptions devOptions, De
paramsToQuarkusArgs(params, args);

BuildCommandArgs buildCommandArgs = prependExecutable(args);
return Collections.singletonList(new Supplier<BuildCommandArgs>() {
@Override
public BuildCommandArgs get() {
return buildCommandArgs;
}
});
return Collections.singletonList(() -> buildCommandArgs);
}

void setSkipTests(ArrayDeque<String> args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class DevOptions {
"--clean" }, description = "Perform clean as part of build. False by default.", negatable = true)
public boolean clean = false;

@CommandLine.Option(order = 4, names = {
"--no-tests" }, description = "Toggle continuous testing mode. Enabled by default.", negatable = true, hidden = true)
public boolean runTests = true; // TODO: does this make sense re: continuous test?
// Invalid w/ continuous test mode. Leave for compat (hidden)
@CommandLine.Option(order = 4, names = { "--no-tests" }, negatable = true, hidden = true)
public boolean runTests = true;

@CommandLine.Option(order = 5, names = { "--offline" }, description = "Work offline.", defaultValue = "false")
public boolean offline = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ public void testDevOptions() throws Exception {
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("-x test"),
"gradle command should not specify '-x test' (ignored)\n" + result);

Assertions.assertTrue(result.stdout.contains("-Ddebug=false"),
"gradle command should specify '-Ddebug=false'\n" + result);
Expand Down
16 changes: 11 additions & 5 deletions devtools/cli/src/test/java/io/quarkus/cli/CliProjectMavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void testBuildOptions() throws Exception {
}

@Test
public void testDevOptions() throws Exception {
public void testDevTestOptions() throws Exception {
CliDriver.Result result = CliDriver.execute(workspaceRoot, "create", "app", "-e", "-B", "--verbose");
Assertions.assertEquals(CommandLine.ExitCode.OK, result.exitCode, "Expected OK return code." + result);

Expand Down Expand Up @@ -225,10 +225,10 @@ public void testDevOptions() throws Exception {
Assertions.assertFalse(result.stdout.contains(" clean"),
"mvn command should not specify 'clean'\n" + result);

Assertions.assertTrue(result.stdout.contains("-DskipTests"),
"mvn command should specify -DskipTests\n" + result);
Assertions.assertTrue(result.stdout.contains("-Dmaven.test.skip=true"),
"mvn command should specify -Dmaven.test.skip=true\n" + result);
Assertions.assertFalse(result.stdout.contains("-DskipTests"),
"mvn command should not specify -DskipTests (ignored)\n" + result);
Assertions.assertFalse(result.stdout.contains("-Dmaven.test.skip=true"),
"mvn command should not specify -Dmaven.test.skip=true (ignored)\n" + result);

Assertions.assertTrue(result.stdout.contains("-Ddebug=false"),
"mvn command should specify '-Ddebug=false'\n" + result);
Expand All @@ -254,6 +254,12 @@ public void testDevOptions() throws Exception {

Assertions.assertTrue(result.stdout.contains("-Dquarkus.args='arg1 arg2'"),
"mvn 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);
}

@Test
Expand Down

0 comments on commit 5614878

Please sign in to comment.