Metric Fu: RCov fails to load spec_helper - ruby

I am trying to run metric_fu on a Rails 3 application. All is good, with the exception of rcov. I have RSpec configured and my tests follow the spec/**/*.rb format and run fine in RSpec. Trying to check coverage with rcov, however, I get the following errors:
** Running the specs/tests in the [test] environment
No file to analyze was found. All the files loaded by rcov matched one of the
following expressions, and were thus ignored:
[/\A\/Users\/Eric\/\.rvm\/rubies\/ruby\-1\.8\.7\-p299\/lib/,
/\btc_[^.]*.rb/,
/_test\.rb\z/,
/\btest\//,
/\bvendor\//,
/\A\/Users\/Eric\/\.rvm\/gems\/ruby\-1\.8\.7\-p299#-backend\/gems\/rcov\-0\.9\.8\/lib\/rcov\/formatters\/base_formatter\.rb\z/,
/\bvendor\//,
/\bconfig\//,
/\benvironment\//,
/\/gems\//,
/\/Library\//,
/\/usr\//,
/spec/]
You can solve this by doing one or more of the following:
* rename the files not to be ignored so they don't match the above regexps
* use --include-file to give a list of patterns for files not to be ignored
* use --exclude-only to give the new list of regexps to match against
* structure your code as follows:
test/test_*.rb for the test cases
lib/**/*.rb for the target source code whose coverage you want
making sure that the test/test_*.rb files are loading from lib/, e.g. by
using the -Ilib command-line argument, adding
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
to test/test_*.rb, or running rcov via a Rakefile (read the RDoc
documentation or README.rake in the source distribution).
/Users/Eric/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- spec_helper (LoadError)
from /Users/Eric/.rvm/rubies/ruby-1.8.7-p299/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from ./spec/models/account_spec.rb:1
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/gems/rcov-0.9.8/bin/rcov:511:in `load'
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/gems/rcov-0.9.8/bin/rcov:511
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/bin/rcov:19:in `load'
from /Users/Eric/.rvm/gems/ruby-1.8.7-p299#backend/bin/rcov:19
When I run rcov directly, it appears that part of the issue is rcov not correctly loading the spec_helper in my specs, which is required as "require 'spec_helper'".
Any help on how to resolve this? Thanks.

Adding "-Ispec" to the Metric-Fu configuration fixes this.

Related

Unable to include file in the spec file

I am trying to write an RSpec test for a ruby project(Sketchup plugin) but I am facing an issue with the require.
Below is how my folder is structured
In my smoke_tests_spec.rb
require "main_folder/subfolder1/file_to_test.rb"
When I run the rspec using rspec-core/rspec from the root directory I get the following error
Failure/Error: require "main_folder/subfolder1/file_to_test.rb"
LoadError:
cannot load such file -- main_folder/subfolder1/file_to_test.rb
You need to change the LOAD_PATH or use require_relative
Here you have more info.

Rspec: requiring spec/workers

I've got a bunch of Sidekiq workers in app/workers, and some matching specs in spec/workers. How can I add both to my rspec runs? As in stands none are included when I hit up rspec on the Terminal.
I see a few alternatives for your situation:
Rspec loading:
Make sure your test files in spec/worker follow the pattern setup for Rspec (e.g. *.rb or *_spec.rb).
Require the worker files in the spec_helper.rb.
Manual loading: Require the spec/worker and app/worker files in your spec_helper.rb with a Ruby-defined glob.
Sidekiq helper gem: Add the rspec-sidekiq gem to your project, as explained at Sidekiq's.
To add a bulk of files to the load path, you can either:
Add path strings to the $: global variable, early in your boot process (e.g. at the beginning of spec_helper.rb):
$:.unshift(File.expand_path('../app/workers/**/*_worker.rb', File.dirname(__FILE__))
Use a glob to load the files without modifying the load path:
Dir[File.expand_path('../app/workers/**/*_worker.rb', File.dirname(__FILE__))].each do |file|
require file
end

Heroku load error on require?

My Ruby application runs fine on my nitrous.io box, but when I push it to Heroku and it attempts to run a scheduled process, the logs show this error:
2013-12-23T22:37:11.902160+00:00 heroku[scheduler.4283]: State changed from starting to up
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: from /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require'
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: from /app/bin/rbtc:3:in `<main>'
2013-12-23T22:37:12.178751+00:00 app[scheduler.4283]: /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:53:in `require': cannot load such file -- rbtc_arbitrage (LoadError)
2013-12-23T22:37:13.432972+00:00 heroku[scheduler.4283]: Process exited with status 1
2013-12-23T22:37:13.461438+00:00 heroku[scheduler.4283]: State changed from up to complete
This is the code in /app/bin/rbtc:3
#!/usr/bin/env ruby
require 'rbtc_arbitrage'
RbtcArbitrage::CLI.start ARGV
File structure link
I tried changing this to require_relative as in a answer to someone else on Stack Overflow to no avail.
I'm kinda at a loss here. Any help is appreciated!
Please, make sure that this file exists: lib/rbtc_arbitrage.rb which loads other files in your repo like this (syntax is valid if you are using bundler):
require 'rbtc_arbitrage/version'
require 'rbtc_arbitrage/file1'
# .. and so on
Now, adding this file should work alone, but if this does not work, try adding your lib directory to the LOAD PATH in your bin/rbtc file before any require statments, like this:
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
This explicitly tells ruby to add the lib directory to the load path, and should solve your problem.

trouble setting up cucumber to test without actually changing the file system

I have been struggling for a while with this problem. I'm trying to test a ruby CLI that uses a lot of fileutils operations like making directories and copying files, etc. But when I'm writing tests, I need some way to test the functionality without touching and changing the actual filesystem. I am trying to use the fakefs gem but I am running into a error right away
When I successfully run `dotfiles init` # aruba-0.5.3/lib/aruba/cucumber.rb:71
No such file or directory - No such file or directory - /tmp/aruba-out20130817-15800-ke8nou (Errno::ENOENT)
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:146:in `open'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:146:in `block in initialize'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tmpdir.rb:142:in `create'
/Users/Brian/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/tempfile.rb:136:in `initialize'
features/dotfiles_repository.feature:8:in `When I successfully run `dotfiles init`'
So its not finding /tmp/aruba which is where cucumber aruba does all of its file operations according to the readme.
The tests run fine without fakefs, so I'm assuming its something with the setup.
Here is my features/support/env.rb file
require 'aruba/cucumber'
require 'fakefs/safe'
Before do
FakeFS.activate!
end
After do
FakeFS::FileSystem.clear
FakeFS.deactivate!
end
Does anyone have any idea what I may be doing wrong? Any help is appreciated. Even if its an idea that doesn't use fakefs, I would like to hear it. Thanks

Weird LoadError on custom ruby gem

I have a custom gem and am encountering a really weird LoadError when I install it as a gem and attempt to require it in irb.
Everything works fine with my rspec tests inside the project folder. This only occurs when using it as an actual gem in irb.
The file it throws a LoadError exception at (/lib/mws/api/order_response.rb) does in fact exist. I've tried renaming the file and updating the file that requires it (/lib/mws.rb). I've tried recreating the file thinking maybe there was a permissions issue. Nothing works.
If I comment out the require line for that specific file, everything works. There's nothing special about the file. There's 4 other files nearly identical to it (*_response.rb).
I feel like I'm taking crazy pills. I must being overlooking something but I sure don't see it.
Trace:
chris#Samus:~$ irb
1.9.3p194 :001 > require 'mws'
LoadError: cannot load such file -- mws/api/order_response
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/chris/.rvm/gems/ruby-1.9.3-p194/gems/mws-0.1.18/lib/mws.rb:14:in `<top (required)>'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):1
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
File with the requires (/lib/mws.rb)
require 'mws/base'
require 'mws/connection'
require 'mws/utility'
require 'mws/api/seller'
require 'mws/api/product'
require 'mws/api/order'
require 'mws/api/report'
require 'mws/api/general_response'
require 'mws/api/product_response'
require 'mws/api/report_response'
require 'mws/api/seller_response'
require 'mws/api/order_response' # <--- the offending line
module MWS
# #see Base#initialize MWS::Base for instantiation details.
# #return [Base] returns MWS::Base object.
def self.new(merchant_id, access_key, secret_key)
MWS::Base.new(merchant_id, access_key, secret_key)
end
end
# The below is for documentation generation purposes.
# MWS is a wrapper for the Amazon Marketplace Web Service (MWS) API.
module MWS
# API handles all the Amazon MWS API specific stuff.
module API
end
# Utilities contains various functions needed throughout MWS. Utilities is a mixin to multiple classes.
module Utilities
end
end
File I'm requiring (/lib/mws/api/order_response.rb):
module MWS
module API
# Class for parsing Amazon's XML responses into managable objects.
class OrderResponse
# Include GeneralResponse instance methods as class methods
extend GeneralResponse
end
end
end
And my file structure
For anyone interested, I was using Jeweler to handle building this gem. As it turns out, Jeweler uses your Git repository when building a gemspec.
If you haven't added all required files to your git repository, Jeweler's gemspec rake task will not include them when generating a new gemspec file.
Can should check in /Users/chris/.rvm/gems/ruby-1.9.3-p194/gems/mws-0.1.18/lib/mws/api if the file lies there (and doesn't have obscure permissions).
If that's not the case, you probably forgot to add it in your gemspec.
If it is there, please try requiring/loading it with the absolute path (for debugging purpose).

Resources