Skip to content

Commit c06dcba

Browse files
ima1zumimatzbot
authored andcommitted
[ruby/reline] Fix RELINE_TEST_ENCODING
(ruby/reline#743) * Fix RELINE_TEST_ENCODING It was not working because it was not environment variable. * Fix Encoding::CompatibilityError: Shift_JIS is not compatible with UTF-8 Error: test_completion_append_character(Reline::KeyActor::EmacsTest): Encoding::CompatibilityError: Shift_JIS is not compatible with UTF-8 /home/runner/work/reline/reline/lib/reline/line_editor.rb:814:in 'block in Reline::LineEditor#filter_normalize_candidates' /home/runner/work/reline/reline/lib/reline/line_editor.rb:808:in 'Array#select' /home/runner/work/reline/reline/lib/reline/line_editor.rb:808:in 'Reline::LineEditor#filter_normalize_candidates' /home/runner/work/reline/reline/lib/reline/line_editor.rb:831:in 'Reline::LineEditor#perform_completion' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1434:in 'Reline::LineEditor#complete' /home/runner/work/reline/reline/lib/reline/line_editor.rb:961:in 'Method#call' /home/runner/work/reline/reline/lib/reline/line_editor.rb:961:in 'Reline::LineEditor#wrap_method_call' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1029:in 'block in Reline::LineEditor#process_key' /home/runner/work/reline/reline/lib/reline/line_editor.rb:932:in 'Reline::LineEditor#run_for_operators' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1028:in 'Reline::LineEditor#process_key' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1051:in 'Reline::LineEditor#normal_char' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1089:in 'Reline::LineEditor#input_key' /home/runner/work/reline/reline/test/reline/helper.rb:124:in 'block in Reline::TestCase#input_keys' /home/runner/work/reline/reline/test/reline/helper.rb:117:in 'Array#each' /home/runner/work/reline/reline/test/reline/helper.rb:117:in 'Reline::TestCase#input_keys' /home/runner/work/reline/reline/test/reline/test_key_actor_emacs.rb:948:in 'Reline::KeyActor::EmacsTest#test_completion_append_character' =============================================================================== =============================================================================== Error: test_continuous_completion_disabled_with_perfect_match(Reline::KeyActor::EmacsTest): Encoding::CompatibilityError: Shift_JIS is not compatible with UTF-8 /home/runner/work/reline/reline/lib/reline/line_editor.rb:814:in 'block in Reline::LineEditor#filter_normalize_candidates' /home/runner/work/reline/reline/lib/reline/line_editor.rb:808:in 'Array#select' /home/runner/work/reline/reline/lib/reline/line_editor.rb:808:in 'Reline::LineEditor#filter_normalize_candidates' /home/runner/work/reline/reline/lib/reline/line_editor.rb:831:in 'Reline::LineEditor#perform_completion' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1434:in 'Reline::LineEditor#complete' /home/runner/work/reline/reline/lib/reline/line_editor.rb:961:in 'Method#call' /home/runner/work/reline/reline/lib/reline/line_editor.rb:961:in 'Reline::LineEditor#wrap_method_call' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1029:in 'block in Reline::LineEditor#process_key' /home/runner/work/reline/reline/lib/reline/line_editor.rb:932:in 'Reline::LineEditor#run_for_operators' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1028:in 'Reline::LineEditor#process_key' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1051:in 'Reline::LineEditor#normal_char' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1089:in 'Reline::LineEditor#input_key' /home/runner/work/reline/reline/test/reline/helper.rb:124:in 'block in Reline::TestCase#input_keys' /home/runner/work/reline/reline/test/reline/helper.rb:117:in 'Array#each' /home/runner/work/reline/reline/test/reline/helper.rb:117:in 'Reline::TestCase#input_keys' /home/runner/work/reline/reline/test/reline/test_key_actor_emacs.rb:936:in 'Reline::KeyActor::EmacsTest#test_continuous_completion_disabled_with_perfect_match' =============================================================================== =============================================================================== Error: test_continuous_completion_with_perfect_match(Reline::KeyActor::EmacsTest): Encoding::CompatibilityError: Shift_JIS is not compatible with UTF-8 /home/runner/work/reline/reline/lib/reline/line_editor.rb:814:in 'block in Reline::LineEditor#filter_normalize_candidates' /home/runner/work/reline/reline/lib/reline/line_editor.rb:808:in 'Array#select' /home/runner/work/reline/reline/lib/reline/line_editor.rb:808:in 'Reline::LineEditor#filter_normalize_candidates' /home/runner/work/reline/reline/lib/reline/line_editor.rb:831:in 'Reline::LineEditor#perform_completion' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1434:in 'Reline::LineEditor#complete' /home/runner/work/reline/reline/lib/reline/line_editor.rb:961:in 'Method#call' /home/runner/work/reline/reline/lib/reline/line_editor.rb:961:in 'Reline::LineEditor#wrap_method_call' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1029:in 'block in Reline::LineEditor#process_key' /home/runner/work/reline/reline/lib/reline/line_editor.rb:932:in 'Reline::LineEditor#run_for_operators' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1028:in 'Reline::LineEditor#process_key' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1051:in 'Reline::LineEditor#normal_char' /home/runner/work/reline/reline/lib/reline/line_editor.rb:1089:in 'Reline::LineEditor#input_key' /home/runner/work/reline/reline/test/reline/helper.rb:124:in 'block in Reline::TestCase#input_keys' /home/runner/work/reline/reline/test/reline/helper.rb:117:in 'Array#each' /home/runner/work/reline/reline/test/reline/helper.rb:117:in 'Reline::TestCase#input_keys' /home/runner/work/reline/reline/test/reline/test_key_actor_emacs.rb:924:in 'Reline::KeyActor::EmacsTest#test_continuous_completion_with_perfect_match' =============================================================================== Finished in 2.118582151 seconds. 385 tests, 1762 assertions, 0 failures, 3 errors, 0 pendings, 3 omissions, 0 notifications ruby/reline@4df825c48f
1 parent 7f34c75 commit c06dcba

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

lib/reline/line_editor.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,11 +807,13 @@ def editing_mode
807807
target = target.downcase if @config.completion_ignore_case
808808
list.select do |item|
809809
next unless item
810-
811810
unless Encoding.compatible?(target.encoding, item.encoding)
812-
# Crash with Encoding::CompatibilityError is required by readline-ext/test/readline/test_readline.rb
813-
# TODO: fix the test
814-
raise Encoding::CompatibilityError, "#{target.encoding.name} is not compatible with #{item.encoding.name}"
811+
# Workaround for Readline test
812+
if defined?(::Readline) && ::Readline == ::Reline
813+
raise Encoding::CompatibilityError, "incompatible character encodings: #{target.encoding} and #{item.encoding}"
814+
end
815+
816+
next true
815817
end
816818

817819
if @config.completion_ignore_case

test/reline/helper.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
end
2020

2121
module Reline
22-
class <<self
22+
class << self
2323
def test_mode(ansi: false)
2424
@original_iogate = IOGate
2525

26-
if ENV['RELINE_TEST_ENCODING']
27-
encoding = Encoding.find(ENV['RELINE_TEST_ENCODING'])
26+
if defined?(RELINE_TEST_ENCODING)
27+
encoding = RELINE_TEST_ENCODING
2828
else
2929
encoding = Encoding::UTF_8
3030
end
@@ -88,21 +88,24 @@ def test_rubybin
8888
class Reline::TestCase < Test::Unit::TestCase
8989
private def convert_str(input, options = {}, normalized = nil)
9090
return nil if input.nil?
91-
input.chars.map { |c|
91+
input = input.chars.map { |c|
9292
if Reline::Unicode::EscapedChars.include?(c.ord)
9393
c
9494
else
9595
c.encode(@line_editor.encoding, Encoding::UTF_8, **options)
9696
end
9797
}.join
9898
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
99-
input = input.unicode_normalize(:nfc)
100-
if normalized
101-
options[:undef] = :replace
102-
options[:replace] = '?'
99+
if unicode?(input.encoding)
100+
input = input.unicode_normalize(:nfc)
101+
if normalized
102+
options[:undef] = :replace
103+
options[:replace] = '?'
104+
end
105+
normalized = true
106+
retry
103107
end
104-
normalized = true
105-
retry
108+
input
106109
end
107110

108111
def input_key_by_symbol(input)
@@ -176,4 +179,8 @@ def assert_key_binding(input, method_symbol, editing_modes = [:emacs, :vi_insert
176179
assert_equal(method_symbol, @config.editing_mode.get(input.bytes))
177180
end
178181
end
182+
183+
private def unicode?(encoding)
184+
[Encoding::UTF_8, Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE].include?(encoding)
185+
end
179186
end

test/reline/test_key_actor_emacs.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,7 @@ def test_ed_argument_digit_by_meta_num
14871487
end
14881488

14891489
def test_halfwidth_kana_width_dakuten
1490+
omit "This test is for UTF-8 but the locale is #{Reline.core.encoding}" if Reline.core.encoding != Encoding::UTF_8
14901491
input_raw_keys('ガギゲゴ')
14911492
assert_line_around_cursor('ガギゲゴ', '')
14921493
input_keys("\C-b\C-b", false)
@@ -1519,7 +1520,7 @@ def test_vi_editing_mode
15191520
def test_undo
15201521
input_keys("\C-_", false)
15211522
assert_line_around_cursor('', '')
1522-
input_keys("aあb\C-h\C-h\C-h", false)
1523+
input_keys("aあb\C-h\C-h\C-h".encode(@encoding), false)
15231524
assert_line_around_cursor('', '')
15241525
input_keys("\C-_", false)
15251526
assert_line_around_cursor('a', '')
@@ -1540,7 +1541,7 @@ def test_undo_with_cursor_position
15401541
assert_line_around_cursor('a', 'c')
15411542
input_keys("\C-_", false)
15421543
assert_line_around_cursor('ab', 'c')
1543-
input_keys("あいう\C-b\C-h", false)
1544+
input_keys("あいう\C-b\C-h".encode(@encoding), false)
15441545
assert_line_around_cursor('abあ', 'うc')
15451546
input_keys("\C-_", false)
15461547
assert_line_around_cursor('abあい', 'うc')
@@ -1585,7 +1586,7 @@ def test_undo_with_many_times
15851586
end
15861587

15871588
def test_redo
1588-
input_keys("aあb", false)
1589+
input_keys("aあb".encode(@encoding), false)
15891590
assert_line_around_cursor('aあb', '')
15901591
input_keys("\M-\C-_", false)
15911592
assert_line_around_cursor('aあb', '')

0 commit comments

Comments
 (0)