[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

---------

9e6fa67212

Co-authored-by: Stan Lo <stan001212@gmail.com>
This commit is contained in:
Ignacio Chiazzo Cardarello 2024-02-02 18:58:19 -03:00 committed by git
parent 6afccdf449
commit aa780a678e
2 changed files with 19 additions and 0 deletions

View file

@ -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

View file

@ -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)