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

Commit 64e3278

Browse files
committed
Address String#split failures by using EncodedString
Add spec for exception when failure_lines has a bad encoding
1 parent c096053 commit 64e3278

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/rspec/core/notifications.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,19 @@ def exception_class_name
220220
name
221221
end
222222

223+
def exception_message
224+
@exception_message ||= begin
225+
string = exception.message.to_s
226+
RSpec::Support::EncodedString.new(string, encoding_of(string))
227+
end
228+
end
229+
223230
def failure_lines
224231
@failure_lines ||=
225232
begin
226233
lines = ["Failure/Error: #{read_failed_line.strip}"]
227234
lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/
228-
exception.message.to_s.split("\n").each do |line|
235+
exception_message.split("\n").each do |line|
229236
lines << " #{line}" if exception.message
230237
end
231238
lines

spec/rspec/core/encoding_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# encoding: utf-8
2+
require 'spec_helper'
3+
require 'rspec/core/notifications'
4+
5+
RSpec.describe "FailedExampleNotification", :if => String.method_defined?(:encoding) do
6+
include FormatterSupport
7+
8+
let(:notification) { ::RSpec::Core::Notifications::FailedExampleNotification.new(example) }
9+
10+
before do
11+
example.metadata[:absolute_file_path] = __FILE__
12+
end
13+
14+
describe '#message_lines' do
15+
let(:message_with_invalid_byte_sequence) do
16+
"\xEF \255 \xAD I have bad bytes".force_encoding(Encoding::UTF_8)
17+
end
18+
let(:expected_message) { "? ? ? I have bad bytes" }
19+
let(:exception) do
20+
instance_double(
21+
Exception,
22+
:backtrace => [ "#{__FILE__}:#{__LINE__}"],
23+
:message => message_with_invalid_byte_sequence
24+
)
25+
end
26+
27+
let(:example_group) do
28+
class_double(
29+
RSpec::Core::ExampleGroup,
30+
:metadata => {},
31+
:parent_groups => [],
32+
:location => "#{__FILE__}:#{__LINE__}"
33+
)
34+
end
35+
36+
before do
37+
allow(example).to receive(:example_group) { example_group }
38+
end
39+
40+
context "when failure_lines contains an invalid byte sequence" do
41+
it "replaces bad bytes with a '?'" do
42+
lines = notification.message_lines
43+
expect(lines[0]).to match %r{\AFailure\/Error}
44+
expect(lines[1].strip).to eq(expected_message)
45+
end
46+
end
47+
48+
end
49+
end

0 commit comments

Comments
 (0)