2014-03-14 00:31:24 -04:00
|
|
|
require_relative '../helper'
|
2012-09-15 17:50:15 -04:00
|
|
|
|
|
|
|
describe "hist" do
|
|
|
|
before do
|
|
|
|
Pry.history.clear
|
|
|
|
@hist = Pry.history
|
|
|
|
|
|
|
|
@str_output = StringIO.new
|
2012-12-09 16:37:36 -05:00
|
|
|
@t = pry_tester :history => @hist do
|
2013-03-17 04:41:21 -04:00
|
|
|
# For looking at what hist pushes into the input stack. The implementation
|
|
|
|
# of this helper will definitely have to change at some point.
|
2012-09-15 17:50:15 -04:00
|
|
|
def next_input
|
|
|
|
@pry.input.string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should replay history correctly (single item)' do
|
|
|
|
o = Object.new
|
|
|
|
@hist.push "@x = 10"
|
|
|
|
@hist.push "@y = 20"
|
|
|
|
@hist.push "@z = 30"
|
|
|
|
|
2012-12-18 02:58:02 -05:00
|
|
|
@t.push_binding o
|
2012-09-15 17:50:15 -04:00
|
|
|
@t.eval 'hist --replay -1'
|
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
o.instance_variable_get(:@z).should eq 30
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should replay a range of history correctly (range of items)' do
|
|
|
|
o = Object.new
|
|
|
|
@hist.push "@x = 10"
|
|
|
|
@hist.push "@y = 20"
|
|
|
|
|
2012-12-18 02:58:02 -05:00
|
|
|
@t.push_binding o
|
2012-09-15 17:50:15 -04:00
|
|
|
@t.eval 'hist --replay 0..2'
|
2015-01-23 04:30:41 -05:00
|
|
|
@t.eval('[@x, @y]').should eq [10, 20]
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# this is to prevent a regression where input redirection is
|
|
|
|
# replaced by just appending to `eval_string`
|
|
|
|
it 'should replay a range of history correctly (range of commands)' do
|
|
|
|
@hist.push "cd 1"
|
|
|
|
@hist.push "cd 2"
|
2012-12-28 09:44:04 -05:00
|
|
|
|
|
|
|
@t.eval("hist --replay 0..2")
|
|
|
|
stack = @t.eval("Pad.stack = _pry_.binding_stack.dup")
|
2015-01-23 04:30:41 -05:00
|
|
|
stack.map{ |b| b.eval("self") }.should eq [TOPLEVEL_BINDING.eval("self"), 1, 2]
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should grep for correct lines in history' do
|
|
|
|
@hist.push "abby"
|
|
|
|
@hist.push "box"
|
|
|
|
@hist.push "button"
|
|
|
|
@hist.push "pepper"
|
|
|
|
@hist.push "orange"
|
|
|
|
@hist.push "grape"
|
|
|
|
@hist.push "def blah 1"
|
|
|
|
@hist.push "def boink 2"
|
|
|
|
@hist.push "place holder"
|
|
|
|
|
|
|
|
@t.eval('hist --grep o').should =~ /\d:.*?box\n\d:.*?button\n\d:.*?orange/
|
|
|
|
|
|
|
|
# test more than one word in a regex match (def blah)
|
|
|
|
@t.eval('hist --grep def blah').should =~ /def blah 1/
|
|
|
|
|
|
|
|
# test more than one word with leading white space in a regex match (def boink)
|
|
|
|
@t.eval('hist --grep def boink').should =~ /def boink 2/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return last N lines in history with --tail switch' do
|
|
|
|
("a".."z").each do |v|
|
|
|
|
@hist.push v
|
|
|
|
end
|
|
|
|
|
|
|
|
out = @t.eval 'hist --tail 3'
|
2015-01-23 04:30:41 -05:00
|
|
|
out.each_line.count.should eq 3
|
2012-09-15 17:50:15 -04:00
|
|
|
out.should =~ /x\n\d+:.*y\n\d+:.*z/
|
|
|
|
end
|
|
|
|
|
2013-03-25 00:22:43 -04:00
|
|
|
it "should start from beginning if tail number is longer than history" do
|
|
|
|
@hist.push 'Hyacinth'
|
|
|
|
out = @t.eval 'hist --tail'
|
|
|
|
out.should =~ /Hyacinth/
|
|
|
|
end
|
|
|
|
|
2012-09-15 17:50:15 -04:00
|
|
|
it 'should apply --tail after --grep' do
|
|
|
|
@hist.push "print 1"
|
|
|
|
@hist.push "print 2"
|
|
|
|
@hist.push "puts 3"
|
|
|
|
@hist.push "print 4"
|
|
|
|
@hist.push "puts 5"
|
|
|
|
|
|
|
|
out = @t.eval 'hist --tail 2 --grep print'
|
2015-01-23 04:30:41 -05:00
|
|
|
out.each_line.count.should eq 2
|
2012-09-15 17:50:15 -04:00
|
|
|
out.should =~ /\d:.*?print 2\n\d:.*?print 4/
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should apply --head after --grep' do
|
|
|
|
@hist.push "puts 1"
|
|
|
|
@hist.push "print 2"
|
|
|
|
@hist.push "puts 3"
|
|
|
|
@hist.push "print 4"
|
|
|
|
@hist.push "print 5"
|
|
|
|
|
|
|
|
out = @t.eval 'hist --head 2 --grep print'
|
2015-01-23 04:30:41 -05:00
|
|
|
out.each_line.count.should eq 2
|
2012-09-15 17:50:15 -04:00
|
|
|
out.should =~ /\d:.*?print 2\n\d:.*?print 4/
|
|
|
|
end
|
|
|
|
|
|
|
|
# strangeness in this test is due to bug in Readline::HISTORY not
|
|
|
|
# always registering first line of input
|
|
|
|
it 'should return first N lines in history with --head switch' do
|
|
|
|
("a".."z").each do |v|
|
|
|
|
@hist.push v
|
|
|
|
end
|
|
|
|
|
|
|
|
out = @t.eval 'hist --head 4'
|
2015-01-23 04:30:41 -05:00
|
|
|
out.each_line.count.should eq 4
|
2012-09-15 17:50:15 -04:00
|
|
|
out.should =~ /a\n\d+:.*b\n\d+:.*c/
|
|
|
|
end
|
|
|
|
|
|
|
|
# strangeness in this test is due to bug in Readline::HISTORY not
|
|
|
|
# always registering first line of input
|
|
|
|
it 'should show lines between lines A and B with the --show switch' do
|
|
|
|
("a".."z").each do |v|
|
|
|
|
@hist.push v
|
|
|
|
end
|
|
|
|
|
|
|
|
out = @t.eval 'hist --show 1..4'
|
2015-01-23 04:30:41 -05:00
|
|
|
out.each_line.count.should eq 4
|
2012-09-15 17:50:15 -04:00
|
|
|
out.should =~ /b\n\d+:.*c\n\d+:.*d/
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should store a call with `--replay` flag" do
|
2012-12-28 09:44:04 -05:00
|
|
|
@t.eval ":banzai"
|
|
|
|
@t.eval "hist --replay 1"
|
|
|
|
@t.eval("hist").should =~ /hist --replay 1/
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not contain lines produced by `--replay` flag" do
|
2012-12-28 09:44:04 -05:00
|
|
|
@t.eval ":banzai"
|
|
|
|
@t.eval ":geronimo"
|
|
|
|
@t.eval ":huzzah"
|
|
|
|
@t.eval("hist --replay 1..3")
|
2012-09-15 17:50:15 -04:00
|
|
|
|
2012-12-28 09:44:04 -05:00
|
|
|
output = @t.eval("hist")
|
2015-01-23 04:30:41 -05:00
|
|
|
output.should eq "1: :banzai\n2: :geronimo\n3: :huzzah\n4: hist --replay 1..3\n"
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise CommandError when index of `--replay` points out to another `hist --replay`" do
|
2012-12-28 09:44:04 -05:00
|
|
|
@t.eval ":banzai"
|
|
|
|
@t.eval "hist --replay 1"
|
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 { @t.eval "hist --replay 2" }.to raise_error(Pry::CommandError, /Replay index 2 points out to another replay call: `hist --replay 1`/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should disallow execution of `--replay <i>` when CommandError raised" do
|
2012-12-28 09:44:04 -05:00
|
|
|
@t.eval "a = 0"
|
|
|
|
@t.eval "a += 1"
|
|
|
|
@t.eval "hist --replay 2"
|
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 { @t.eval "hist --replay 3" }.to raise_error Pry::CommandError
|
2015-01-23 04:30:41 -05:00
|
|
|
@t.eval("a").should eq 2
|
|
|
|
@t.eval("hist").lines.to_a.size.should eq 5
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
2013-03-10 03:41:42 -04:00
|
|
|
|
|
|
|
it "excludes Pry commands from the history with `-e` switch" do
|
|
|
|
@hist.push('a = 20')
|
|
|
|
@hist.push('ls')
|
2015-01-23 04:30:41 -05:00
|
|
|
pry_eval('hist -e').should eq "1: a = 20\n"
|
2013-03-10 03:41:42 -04:00
|
|
|
end
|
2013-03-17 04:41:21 -04:00
|
|
|
|
|
|
|
describe "sessions" do
|
|
|
|
before do
|
|
|
|
@old_file = Pry.config.history.file
|
|
|
|
Pry.config.history.file = File.expand_path('spec/fixtures/pry_history')
|
|
|
|
@hist.load
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Pry.config.history.file = @old_file
|
|
|
|
end
|
|
|
|
|
|
|
|
it "displays history only for current session" do
|
|
|
|
@hist.push('hello')
|
|
|
|
@hist.push('world')
|
|
|
|
|
|
|
|
@t.eval('hist').should =~ /1:\shello\n2:\sworld/
|
|
|
|
end
|
|
|
|
|
|
|
|
it "displays all history (including the current sesion) with `--all` switch" do
|
|
|
|
@hist.push('goodbye')
|
|
|
|
@hist.push('world')
|
|
|
|
|
|
|
|
output = @t.eval('hist --all')
|
|
|
|
output.should =~ /1:\s:athos\n2:\s:porthos\n3:\s:aramis\n/
|
|
|
|
output.should =~ /4:\sgoodbye\n5:\sworld/
|
|
|
|
end
|
|
|
|
end
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|