mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Always expand history file path (fix #1262)
This commit is contained in:
parent
a6fad5f465
commit
98cbcc3b0e
2 changed files with 31 additions and 11 deletions
|
@ -81,10 +81,10 @@ class Pry
|
||||||
|
|
||||||
# The default loader. Yields lines from `Pry.history.config.file`.
|
# The default loader. Yields lines from `Pry.history.config.file`.
|
||||||
def read_from_file
|
def read_from_file
|
||||||
filename = File.expand_path(Pry.config.history.file)
|
path = history_file_path
|
||||||
|
|
||||||
if File.exists?(filename)
|
if File.exists?(path)
|
||||||
File.foreach(filename) { |line| yield(line) }
|
File.foreach(path) { |line| yield(line) }
|
||||||
end
|
end
|
||||||
rescue => error
|
rescue => error
|
||||||
warn "History file not loaded: #{error.message}"
|
warn "History file not loaded: #{error.message}"
|
||||||
|
@ -111,15 +111,17 @@ class Pry
|
||||||
if defined?(@history_file)
|
if defined?(@history_file)
|
||||||
@history_file
|
@history_file
|
||||||
else
|
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
|
end
|
||||||
rescue Errno::EACCES
|
rescue Errno::EACCES
|
||||||
warn 'History not saved; unable to open your history file for writing.'
|
warn 'History not saved; unable to open your history file for writing.'
|
||||||
@history_file = false
|
@history_file = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def file_path
|
def history_file_path
|
||||||
@file_path || Pry.config.history.file
|
File.expand_path(@file_path || Pry.config.history.file)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,14 +21,14 @@ describe Pry do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#push' do
|
describe '#push' do
|
||||||
it "should not record duplicated lines" do
|
it "does not record duplicated lines" do
|
||||||
Pry.history << '3'
|
Pry.history << '3'
|
||||||
Pry.history << '_ += 1'
|
Pry.history << '_ += 1'
|
||||||
Pry.history << '_ += 1'
|
Pry.history << '_ += 1'
|
||||||
Pry.history.to_a.grep('_ += 1').count.should == 1
|
Pry.history.to_a.grep('_ += 1').count.should == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not record empty lines" do
|
it "does not record empty lines" do
|
||||||
c = Pry.history.to_a.count
|
c = Pry.history.to_a.count
|
||||||
Pry.history << ''
|
Pry.history << ''
|
||||||
Pry.history.to_a.count.should == c
|
Pry.history.to_a.count.should == c
|
||||||
|
@ -120,7 +120,7 @@ describe Pry do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".load_history" do
|
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)
|
Pry.history.to_a[-2..-1].should == %w(2 3)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -138,12 +138,12 @@ describe Pry do
|
||||||
Pry.config.history.should_save = false
|
Pry.config.history.should_save = false
|
||||||
end
|
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"
|
@history.push "5"
|
||||||
File.read(@histfile.path).should == "5\n"
|
File.read(@histfile.path).should == "5\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should interleave lines from many places" do
|
it "interleaves lines from many places" do
|
||||||
@history.push "5"
|
@history.push "5"
|
||||||
File.open(@histfile.path, 'a'){ |f| f.puts "6" }
|
File.open(@histfile.path, 'a'){ |f| f.puts "6" }
|
||||||
@history.push "7"
|
@history.push "7"
|
||||||
|
@ -151,4 +151,22 @@ describe Pry do
|
||||||
File.read(@histfile.path).should == "5\n6\n7\n"
|
File.read(@histfile.path).should == "5\n6\n7\n"
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue