[前][次][番号順一覧][スレッド一覧]

ruby-changes:73543

From: Jeremy <ko1@a...>
Date: Wed, 14 Sep 2022 10:15:57 +0900 (JST)
Subject: [ruby-changes:73543] 9299db49f5 (master): [ruby/irb] Fix history file saving with concurrent irb sessions when history file doesn't exist

https://git.ruby-lang.org/ruby.git/commit/?id=9299db49f5

From 9299db49f567025e28082be698e1f624b3e3f3ed Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 14 Sep 2022 10:15:32 +0900
Subject: [ruby/irb] Fix history file saving with concurrent irb sessions when
 history file 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 #388

https://github.com/ruby/irb/commit/8d277aafcb
---
 lib/irb/ext/save-history.rb |  4 ++--
 test/irb/test_history.rb    | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/lib/irb/ext/save-history.rb b/lib/irb/ext/save-history.rb
index 7acaebe36a..ad954c3558 100644
--- a/lib/irb/ext/save-history.rb
+++ b/lib/irb/ext/save-history.rb
@@ -107,9 +107,9 @@ module IRB https://github.com/ruby/ruby/blob/trunk/lib/irb/ext/save-history.rb#L107
           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]
+          history = history[@loaded_history_lines..-1] if @loaded_history_lines
           append_history = true
         end
 
diff --git a/test/irb/test_history.rb b/test/irb/test_history.rb
index 81b7fe8679..38a002d319 100644
--- a/test/irb/test_history.rb
+++ b/test/irb/test_history.rb
@@ -158,6 +158,28 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_history.rb#L158
       end
     end
 
+    def test_history_concurrent_use_not_present
+      backup_home = ENV["HOME"]
+      backup_xdg_config_home = ENV.delete("XDG_CONFIG_HOME")
+      IRB.conf[:SAVE_HISTORY] = 1
+      Dir.mktmpdir("test_irb_history_#{$$}") do |tmpdir|
+        ENV["HOME"] = tmpdir
+        io = TestInputMethod.new
+        io.class::HISTORY.clear
+        io.load_history
+        io.class::HISTORY.concat(%w"line1 line2")
+
+        history_file = IRB.rc_file("_history")
+        assert !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
+    end
+
     private
 
     def assert_history(expected_history, initial_irb_history, input)
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]