Skip to content

Commit aff7bc6

Browse files
committed
Merge pull request rspec#1760 from bf4/handle_non_utf8_exception_messages
Handle exception message with invalid UTF-8; For rbx
2 parents aa1a0c6 + 77bc402 commit aff7bc6

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/rspec/core/notifications.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,17 @@ def fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::Console
205205
def encoding_of(string)
206206
string.encoding
207207
end
208+
209+
def encoded_string(string)
210+
RSpec::Support::EncodedString.new(string, Encoding.default_external)
211+
end
208212
else
209213
def encoding_of(_string)
210214
end
215+
216+
def encoded_string(string)
217+
RSpec::Support::EncodedString.new(string)
218+
end
211219
end
212220

213221
def backtrace_formatter
@@ -225,8 +233,8 @@ def failure_lines
225233
begin
226234
lines = ["Failure/Error: #{read_failed_line.strip}"]
227235
lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/
228-
exception.message.to_s.split("\n").each do |line|
229-
lines << " #{line}" if exception.message
236+
encoded_string(exception.message.to_s).split("\n").each do |line|
237+
lines << " #{line}"
230238
end
231239
lines
232240
end

spec/rspec/core/notifications_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,18 @@
100100
expect(lines[0]).to match %r{\AFailure\/Error}
101101
expect(lines[1]).to match %r{\A\s*Test exception\z}
102102
end
103+
104+
if String.method_defined?(:encoding)
105+
it "returns failures_lines with invalid bytes replace by '?'" do
106+
message_with_invalid_byte_sequence =
107+
"\xEF \255 \xAD I have bad bytes".force_encoding(Encoding::UTF_8)
108+
allow(exception).to receive(:message).
109+
and_return(message_with_invalid_byte_sequence)
110+
111+
lines = notification.message_lines
112+
expect(lines[0]).to match %r{\AFailure\/Error}
113+
expect(lines[1].strip).to eq("? ? ? I have bad bytes")
114+
end
115+
end
103116
end
104117
end

0 commit comments

Comments
 (0)