Skip to content

Commit

Permalink
Always expand history file path (fix #1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
rf- committed Jul 6, 2014
1 parent a6fad5f commit 98cbcc3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
14 changes: 8 additions & 6 deletions lib/pry/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ def to_a

# The default loader. Yields lines from `Pry.history.config.file`.
def read_from_file
filename = File.expand_path(Pry.config.history.file)
path = history_file_path

if File.exists?(filename)
File.foreach(filename) { |line| yield(line) }
if File.exists?(path)
File.foreach(path) { |line| yield(line) }
end
rescue => error
warn "History file not loaded: #{error.message}"
Expand All @@ -111,15 +111,17 @@ def history_file
if defined?(@history_file)
@history_file
else
@history_file = File.open(file_path, 'a', 0600).tap { |f| f.sync = true }
@history_file = File.open(history_file_path, 'a', 0600).tap do |file|
file.sync = true
end
end
rescue Errno::EACCES
warn 'History not saved; unable to open your history file for writing.'
@history_file = false
end

def file_path
@file_path || Pry.config.history.file
def history_file_path
File.expand_path(@file_path || Pry.config.history.file)
end
end
end
28 changes: 23 additions & 5 deletions spec/history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
end

describe '#push' do
it "should not record duplicated lines" do
it "does not record duplicated lines" do
Pry.history << '3'
Pry.history << '_ += 1'
Pry.history << '_ += 1'
Pry.history.to_a.grep('_ += 1').count.should == 1
end

it "should not record empty lines" do
it "does not record empty lines" do
c = Pry.history.to_a.count
Pry.history << ''
Pry.history.to_a.count.should == c
Expand Down Expand Up @@ -120,7 +120,7 @@
end

describe ".load_history" do
it "should read the contents of the file" do
it "reads the contents of the file" do
Pry.history.to_a[-2..-1].should == %w(2 3)
end
end
Expand All @@ -138,17 +138,35 @@
Pry.config.history.should_save = false
end

it "should save lines to a file as they are written" do
it "saves lines to a file as they are written" do
@history.push "5"
File.read(@histfile.path).should == "5\n"
end

it "should interleave lines from many places" do
it "interleaves lines from many places" do
@history.push "5"
File.open(@histfile.path, 'a'){ |f| f.puts "6" }
@history.push "7"

File.read(@histfile.path).should == "5\n6\n7\n"
end
end

describe "expanding the history file path" do
before { Pry.config.history.should_save = true }
after { Pry.config.history.should_save = false }

it "recognizes ~ (#1262)" do
# This is a pretty dumb way of testing this, but at least it shouldn't
# succeed spuriously.
history = Pry::History.new(file_path: '~/test_history')
error = Class.new(RuntimeError)

File.expects(:open).
with("#{ENV['HOME']}/test_history", 'a', 0600).
raises(error)

-> { history.push 'a line' }.should.raise(error)
end
end
end

0 comments on commit 98cbcc3

Please sign in to comment.