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