Skip to content

Commit

Permalink
Fix history file saving with concurrent irb sessions when history fil…
Browse files Browse the repository at this point in the history
…e doesn't exist

If history file didn't exist when irb was started, @loaded_history_mtime
would be nil.  However, if the history file didn't exist before, but it
exists when saving history, that means the history file was modified,
and we should handle it the same way as we handle the other case where
the history file was modified.

Fixes ruby#388
  • Loading branch information
jeremyevans committed Aug 5, 2022
1 parent 8a074a6 commit 5ee30fb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/irb/ext/save-history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def save_history
raise
end

if File.exist?(history_file) && @loaded_history_mtime &&
if File.exist?(history_file) &&
File.mtime(history_file) != @loaded_history_mtime
history = history[@loaded_history_lines..-1]
append_history = true
Expand Down
27 changes: 25 additions & 2 deletions test/irb/test_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,27 @@ def test_history_concurrent_use
end
end

def test_history_concurrent_use_not_present
omit "Skip Editline" if /EditLine/n.match(Readline::VERSION)
IRB.conf[:SAVE_HISTORY] = 1
assert_history(<<~EXPECTED_HISTORY, nil, <<~INPUT) do |history_file|
exit
5
exit
EXPECTED_HISTORY
5
exit
INPUT
assert_history(<<~EXPECTED_HISTORY2, nil, <<~INPUT2)
exit
EXPECTED_HISTORY2
5
exit
INPUT2
File.utime(File.atime(history_file), File.mtime(history_file) + 2, history_file)
end
end

private

def assert_history(expected_history, initial_irb_history, input)
Expand All @@ -168,8 +189,10 @@ def assert_history(expected_history, initial_irb_history, input)
actual_history = nil
Dir.mktmpdir("test_irb_history_#{$$}") do |tmpdir|
ENV["HOME"] = tmpdir
open(IRB.rc_file("_history"), "w") do |f|
f.write(initial_irb_history)
if initial_irb_history
open(IRB.rc_file("_history"), "w") do |f|
f.write(initial_irb_history)
end
end

io = TestInputMethod.new
Expand Down

0 comments on commit 5ee30fb

Please sign in to comment.