2011-08-07 06:16:54 -04:00
|
|
|
require 'helper'
|
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
describe Pry do
|
|
|
|
|
|
|
|
before do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.clear
|
2011-11-06 03:09:11 -05:00
|
|
|
@file = Tempfile.new(["pry", ".pry_history"])
|
2011-09-07 22:17:41 -04:00
|
|
|
@hist = @file.path
|
2011-08-07 06:16:54 -04:00
|
|
|
File.open(@hist, 'w') {|f| f << "1\n2\n3\n" }
|
|
|
|
@old_hist = Pry.config.history.file
|
|
|
|
Pry.config.history.file = @hist
|
|
|
|
Pry.load_history
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2011-09-07 22:17:41 -04:00
|
|
|
@file.close
|
|
|
|
File.unlink(@hist)
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.config.history.file = @old_hist
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".load_history" do
|
|
|
|
it "should read the contents of the file" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.to_a[-2..-1].should === ["2", "3"]
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".save_history" do
|
|
|
|
it "should include a trailing newline" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "4"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
|
|
|
File.read(@hist).should =~ /4\n\z/
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not change anything if history is not changed" do
|
|
|
|
File.open(@hist, 'w') {|f| f << "4\n5\n6\n" }
|
|
|
|
Pry.save_history
|
|
|
|
File.read(@hist).should == "4\n5\n6\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should append new lines to the file" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "4"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
|
|
|
File.read(@hist).should == "1\n2\n3\n4\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not clobber lines written by other Pry's in the meantime" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "5"
|
2011-08-07 06:16:54 -04:00
|
|
|
File.open(@hist, 'a') {|f| f << "4\n" }
|
|
|
|
Pry.save_history
|
|
|
|
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.to_a[-3..-1].should == ["2", "3", "5"]
|
2011-08-07 06:16:54 -04:00
|
|
|
File.read(@hist).should == "1\n2\n3\n4\n5\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not delete lines from the file if this session's history was cleared" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.clear
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
|
|
|
File.read(@hist).should == "1\n2\n3\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should save new lines that are added after the history was cleared" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.clear
|
|
|
|
Pry.history << "4"
|
2011-08-11 07:37:12 -04:00
|
|
|
|
|
|
|
# doing this twice as libedit on 1.8.7 has bugs and sometimes ignores the
|
|
|
|
# first line in history
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "4"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
2011-08-11 07:37:12 -04:00
|
|
|
File.read(@hist).should =~ /1\n2\n3\n4\n/
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should only append new lines the second time it is saved" do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "4"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
|
|
|
File.open(@hist, 'a') {|f| f << "5\n" }
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history << "6"
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.save_history
|
|
|
|
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.to_a[-4..-1].should == ["2", "3", "4", "6"]
|
2011-08-07 06:16:54 -04:00
|
|
|
File.read(@hist).should == "1\n2\n3\n4\n5\n6\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|