From 138ee9a56339542960efdda5b3983a4ffc87a212 Mon Sep 17 00:00:00 2001 From: "M.P. Korstanje" Date: Sun, 31 Aug 2025 20:24:52 +0200 Subject: [PATCH] Filter unused messages Because the message stream simply writes all messages, the report is larger than it needs to be. By filtering out step definitions, hooks and parameter types the report becomes smaller (but not much). --- .../MessagesToHtmlWriter.cs | 8 ++++++++ .../htmlformatter/MessagesToHtmlWriter.java | 6 ++++++ .../htmlformatter/MessagesToHtmlWriterTest.java | 15 +++++++++++++++ javascript/src/CucumberHtmlStream.ts | 4 ++++ ruby/lib/cucumber/html_formatter/formatter.rb | 4 ++++ 5 files changed, 37 insertions(+) diff --git a/dotnet/Cucumber.HtmlFormatter/MessagesToHtmlWriter.cs b/dotnet/Cucumber.HtmlFormatter/MessagesToHtmlWriter.cs index bb3a8e3f..1cd26ecd 100644 --- a/dotnet/Cucumber.HtmlFormatter/MessagesToHtmlWriter.cs +++ b/dotnet/Cucumber.HtmlFormatter/MessagesToHtmlWriter.cs @@ -112,6 +112,10 @@ public void Write(Envelope envelope) _preMessageWritten = true; _writer.Flush(); } + if(envelope.StepDefinition != null || envelope.Hook != null || envelope.ParameterType != null) + { + return; + } if (!_firstMessageWritten) { _firstMessageWritten = true; @@ -142,6 +146,10 @@ public async Task WriteAsync(Envelope envelope) _preMessageWritten = true; await _writer.FlushAsync(); } + if(envelope.Source != null || envelope.StepDefinition != null) + { + return; + } if (!_firstMessageWritten) { _firstMessageWritten = true; diff --git a/java/src/main/java/io/cucumber/htmlformatter/MessagesToHtmlWriter.java b/java/src/main/java/io/cucumber/htmlformatter/MessagesToHtmlWriter.java index c410b682..e97b0cee 100644 --- a/java/src/main/java/io/cucumber/htmlformatter/MessagesToHtmlWriter.java +++ b/java/src/main/java/io/cucumber/htmlformatter/MessagesToHtmlWriter.java @@ -143,6 +143,12 @@ public void write(Envelope envelope) throws IOException { preMessageWritten = true; } + if (envelope.getStepDefinition().isPresent() + || envelope.getHook().isPresent() + || envelope.getParameterType().isPresent()) { + return; + } + if (!firstMessageWritten) { firstMessageWritten = true; } else { diff --git a/java/src/test/java/io/cucumber/htmlformatter/MessagesToHtmlWriterTest.java b/java/src/test/java/io/cucumber/htmlformatter/MessagesToHtmlWriterTest.java index fa69816c..e0f0044f 100644 --- a/java/src/test/java/io/cucumber/htmlformatter/MessagesToHtmlWriterTest.java +++ b/java/src/test/java/io/cucumber/htmlformatter/MessagesToHtmlWriterTest.java @@ -6,6 +6,12 @@ import io.cucumber.messages.types.Envelope; import io.cucumber.messages.types.GherkinDocument; import io.cucumber.messages.types.Location; +import io.cucumber.messages.types.Source; +import io.cucumber.messages.types.SourceMediaType; +import io.cucumber.messages.types.SourceReference; +import io.cucumber.messages.types.StepDefinition; +import io.cucumber.messages.types.StepDefinitionPattern; +import io.cucumber.messages.types.StepDefinitionPatternType; import io.cucumber.messages.types.TestRunFinished; import io.cucumber.messages.types.TestRunStarted; import org.junit.jupiter.api.Test; @@ -15,6 +21,7 @@ import java.io.IOException; import java.time.Instant; +import static io.cucumber.messages.types.SourceMediaType.TEXT_X_CUCUMBER_GHERKIN_PLAIN; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Collections.singletonList; import static org.hamcrest.CoreMatchers.containsString; @@ -61,6 +68,14 @@ void it_writes_no_message_to_html() throws IOException { assertThat(html, containsString("window.CUCUMBER_MESSAGES = [];")); } + @Test + void it_ignores_step_definition_messages() throws IOException { + StepDefinitionPattern pattern = new StepDefinitionPattern("hello {int} cucumber(s)", StepDefinitionPatternType.CUCUMBER_EXPRESSION); + Envelope envelope = Envelope.of(new StepDefinition("123", pattern, SourceReference.of("path/to/StepDefinition.java"))); + String html = renderAsHtml(envelope); + assertThat(html, containsString("window.CUCUMBER_MESSAGES = [];")); + } + @Test void it_writes_default_title() throws IOException { String html = renderAsHtml(MessagesToHtmlWriter.builder(serializer)); diff --git a/javascript/src/CucumberHtmlStream.ts b/javascript/src/CucumberHtmlStream.ts index 8738fb4d..63d4cca9 100644 --- a/javascript/src/CucumberHtmlStream.ts +++ b/javascript/src/CucumberHtmlStream.ts @@ -138,6 +138,10 @@ export class CucumberHtmlStream extends Transform { } private writeMessage(envelope: messages.Envelope) { + if (envelope.stepDefinition || envelope.hook || envelope.parameterType) { + return; + } + if (!this.firstMessageWritten) { this.firstMessageWritten = true } else { diff --git a/ruby/lib/cucumber/html_formatter/formatter.rb b/ruby/lib/cucumber/html_formatter/formatter.rb index 3d7243aa..263ca1be 100644 --- a/ruby/lib/cucumber/html_formatter/formatter.rb +++ b/ruby/lib/cucumber/html_formatter/formatter.rb @@ -22,6 +22,10 @@ def process_messages(messages) end def write_message(message) + if message.step_definition != nil || message.hook != nil || message.parameter_type != nil + return + end + out.puts(',') unless @first_message # Replace < with \x3C # https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements