summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgnacio Chiazzo Cardarello <ignaciochiazzo@gmail.com>2024-02-02 18:58:19 -0300
committergit <svn-admin@ruby-lang.org>2024-02-02 21:58:22 +0000
commitaa780a678e98599fdd9011760dabe17f9e26826c (patch)
tree60e3243debbd3cb1e7bf8b52e8f43beae4bd1244
parent6afccdf4499931b94f513dea9e3c1c9bab4a8787 (diff)
[ruby/irb] Add a warning for when the history path doesn't exist
(https://github.com/ruby/irb/pull/852) * Add a warning for when the history path doesn't exist * warn when the directory does not exist * added test for when the history_file does not exist * Update lib/irb/history.rb --------- https://github.com/ruby/irb/commit/9e6fa67212 Co-authored-by: Stan Lo <stan001212@gmail.com>
-rw-r--r--lib/irb/history.rb6
-rw-r--r--test/irb/test_history.rb13
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/irb/history.rb b/lib/irb/history.rb
index 90aa9f0bcf..ad17347fbd 100644
--- a/lib/irb/history.rb
+++ b/lib/irb/history.rb
@@ -59,6 +59,12 @@ module IRB
append_history = true
end
+ pathname = Pathname.new(history_file)
+ unless Dir.exist?(pathname.dirname)
+ warn "Warning: The directory to save IRB's history file does not exist. Please double check `IRB.conf[:HISTORY_FILE]`'s value."
+ 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
diff --git a/test/irb/test_history.rb b/test/irb/test_history.rb
index fef42b4982..2c762ae466 100644
--- a/test/irb/test_history.rb
+++ b/test/irb/test_history.rb
@@ -167,6 +167,19 @@ module TestIRB
$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)