Skip to content

Instantly share code, notes, and snippets.

@whatcould
Created February 9, 2009 23:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save whatcould/61104 to your computer and use it in GitHub Desktop.
Save whatcould/61104 to your computer and use it in GitHub Desktop.
.autotest your engine plugins with your main app
# autotest your engine plugins together with your rails app
# put this .autotest file in your rails root
# see http://code.whatcould.com/2009/02/09/gentlemen-test-your-engines.html for details
# iirc, I submitted a pull request to zentest but never heard back.
# so you'll have to use my zentest fork -- I've been trying to keep it up.
# https://github.com/whatcould/zentest -- just build the gem (rake package) and install from that file
# rewrite the Autotest::Rails #path_to_classname to include the entire test path
class Autotest::Rails < Autotest
# Convert the pathname s to the name of class.
def path_to_classname_with_test_paths(s)
sep = File::SEPARATOR
f = s.sub(/^(#{test_paths})#{sep}((unit|functional|fixtures|integration|views|performance|controllers|helpers)#{sep})?/, '').sub(/\.(rb|yml)$/, '').split(sep)
f = f.map { |path| path.split(/_|(\d+)/).map { |seg| seg.capitalize }.join }
f = f.map { |path| path =~ /Test/ ? path : "#{path}Test" }
f.join('::')
end
alias_method :path_to_classname_without_test_paths, :path_to_classname
alias_method :path_to_classname, :path_to_classname_with_test_paths
end
Autotest.add_hook :initialize do |at|
# find all your plugins with an app directory
engine_plugins = Dir['vendor/plugins/*'].select{|dir| File.directory?("#{dir}/app")}
# this bit requires my autotest fork
at.extra_test_paths = engine_plugins.map{|p| p + '/test'}
# autotest comes with an exception for your vendor; have to get rid of that
at.remove_exception(%r%^\./(?:db|doc|log|public|script|tmp|vendor)%)
at.add_exception %r%^\./(?:db|doc|log|public|script|tmp)%
at.add_exception(/^\.\/vendor\/rails/)
at.add_exception(/^\.\/vendor\/gems/)
# exceptions for all your non-engine plugins
plugin_names = engine_plugins.map{|p| p.sub('vendor/plugins/','')}
at.add_exception(%r%vendor/plugins/(?!#{plugin_names.join('|')})%)
engine_plugins.each do |plugin_path|
at.add_exception("#{plugin_path}/db")
# these mirror the mappings in the autotest rails profile
#
# TODO: these don't account for overriding methods, views, etc in your app
# eg, a change in your main app/ doesn't run the corresponding test in the plugin/app
# fixtures
at.add_mapping %r%^#{plugin_path}/test/fixtures/(.*)s.yml% do |_, m|
["#{plugin_path}/test/unit/#{m[1]}_test.rb",
"#{plugin_path}/test/controllers/#{m[1]}_controller_test.rb",
"#{plugin_path}/test/views/#{m[1]}_view_test.rb",
"#{plugin_path}/test/functional/#{m[1]}_controller_test.rb"]
end
at.add_mapping %r%^#{plugin_path}/app/models/(.*)\.rb$% do |_, m|
"#{plugin_path}/test/unit/#{m[1]}_test.rb"
end
at.add_mapping %r%^#{plugin_path}/app/controllers/(.*)\.rb$% do |_, m|
["#{plugin_path}/test/controllers/#{m[1]}_test.rb",
"#{plugin_path}/test/functional/#{m[1]}_test.rb"]
end
at.add_mapping %r%^#{plugin_path}/app/helpers/(.*)_helper.rb% do |_, m|
["#{plugin_path}/test/views/#{m[1]}_view_test.rb",
"#{plugin_path}/test/functional/#{m[1]}_controller_test.rb"]
end
at.add_mapping %r%^#{plugin_path}/app/views/(.*)/% do |_, m|
["#{plugin_path}/test/views/#{m[1]}_view_test.rb",
"#{plugin_path}/test/functional/#{m[1]}_controller_test.rb"]
end
# changes in routes test your app functional tests as well
at.add_mapping %r%^#{plugin_path}/config/routes.rb$% do
at.files_matching %r%^(#{plugin_path}/)?test/(controllers|views|functional)/.*_test\.rb$%
end
at.add_mapping %r%^#{plugin_path}/test/(unit|integration|controllers|views|functional)/.*rb$% do |filename, _|
filename
end
end
# keeps your fixtures updated in the shared tmp dir.
# see rake test:plugins:setup_plugin_fixtures
# if you specify your temporary_fixtures_directory for some reason,
# you should change tmp_dir below
Autotest.add_hook :updated do |at,*args|
filenames = args.collect{|arr|arr.first.first}.uniq
filenames.each do |filename|
if filename =~ /.(yml|csv)$/
require 'tempfile'
tmp_dir = FileUtils.mkdir_p(File.join(Dir.tmpdir, "rails_fixtures"))
FileUtils.copy(filename,tmp_dir)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment