Skip to content

Commit

Permalink
[core] Handle RSpec description with japanese char in CP932 encoded f…
Browse files Browse the repository at this point in the history
…iles

When the user create a test with CP932 encoding and japanese chars RSpec
fail to properly display characters or crash with the error:

>  Encoding::CompatibilityError: incompatible character encodings: Windows-31J and UTF-8

Fix:
- rspec/rspec-core#2570
- https://github.com/rspec/rspec-core/issues/2543

For Ruby 1.8.7
We are following the same behavior as for rspec-support
https://github.com/rspec/rspec-support/blob/9940a8656071655b807f772f36101b4d27f1b67d/lib/rspec/support/spec/string_matcher.rb#L8

---
This commit was imported from rspec/rspec-core@05611c5.
  • Loading branch information
benoittgt committed Nov 16, 2018
1 parent 211040c commit 63398c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
12 changes: 11 additions & 1 deletion rspec-core/lib/rspec/core/formatters/exception_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::Console

def fully_formatted_lines(failure_number, colorizer)
lines = [
description,
encoded_description(description),
detail_formatter.call(example, colorizer),
formatted_message_and_backtrace(colorizer),
extra_detail_formatter.call(failure_number, colorizer),
Expand Down Expand Up @@ -244,6 +244,16 @@ def formatted_message_and_backtrace(colorizer)
end
end

def encoded_description(description)
return if description.nil?

if String.method_defined?(:encoding)
encoded_string(description)
else # for 1.8.7
description
end
end

def exception_backtrace
exception.backtrace || []
end
Expand Down
17 changes: 17 additions & 0 deletions rspec-core/spec/rspec/core/formatters/exception_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ module RSpec::Core
EOS
end

if String.method_defined?(:encoding)
it 'allows the caller to add encoded description' do
the_presenter = Formatters::ExceptionPresenter.new(exception, example,
:description => "ジ".encode("CP932"))

expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
|
| 1) ジ
| Failure/Error: # The failure happened here!#{ encoding_check }
|
| Boom
| Bam
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
EOS
end
end

it 'allows the caller to omit the description' do
the_presenter = Formatters::ExceptionPresenter.new(exception, example,
:detail_formatter => Proc.new { "Detail!" },
Expand Down

0 comments on commit 63398c8

Please sign in to comment.