Output received messages on JVM test failure

This commit is contained in:
Peter Palaga
2020-11-15 16:11:49 +01:00
parent ab85aa1903
commit fa2af29bd4
5 changed files with 120 additions and 23 deletions

View File

@@ -366,11 +366,11 @@ public class DefaultClient implements Client {
@Override @Override
public ExecutionResult assertSuccess() { public ExecutionResult assertSuccess() {
if (exception != null) { 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) { if (exitCode != 0) {
throw new AssertionError( 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); exception);
} }
return this; return this;
@@ -379,7 +379,7 @@ public class DefaultClient implements Client {
@Override @Override
public ExecutionResult assertFailure() { public ExecutionResult assertFailure() {
if (exception == null && exitCode == 0) { 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; return this;
} }
@@ -394,13 +394,6 @@ public class DefaultClient implements Client {
return exception == null; return exception == null;
} }
StringBuilder appendCommand(StringBuilder sb) {
sb.append("mvnd");
for (String arg : args) {
sb.append(" \"").append(arg).append('"');
}
return sb;
}
} }
} }

View File

@@ -15,6 +15,8 @@
*/ */
package org.mvndaemon.mvnd.client; package org.mvndaemon.mvnd.client;
import java.util.List;
/** /**
* A result of a {@code mvnd} build. * A result of a {@code mvnd} build.
*/ */
@@ -28,4 +30,12 @@ public interface ExecutionResult {
int getExitCode(); 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;
}
} }

View File

@@ -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.toString(), e);
}
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.toString(), e);
}
return this;
}
@Override
public int getExitCode() {
return delegate.getExitCode();
}
@Override
public boolean isSuccess() {
return delegate.isSuccess();
}
}
}

View File

@@ -32,7 +32,6 @@ import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Store; import org.junit.jupiter.api.extension.ExtensionContext.Store;
import org.mvndaemon.mvnd.client.Client; import org.mvndaemon.mvnd.client.Client;
import org.mvndaemon.mvnd.client.DaemonParameters; import org.mvndaemon.mvnd.client.DaemonParameters;
import org.mvndaemon.mvnd.client.DefaultClient;
import org.mvndaemon.mvnd.common.DaemonRegistry; import org.mvndaemon.mvnd.common.DaemonRegistry;
import org.mvndaemon.mvnd.common.Environment; import org.mvndaemon.mvnd.common.Environment;
import org.mvndaemon.mvnd.common.TimeUtils; import org.mvndaemon.mvnd.common.TimeUtils;
@@ -122,7 +121,7 @@ public class MvndTestExtension implements BeforeAllCallback, BeforeEachCallback,
} }
return new NativeTestClient(parameters, mvndNativeExecutablePath, timeoutMs); return new NativeTestClient(parameters, mvndNativeExecutablePath, timeoutMs);
} else { } else {
return new DefaultClient(parameters); return new JvmTestClient(parameters);
} }
} }

View File

@@ -113,19 +113,18 @@ public class NativeTestClient implements Client {
this.log = log; this.log = log;
} }
StringBuilder appendCommand(StringBuilder sb) {
for (String arg : args) {
sb.append(" \"").append(arg).append('"');
}
return sb;
}
@Override @Override
public Result assertFailure() { public Result assertFailure() {
if (exitCode == 0) { if (exitCode == 0) {
throw new AssertionError(appendCommand( final StringBuilder sb = ExecutionResult.appendCommand(
new StringBuilder("mvnd returned ").append(exitCode).append(" instead of non-zero exit code: "))); 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; return this;
} }
@@ -133,7 +132,9 @@ public class NativeTestClient implements Client {
@Override @Override
public Result assertSuccess() { public Result assertSuccess() {
if (exitCode != 0) { 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) { if (exitCode == CommandProcess.TIMEOUT_EXIT_CODE) {
sb.append(" (timeout)"); sb.append(" (timeout)");
} }