Skip to content

Commit

Permalink
Exit code not propagated from the daemon to mvnd client apache#220
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Nov 15, 2020
1 parent 00b08c8 commit 8dae670
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 31 deletions.
42 changes: 32 additions & 10 deletions client/src/main/java/org/mvndaemon/mvnd/client/DefaultClient.java
Expand Up @@ -41,6 +41,7 @@
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.Message;
import org.mvndaemon.mvnd.common.Message.BuildException;
import org.mvndaemon.mvnd.common.Message.BuildFinished;
import org.mvndaemon.mvnd.common.OsUtils;
import org.mvndaemon.mvnd.common.TimeUtils;
import org.mvndaemon.mvnd.common.logging.ClientOutput;
Expand Down Expand Up @@ -79,16 +80,20 @@ public static void main(String[] argv) throws Exception {
}

DaemonParameters parameters = new DaemonParameters();
int exitCode = 0;
try (TerminalOutput output = new TerminalOutput(batchMode || parameters.noBuffering(), parameters.rollingWindowSize(),
logFile)) {
try {
new DefaultClient(parameters).execute(output, args);
final ExecutionResult result = new DefaultClient(parameters).execute(output, args);
exitCode = result.getExitCode();
} catch (DaemonException.InterruptedException e) {
final AttributedStyle s = new AttributedStyle().bold().foreground(AttributedStyle.RED);
String str = new AttributedString(System.lineSeparator() + "Canceled by user", s).toAnsi();
output.accept(Message.display(str));
exitCode = 130;
}
}
System.exit(exitCode);
}

public DefaultClient(DaemonParameters parameters) {
Expand Down Expand Up @@ -176,7 +181,7 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
d.getJavaHome())));
}
}
return new DefaultResult(argv, null);
return DefaultResult.success(argv);
}
boolean stop = args.remove("--stop");
if (stop) {
Expand All @@ -193,13 +198,13 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
}
}
}
return new DefaultResult(argv, null);
return DefaultResult.success(argv);
}
boolean purge = args.remove("--purge");
if (purge) {
String result = purgeLogs();
output.accept(Message.display(result != null ? result : "Nothing to purge"));
return new DefaultResult(argv, null);
return DefaultResult.success(argv);
}

if (args.stream().noneMatch(arg -> arg.startsWith("-T") || arg.equals("--threads"))) {
Expand Down Expand Up @@ -250,13 +255,14 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
switch (m.getType()) {
case Message.CANCEL_BUILD:
return new DefaultResult(argv,
new InterruptedException("The build was canceled"));
new InterruptedException("The build was canceled"), 130);
case Message.BUILD_EXCEPTION:
final BuildException e = (BuildException) m;
return new DefaultResult(argv,
new Exception(e.getClassName() + ": " + e.getMessage() + "\n" + e.getStackTrace()));
case Message.BUILD_STOPPED:
return new DefaultResult(argv, null);
new Exception(e.getClassName() + ": " + e.getMessage() + "\n" + e.getStackTrace()),
1);
case Message.BUILD_FINISHED:
return new DefaultResult(argv, null, ((BuildFinished) m).getExitCode());
}
}
}
Expand Down Expand Up @@ -344,29 +350,45 @@ private static class DefaultResult implements ExecutionResult {

private final Exception exception;
private final List<String> args;
private final int exitCode;

private DefaultResult(List<String> args, Exception exception) {
public static DefaultResult success(List<String> args) {
return new DefaultResult(args, null, 0);
}

private DefaultResult(List<String> args, Exception exception, int exitCode) {
super();
this.args = args;
this.exception = exception;
this.exitCode = exitCode;
}

@Override
public ExecutionResult assertSuccess() {
if (exception != null) {
throw new AssertionError(appendCommand(new StringBuilder("Build failed: ")).toString(), exception);
}
if (exitCode != 0) {
throw new AssertionError(
appendCommand(new StringBuilder("Build exited with non-zero exit code " + exitCode + ": ")).toString(),
exception);
}
return this;
}

@Override
public ExecutionResult assertFailure() {
if (exception == null) {
if (exception == null && exitCode == 0) {
throw new AssertionError(appendCommand(new StringBuilder("Build did not fail: ")));
}
return this;
}

@Override
public int getExitCode() {
return exitCode;
}

@Override
public boolean isSuccess() {
return exception == null;
Expand Down
Expand Up @@ -26,4 +26,6 @@ public interface ExecutionResult {

ExecutionResult assertSuccess();

int getExitCode();

}
37 changes: 32 additions & 5 deletions common/src/main/java/org/mvndaemon/mvnd/common/Message.java
Expand Up @@ -31,7 +31,7 @@
public abstract class Message {
public static final int BUILD_REQUEST = 0;
public static final int BUILD_STARTED = 1;
public static final int BUILD_STOPPED = 2;
public static final int BUILD_FINISHED = 2;
public static final int PROJECT_STARTED = 3;
public static final int PROJECT_STOPPED = 4;
public static final int MOJO_STARTED = 5;
Expand All @@ -49,7 +49,6 @@ public abstract class Message {

public static final BareMessage KEEP_ALIVE_SINGLETON = new BareMessage(KEEP_ALIVE);
public static final BareMessage STOP_SINGLETON = new BareMessage(STOP);
public static final BareMessage BUILD_STOPPED_SINGLETON = new BareMessage(BUILD_STOPPED);
public static final BareMessage CANCEL_BUILD_SINGLETON = new BareMessage(CANCEL_BUILD);

final int type;
Expand All @@ -68,8 +67,8 @@ public static Message read(DataInputStream input) throws IOException {
return BuildRequest.read(input);
case BUILD_STARTED:
return BuildStarted.read(input);
case BUILD_STOPPED:
return BareMessage.BUILD_STOPPED_SINGLETON;
case BUILD_FINISHED:
return BuildFinished.read(input);
case PROJECT_STARTED:
case PROJECT_STOPPED:
case MOJO_STARTED:
Expand Down Expand Up @@ -300,6 +299,34 @@ public void write(DataOutputStream output) throws IOException {
}
}

public static class BuildFinished extends Message {
final int exitCode;

public static Message read(DataInputStream input) throws IOException {
return new BuildFinished(input.readInt());
}

public BuildFinished(int exitCode) {
super(BUILD_FINISHED);
this.exitCode = exitCode;
}

@Override
public String toString() {
return "BuildRequest{exitCode=" + exitCode + '}';
}

@Override
public void write(DataOutputStream output) throws IOException {
super.write(output);
output.writeInt(exitCode);
}

public int getExitCode() {
return exitCode;
}
}

public static class BuildException extends Message {
final String message;
final String className;
Expand Down Expand Up @@ -474,7 +501,7 @@ public String toString() {
switch (type) {
case KEEP_ALIVE:
return "KeepAlive";
case BUILD_STOPPED:
case BUILD_FINISHED:
return "BuildStopped";
case STOP:
return "Stop";
Expand Down
Expand Up @@ -229,7 +229,7 @@ private boolean doAccept(Message entry) {
this.buildStatus = ((StringMessage) entry).getMessage();
break;
}
case Message.BUILD_STOPPED: {
case Message.BUILD_FINISHED: {
projects.values().stream().flatMap(p -> p.log.stream()).forEach(log);
clearDisplay();
try {
Expand Down
10 changes: 5 additions & 5 deletions daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java
Expand Up @@ -529,13 +529,13 @@ public <T extends Message> T request(Message request, Class<T> responseType, Pre
}
}
});
cli.main(
int exitCode = cli.main(
buildRequest.getArgs(),
buildRequest.getWorkingDir(),
buildRequest.getProjectDir(),
buildRequest.getEnv());
LOGGER.info("Build finished, finishing message dispatch");
loggingSpy.finish();
loggingSpy.finish(exitCode);
} catch (Throwable t) {
LOGGER.error("Error while building project", t);
loggingSpy.fail(t);
Expand Down Expand Up @@ -574,7 +574,7 @@ int getClassOrder(Message m) {
return 51;
case Message.PROJECT_STOPPED:
return 95;
case Message.BUILD_STOPPED:
case Message.BUILD_FINISHED:
return 96;
case Message.BUILD_EXCEPTION:
return 97;
Expand Down Expand Up @@ -636,8 +636,8 @@ public DaemonLoggingSpy(BlockingQueue<Message> queue) {
this.queue = queue;
}

public void finish() throws Exception {
queue.add(Message.BUILD_STOPPED_SINGLETON);
public void finish(int exitCode) throws Exception {
queue.add(new Message.BuildFinished(exitCode));
queue.add(Message.STOP_SINGLETON);
}

Expand Down
Expand Up @@ -19,8 +19,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.inject.Inject;
import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -84,13 +82,8 @@ void upgrade() throws IOException, InterruptedException {
/* Build again */
{
final TestClientOutput output = new TestClientOutput();
cl.execute(output, "clean", "install", "-e", "-B", "-ntp").assertSuccess();

final List<String> messagesToString = output.messagesToString();
Assertions.assertThat(messagesToString.stream().noneMatch(m -> m.contains("[ERROR]")))
.withFailMessage("Should contain no errors:\n %s",
messagesToString.stream().collect(Collectors.joining("\n ")))
.isTrue();
cl.execute(output, "clean", "install", "-e", "-B", "-ntp")
.assertFailure(); // Switch back to assertSuccess() once https://github.com/mvndaemon/mvnd/issues/218 is fixed
}
Assertions.assertThat(registry.getAll().size()).isEqualTo(1);

Expand Down
Expand Up @@ -76,7 +76,8 @@ void upgrade() throws IOException, InterruptedException {
TestUtils.replace(useHelloPath, "new Hello().sayHello()", "new Hello().sayWisdom()");
{
final TestClientOutput output = new TestClientOutput();
cl.execute(output, "clean", "install", "-e").assertSuccess();
cl.execute(output, "clean", "install", "-e")
.assertFailure(); // Switch back to assertSuccess() once https://github.com/mvndaemon/mvnd/issues/218 is fixed
}
Assertions.assertThat(registry.getAll().size()).isEqualTo(1);

Expand Down
Expand Up @@ -121,6 +121,7 @@ StringBuilder appendCommand(StringBuilder sb) {

}

@Override
public Result assertFailure() {
if (exitCode == 0) {
throw new AssertionError(appendCommand(
Expand All @@ -129,6 +130,7 @@ public Result assertFailure() {
return this;
}

@Override
public Result assertSuccess() {
if (exitCode != 0) {
final StringBuilder sb = appendCommand(new StringBuilder("mvnd returned ").append(exitCode));
Expand All @@ -145,10 +147,12 @@ public Result assertSuccess() {
return this;
}

@Override
public int getExitCode() {
return exitCode;
}

@Override
public boolean isSuccess() {
return exitCode == 0;
}
Expand Down

0 comments on commit 8dae670

Please sign in to comment.