From 8436627598aed0e3eb12daae322ae049b08adf8c Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 28 May 2014 15:47:55 +0100 Subject: [PATCH] Allow tests to System.exit() by default Otherwise the ApplicationContext stays alive and if it's a server app the JVM does not exit at the end of "spring test". User can override with "spring test foo.groovy --nohup" (which we have to do in our unit tests). --- .gitignore | 1 + .../java/org/springframework/boot/cli/SpringCli.java | 1 + .../boot/cli/command/test/TestCommand.java | 10 ++++++++++ .../java/org/springframework/boot/cli/CliTester.java | 5 ++++- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index dc9fe73105..a0516e3ec5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ build lib/ target .springBeans +interpolated*.xml dependency-reduced-pom.xml build.log _site/ diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java index c86e3401f6..8789bd505f 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/SpringCli.java @@ -50,6 +50,7 @@ public class SpringCli { int exitCode = runner.runAndHandleErrors(args); if (exitCode != 0) { + // If successful, leave it to run in case it's a server app System.exit(exitCode); } } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/test/TestCommand.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/test/TestCommand.java index e333649984..fc350e1944 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/test/TestCommand.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/test/TestCommand.java @@ -17,6 +17,7 @@ package org.springframework.boot.cli.command.test; import joptsimple.OptionSet; +import joptsimple.OptionSpec; import org.springframework.boot.cli.command.Command; import org.springframework.boot.cli.command.OptionParsingCommand; @@ -45,6 +46,12 @@ public class TestCommand extends OptionParsingCommand { private TestRunner runner; + @Override + protected void doOptions() { + option("nohup", + "Flag to indicate that the JVM should not exit when tests are finished"); + } + @Override protected void run(OptionSet options) throws Exception { SourceOptions sourceOptions = new SourceOptions(options); @@ -53,6 +60,9 @@ public class TestCommand extends OptionParsingCommand { this.runner = new TestRunner(configuration, sourceOptions.getSourcesArray(), sourceOptions.getArgsArray()); this.runner.compileAndRunTests(); + if (!options.has("nohup")) { + System.exit(0); // TODO: non-zero if test fails? + } } /** diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java index a11a2a041d..1f7a8037f9 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java @@ -75,7 +75,10 @@ public class CliTester implements TestRule { } public String test(String... args) throws Exception { - Future future = submitCommand(new TestCommand(), args); + String[] argsToUse = new String[args.length + 1]; + System.arraycopy(args, 0, argsToUse, 1, args.length); + argsToUse[0] = "--nohup"; + Future future = submitCommand(new TestCommand(), argsToUse); this.commands.add(future.get(this.timeout, TimeUnit.MILLISECONDS)); return getOutput(); }