From d5318b903eb3b2bd0d2bd754daca68cbfd50c487 Mon Sep 17 00:00:00 2001 From: Jelani Woods Date: Thu, 24 Jul 2025 16:06:44 -0500 Subject: [PATCH 1/4] Render status of test output as HTML --- lib/rspec/html_messages.rb | 28 +++++++++++++++---- .../html_messages/templates/_status.html.erb | 3 ++ .../html_messages/templates/example.html.erb | 6 ++++ lib/rspec/html_messages/version.rb | 2 +- 4 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 lib/rspec/html_messages/templates/_status.html.erb diff --git a/lib/rspec/html_messages.rb b/lib/rspec/html_messages.rb index cda7a7d..444d6d9 100644 --- a/lib/rspec/html_messages.rb +++ b/lib/rspec/html_messages.rb @@ -81,16 +81,28 @@ def backtrace_html(**options) render_template("_backtrace") end + def status_html(**options) + css_class, message = "" + if passed? + css_class = "alert-success" + message = "This test passed!" + else + css_class = "alert-danger" + message = "The test did not pass." + end + render_template("_status", css_class: css_class, message: message) + end + # Public boolean methods so users can make their own decisions def has_output? # Don't show output for errors before assertions return false if error_before_assertion? - status == "failed" || has_actual? + failed? || has_actual? end def has_failure_message? - status == "failed" && !error_before_assertion? && failure_message.present? + failed? && !error_before_assertion? && failure_message.present? end def has_exception_details? @@ -142,6 +154,14 @@ def self.diff_css private + def passed? + status == "passed" + end + + def failed? + !passed? + end + def default_options { force_diffable: FORCE_DIFFABLE_MATCHERS, @@ -174,7 +194,7 @@ def failure_message end def calculate_failure_message - return nil unless status == "failed" + return nil unless failed? message = example.dig("exception", "message") return nil unless message @@ -314,8 +334,6 @@ def friendly_error_location end end - private - def validate_example!(example) raise ArgumentError, "Example cannot be nil" if example.nil? raise ArgumentError, "Example must be a Hash" unless example.is_a?(Hash) diff --git a/lib/rspec/html_messages/templates/_status.html.erb b/lib/rspec/html_messages/templates/_status.html.erb new file mode 100644 index 0000000..5ded5f1 --- /dev/null +++ b/lib/rspec/html_messages/templates/_status.html.erb @@ -0,0 +1,3 @@ + diff --git a/lib/rspec/html_messages/templates/example.html.erb b/lib/rspec/html_messages/templates/example.html.erb index 2a2c735..ff0de6e 100644 --- a/lib/rspec/html_messages/templates/example.html.erb +++ b/lib/rspec/html_messages/templates/example.html.erb @@ -1,4 +1,10 @@
+ <% if status_content = status_html(**@options) %> +
+ <%= status_content %> +
+ <% end %> + <% if output_content = output_html(**@options) %>
<%= output_content %> diff --git a/lib/rspec/html_messages/version.rb b/lib/rspec/html_messages/version.rb index 7f22353..0b614b6 100644 --- a/lib/rspec/html_messages/version.rb +++ b/lib/rspec/html_messages/version.rb @@ -2,6 +2,6 @@ module Rspec class HtmlMessages - VERSION = "0.2.0" + VERSION = "0.2.1" end end From dfcdeb9444eb6d0a0c01fff00be4e328ab3e755e Mon Sep 17 00:00:00 2001 From: Jelani Woods Date: Thu, 24 Jul 2025 16:28:28 -0500 Subject: [PATCH 2/4] Update README --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 27c2e28..acf6028 100644 --- a/README.md +++ b/README.md @@ -267,8 +267,11 @@ Renders just the exception details section. Returns `nil` if no exception. #### `backtrace_html(**options)` Renders just the backtrace section. Returns `nil` if no backtrace. +#### `status_html(**options)` +Renders just the status section. + #### `render_html(**options)` -Convenience method that renders all three sections in a standard layout. +Convenience method that renders all four sections in a standard layout. ### Class Methods From 6502ec6549382633ba30569630633cb04b21c18d Mon Sep 17 00:00:00 2001 From: Jelani Woods Date: Thu, 24 Jul 2025 16:50:48 -0500 Subject: [PATCH 3/4] WIP --- lib/rspec/html_messages.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rspec/html_messages.rb b/lib/rspec/html_messages.rb index 444d6d9..602a1db 100644 --- a/lib/rspec/html_messages.rb +++ b/lib/rspec/html_messages.rb @@ -157,9 +157,9 @@ def self.diff_css def passed? status == "passed" end - + def failed? - !passed? + status == "failed" end def default_options From ca316b29cdd00973fa5dae5bea07d324cf345e04 Mon Sep 17 00:00:00 2001 From: Jelani Woods Date: Fri, 25 Jul 2025 07:58:24 -0500 Subject: [PATCH 4/4] Address PR feedback --- lib/rspec/html_messages.rb | 3 +-- lib/rspec/html_messages/templates/example.html.erb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/rspec/html_messages.rb b/lib/rspec/html_messages.rb index 602a1db..21c4d8d 100644 --- a/lib/rspec/html_messages.rb +++ b/lib/rspec/html_messages.rb @@ -82,12 +82,11 @@ def backtrace_html(**options) end def status_html(**options) - css_class, message = "" if passed? css_class = "alert-success" message = "This test passed!" else - css_class = "alert-danger" + css_class = "alert-warning" message = "The test did not pass." end render_template("_status", css_class: css_class, message: message) diff --git a/lib/rspec/html_messages/templates/example.html.erb b/lib/rspec/html_messages/templates/example.html.erb index ff0de6e..fa9ac61 100644 --- a/lib/rspec/html_messages/templates/example.html.erb +++ b/lib/rspec/html_messages/templates/example.html.erb @@ -6,7 +6,7 @@ <% end %> <% if output_content = output_html(**@options) %> -
+
<%= output_content %>
<% end %>