Skip to content

Commit 010dce9

Browse files
committed
Always add input method when calling Irb.new in tests
When passes input method as nil to Context.new through Irb.new, ReidlineInputMethod.new is executed and the global internal state of Reline is rewritten, therefore other tests are failed in the Ruby repository. This commit changes to use TestInputMethod.
1 parent 882b24f commit 010dce9

File tree

1 file changed

+42
-44
lines changed

1 file changed

+42
-44
lines changed

test/irb/test_cmd.rb

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@
55

66
module TestIRB
77
class ExtendCommand < Test::Unit::TestCase
8+
class TestInputMethod < ::IRB::InputMethod
9+
attr_reader :list, :line_no
10+
11+
def initialize(list = [])
12+
super("test")
13+
@line_no = 0
14+
@list = list
15+
end
16+
17+
def gets
18+
@list[@line_no]&.tap {@line_no += 1}
19+
end
20+
21+
def eof?
22+
@line_no >= @list.size
23+
end
24+
25+
def encoding
26+
Encoding.default_external
27+
end
28+
29+
def reset
30+
@line_no = 0
31+
end
32+
end
33+
834
def setup
935
@pwd = Dir.pwd
1036
@tmpdir = File.join(Dir.tmpdir, "test_reline_config_#{$$}")
@@ -44,12 +70,12 @@ def test_irb_info_multiline
4470
IRB.conf[:USE_SINGLELINE] = false
4571
IRB.conf[:VERBOSE] = false
4672
workspace = IRB::WorkSpace.new(self)
47-
irb = IRB::Irb.new(workspace)
73+
irb = IRB::Irb.new(workspace, TestInputMethod.new([]))
4874
IRB.conf[:MAIN_CONTEXT] = irb.context
4975
expected = %r{
5076
Ruby\sversion: .+\n
5177
IRB\sversion:\sirb .+\n
52-
InputMethod:\sReidlineInputMethod\swith\sReline .+ and .+\n
78+
InputMethod:\sAbstract\sInputMethod\n
5379
\.irbrc\spath: .+\n
5480
RUBY_PLATFORM: .+
5581
}x
@@ -64,12 +90,12 @@ def test_irb_info_singleline
6490
IRB.conf[:USE_SINGLELINE] = true
6591
IRB.conf[:VERBOSE] = false
6692
workspace = IRB::WorkSpace.new(self)
67-
irb = IRB::Irb.new(workspace)
93+
irb = IRB::Irb.new(workspace, TestInputMethod.new([]))
6894
IRB.conf[:MAIN_CONTEXT] = irb.context
6995
expected = %r{
7096
Ruby\sversion: .+\n
7197
IRB\sversion:\sirb .+\n
72-
InputMethod:\sReadlineInputMethod\swith .+ and .+\n
98+
InputMethod:\sAbstract\sInputMethod\n
7399
\.irbrc\spath: .+\n
74100
RUBY_PLATFORM: .+
75101
}x
@@ -87,12 +113,12 @@ def test_irb_info_multiline_without_rc_files
87113
IRB.conf[:USE_SINGLELINE] = false
88114
IRB.conf[:VERBOSE] = false
89115
workspace = IRB::WorkSpace.new(self)
90-
irb = IRB::Irb.new(workspace)
116+
irb = IRB::Irb.new(workspace, TestInputMethod.new([]))
91117
IRB.conf[:MAIN_CONTEXT] = irb.context
92118
expected = %r{
93119
Ruby\sversion: .+\n
94120
IRB\sversion:\sirb .+\n
95-
InputMethod:\sReidlineInputMethod\swith\sReline\s[^ ]+(?!\sand\s.+)\n
121+
InputMethod:\sAbstract\sInputMethod\n
96122
RUBY_PLATFORM: .+\n
97123
\z
98124
}x
@@ -114,12 +140,12 @@ def test_irb_info_singleline_without_rc_files
114140
IRB.conf[:USE_SINGLELINE] = true
115141
IRB.conf[:VERBOSE] = false
116142
workspace = IRB::WorkSpace.new(self)
117-
irb = IRB::Irb.new(workspace)
143+
irb = IRB::Irb.new(workspace, TestInputMethod.new([]))
118144
IRB.conf[:MAIN_CONTEXT] = irb.context
119145
expected = %r{
120146
Ruby\sversion: .+\n
121147
IRB\sversion:\sirb .+\n
122-
InputMethod:\sReadlineInputMethod\swith\s(?~.*\sand\s.+)\n
148+
InputMethod:\sAbstract\sInputMethod\n
123149
RUBY_PLATFORM: .+\n
124150
\z
125151
}x
@@ -130,32 +156,6 @@ def test_irb_info_singleline_without_rc_files
130156
IRB.const_set(:IRBRC_EXT, ext_backup)
131157
end
132158

133-
class TestInputMethod < ::IRB::InputMethod
134-
attr_reader :list, :line_no
135-
136-
def initialize(list = [])
137-
super("test")
138-
@line_no = 0
139-
@list = list
140-
end
141-
142-
def gets
143-
@list[@line_no]&.tap {@line_no += 1}
144-
end
145-
146-
def eof?
147-
@line_no >= @list.size
148-
end
149-
150-
def encoding
151-
Encoding.default_external
152-
end
153-
154-
def reset
155-
@line_no = 0
156-
end
157-
end
158-
159159
def test_measure
160160
IRB.init_config(nil)
161161
IRB.conf[:PROMPT] = {
@@ -376,15 +376,14 @@ def test_irb_load
376376
end
377377

378378
def test_ls
379+
input = TestInputMethod.new([
380+
"ls Object.new.tap { |o| o.instance_variable_set(:@a, 1) }\n",
381+
])
379382
IRB.init_config(nil)
380383
workspace = IRB::WorkSpace.new(self)
384+
irb = IRB::Irb.new(workspace, input)
381385
IRB.conf[:VERBOSE] = false
382-
irb = IRB::Irb.new(workspace)
383386
IRB.conf[:MAIN_CONTEXT] = irb.context
384-
input = TestInputMethod.new([
385-
"ls Object.new.tap { |o| o.instance_variable_set(:@a, 1) }\n",
386-
])
387-
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
388387
irb.context.return_format = "=> %s\n"
389388
out, err = capture_output do
390389
irb.eval_input
@@ -394,15 +393,14 @@ def test_ls
394393
end
395394

396395
def test_whereami
396+
input = TestInputMethod.new([
397+
"whereami\n",
398+
])
397399
IRB.init_config(nil)
398400
workspace = IRB::WorkSpace.new(self)
401+
irb = IRB::Irb.new(workspace, input)
399402
IRB.conf[:VERBOSE] = false
400-
irb = IRB::Irb.new(workspace)
401403
IRB.conf[:MAIN_CONTEXT] = irb.context
402-
input = TestInputMethod.new([
403-
"whereami\n",
404-
])
405-
irb = IRB::Irb.new(IRB::WorkSpace.new(Object.new), input)
406404
irb.context.return_format = "=> %s\n"
407405
out, err = capture_output do
408406
irb.eval_input

0 commit comments

Comments
 (0)