Skip to content

Commit

Permalink
Properly restore Reline::IOGate in test teardown (#593)
Browse files Browse the repository at this point in the history
* Properly restore Reline::IOGate in test teardown

* GeneralIO.reset should reset class variable existence
  • Loading branch information
tompng authored Sep 28, 2023
1 parent 4d34e52 commit c16d33d
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 c16d33d

Please sign in to comment.