2012-09-15 17:50:15 -04:00
|
|
|
describe "cat" do
|
|
|
|
before do
|
|
|
|
@str_output = StringIO.new
|
|
|
|
|
|
|
|
@t = pry_tester do
|
|
|
|
def insert_nil_input
|
|
|
|
@pry.update_input_history(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_exception=(e)
|
|
|
|
@pry.last_exception = e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-03 06:18:48 -04:00
|
|
|
describe "when invoked without arguments" do
|
|
|
|
it 'should display an error message' do
|
|
|
|
expect { @t.eval 'cat' }.to raise_error(Pry::CommandError, /Must provide a filename/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-15 17:50:15 -04:00
|
|
|
describe "on receiving a file that does not exist" do
|
|
|
|
it 'should display an error message' do
|
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 'cat supercalifragilicious66' }.to raise_error(StandardError, /Cannot open/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with --in" do
|
|
|
|
it 'should display the last few expressions with indices' do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.eval('10', '20', 'cat --in')).to eq unindent(<<-STR)
|
2012-09-15 17:50:15 -04:00
|
|
|
1:
|
|
|
|
10
|
|
|
|
2:
|
|
|
|
20
|
|
|
|
STR
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with --in 1" do
|
|
|
|
it 'should display the first expression with no index' do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.eval('10', '20', 'cat --in 1')).to eq("10\n")
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with --in -1" do
|
|
|
|
it 'should display the last expression with no index' do
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.eval('10', '20', 'cat --in -1')).to eq("20\n")
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with --in 1..2" do
|
|
|
|
it 'should display the given range with indices, omitting nils' do
|
|
|
|
@t.eval '10'
|
|
|
|
@t.insert_nil_input # normally happens when a command is executed
|
|
|
|
@t.eval ':hello'
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.eval('cat --in 1..3')).to eq unindent(<<-EOS)
|
2012-09-15 17:50:15 -04:00
|
|
|
1:
|
|
|
|
10
|
|
|
|
3:
|
|
|
|
:hello
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with --ex" do
|
2012-12-06 19:52:37 -05:00
|
|
|
before do
|
|
|
|
@o = Object.new
|
|
|
|
|
|
|
|
# this is to test exception code (cat --ex)
|
|
|
|
def @o.broken_method
|
|
|
|
this method is broken
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-06 12:58:53 -04:00
|
|
|
it 'cat --ex should display repl code that generated exception' do
|
|
|
|
@t.eval unindent(<<-EOS)
|
2012-09-15 17:50:15 -04:00
|
|
|
begin
|
2018-10-06 12:58:53 -04:00
|
|
|
this raises error
|
2012-09-15 17:50:15 -04:00
|
|
|
rescue => e
|
2018-10-06 12:58:53 -04:00
|
|
|
_pry_.last_exception = e
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
2018-10-06 12:58:53 -04:00
|
|
|
EOS
|
|
|
|
expect(@t.eval('cat --ex')).to match(/\d+:(\s*) this raises error/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'cat --ex should correctly display code that generated exception' do
|
|
|
|
begin
|
|
|
|
@o.broken_method
|
|
|
|
rescue => e
|
|
|
|
@t.last_exception = e
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
2018-10-06 12:58:53 -04:00
|
|
|
expect(@t.eval('cat --ex')).to match(/this method is broken/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with --ex N" do
|
|
|
|
it 'should cat first level of backtrace when --ex used with no argument ' do
|
2012-12-07 17:08:49 -05:00
|
|
|
temp_file do |f|
|
2012-09-15 17:50:15 -04:00
|
|
|
f << "bt number 1"
|
|
|
|
f.flush
|
|
|
|
@t.last_exception = mock_exception("#{f.path}:1", 'x', 'x')
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.eval('cat --ex')).to match(/bt number 1/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cat first level of backtrace when --ex 0 used ' do
|
2012-12-07 17:08:49 -05:00
|
|
|
temp_file do |f|
|
2012-09-15 17:50:15 -04:00
|
|
|
f << "bt number 1"
|
|
|
|
f.flush
|
|
|
|
@t.last_exception = mock_exception("#{f.path}:1", 'x', 'x')
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.eval('cat --ex 0')).to match(/bt number 1/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cat second level of backtrace when --ex 1 used ' do
|
2012-12-07 17:08:49 -05:00
|
|
|
temp_file do |f|
|
2012-09-15 17:50:15 -04:00
|
|
|
f << "bt number 2"
|
|
|
|
f.flush
|
|
|
|
@t.last_exception = mock_exception('x', "#{f.path}:1", 'x')
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.eval('cat --ex 1')).to match(/bt number 2/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should cat third level of backtrace when --ex 2 used' do
|
2012-12-07 17:08:49 -05:00
|
|
|
temp_file do |f|
|
2012-09-15 17:50:15 -04:00
|
|
|
f << "bt number 3"
|
|
|
|
f.flush
|
|
|
|
@t.last_exception = mock_exception('x', 'x', "#{f.path}:1")
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.eval('cat --ex 2')).to match(/bt number 3/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should show error when backtrace level out of bounds' do
|
|
|
|
@t.last_exception = mock_exception('x', 'x', 'x')
|
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('cat --ex 3') }.to raise_error(Pry::CommandError, /out of bounds/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'each successive cat --ex should show the next level of backtrace, and going past the final level should return to the first' do
|
|
|
|
temp_files = []
|
|
|
|
3.times do |i|
|
2013-01-07 18:02:39 -05:00
|
|
|
temp_files << Tempfile.new(['pry', '.rb'])
|
2012-09-15 17:50:15 -04:00
|
|
|
temp_files.last << "bt number #{i}"
|
|
|
|
temp_files.last.flush
|
|
|
|
end
|
|
|
|
|
|
|
|
@t.last_exception = mock_exception(*temp_files.map { |f| "#{f.path}:1" })
|
|
|
|
|
|
|
|
3.times do |i|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.eval('cat --ex')).to match(/bt number #{i}/)
|
2012-09-15 17:50:15 -04:00
|
|
|
end
|
|
|
|
|
2015-03-10 16:49:29 -04:00
|
|
|
expect(@t.eval('cat --ex')).to match(/bt number 0/)
|
2012-09-15 17:50:15 -04:00
|
|
|
|
|
|
|
temp_files.each do |file|
|
|
|
|
file.close(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|