Skip to content

Commit c2874ec

Browse files
authored
Provide a base test class and let tests restore encodings conveniently (#429)
* Create a base TestIRB::TestCase class * Save/restore encodings for tests that initializes InputMethod classes Because `RelineInputMethod#initializes` calls `set_encoding`, which changes stdio/out/err and Encoding's default encoding values, we need to make sure any test that directly or indirectly (e.g. through Context) initializes `RelineInputMethod` restores encodings. `ReadlineInputMethod` also changes encodings but currently no tests cover it. * Remove unnecessary TestHelper module Since we now have a base TestCase, without_rdoc can just live there.
1 parent e23db51 commit c2874ec

13 files changed

+63
-53
lines changed

test/irb/helper.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
module IRB
2-
module TestHelper
3-
def self.without_rdoc(&block)
1+
require "test/unit"
2+
3+
module TestIRB
4+
class TestCase < Test::Unit::TestCase
5+
def save_encodings
6+
@default_encoding = [Encoding.default_external, Encoding.default_internal]
7+
@stdio_encodings = [STDIN, STDOUT, STDERR].map {|io| [io.external_encoding, io.internal_encoding] }
8+
end
9+
10+
def restore_encodings
11+
EnvUtil.suppress_warning do
12+
Encoding.default_external, Encoding.default_internal = *@default_encoding
13+
[STDIN, STDOUT, STDERR].zip(@stdio_encodings) do |io, encs|
14+
io.set_encoding(*encs)
15+
end
16+
end
17+
end
18+
19+
def without_rdoc(&block)
420
::Kernel.send(:alias_method, :old_require, :require)
521

622
::Kernel.define_method(:require) do |name|

test/irb/test_cmd.rb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# frozen_string_literal: false
2-
require "test/unit"
32
require "irb"
43
require "irb/extend-command"
54

65
require_relative "helper"
76

87
module TestIRB
9-
class ExtendCommand < Test::Unit::TestCase
8+
class ExtendCommand < TestCase
109
class TestInputMethod < ::IRB::InputMethod
1110
attr_reader :list, :line_no
1211

@@ -46,8 +45,7 @@ def setup
4645
@home_backup = ENV["HOME"]
4746
ENV["HOME"] = @tmpdir
4847
@xdg_config_home_backup = ENV.delete("XDG_CONFIG_HOME")
49-
@default_encoding = [Encoding.default_external, Encoding.default_internal]
50-
@stdio_encodings = [STDIN, STDOUT, STDERR].map {|io| [io.external_encoding, io.internal_encoding] }
48+
save_encodings
5149
IRB.instance_variable_get(:@CONF).clear
5250
@is_win = (RbConfig::CONFIG['host_os'] =~ /mswin|msys|mingw|cygwin|bccwin|wince|emc/)
5351
end
@@ -57,12 +55,7 @@ def teardown
5755
ENV["HOME"] = @home_backup
5856
Dir.chdir(@pwd)
5957
FileUtils.rm_rf(@tmpdir)
60-
EnvUtil.suppress_warning {
61-
Encoding.default_external, Encoding.default_internal = *@default_encoding
62-
[STDIN, STDOUT, STDERR].zip(@stdio_encodings) do |io, encs|
63-
io.set_encoding(*encs)
64-
end
65-
}
58+
restore_encodings
6659
end
6760

6861
def test_irb_info_multiline
@@ -450,7 +443,7 @@ def test_help_without_rdoc
450443
IRB.conf[:VERBOSE] = false
451444
irb = IRB::Irb.new(IRB::WorkSpace.new(self), input)
452445
out, _ = capture_output do
453-
IRB::TestHelper.without_rdoc do
446+
without_rdoc do
454447
irb.eval_input
455448
end
456449
end

test/irb/test_color.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# frozen_string_literal: false
2-
require 'test/unit'
32
require 'irb/color'
43
require 'rubygems'
54
require 'stringio'
65

6+
require_relative "helper"
7+
78
module TestIRB
8-
class TestColor < Test::Unit::TestCase
9+
class TestColor < TestCase
910
CLEAR = "\e[0m"
1011
BOLD = "\e[1m"
1112
UNDERLINE = "\e[4m"

test/irb/test_color_printer.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# frozen_string_literal: false
2-
require 'test/unit'
32
require 'irb/color_printer'
43
require 'rubygems'
54
require 'stringio'
65

6+
require_relative "helper"
7+
78
module TestIRB
8-
class TestColorPrinter < Test::Unit::TestCase
9+
class TestColorPrinter < TestCase
910
CLEAR = "\e[0m"
1011
BOLD = "\e[1m"
1112
RED = "\e[31m"

test/irb/test_completion.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# frozen_string_literal: false
2-
require "test/unit"
32
require "pathname"
43
require "irb"
54

65
require_relative "helper"
76

87
module TestIRB
9-
class TestCompletion < Test::Unit::TestCase
8+
class TestCompletion < TestCase
109
def setup
1110
# make sure require completion candidates are not cached
1211
IRB::InputCompletor.class_variable_set(:@@files_from_load_path, nil)
@@ -276,7 +275,7 @@ def test_perfect_matching_stops_without_rdoc
276275
result = nil
277276

278277
out, err = capture_output do
279-
IRB::TestHelper.without_rdoc do
278+
without_rdoc do
280279
result = IRB::InputCompletor::PerfectMatchedProc.("String", bind: binding)
281280
end
282281
end

test/irb/test_context.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# frozen_string_literal: false
2-
require 'test/unit'
32
require 'tempfile'
43
require 'irb'
54
require 'rubygems' if defined?(Gem)
65

6+
require_relative "helper"
7+
78
module TestIRB
8-
class TestContext < Test::Unit::TestCase
9+
class TestContext < TestCase
910
class TestInputMethod < ::IRB::InputMethod
1011
attr_reader :list, :line_no
1112

test/irb/test_history.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# frozen_string_literal: false
2-
require 'test/unit'
32
require 'irb'
43
require 'irb/ext/save-history'
54
require 'readline'
65

6+
require_relative "helper"
7+
78
module TestIRB
8-
class TestHistory < Test::Unit::TestCase
9+
class TestHistory < TestCase
910
def setup
1011
IRB.conf[:RC_NAME_GENERATOR] = nil
1112
end

test/irb/test_init.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# frozen_string_literal: false
2-
require "test/unit"
32
require "irb"
43
require "fileutils"
54

5+
require_relative "helper"
6+
67
module TestIRB
7-
class TestInit < Test::Unit::TestCase
8+
class TestInit < TestCase
89
def setup
910
# IRBRC is for RVM...
1011
@backup_env = %w[HOME XDG_CONFIG_HOME IRBRC].each_with_object({}) do |env, hash|

test/irb/test_input_method.rb

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
11
# frozen_string_literal: false
22

3-
require "test/unit"
43
require "irb"
54

65
require_relative "helper"
76

87
module TestIRB
9-
class TestRelineInputMethod < Test::Unit::TestCase
8+
class TestRelineInputMethod < TestCase
109
def setup
1110
@conf_backup = IRB.conf.dup
1211
IRB.conf[:LC_MESSAGES] = IRB::Locale.new
13-
14-
# RelineInputMethod#initialize calls IRB.set_encoding, which mutates standard input/output's encoding
15-
# so we need to make sure we set them back
16-
@original_io_encodings = {
17-
STDIN => [STDIN.external_encoding, STDIN.internal_encoding],
18-
STDOUT => [STDOUT.external_encoding, STDOUT.internal_encoding],
19-
STDERR => [STDERR.external_encoding, STDERR.internal_encoding],
20-
}
21-
@original_default_encodings = [Encoding.default_external, Encoding.default_internal]
12+
save_encodings
2213
end
2314

2415
def teardown
2516
IRB.conf.replace(@conf_backup)
26-
27-
@original_io_encodings.each do |io, (external_encoding, internal_encoding)|
28-
io.set_encoding(external_encoding, internal_encoding)
29-
end
30-
31-
EnvUtil.suppress_warning { Encoding.default_external, Encoding.default_internal = @original_default_encodings }
17+
restore_encodings
3218
end
3319

3420
def test_initialization
@@ -78,7 +64,7 @@ def test_initialization_with_use_autocomplete_but_without_rdoc
7864

7965
IRB.conf[:USE_AUTOCOMPLETE] = true
8066

81-
IRB::TestHelper.without_rdoc do
67+
without_rdoc do
8268
IRB::RelineInputMethod.new
8369
end
8470

test/irb/test_option.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: false
2-
require 'test/unit'
2+
require_relative "helper"
33

44
module TestIRB
5-
class TestOption < Test::Unit::TestCase
5+
class TestOption < TestCase
66
def test_end_of_option
77
bug4117 = '[ruby-core:33574]'
88
bundle_exec = ENV.key?('BUNDLE_GEMFILE') ? ['-rbundler/setup'] : []

0 commit comments

Comments
 (0)