diff --git a/lib/rspec/core/notifications.rb b/lib/rspec/core/notifications.rb index 93be6a4eb5..b88d54ade4 100644 --- a/lib/rspec/core/notifications.rb +++ b/lib/rspec/core/notifications.rb @@ -205,9 +205,17 @@ def fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::Console def encoding_of(string) string.encoding end + + def encoded_string(string) + RSpec::Support::EncodedString.new(string, Encoding.default_external) + end else def encoding_of(_string) end + + def encoded_string(string) + RSpec::Support::EncodedString.new(string) + end end def backtrace_formatter @@ -225,8 +233,8 @@ def failure_lines begin lines = ["Failure/Error: #{read_failed_line.strip}"] lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/ - exception.message.to_s.split("\n").each do |line| - lines << " #{line}" if exception.message + encoded_string(exception.message.to_s).split("\n").each do |line| + lines << " #{line}" end lines end diff --git a/spec/rspec/core/notifications_spec.rb b/spec/rspec/core/notifications_spec.rb index 83428f5026..cbcf5c0ca4 100644 --- a/spec/rspec/core/notifications_spec.rb +++ b/spec/rspec/core/notifications_spec.rb @@ -100,5 +100,18 @@ expect(lines[0]).to match %r{\AFailure\/Error} expect(lines[1]).to match %r{\A\s*Test exception\z} end + + if String.method_defined?(:encoding) + it "returns failures_lines with invalid bytes replace by '?'" do + message_with_invalid_byte_sequence = + "\xEF \255 \xAD I have bad bytes".force_encoding(Encoding::UTF_8) + allow(exception).to receive(:message). + and_return(message_with_invalid_byte_sequence) + + lines = notification.message_lines + expect(lines[0]).to match %r{\AFailure\/Error} + expect(lines[1].strip).to eq("? ? ? I have bad bytes") + end + end end end