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

Commit 6c5628f

Browse files
authored
Merge pull request #2575 from benoittgt/deal_with_encoding_error
Handle RSpec description with japanese char in CP932 encoded files
2 parents 5df0e1e + 8372e5c commit 6c5628f

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

appveyor.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ before_test:
2929
- bundle --version
3030

3131
test_script:
32-
- bundle exec rspec --backtrace
32+
- chcp 65001 && bundle exec rspec --backtrace
3333

3434
environment:
3535
matrix:

lib/rspec/core/formatters/exception_presenter.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::Console
8181

8282
def fully_formatted_lines(failure_number, colorizer)
8383
lines = [
84-
description,
84+
encoded_description(description),
8585
detail_formatter.call(example, colorizer),
8686
formatted_message_and_backtrace(colorizer),
8787
extra_detail_formatter.call(failure_number, colorizer),
@@ -244,6 +244,17 @@ def formatted_message_and_backtrace(colorizer)
244244
end
245245
end
246246

247+
if String.method_defined?(:encoding)
248+
def encoded_description(description)
249+
return if description.nil?
250+
encoded_string(description)
251+
end
252+
else # for 1.8.7
253+
def encoded_description(description)
254+
description
255+
end
256+
end
257+
247258
def exception_backtrace
248259
exception.backtrace || []
249260
end

spec/rspec/core/formatters/exception_presenter_spec.rb

+17
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ module RSpec::Core
9494
EOS
9595
end
9696

97+
if String.method_defined?(:encoding)
98+
it 'allows the caller to add encoded description' do
99+
the_presenter = Formatters::ExceptionPresenter.new(exception, example,
100+
:description => "ジ".encode("CP932"))
101+
102+
expect(the_presenter.fully_formatted(1)).to eq(<<-EOS.gsub(/^ +\|/, ''))
103+
|
104+
| 1) ジ
105+
| Failure/Error: # The failure happened here!#{ encoding_check }
106+
|
107+
| Boom
108+
| Bam
109+
| # ./spec/rspec/core/formatters/exception_presenter_spec.rb:#{line_num}
110+
EOS
111+
end
112+
end
113+
97114
it 'allows the caller to omit the description' do
98115
the_presenter = Formatters::ExceptionPresenter.new(exception, example,
99116
:detail_formatter => Proc.new { "Detail!" },

0 commit comments

Comments
 (0)