Skip to content
This repository has been archived by the owner on Nov 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1760 from bf4/handle_non_utf8_exception_messages
Browse files Browse the repository at this point in the history
Handle exception message with invalid UTF-8; For rbx
  • Loading branch information
JonRowe committed Feb 18, 2015
2 parents 850b904 + 62204c8 commit 4152945
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/rspec/core/notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions spec/rspec/core/notifications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4152945

Please sign in to comment.