2011-08-07 06:16:54 -04:00
|
|
|
require 'tempfile'
|
|
|
|
|
|
|
|
describe Pry do
|
|
|
|
before do
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.clear
|
2011-12-02 00:26:22 -05:00
|
|
|
|
2018-10-09 08:24:55 -04:00
|
|
|
@saved_history = "1\n2\n3\ninvalid\0 line\n"
|
2011-12-02 00:26:22 -05:00
|
|
|
|
|
|
|
Pry.history.loader = proc do |&blk|
|
|
|
|
@saved_history.lines.each { |l| blk.call(l) }
|
|
|
|
end
|
|
|
|
|
2011-08-07 06:16:54 -04:00
|
|
|
Pry.load_history
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
2011-12-02 00:26:22 -05:00
|
|
|
Pry.history.clear
|
2013-03-17 04:18:52 -04:00
|
|
|
Pry.history.instance_variable_set(:@original_lines, 0)
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
2012-08-19 16:04:05 -04:00
|
|
|
describe '#push' do
|
2014-07-06 18:20:03 -04:00
|
|
|
it "does not record duplicated lines" do
|
2012-08-19 16:04:05 -04:00
|
|
|
Pry.history << '3'
|
|
|
|
Pry.history << '_ += 1'
|
|
|
|
Pry.history << '_ += 1'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pry.history.to_a.grep('_ += 1').count).to eq 1
|
2012-08-19 16:04:05 -04:00
|
|
|
end
|
2012-09-05 00:30:51 -04:00
|
|
|
|
2018-10-09 08:24:55 -04:00
|
|
|
it "does not record lines that contain a NULL byte" do
|
|
|
|
c = Pry.history.to_a.count
|
|
|
|
Pry.history << "a\0b"
|
|
|
|
expect(Pry.history.to_a.count).to eq c
|
|
|
|
end
|
|
|
|
|
2014-07-06 18:20:03 -04:00
|
|
|
it "does not record empty lines" do
|
2012-09-05 00:30:51 -04:00
|
|
|
c = Pry.history.to_a.count
|
|
|
|
Pry.history << ''
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pry.history.to_a.count).to eq c
|
2012-09-05 00:30:51 -04:00
|
|
|
end
|
2012-08-19 16:04:05 -04:00
|
|
|
end
|
|
|
|
|
2013-03-18 05:20:16 -04:00
|
|
|
describe "#clear" do
|
|
|
|
before do
|
|
|
|
@old_file = Pry.config.history.file
|
|
|
|
@hist_file_path = File.expand_path('spec/fixtures/pry_history')
|
|
|
|
Pry.config.history.file = @hist_file_path
|
|
|
|
Pry.history.clear
|
|
|
|
Pry.load_history
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Pry.config.history.file = @old_file
|
|
|
|
end
|
|
|
|
|
|
|
|
it "clears this session's history" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pry.history.to_a.size).to be > 0
|
2013-03-18 05:20:16 -04:00
|
|
|
Pry.history.clear
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pry.history.to_a.size).to eq 0
|
|
|
|
expect(Pry.history.original_lines).to eq 0
|
2013-03-18 05:20:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't affect the contents of the history file" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pry.history.to_a.size).to eq 3
|
2013-03-18 05:20:16 -04:00
|
|
|
Pry.history.clear
|
|
|
|
|
2018-11-17 11:36:09 -05:00
|
|
|
File.open(@hist_file_path, 'r') do |fh|
|
2013-03-18 05:20:16 -04:00
|
|
|
file = fh.to_a
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(file.length).to eq 3
|
|
|
|
expect(file.any? { |a| a =~ /athos/ }).to eq true
|
2018-11-17 11:36:09 -05:00
|
|
|
end
|
2013-03-18 05:20:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#history_line_count" do
|
|
|
|
it "counts entries in history" do
|
|
|
|
Pry.history.clear
|
|
|
|
saved_history = "olgierd\ngustlik\njanek\ngrzes\ntomek\n"
|
|
|
|
Pry.history.loader = proc do |&blk|
|
|
|
|
saved_history.lines.each { |l| blk.call(l) }
|
|
|
|
end
|
|
|
|
Pry.load_history
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pry.history.history_line_count).to eq 5
|
2013-03-18 05:20:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-17 04:18:52 -04:00
|
|
|
describe "#session_line_count" do
|
|
|
|
it "returns the number of lines in history from just this session" do
|
|
|
|
Pry.history << 'you?'
|
|
|
|
Pry.history << 'you are so precious'
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pry.history.session_line_count).to eq 2
|
2013-03-17 04:18:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-07 06:16:54 -04:00
|
|
|
describe ".load_history" do
|
2014-07-06 18:20:03 -04:00
|
|
|
it "reads the contents of the file" do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(Pry.history.to_a[-2..-1]).to eq %w(2 3)
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-28 19:57:30 -05:00
|
|
|
describe "saving to a file" do
|
|
|
|
before do
|
|
|
|
@histfile = Tempfile.new(["pryhistory", "txt"])
|
2018-10-12 15:09:29 -04:00
|
|
|
@history = Pry::History.new(file_path: @histfile.path)
|
2012-12-28 19:57:30 -05:00
|
|
|
Pry.config.history.should_save = true
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
2012-12-28 19:57:30 -05:00
|
|
|
after do
|
|
|
|
@histfile.close(true)
|
|
|
|
Pry.config.history.should_save = false
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
2014-07-06 18:20:03 -04:00
|
|
|
it "saves lines to a file as they are written" do
|
2012-12-28 19:57:30 -05:00
|
|
|
@history.push "5"
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(File.read(@histfile.path)).to eq "5\n"
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
|
|
|
|
2014-07-06 18:20:03 -04:00
|
|
|
it "interleaves lines from many places" do
|
2012-12-28 19:57:30 -05:00
|
|
|
@history.push "5"
|
2018-11-04 04:34:24 -05:00
|
|
|
File.open(@histfile.path, 'a') { |f| f.puts "6" }
|
2012-12-28 19:57:30 -05:00
|
|
|
@history.push "7"
|
2011-08-07 06:16:54 -04:00
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(File.read(@histfile.path)).to eq "5\n6\n7\n"
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
2015-10-25 12:42:28 -04:00
|
|
|
|
|
|
|
it "should not write histignore words to the history file" do
|
|
|
|
Pry.config.history.histignore = [ "ls", /hist*/, 'exit' ]
|
|
|
|
@history.push "ls"
|
|
|
|
@history.push "hist"
|
|
|
|
@history.push "kakaroto"
|
|
|
|
@history.push "exit"
|
|
|
|
|
|
|
|
expect(File.open(@histfile.path).entries.size).to eq 1
|
2016-02-11 14:06:11 -05:00
|
|
|
expect(IO.readlines(@histfile.path).first).to eq "kakaroto\n"
|
2015-10-25 12:42:28 -04:00
|
|
|
end
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|
2014-07-06 18:20:03 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2019-02-24 17:11:05 -05:00
|
|
|
expect(File).to receive(:open)
|
|
|
|
.with(File.join(ENV['HOME'].to_s, "/test_history"), 'a', 0600)
|
|
|
|
.and_raise(error)
|
2014-07-06 18:20:03 -04:00
|
|
|
|
Switch test suite to RSpec
Removes Bacon and Mocha
Reasoning explained in this comment: https://github.com/pry/pry/issues/277#issuecomment-51708712
Mostly this went smoothly. There were a few errors that I fixed along
the way, e.g. tests that were failing but for various reasons still
passed. Should have documented them, but didn't think about it until
very near the end. But generaly, I remember 2 reasons this would happen:
`lambda { raise "omg" }.should.raise(RuntimeError, /not-omg/)` will pass
because the second argument is ignored by Bacon. And `1.should == 2`
will return false instead of raising an error when it is not in an it
block (e.g. if stuck in a describe block, that would just return false)
The only one that I felt unsure about was spec/helpers/table_spec.rb
`Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing'`
This is wrong, but was not failing because it was in a describe block
instead of an it block. In reality, it returns `"head: ing\n"`,
I updated the test to reflect this, though I don't know for sure
this is the right thing to do
This will fail on master until https://github.com/pry/pry/pull/1281 is merged.
This makes https://github.com/pry/pry/pull/1278 unnecessary.
2014-08-10 18:26:47 -04:00
|
|
|
expect { history.push 'a line' }.to raise_error error
|
2014-07-06 18:20:03 -04:00
|
|
|
end
|
|
|
|
end
|
2018-02-11 15:15:34 -05:00
|
|
|
|
|
|
|
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
|
2011-08-07 06:16:54 -04:00
|
|
|
end
|