Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent irb_history's creation during HistoryTest #893

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 45 additions & 52 deletions test/irb/test_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@
module TestIRB
class HistoryTest < TestCase
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
end

def teardown
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

class TestInputMethodWithRelineHistory < TestInputMethod
Expand Down Expand Up @@ -123,35 +135,23 @@ def test_history_concurrent_use_readline
end

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[:SAVE_HISTORY] = 1
Dir.mktmpdir("test_irb_history_") do |tmpdir|
ENV["HOME"] = tmpdir
io = TestInputMethodWithRelineHistory.new
io.class::HISTORY.clear
io.load_history
io.class::HISTORY << 'line1'
io.class::HISTORY << 'line2'
io = TestInputMethodWithRelineHistory.new
io.class::HISTORY.clear
io.load_history
io.class::HISTORY << 'line1'
io.class::HISTORY << 'line2'

history_file = IRB.rc_files("_history").first
assert_not_send [File, :file?, history_file]
File.write(history_file, "line0\n")
io.save_history
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
history_file = IRB.rc_files("_history").first
assert_not_send [File, :file?, history_file]
File.write(history_file, "line0\n")
io.save_history
assert_equal(%w"line0 line1 line2", File.read(history_file).split)
end

def test_history_different_encodings
backup_default_external = Encoding.default_external
IRB.conf[:SAVE_HISTORY] = 2
verbose_bak, $VERBOSE = $VERBOSE, nil
Encoding.default_external = Encoding::US_ASCII
locale = IRB::Locale.new("C")
assert_history(<<~EXPECTED_HISTORY.encode(Encoding::US_ASCII), <<~INITIAL_HISTORY.encode(Encoding::UTF_8), <<~INPUT, locale: locale)
Expand All @@ -162,9 +162,6 @@ def test_history_different_encodings
INITIAL_HISTORY
exit
INPUT
ensure
Encoding.default_external = backup_default_external
$VERBOSE = verbose_bak
end

def test_history_does_not_raise_when_history_file_directory_does_not_exist
Expand All @@ -176,6 +173,11 @@ def test_history_does_not_raise_when_history_file_directory_does_not_exist
assert_warn(/history file does not exist/) do
io.save_history
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
IRB.conf[:HISTORY_FILE] = backup_history_file
end
Expand Down Expand Up @@ -212,46 +214,37 @@ def history_concurrent_use_for_input_method(input_method)
end

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
actual_history = nil
history_file = IRB.rc_files("_history").first
Dir.mktmpdir("test_irb_history_") do |tmpdir|
ENV["HOME"] = tmpdir
File.open(history_file, "w") do |f|
f.write(initial_irb_history)
end
ENV["HOME"] = @tmpdir
File.open(history_file, "w") do |f|
f.write(initial_irb_history)
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.load_history
if block_given?
previous_history = []
io.class::HISTORY.each { |line| previous_history << line }
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
previous_history.each { |line| io.class::HISTORY << line }
end
input.split.each { |line| io.class::HISTORY << line }
io.save_history

io.load_history
File.open(history_file, "r") do |f|
actual_history = f.read
end
io.load_history
File.open(history_file, "r") do |f|
actual_history = f.read
end
assert_equal(expected_history, actual_history, <<~MESSAGE)
expected:
#{expected_history}
but actual:
#{actual_history}
MESSAGE
ensure
$VERBOSE = backup_verbose
ENV["HOME"] = backup_home
ENV["XDG_CONFIG_HOME"] = backup_xdg_config_home
end

def with_temp_stdio
Expand Down
Loading