diff options
author | Kevin McPhillips <github@kevinmcphillips.ca> | 2018-02-11 15:15:34 -0500 |
---|---|---|
committer | Kevin McPhillips <github@kevinmcphillips.ca> | 2018-02-11 15:15:34 -0500 |
commit | c4bddce853eb84ee1c6be83e16838c2e269bd178 (patch) | |
tree | 37b6f4aa05351f7006dad19b1c580ea45f84540d | |
parent | 0de06a235b91ece2ca945abc5437c565455d605e (diff) | |
download | pry-c4bddce853eb84ee1c6be83e16838c2e269bd178.tar.gz |
Recover from any SystemCallError on failure to read or write history file.
-rw-r--r-- | lib/pry/history.rb | 8 | ||||
-rw-r--r-- | spec/history_spec.rb | 20 |
2 files changed, 24 insertions, 4 deletions
diff --git a/lib/pry/history.rb b/lib/pry/history.rb index 72cbc81c..16dd2656 100644 --- a/lib/pry/history.rb +++ b/lib/pry/history.rb @@ -107,8 +107,8 @@ class Pry if File.exist?(path) File.foreach(path) { |line| yield(line) } end - rescue => error - warn "History file not loaded: #{error.message}" + rescue SystemCallError => error + warn "Unable to read history file: #{error.message}" end # The default pusher. Appends the given line to Readline::HISTORY. @@ -136,8 +136,8 @@ class Pry file.sync = true end end - rescue Errno::EACCES - warn 'History not saved; unable to open your history file for writing.' + rescue SystemCallError => error + warn "Unable to write history file: #{error.message}" @history_file = false end diff --git a/spec/history_spec.rb b/spec/history_spec.rb index 5aa09e5a..d506da5a 100644 --- a/spec/history_spec.rb +++ b/spec/history_spec.rb @@ -181,4 +181,24 @@ describe Pry do expect { history.push 'a line' }.to raise_error error end end + + describe "file io errors" do + let(:history) { Pry::History.new(file_path: file_path) } + let(:file_path) { Tempfile.new("pry_history_spec").path } + + [Errno::EACCES, Errno::ENOENT].each do |error_class| + it "handles #{ error_class } failure to read from history" do + expect(File).to receive(:foreach).and_raise(error_class) + expect(history).to receive(:warn).with(/Unable to read history file:/) + expect { history.load }.to_not raise_error + end + + it "handles #{ error_class } failure to write history" do + Pry.config.history.should_save = true + expect(File).to receive(:open).with(file_path, "a", 0600).and_raise(error_class) + expect(history).to receive(:warn).with(/Unable to write history file:/) + expect { history.push("anything") }.to_not raise_error + end + end + end end |