Snowcrash
Snowcrash

Reputation: 86137

How do I reduce the output in a stack trace?

Is there any way to reduce the amount of output provided by Ruby when there's an error?

For example:

rspec bowling_spec.rb 
/Users/snowcrash/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- bowling (LoadError)
  from /Users/snowcrash/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
  from /Users/snowcrash/Developer/Code/Ruby/RSpec/bowling_spec.rb:2:in `<top (required)>'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `load'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `each'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `load_spec_files'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/command_line.rb:22:in `run'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/runner.rb:80:in `run'
  from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/runner.rb:17:in `block in autorun'

All I'm interested in is the first line, cannot load such file -- bowling (LoadError). Ideally I'd like Ruby to not spit out the rest of the from lines.

Is this possible?

Upvotes: 5

Views: 264

Answers (2)

Andrew Grimm
Andrew Grimm

Reputation: 81520

If you're on a Unix/Linux OS, and you're running tests from the command line, you can do

rspec bowling_spec.rb | head -n 20

to ensure you don't have to scroll up to see your error.

Upvotes: 0

sawa
sawa

Reputation: 168101

Do something like this:

module Kernel
  at_exit do
    case $!
    when nil, SystemExit, Interrupt
    else puts $!.message, [email protected]
    end
    $stderr.reopen(IO::NULL)
    $stdout.reopen(IO::NULL)
  end
end

Upvotes: 2

Related Questions