summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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)