[ruby/irb] Prevent irb_history
's creation during HistoryTest
(https://github.com/ruby/irb/pull/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. https://github.com/ruby/irb/commit/a2a3cbb0ef
This commit is contained in:
parent
51965399df
commit
97d32bb2fc
@ -10,11 +10,23 @@ return if RUBY_PLATFORM.match?(/solaris|mswin|mingw/i)
|
|||||||
module TestIRB
|
module TestIRB
|
||||||
class HistoryTest < TestCase
|
class HistoryTest < TestCase
|
||||||
def setup
|
def setup
|
||||||
|
@original_verbose, $VERBOSE = $VERBOSE, nil
|
||||||
|
@tmpdir = Dir.mktmpdir("test_irb_history_")
|
||||||
|
@backup_home = ENV["HOME"]
|
||||||
|
@backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
|
||||||
|
@backup_irbrc = ENV.delete("IRBRC")
|
||||||
|
@backup_default_external = Encoding.default_external
|
||||||
|
ENV["HOME"] = @tmpdir
|
||||||
IRB.conf[:RC_NAME_GENERATOR] = nil
|
IRB.conf[:RC_NAME_GENERATOR] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
IRB.conf[:RC_NAME_GENERATOR] = nil
|
IRB.conf[:RC_NAME_GENERATOR] = nil
|
||||||
|
ENV["HOME"] = @backup_home
|
||||||
|
ENV["XDG_CONFIG_HOME"] = @backup_xdg_config_home
|
||||||
|
ENV["IRBRC"] = @backup_irbrc
|
||||||
|
Encoding.default_external = @backup_default_external
|
||||||
|
$VERBOSE = @original_verbose
|
||||||
end
|
end
|
||||||
|
|
||||||
class TestInputMethodWithRelineHistory < TestInputMethod
|
class TestInputMethodWithRelineHistory < TestInputMethod
|
||||||
@ -123,35 +135,23 @@ module TestIRB
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_history_concurrent_use_not_present
|
def test_history_concurrent_use_not_present
|
||||||
backup_home = ENV["HOME"]
|
|
||||||
backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
|
|
||||||
backup_irbrc = ENV.delete("IRBRC")
|
|
||||||
IRB.conf[:LC_MESSAGES] = IRB::Locale.new
|
IRB.conf[:LC_MESSAGES] = IRB::Locale.new
|
||||||
IRB.conf[:SAVE_HISTORY] = 1
|
IRB.conf[:SAVE_HISTORY] = 1
|
||||||
Dir.mktmpdir("test_irb_history_") do |tmpdir|
|
io = TestInputMethodWithRelineHistory.new
|
||||||
ENV["HOME"] = tmpdir
|
io.class::HISTORY.clear
|
||||||
io = TestInputMethodWithRelineHistory.new
|
io.load_history
|
||||||
io.class::HISTORY.clear
|
io.class::HISTORY << 'line1'
|
||||||
io.load_history
|
io.class::HISTORY << 'line2'
|
||||||
io.class::HISTORY << 'line1'
|
|
||||||
io.class::HISTORY << 'line2'
|
|
||||||
|
|
||||||
history_file = IRB.rc_files("_history").first
|
history_file = IRB.rc_files("_history").first
|
||||||
assert_not_send [File, :file?, history_file]
|
assert_not_send [File, :file?, history_file]
|
||||||
File.write(history_file, "line0\n")
|
File.write(history_file, "line0\n")
|
||||||
io.save_history
|
io.save_history
|
||||||
assert_equal(%w"line0 line1 line2", File.read(history_file).split)
|
assert_equal(%w"line0 line1 line2", File.read(history_file).split)
|
||||||
end
|
|
||||||
ensure
|
|
||||||
ENV["HOME"] = backup_home
|
|
||||||
ENV["XDG_CONFIG_HOME"] = backup_xdg_config_home
|
|
||||||
ENV["IRBRC"] = backup_irbrc
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_history_different_encodings
|
def test_history_different_encodings
|
||||||
backup_default_external = Encoding.default_external
|
|
||||||
IRB.conf[:SAVE_HISTORY] = 2
|
IRB.conf[:SAVE_HISTORY] = 2
|
||||||
verbose_bak, $VERBOSE = $VERBOSE, nil
|
|
||||||
Encoding.default_external = Encoding::US_ASCII
|
Encoding.default_external = Encoding::US_ASCII
|
||||||
locale = IRB::Locale.new("C")
|
locale = IRB::Locale.new("C")
|
||||||
assert_history(<<~EXPECTED_HISTORY.encode(Encoding::US_ASCII), <<~INITIAL_HISTORY.encode(Encoding::UTF_8), <<~INPUT, locale: locale)
|
assert_history(<<~EXPECTED_HISTORY.encode(Encoding::US_ASCII), <<~INITIAL_HISTORY.encode(Encoding::UTF_8), <<~INPUT, locale: locale)
|
||||||
@ -162,9 +162,6 @@ module TestIRB
|
|||||||
INITIAL_HISTORY
|
INITIAL_HISTORY
|
||||||
exit
|
exit
|
||||||
INPUT
|
INPUT
|
||||||
ensure
|
|
||||||
Encoding.default_external = backup_default_external
|
|
||||||
$VERBOSE = verbose_bak
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_history_does_not_raise_when_history_file_directory_does_not_exist
|
def test_history_does_not_raise_when_history_file_directory_does_not_exist
|
||||||
@ -176,6 +173,11 @@ module TestIRB
|
|||||||
assert_warn(/history file does not exist/) do
|
assert_warn(/history file does not exist/) do
|
||||||
io.save_history
|
io.save_history
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# assert_warn reverts $VERBOSE to EnvUtil.original_verbose, which is true in some cases
|
||||||
|
# We want to keep $VERBOSE as nil until teardown is called
|
||||||
|
# TODO: check if this is an assert_warn issue
|
||||||
|
$VERBOSE = nil
|
||||||
ensure
|
ensure
|
||||||
IRB.conf[:HISTORY_FILE] = backup_history_file
|
IRB.conf[:HISTORY_FILE] = backup_history_file
|
||||||
end
|
end
|
||||||
@ -212,35 +214,30 @@ module TestIRB
|
|||||||
end
|
end
|
||||||
|
|
||||||
def assert_history(expected_history, initial_irb_history, input, input_method = TestInputMethodWithRelineHistory, locale: IRB::Locale.new)
|
def assert_history(expected_history, initial_irb_history, input, input_method = TestInputMethodWithRelineHistory, locale: IRB::Locale.new)
|
||||||
backup_verbose, $VERBOSE = $VERBOSE, nil
|
|
||||||
backup_home = ENV["HOME"]
|
|
||||||
backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
|
|
||||||
IRB.conf[:LC_MESSAGES] = locale
|
IRB.conf[:LC_MESSAGES] = locale
|
||||||
actual_history = nil
|
actual_history = nil
|
||||||
history_file = IRB.rc_files("_history").first
|
history_file = IRB.rc_files("_history").first
|
||||||
Dir.mktmpdir("test_irb_history_") do |tmpdir|
|
ENV["HOME"] = @tmpdir
|
||||||
ENV["HOME"] = tmpdir
|
File.open(history_file, "w") do |f|
|
||||||
File.open(history_file, "w") do |f|
|
f.write(initial_irb_history)
|
||||||
f.write(initial_irb_history)
|
end
|
||||||
end
|
|
||||||
|
|
||||||
io = input_method.new
|
io = input_method.new
|
||||||
|
io.class::HISTORY.clear
|
||||||
|
io.load_history
|
||||||
|
if block_given?
|
||||||
|
previous_history = []
|
||||||
|
io.class::HISTORY.each { |line| previous_history << line }
|
||||||
|
yield history_file
|
||||||
io.class::HISTORY.clear
|
io.class::HISTORY.clear
|
||||||
io.load_history
|
previous_history.each { |line| io.class::HISTORY << line }
|
||||||
if block_given?
|
end
|
||||||
previous_history = []
|
input.split.each { |line| io.class::HISTORY << line }
|
||||||
io.class::HISTORY.each { |line| previous_history << line }
|
io.save_history
|
||||||
yield history_file
|
|
||||||
io.class::HISTORY.clear
|
|
||||||
previous_history.each { |line| io.class::HISTORY << line }
|
|
||||||
end
|
|
||||||
input.split.each { |line| io.class::HISTORY << line }
|
|
||||||
io.save_history
|
|
||||||
|
|
||||||
io.load_history
|
io.load_history
|
||||||
File.open(history_file, "r") do |f|
|
File.open(history_file, "r") do |f|
|
||||||
actual_history = f.read
|
actual_history = f.read
|
||||||
end
|
|
||||||
end
|
end
|
||||||
assert_equal(expected_history, actual_history, <<~MESSAGE)
|
assert_equal(expected_history, actual_history, <<~MESSAGE)
|
||||||
expected:
|
expected:
|
||||||
@ -248,10 +245,6 @@ module TestIRB
|
|||||||
but actual:
|
but actual:
|
||||||
#{actual_history}
|
#{actual_history}
|
||||||
MESSAGE
|
MESSAGE
|
||||||
ensure
|
|
||||||
$VERBOSE = backup_verbose
|
|
||||||
ENV["HOME"] = backup_home
|
|
||||||
ENV["XDG_CONFIG_HOME"] = backup_xdg_config_home
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_temp_stdio
|
def with_temp_stdio
|
||||||
|
Loading…
x
Reference in New Issue
Block a user