Skip to content

Commit a2a3cbb

Browse files
authored
Prevent irb_history's creation during HistoryTest (#893)
Some cases of it currently create `~/.irb_history` files unintentionally while others don't. This is caused by the varying levels of setup/cleanup between them. This commit fixes the issue by wrapping every single test inside a consistent test setup and teardown callbacks.
1 parent b53ebc6 commit a2a3cbb

File tree

1 file changed

+45
-52
lines changed

1 file changed

+45
-52
lines changed

test/irb/test_history.rb

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,23 @@
1010
module TestIRB
1111
class HistoryTest < TestCase
1212
def setup
13+
@original_verbose, $VERBOSE = $VERBOSE, nil
14+
@tmpdir = Dir.mktmpdir("test_irb_history_")
15+
@backup_home = ENV["HOME"]
16+
@backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
17+
@backup_irbrc = ENV.delete("IRBRC")
18+
@backup_default_external = Encoding.default_external
19+
ENV["HOME"] = @tmpdir
1320
IRB.conf[:RC_NAME_GENERATOR] = nil
1421
end
1522

1623
def teardown
1724
IRB.conf[:RC_NAME_GENERATOR] = nil
25+
ENV["HOME"] = @backup_home
26+
ENV["XDG_CONFIG_HOME"] = @backup_xdg_config_home
27+
ENV["IRBRC"] = @backup_irbrc
28+
Encoding.default_external = @backup_default_external
29+
$VERBOSE = @original_verbose
1830
end
1931

2032
class TestInputMethodWithRelineHistory < TestInputMethod
@@ -123,35 +135,23 @@ def test_history_concurrent_use_readline
123135
end
124136

125137
def test_history_concurrent_use_not_present
126-
backup_home = ENV["HOME"]
127-
backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
128-
backup_irbrc = ENV.delete("IRBRC")
129138
IRB.conf[:LC_MESSAGES] = IRB::Locale.new
130139
IRB.conf[:SAVE_HISTORY] = 1
131-
Dir.mktmpdir("test_irb_history_") do |tmpdir|
132-
ENV["HOME"] = tmpdir
133-
io = TestInputMethodWithRelineHistory.new
134-
io.class::HISTORY.clear
135-
io.load_history
136-
io.class::HISTORY << 'line1'
137-
io.class::HISTORY << 'line2'
140+
io = TestInputMethodWithRelineHistory.new
141+
io.class::HISTORY.clear
142+
io.load_history
143+
io.class::HISTORY << 'line1'
144+
io.class::HISTORY << 'line2'
138145

139-
history_file = IRB.rc_files("_history").first
140-
assert_not_send [File, :file?, history_file]
141-
File.write(history_file, "line0\n")
142-
io.save_history
143-
assert_equal(%w"line0 line1 line2", File.read(history_file).split)
144-
end
145-
ensure
146-
ENV["HOME"] = backup_home
147-
ENV["XDG_CONFIG_HOME"] = backup_xdg_config_home
148-
ENV["IRBRC"] = backup_irbrc
146+
history_file = IRB.rc_files("_history").first
147+
assert_not_send [File, :file?, history_file]
148+
File.write(history_file, "line0\n")
149+
io.save_history
150+
assert_equal(%w"line0 line1 line2", File.read(history_file).split)
149151
end
150152

151153
def test_history_different_encodings
152-
backup_default_external = Encoding.default_external
153154
IRB.conf[:SAVE_HISTORY] = 2
154-
verbose_bak, $VERBOSE = $VERBOSE, nil
155155
Encoding.default_external = Encoding::US_ASCII
156156
locale = IRB::Locale.new("C")
157157
assert_history(<<~EXPECTED_HISTORY.encode(Encoding::US_ASCII), <<~INITIAL_HISTORY.encode(Encoding::UTF_8), <<~INPUT, locale: locale)
@@ -162,9 +162,6 @@ def test_history_different_encodings
162162
INITIAL_HISTORY
163163
exit
164164
INPUT
165-
ensure
166-
Encoding.default_external = backup_default_external
167-
$VERBOSE = verbose_bak
168165
end
169166

170167
def test_history_does_not_raise_when_history_file_directory_does_not_exist
@@ -176,6 +173,11 @@ def test_history_does_not_raise_when_history_file_directory_does_not_exist
176173
assert_warn(/history file does not exist/) do
177174
io.save_history
178175
end
176+
177+
# assert_warn reverts $VERBOSE to EnvUtil.original_verbose, which is true in some cases
178+
# We want to keep $VERBOSE as nil until teardown is called
179+
# TODO: check if this is an assert_warn issue
180+
$VERBOSE = nil
179181
ensure
180182
IRB.conf[:HISTORY_FILE] = backup_history_file
181183
end
@@ -212,46 +214,37 @@ def history_concurrent_use_for_input_method(input_method)
212214
end
213215

214216
def assert_history(expected_history, initial_irb_history, input, input_method = TestInputMethodWithRelineHistory, locale: IRB::Locale.new)
215-
backup_verbose, $VERBOSE = $VERBOSE, nil
216-
backup_home = ENV["HOME"]
217-
backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
218217
IRB.conf[:LC_MESSAGES] = locale
219218
actual_history = nil
220219
history_file = IRB.rc_files("_history").first
221-
Dir.mktmpdir("test_irb_history_") do |tmpdir|
222-
ENV["HOME"] = tmpdir
223-
File.open(history_file, "w") do |f|
224-
f.write(initial_irb_history)
225-
end
220+
ENV["HOME"] = @tmpdir
221+
File.open(history_file, "w") do |f|
222+
f.write(initial_irb_history)
223+
end
226224

227-
io = input_method.new
225+
io = input_method.new
226+
io.class::HISTORY.clear
227+
io.load_history
228+
if block_given?
229+
previous_history = []
230+
io.class::HISTORY.each { |line| previous_history << line }
231+
yield history_file
228232
io.class::HISTORY.clear
229-
io.load_history
230-
if block_given?
231-
previous_history = []
232-
io.class::HISTORY.each { |line| previous_history << line }
233-
yield history_file
234-
io.class::HISTORY.clear
235-
previous_history.each { |line| io.class::HISTORY << line }
236-
end
237-
input.split.each { |line| io.class::HISTORY << line }
238-
io.save_history
233+
previous_history.each { |line| io.class::HISTORY << line }
234+
end
235+
input.split.each { |line| io.class::HISTORY << line }
236+
io.save_history
239237

240-
io.load_history
241-
File.open(history_file, "r") do |f|
242-
actual_history = f.read
243-
end
238+
io.load_history
239+
File.open(history_file, "r") do |f|
240+
actual_history = f.read
244241
end
245242
assert_equal(expected_history, actual_history, <<~MESSAGE)
246243
expected:
247244
#{expected_history}
248245
but actual:
249246
#{actual_history}
250247
MESSAGE
251-
ensure
252-
$VERBOSE = backup_verbose
253-
ENV["HOME"] = backup_home
254-
ENV["XDG_CONFIG_HOME"] = backup_xdg_config_home
255248
end
256249

257250
def with_temp_stdio

0 commit comments

Comments
 (0)