Skip to content

Commit

Permalink
Output received messages on JVM test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Nov 15, 2020
1 parent 8dae670 commit a328a69
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 23 deletions.
13 changes: 3 additions & 10 deletions client/src/main/java/org/mvndaemon/mvnd/client/DefaultClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,11 @@ private DefaultResult(List<String> args, Exception exception, int exitCode) {
@Override
public ExecutionResult assertSuccess() {
if (exception != null) {
throw new AssertionError(appendCommand(new StringBuilder("Build failed: ")).toString(), exception);
throw new AssertionError(ExecutionResult.appendCommand(new StringBuilder("Build failed: "), args).toString(), exception);
}
if (exitCode != 0) {
throw new AssertionError(
appendCommand(new StringBuilder("Build exited with non-zero exit code " + exitCode + ": ")).toString(),
ExecutionResult.appendCommand(new StringBuilder("Build exited with non-zero exit code " + exitCode + ": "), args).toString(),
exception);
}
return this;
Expand All @@ -379,7 +379,7 @@ public ExecutionResult assertSuccess() {
@Override
public ExecutionResult assertFailure() {
if (exception == null && exitCode == 0) {
throw new AssertionError(appendCommand(new StringBuilder("Build did not fail: ")));
throw new AssertionError(ExecutionResult.appendCommand(new StringBuilder("Build did not fail: "), args));
}
return this;
}
Expand All @@ -394,13 +394,6 @@ public boolean isSuccess() {
return exception == null;
}

StringBuilder appendCommand(StringBuilder sb) {
sb.append("mvnd");
for (String arg : args) {
sb.append(" \"").append(arg).append('"');
}
return sb;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.mvndaemon.mvnd.client;

import java.util.List;

/**
* A result of a {@code mvnd} build.
*/
Expand All @@ -28,4 +30,12 @@ public interface ExecutionResult {

int getExitCode();

public static StringBuilder appendCommand(StringBuilder sb, List<String> args) {
sb.append("mvnd");
for (String arg : args) {
sb.append(" \"").append(arg).append('"');
}
return sb;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mvndaemon.mvnd.junit;

import java.util.List;
import org.mvndaemon.mvnd.assertj.TestClientOutput;
import org.mvndaemon.mvnd.client.DaemonParameters;
import org.mvndaemon.mvnd.client.DefaultClient;
import org.mvndaemon.mvnd.client.ExecutionResult;
import org.mvndaemon.mvnd.common.logging.ClientOutput;

public class JvmTestClient extends DefaultClient {

public JvmTestClient(DaemonParameters parameters) {
super(parameters);
}

@Override
public ExecutionResult execute(ClientOutput output, List<String> argv) {
final ExecutionResult delegate = super.execute(output, argv);
if (output instanceof TestClientOutput) {
return new JvmTestResult(delegate, ((TestClientOutput) output).messagesToString());
}
return delegate;
}

public static class JvmTestResult implements ExecutionResult {

private final ExecutionResult delegate;
private final List<String> log;

public JvmTestResult(ExecutionResult delegate, List<String> log) {
this.delegate = delegate;
this.log = log;
}

@Override
public JvmTestResult assertFailure() {
try {
delegate.assertFailure();
} catch (AssertionError e) {
final StringBuilder sb = new StringBuilder(e.getMessage());
sb.append("\n--- received messages start ---");
synchronized (log) {
log.forEach(s -> sb.append('\n').append(s));
}
sb.append("\n--- received messages end ---");
throw new AssertionError(sb);
}
return this;
}

@Override
public JvmTestResult assertSuccess() {
try {
delegate.assertSuccess();
} catch (AssertionError e) {
final StringBuilder sb = new StringBuilder(e.getMessage());
sb.append("\n--- received messages start ---");
synchronized (log) {
log.forEach(s -> sb.append('\n').append(s));
}
sb.append("\n--- received messages end ---");
throw new AssertionError(sb);
}
return this;
}

@Override
public int getExitCode() {
return delegate.getExitCode();
}

@Override
public boolean isSuccess() {
return delegate.isSuccess();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.junit.jupiter.api.extension.ExtensionContext.Store;
import org.mvndaemon.mvnd.client.Client;
import org.mvndaemon.mvnd.client.DaemonParameters;
import org.mvndaemon.mvnd.client.DefaultClient;
import org.mvndaemon.mvnd.common.DaemonRegistry;
import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.TimeUtils;
Expand Down Expand Up @@ -122,7 +121,7 @@ Client newClient(boolean isNative, DaemonParameters parameters, long timeoutMs)
}
return new NativeTestClient(parameters, mvndNativeExecutablePath, timeoutMs);
} else {
return new DefaultClient(parameters);
return new JvmTestClient(parameters);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,28 @@ public Result(List<String> args, int exitCode, List<String> log) {
this.log = log;
}

StringBuilder appendCommand(StringBuilder sb) {
for (String arg : args) {
sb.append(" \"").append(arg).append('"');
}
return sb;

}

@Override
public Result assertFailure() {
if (exitCode == 0) {
throw new AssertionError(appendCommand(
new StringBuilder("mvnd returned ").append(exitCode).append(" instead of non-zero exit code: ")));
final StringBuilder sb = ExecutionResult.appendCommand(
new StringBuilder("mvnd returned ").append(exitCode).append(" instead of non-zero exit code: "),
args);
sb.append("\n--- stderr+stdout start ---");
synchronized (log) {
log.forEach(s -> sb.append('\n').append(s));
}
sb.append("\n--- stderr+stdout end ---");
throw new AssertionError(sb);
}
return this;
}

@Override
public Result assertSuccess() {
if (exitCode != 0) {
final StringBuilder sb = appendCommand(new StringBuilder("mvnd returned ").append(exitCode));
final StringBuilder sb = ExecutionResult.appendCommand(
new StringBuilder("mvnd returned ").append(exitCode),
args);
if (exitCode == CommandProcess.TIMEOUT_EXIT_CODE) {
sb.append(" (timeout)");
}
Expand Down

0 comments on commit a328a69

Please sign in to comment.