Skip to content

[Core] Indent stacktrace in pretty formatter #2970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- [Core] Include root cause when using DataTable.asList and friends ([#2949](https://github.com/cucumber/cucumber-jvm/pull/2949) M.P. Korstanje)
- [Core] Indent stacktrace in pretty formatter ([#2970](https://github.com/cucumber/cucumber-jvm/pull/2970) M.P. Korstanje)
- [JUnit Platform Engine] Set Engine-Version-cucumber attribute ([#2963](https://github.com/cucumber/cucumber-jvm/pull/2963) M.P. Korstanje)

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@
public final class PrettyFormatter implements ConcurrentEventListener, ColorAware {

private static final String SCENARIO_INDENT = "";
private static final String STEP_INDENT = " ";
private static final String STEP_SCENARIO_INDENT = " ";
private static final String STEP_INDENT = SCENARIO_INDENT + " ";
private static final String STEP_SCENARIO_INDENT = STEP_INDENT + " ";
private static final String STACK_TRACE_INDENT = STEP_SCENARIO_INDENT + " ";

private final Map<UUID, Integer> commentStartIndex = new HashMap<>();

Expand Down Expand Up @@ -120,7 +121,7 @@ private void preCalculateLocationIndent(TestCaseStarted event) {
private void printTags(TestCaseStarted event) {
List<String> tags = event.getTestCase().getTags();
if (!tags.isEmpty()) {
out.println(PrettyFormatter.SCENARIO_INDENT + String.join(" ", tags));
out.println(SCENARIO_INDENT + String.join(" ", tags));
}
}

Expand Down Expand Up @@ -187,21 +188,23 @@ private String formatLocationComment(

private void printError(TestStepFinished event) {
Result result = event.getResult();
printError(result);
printError(STACK_TRACE_INDENT, result);
}

private void printError(TestRunFinished event) {
Result result = event.getResult();
printError(result);
printError(SCENARIO_INDENT, result);
}

private void printError(Result result) {
private void printError(String prefix, Result result) {
Throwable error = result.getError();
if (error != null) {
String name = result.getStatus().name().toLowerCase(ROOT);
Format format = formats.get(name);
String text = printStackTrace(error);
out.println(" " + format.text(text));
// TODO: Java 12+ use String.indent
String indented = text.replaceAll("(\r\n|\r|\n)", "$1" + prefix).trim();
out.println(prefix + format.text(indented));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.regex.Pattern;

import static io.cucumber.core.options.Constants.FILTER_TAGS_PROPERTY_NAME;
import static io.cucumber.core.plugin.IsEqualCompressingLineSeparators.equalCompressingLineSeparators;
import static io.cucumber.core.resource.ClasspathSupport.rootPackageUri;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyMap;
Expand All @@ -50,7 +51,6 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalToCompressingWhiteSpace;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
Expand Down Expand Up @@ -411,7 +411,7 @@ void ensure_less_than_1_thread_is_not_allowed() {
parser
.parse("--threads", "0")
.build();
assertThat(output(), equalToCompressingWhiteSpace("--threads must be > 0"));
assertThat(output(), equalCompressingLineSeparators("--threads must be > 0"));
assertThat(parser.exitStatus(), is(Optional.of((byte) 0x1)));
}

Expand Down Expand Up @@ -518,7 +518,7 @@ void ensure_less_than_1_count_is_not_allowed() {
parser
.parse("--count", "0")
.build();
assertThat(output(), equalToCompressingWhiteSpace("--count must be > 0"));
assertThat(output(), equalCompressingLineSeparators("--count must be > 0"));
assertThat(parser.exitStatus(), is(Optional.of((byte) 0x1)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import java.util.Locale;
import java.util.UUID;

import static io.cucumber.core.plugin.IsEqualCompressingLineSeparators.equalCompressingLineSeparators;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.time.Instant.ofEpochSecond;
import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.text.IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace;

class DefaultSummaryPrinterTest {

Expand Down Expand Up @@ -56,7 +56,7 @@ void does_not_print_duplicate_snippets() {

bus.send(new TestRunFinished(bus.getInstant(), new Result(Status.PASSED, Duration.ZERO, null)));

assertThat(new String(out.toByteArray(), UTF_8), equalToCompressingWhiteSpace("" +
assertThat(new String(out.toByteArray(), UTF_8), equalCompressingLineSeparators("" +
"\n" +
"0 Scenarios\n" +
"0 Steps\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.cucumber.core.plugin;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.util.Objects;

public class IsEqualCompressingLineSeparators extends TypeSafeMatcher<String> {

private final String expected;

public IsEqualCompressingLineSeparators(String expected) {
Objects.requireNonNull(expected);
this.expected = expected;
}

public String getExpected() {
return expected;
}

@Override
public boolean matchesSafely(String actual) {
return compressNewLines(expected).equals(compressNewLines(actual));
}

@Override
public void describeMismatchSafely(String item, Description mismatchDescription) {
mismatchDescription.appendText("was ").appendValue(item);
}

@Override
public void describeTo(Description description) {
description.appendText("a string equal to ")
.appendValue(expected)
.appendText(" compressing newlines");
}

public String compressNewLines(String actual) {
return actual.replaceAll("[\r\n]+", "\n").trim();
}

public static Matcher<String> equalCompressingLineSeparators(String expectedString) {
return new IsEqualCompressingLineSeparators(expectedString);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void should_format_scenario_with_a_failed_step() throws JSONException {
.withEventBus(new TimeServiceEventBus(timeService, UUID::randomUUID))
.withBackendSupplier(new StubBackendSupplier(
new StubStepDefinition("there are bananas", "StepDefs.there_are_bananas()",
new StubException())))
new StubException("the stack trace"))))
.build()
.run();

Expand Down Expand Up @@ -1485,5 +1485,4 @@ void should_handle_several_features() throws JSONException {
"]";
assertJsonEquals(expected, out);
}

}
Loading