Skip to content

Commit

Permalink
Fix RELINE_TEST_ENCODING (#743)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ima1zumi authored Nov 29, 2024
1 parent d4f6741 commit 4df825c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
10 changes: 6 additions & 4 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -807,11 +807,13 @@ def editing_mode
target = target.downcase if @config.completion_ignore_case
list.select do |item|
next unless item

unless Encoding.compatible?(target.encoding, item.encoding)
# Crash with Encoding::CompatibilityError is required by readline-ext/test/readline/test_readline.rb
# TODO: fix the test
raise Encoding::CompatibilityError, "#{target.encoding.name} is not compatible with #{item.encoding.name}"
# Workaround for Readline test
if defined?(::Readline) && ::Readline == ::Reline
raise Encoding::CompatibilityError, "incompatible character encodings: #{target.encoding} and #{item.encoding}"
end

next true
end

if @config.completion_ignore_case
Expand Down
27 changes: 17 additions & 10 deletions test/reline/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
end

module Reline
class <<self
class << self
def test_mode(ansi: false)
@original_iogate = IOGate

if ENV['RELINE_TEST_ENCODING']
encoding = Encoding.find(ENV['RELINE_TEST_ENCODING'])
if defined?(RELINE_TEST_ENCODING)
encoding = RELINE_TEST_ENCODING
else
encoding = Encoding::UTF_8
end
Expand Down Expand Up @@ -88,21 +88,24 @@ def test_rubybin
class Reline::TestCase < Test::Unit::TestCase
private def convert_str(input, options = {}, normalized = nil)
return nil if input.nil?
input.chars.map { |c|
input = input.chars.map { |c|
if Reline::Unicode::EscapedChars.include?(c.ord)
c
else
c.encode(@line_editor.encoding, Encoding::UTF_8, **options)
end
}.join
rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
input = input.unicode_normalize(:nfc)
if normalized
options[:undef] = :replace
options[:replace] = '?'
if unicode?(input.encoding)
input = input.unicode_normalize(:nfc)
if normalized
options[:undef] = :replace
options[:replace] = '?'
end
normalized = true
retry
end
normalized = true
retry
input
end

def input_key_by_symbol(input)
Expand Down Expand Up @@ -176,4 +179,8 @@ def assert_key_binding(input, method_symbol, editing_modes = [:emacs, :vi_insert
assert_equal(method_symbol, @config.editing_mode.get(input.bytes))
end
end

private def unicode?(encoding)
[Encoding::UTF_8, Encoding::UTF_16BE, Encoding::UTF_16LE, Encoding::UTF_32BE, Encoding::UTF_32LE].include?(encoding)
end
end
7 changes: 4 additions & 3 deletions test/reline/test_key_actor_emacs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,7 @@ def test_ed_argument_digit_by_meta_num
end

def test_halfwidth_kana_width_dakuten
omit "This test is for UTF-8 but the locale is #{Reline.core.encoding}" if Reline.core.encoding != Encoding::UTF_8
input_raw_keys('ガギゲゴ')
assert_line_around_cursor('ガギゲゴ', '')
input_keys("\C-b\C-b", false)
Expand Down Expand Up @@ -1519,7 +1520,7 @@ def test_vi_editing_mode
def test_undo
input_keys("\C-_", false)
assert_line_around_cursor('', '')
input_keys("aあb\C-h\C-h\C-h", false)
input_keys("aあb\C-h\C-h\C-h".encode(@encoding), false)
assert_line_around_cursor('', '')
input_keys("\C-_", false)
assert_line_around_cursor('a', '')
Expand All @@ -1540,7 +1541,7 @@ def test_undo_with_cursor_position
assert_line_around_cursor('a', 'c')
input_keys("\C-_", false)
assert_line_around_cursor('ab', 'c')
input_keys("あいう\C-b\C-h", false)
input_keys("あいう\C-b\C-h".encode(@encoding), false)
assert_line_around_cursor('abあ', 'うc')
input_keys("\C-_", false)
assert_line_around_cursor('abあい', 'うc')
Expand Down Expand Up @@ -1585,7 +1586,7 @@ def test_undo_with_many_times
end

def test_redo
input_keys("aあb", false)
input_keys("aあb".encode(@encoding), false)
assert_line_around_cursor('aあb', '')
input_keys("\M-\C-_", false)
assert_line_around_cursor('aあb', '')
Expand Down

0 comments on commit 4df825c

Please sign in to comment.