How do I reduce the output in a stack trace? - ruby

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?

Do something like this:
module Kernel
at_exit do
case $!
when nil, SystemExit, Interrupt
else puts $!.message, $#.first
end
$stderr.reopen(IO::NULL)
$stdout.reopen(IO::NULL)
end
end

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.

Related

What's the difference between #<foo> and #[foo] in Ruby's RSpec test doubles?

I made an open-source contribution to an older Ruby library to fix several bugs with upstream dependency changes. I would argue it works well and patches the issue, however, the RSpec tests fail with an error message I cannot make any sense of.
.....................................F...........................
Failures:
1) MagicAdmin::Resource::Token instance methods public methods #validate
Failure/Error: validate_public_address!(rec_address, did_token)
#<MagicAdmin::Resource::Token:0x00007fac020ff7c0> received :validate_public_address! with unexpected arguments
expected: (#<Double "rec_address">, "WyIweHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAw...DAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDBcIn0iXQ==")
got: ("#[Double \"rec_address\"]", "WyIweHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAw...DAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDBcIn0iXQ==")
Diff:
## -1 +1 ##
-[#<Double "rec_address">,
+["#[Double \"rec_address\"]",
# ./lib/magic-admin/resource/token.rb:25:in `validate'
# ./test/resource/token_test.rb:31:in `block (4 levels) in <top (required)>'
# /Users/ajen/.rvm/gems/ruby-3.0.1/gems/webmock-3.14.0/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
Finished in 0.21978 seconds (files took 1.9 seconds to load)
65 examples, 1 failure
Failed examples:
rspec ./test/resource/token_test.rb:8 # MagicAdmin::Resource::Token instance methods public methods #validate
Coverage report generated for RSpec to /Users/ajen/pg/magic-admin-ruby/coverage. 633 / 643 LOC (98.44%) covered.
Stopped processing SimpleCov as a previous error not related to SimpleCov has been detected
This is a permanent, deep link to the test in question: magic-admin-ruby/0b7c37e..633516b/test/resource/token_test.rb#L9-L31
Specifically, the following diff puzzles me:
## -1 +1 ##
-[#<Double "rec_address">,
+["#[Double \"rec_address\"]",
How do I read this? What's the difference between #<foo> and #[foo] in Ruby's RSpec test doubles?
When I look at these two lines
expected: (#<Double "rec_address">, "WyIweHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAw...DAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDBcIn0iXQ==")
got: ("#[Double \"rec_address\"]", "WyIweHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAw...DAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDBcIn0iXQ==")
then it looks like you expect an instance of Double, but you got a String representation of a Double. Note that the value in the second line is wrapped with double quotes and the quotes inside are escaped.

use of jruby causes "Errno::EBADF: Bad file descriptor" error

I've got a little executable we'll call "decode" (written in C) that takes a block of data on stdin (an image file), converts it, and spits it back to stdout. So from the linux command line the following command works just fine:
cat image_file.format1 | ./irb/decode > image_file.format2
I'm trying to wrap this binary in some ruby code and eliminate the need for the use of regular file IO using Open3.popen3. Here's the relevant section of Ruby code:
.
.
.
Open3.popen3("decode") do |stdin, stdout, stderr, wait_thr|
stdin.write(f)
stdin.close_write
#image_data = stdout.read
#status_message = stderr.read
exit_status = wait_thr.value
end
.
.
.
Variable f contains the block of data to convert. I was writing it to file and then calling decode on the written file. When trying to run the the above code from irb using jruby, one gets the following error traceback (slightly sanitized):
Traceback (most recent call last):
16: from org/jruby/RubyKernel.java:1180:in `catch'
15: from org/jruby/RubyKernel.java:1418:in `loop'
14: from org/jruby/RubyKernel.java:1037:in `eval'
13: from t:5:in `<eval>'
12: from /home/user/dev/kf_decoder/lib/kf_decoder/kob.rb:46:in `process'
11: from org/jruby/RubyKernel.java:311:in `open'
10: from org/jruby/RubyIO.java:1179:in `open'
9: from /home/user/dev/kf_decoder/lib/kf_decoder/kob.rb:63:in `block in process'
8: from org/jruby/ext/stringio/StringIO.java:423:in `each_byte'
7: from /home/user/dev/kf_decoder/lib/kf_decoder/kob.rb:87:in `block in process'
6: from /home/user/dev/kf_decoder/lib/kf_decoder/kob.rb:205:in `decodeTiff'
5: from /home/user/dev/kf_decoder/lib/kf_decoder/tiff.rb:123:in `initialize'
4: from /home/user/.rbenv/versions/jruby-9.2.0.0/lib/ruby/stdlib/open3.rb:102:in `popen3'
3: from /home/user/.rbenv/versions/jruby-9.2.0.0/lib/ruby/stdlib/open3.rb:206:in `popen_run'
2: from org/jruby/RubyKernel.java:1642:in `spawn'
1: from org/jruby/RubyProcess.java:1570:in `spawn'
Errno::EBADF (Bad file descriptor - irb/decode
The funny thing is that the exact same code works fine unchanged in irb if I'm using the system ruby interpreter, or rubinius (both of which I have installed and can switch between using rbenv).
Can anyone tell me what gives? I'm runing ubuntu linux 18.04 LTS, and jruby 9.2.0.0 (2.5.0). Jruby is the platform of choice because of speed and other considerations, so I need to get this working.
The problem has to do with the version of java that jruby uses. As of this posting java 9 and 10 don't work with jruby and popen3 the way I needed to use it. Java 8 does. For complete details, see the following thread on Github:
https://github.com/jruby/jruby/issues/5249

Problems running rake with rspec and ruby

I am trying to run rake on a ruby file where I am supposed to receive an error so I can debug it as an exercise. However the error I get is not the error Im supposed to receive. I'm receiving the following and I'm having a time interpreting what I need to fix.
~Desktop/learn_ruby-master/00_hello$ rake
(in /~Desktop/learn_ruby-master)
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- hello (LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from ~Desktop/learn_ruby-master/00_hello/hello_spec.rb:117:in `<top (required)>'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
rake aborted!
/usr/bin/ruby1.9.1 -S rspec ~Desktop/learn_ruby-master/00_hello/hello_spec.rb - I/~Desktop/learn_ruby-master/00_hello -I/~Desktop/learn_ruby-master/00_hello/solution -f documentation -r ./rspec_config failed
Tasks: TOP => default => spec
( See full trace by running task with --trace)
Here is the code I am running rake on
require "hello"
describe "the hello function" do
it "says hello" do
hello.should == "Hello!"
end
end
describe "the greet function" do
it "says hello to someone" do
greet("Alice").should == "Hello, Alice!"
end
it "says hello to someone else" do
greet("Bob").should == "Hello, Bob!"
end
end
I had the same problem (going through the exact same tutorial). I just spent three days of my life trying to figure it out. The issue was that one of my folders in the path to 'hello.rb' had a space in between two words. Seriously, that was it. The ruby path couldn't pick it up no matter what I did (except change the space). Uggh. Lesson learned, don't name anything, anywhere with a space from here on out.
Ruby says it all: "Cannot find filename hello.rb on the library loading path". You are missing hello.rb file or Ruby cannot find it. Do you really have it on the disc in the directory you run rake from? If its somewhere else you need to provide relative path.
Also remove the whitespace from the first line, I suspect you have some gargabe there. There should be only one space between -- and hello in the error message.

Running my first RSpec test, is there any way to print just the error with no backtrace?

I'm looking at this tutorial online:
https://www.relishapp.com/rspec/docs/gettingstarted
# game_spec.rb
describe Game do
describe "#score" do
it "returns 0 for all gutter game" do
game = Game.new
20.times { game.roll(0) }
game.score.should == 0
end
end
end
Running the first example, I noticed that the error message they list is much shorter than what I'm seeing. On my terminal the stack trace is so long it scrolls off the top. Is there any way to shorter error message / stack output to make this fit?
/testspec/game_spec.rb:3:in `<top (required)>': uninitialized constant Game (NameError)
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `block in load_spec_files'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `each'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load_spec_files'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:22:in `run'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'
I stripped my home dir from the list of dirs there. Is it possible to order verbosity down to simply the NameError line?
As I guessed in the comment, there is an error with your configuration/installation.
`require': cannot load such file -- ./game
Ruby can not find that file. Make sure you have the correct file in place, and run the test again.

Ruby Net::SFTP does not work, even on its own example code

Trying to use Net::SFTP, version 2.05 (appears to be the latest). But it fails even on its own sample code (below, from http://net-ssh.rubyforge.org/sftp/v2/api/index.html). The domain, user and password are the same as I use to log in manually. I will change the paths when I can get that far.
Net::SFTP.start('ftp.domain.com', 'user', :password => 'password') do |sftp|
# upload a file or directory to the remote host
sftp.upload!("/path/to/local", "/path/to/remote")
# download a file or directory from the remote host
sftp.download!("/path/to/remote", "/path/to/local")
# grab data off the remote host directly to a buffer
data = sftp.download!("/path/to/remote")
# open and write to a pseudo-IO for a remote file
sftp.file.open("/path/to/remote", "w") do |f|
f.puts "Hello, world!\n"
end
# open and read from a pseudo-IO for a remote file
sftp.file.open("/path/to/remote", "r") do |f|
puts f.gets
end
# create a directory
sftp.mkdir! "/path/to/directory"
# list the entries in a directory
sftp.dir.foreach("/html") do |entry|
puts entry.longname
end
end
It hangs on the first line, and the traceback is below (line 14 is the Net::SFTP.start line)...
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:801:in `block in loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:212:in `preprocess'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:197:in `process'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:161:in `block in loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:161:in `loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:161:in `loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:802:in `loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp.rb:35:in `start'
C:/Data/dev/Ruby/RubyProjects/FTP_test/lib/ftp_test.rb:14:in `<top (required)>'
Windows 7, netbeans 6.9.1, ruby 1.9.2. The sample code is from the readme at..
When i used Net::SFTP in Perl i faced similar issue and its becos ssh path was not set properly.
Can you check whether SSH is in path.
I know the answer is not related to Ruby but suggested this as a general observation.

Resources