Skip to content

Commit

Permalink
[core] Merge pull request rspec/rspec-core#2575 from benoittgt/deal_w…
Browse files Browse the repository at this point in the history
…ith_encoding_error

Handle RSpec description with japanese char in CP932 encoded files

---
This commit was imported from rspec/rspec-core@6c5628f.
  • Loading branch information
benoittgt committed Nov 27, 2018
2 parents 7d561ef + e15b6eb commit c5126ed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rspec-core/appveyor.yml
Expand Up @@ -29,7 +29,7 @@ before_test:
- bundle --version

test_script:
- bundle exec rspec --backtrace
- chcp 65001 && bundle exec rspec --backtrace

environment:
matrix:
Expand Down
13 changes: 12 additions & 1 deletion rspec-core/lib/rspec/core/formatters/exception_presenter.rb
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,17 @@ def formatted_message_and_backtrace(colorizer)
end
end

if String.method_defined?(:encoding)
def encoded_description(description)
return if description.nil?
encoded_string(description)
end
else # for 1.8.7
def encoded_description(description)
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
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 c5126ed

Please sign in to comment.