From 18f74e0bdc73fc658d8f96c60cb59eb3488effd5 Mon Sep 17 00:00:00 2001 From: ignacio chiazzo Date: Wed, 31 Jan 2024 18:14:53 -0300 Subject: [PATCH 1/4] Add a warning for when the history path doesn't exist --- lib/irb/history.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/irb/history.rb b/lib/irb/history.rb index 90aa9f0bc..39be0b102 100644 --- a/lib/irb/history.rb +++ b/lib/irb/history.rb @@ -53,6 +53,10 @@ def save_history raise end + if !File.exist?(history_file) + warn "Warning: The path for the HISTORY_FILE does not exist." + end + if File.exist?(history_file) && File.mtime(history_file) != @loaded_history_mtime history = history[@loaded_history_lines..-1] if @loaded_history_lines From 9bf0242c376821cd9a926dbc84b72d50bd0670b4 Mon Sep 17 00:00:00 2001 From: ignacio chiazzo Date: Thu, 1 Feb 2024 13:57:49 -0300 Subject: [PATCH 2/4] warn when the directory does not exist --- lib/irb/history.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/irb/history.rb b/lib/irb/history.rb index 39be0b102..e66ecf5ea 100644 --- a/lib/irb/history.rb +++ b/lib/irb/history.rb @@ -53,16 +53,18 @@ def save_history raise end - if !File.exist?(history_file) - warn "Warning: The path for the HISTORY_FILE does not exist." - end - if File.exist?(history_file) && File.mtime(history_file) != @loaded_history_mtime history = history[@loaded_history_lines..-1] if @loaded_history_lines append_history = true end + pathname = Pathname.new(history_file) + unless Dir.exist?(pathname.dirname) + warn "Warning: The path to save IRB history does not exist. Please, set the env var IRB.conf[:HISTORY_FILE] with a valid path." + return + end + File.open(history_file, (append_history ? 'a' : 'w'), 0o600, encoding: IRB.conf[:LC_MESSAGES]&.encoding) do |f| hist = history.map{ |l| l.scrub.split("\n").join("\\\n") } unless append_history From e143b202a969e2f5dd5d6c81794285bcbf970dbd Mon Sep 17 00:00:00 2001 From: ignacio chiazzo Date: Fri, 2 Feb 2024 16:25:36 -0300 Subject: [PATCH 3/4] added test for when the history_file does not exist --- test/irb/test_history.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/irb/test_history.rb b/test/irb/test_history.rb index fef42b498..2c762ae46 100644 --- a/test/irb/test_history.rb +++ b/test/irb/test_history.rb @@ -167,6 +167,19 @@ def test_history_different_encodings $VERBOSE = verbose_bak end + def test_history_does_not_raise_when_history_file_directory_does_not_exist + backup_history_file = IRB.conf[:HISTORY_FILE] + IRB.conf[:SAVE_HISTORY] = 1 + IRB.conf[:HISTORY_FILE] = "fake/fake/fake/history_file" + io = TestInputMethodWithRelineHistory.new + + assert_nothing_raised do + io.save_history + end + ensure + IRB.conf[:HISTORY_FILE] = backup_history_file + end + private def history_concurrent_use_for_input_method(input_method) From 4491bbdfb2eace44460e6bf55c6069caa39e10fa Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Fri, 2 Feb 2024 21:51:54 +0000 Subject: [PATCH 4/4] Update lib/irb/history.rb --- lib/irb/history.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/irb/history.rb b/lib/irb/history.rb index e66ecf5ea..ad17347fb 100644 --- a/lib/irb/history.rb +++ b/lib/irb/history.rb @@ -61,7 +61,7 @@ def save_history pathname = Pathname.new(history_file) unless Dir.exist?(pathname.dirname) - warn "Warning: The path to save IRB history does not exist. Please, set the env var IRB.conf[:HISTORY_FILE] with a valid path." + warn "Warning: The directory to save IRB's history file does not exist. Please double check `IRB.conf[:HISTORY_FILE]`'s value." return end