Run plugin specs/tests with gem or vendored rails
When I create a plugin I set up the tests or specs to be able to run stand-alone, that is not inside Rails app. But when you want to try the plugin out inside a real app, it can help to be able run the specs against vendored version of Rails if the application has one.
So to neatly auto-detect if the plugin is in an app with vendored Rails and to use that Rails for testing, I hacked this little snippet
vendored_rails = File.dirname(__FILE__) + '/../../../../vendor/rails'
if File.exists?(vendored_rails)
Dir.glob(vendored_rails + "/**/lib").each { |dir| $:.unshift dir }
else
gem 'rails', "=#{ENV['VERSION']}" if ENV['VERSION']
end
This checks for the Rails folder in vendor with extra care by going up to where the app root would be and down through ‘vendor/rails’, rather than directly to the rails. If a vendored Rails is found it puts each module’s parent folder at the front of the load path to make sure its used ahead of the gems.
For a bonus it also allows you to specify a version of the Rails gem if you want, by setting the VERSION parameter on the command line, when not inside an app.
Afterwards you just require the Rails parts you need as usual.
require 'active_support'
require 'active_record'
If the vendored rails exists the the libraries are required from there, otherwise the gems are used. Stick it in the top of your test or spec helper.
No commentsNo comments yet. Be the first.
Leave a reply
