From 4feace81d1275857dfc7ab467ff0fbcc7aa8740f Mon Sep 17 00:00:00 2001 From: hobovsky Date: Thu, 2 Feb 2023 20:03:22 +0100 Subject: [PATCH] Fix `ParameterizedTests` suppressing `PreconditionViolationException` (#1) Fixes https://github.com/codewars/runner/issues/230 --- .../com/codewars/junit5/CodewarsListener.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/codewars/junit5/CodewarsListener.java b/src/main/java/com/codewars/junit5/CodewarsListener.java index 801c62f..b7d06ff 100644 --- a/src/main/java/com/codewars/junit5/CodewarsListener.java +++ b/src/main/java/com/codewars/junit5/CodewarsListener.java @@ -122,6 +122,14 @@ public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult } else { System.out.println("\nFailed for unknown cause"); } + } else { + ++failures; + Optional th = testExecutionResult.getThrowable(); + if (th.isPresent()) { + outputError("Crashed", th.get()); + } else { + System.out.println("\nUnexpected error occurred"); + } } System.out.printf("\n%d\n", getDuration(testIdentifier)); break; @@ -161,6 +169,17 @@ private static void outputFailure(String kind, Throwable throwable) { System.out.printf("\n%s\n", formatMessage(readStackTrace(throwable))); } + private static void outputError(String kind, Throwable throwable) { + String msg = throwable.getMessage(); + String formattedStackTrace = formatMessage(readStackTrace(throwable)); + if (msg == null) { + System.out.printf("\nTest %s<:LF:><:LF:>%s\n", kind, formattedStackTrace); + } else { + String formattedMessage = formatMessage(msg); + System.out.printf("\n%s<:LF:><:LF:>%s\n", formattedMessage, formattedStackTrace); + } + } + // Read the stacktrace of the supplied {@link Throwable} into a String. // https://github.com/junit-team/junit5/blob/946c5980074f466de0688297a6d661d32679599a/junit-platform-commons/src/main/java/org/junit/platform/commons/util/ExceptionUtils.java#L76 private static String readStackTrace(Throwable throwable) { @@ -186,7 +205,7 @@ private long getDuration(TestIdentifier testIdentifier) { } private static String formatMessage(final String s) { - return (s == null) ? "" : s.replaceAll("\n", "<:LF:>"); + return (s == null) ? "" : s.replaceAll(System.lineSeparator(), "<:LF:>"); } private NavigableSet reportEntries(TestIdentifier testIdentifier) {