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

Commit

Permalink
Address String#split failures by using EncodedString
Browse files Browse the repository at this point in the history
Add spec for exception when failure_lines has a bad encoding
  • Loading branch information
bf4 committed Feb 9, 2015
1 parent c096053 commit 64e3278
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/rspec/core/notifications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,19 @@ def exception_class_name
name
end

def exception_message
@exception_message ||= begin
string = exception.message.to_s
RSpec::Support::EncodedString.new(string, encoding_of(string))
end
end

def failure_lines
@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|
exception_message.split("\n").each do |line|
lines << " #{line}" if exception.message
end
lines
Expand Down
49 changes: 49 additions & 0 deletions spec/rspec/core/encoding_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# encoding: utf-8
require 'spec_helper'
require 'rspec/core/notifications'

RSpec.describe "FailedExampleNotification", :if => String.method_defined?(:encoding) do
include FormatterSupport

let(:notification) { ::RSpec::Core::Notifications::FailedExampleNotification.new(example) }

before do
example.metadata[:absolute_file_path] = __FILE__
end

describe '#message_lines' do
let(:message_with_invalid_byte_sequence) do
"\xEF \255 \xAD I have bad bytes".force_encoding(Encoding::UTF_8)
end
let(:expected_message) { "? ? ? I have bad bytes" }
let(:exception) do
instance_double(
Exception,
:backtrace => [ "#{__FILE__}:#{__LINE__}"],
:message => message_with_invalid_byte_sequence
)
end

let(:example_group) do
class_double(
RSpec::Core::ExampleGroup,
:metadata => {},
:parent_groups => [],
:location => "#{__FILE__}:#{__LINE__}"
)
end

before do
allow(example).to receive(:example_group) { example_group }
end

context "when failure_lines contains an invalid byte sequence" do
it "replaces bad bytes with a '?'" do
lines = notification.message_lines
expect(lines[0]).to match %r{\AFailure\/Error}
expect(lines[1].strip).to eq(expected_message)
end
end

end
end

0 comments on commit 64e3278

Please sign in to comment.