Skip to content

Commit

Permalink
[ruby/reline] Properly restore Reline::IOGate in test teardown
Browse files Browse the repository at this point in the history
(ruby/reline#593)

* Properly restore Reline::IOGate in test teardown

* GeneralIO.reset should reset class variable existence

ruby/reline@c16d33dae5
  • Loading branch information
tompng authored and matzbot committed Sep 28, 2023
1 parent 20c561f commit d494254
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
6 changes: 5 additions & 1 deletion lib/reline/general_io.rb
Expand Up @@ -3,7 +3,11 @@
class Reline::GeneralIO
def self.reset(encoding: nil)
@@pasting = false
@@encoding = encoding
if encoding
@@encoding = encoding
elsif defined?(@@encoding)
remove_class_variable(:@@encoding)
end
end

def self.encoding
Expand Down
26 changes: 14 additions & 12 deletions test/reline/helper.rb
Expand Up @@ -21,21 +21,23 @@
module Reline
class <<self
def test_mode(ansi: false)
remove_const('IOGate') if const_defined?('IOGate')
const_set('IOGate', ansi ? Reline::ANSI : Reline::GeneralIO)
if ENV['RELINE_TEST_ENCODING']
encoding = Encoding.find(ENV['RELINE_TEST_ENCODING'])
else
encoding = Encoding::UTF_8
end
Reline::GeneralIO.reset(encoding: encoding) unless ansi
core.config.instance_variable_set(:@test_mode, true)
core.config.reset
@original_iogate = IOGate
remove_const('IOGate')
const_set('IOGate', ansi ? Reline::ANSI : Reline::GeneralIO)
if ENV['RELINE_TEST_ENCODING']
encoding = Encoding.find(ENV['RELINE_TEST_ENCODING'])
else
encoding = Encoding::UTF_8
end
Reline::GeneralIO.reset(encoding: encoding) unless ansi
core.config.instance_variable_set(:@test_mode, true)
core.config.reset
end

def test_reset
remove_const('IOGate') if const_defined?('IOGate')
const_set('IOGate', Reline::GeneralIO)
remove_const('IOGate')
const_set('IOGate', @original_iogate)
Reline::GeneralIO.reset
Reline.instance_variable_set(:@core, nil)
end

Expand Down
1 change: 1 addition & 0 deletions test/reline/test_reline_key.rb
Expand Up @@ -3,6 +3,7 @@

class Reline::TestKey < Reline::TestCase
def setup
Reline.test_mode
end

def teardown
Expand Down

0 comments on commit d494254

Please sign in to comment.