Skip to content

Instantly share code, notes, and snippets.

@tcaddy
Last active December 10, 2015 00:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcaddy/4348855 to your computer and use it in GitHub Desktop.
Save tcaddy/4348855 to your computer and use it in GitHub Desktop.
Place this file in the `lib/tasks` folder of your Rails project. Run `rake -T` to see available rake tasks. This will dynamically create a single rake task for each single `*_spec.rb` file. It will also create additional rake tasks that run all `*_spec.rb` files in a given sub-directory.
require 'rspec/core/rake_task'
orm_setting = Rails.configuration.generators.options[:rails][:orm]
spec_prereq = if(orm_setting == :active_record)
Rails.configuration.active_record[:schema_format] == :sql ? "db:test:clone_structure" : "db:test:prepare"
else
:noop
end
namespace :spec do
[:requests, :models, :controllers, :views, :helpers, :mailers, :lib, :routing].each do |sub|
dn = "#{File.expand_path(::Rails.root.to_s)}/spec/#{sub}"
if File.exists?(dn) and File.directory?(dn)
sub_files = Dir.open(dn).map{|fn| fn unless [".",".."].include?(fn)}.compact
namespace sub.to_s.singularize.to_sym do
(sub_files.map do |fn|
unless File.exists?("#{dn}/#{fn}") and File.directory?("#{dn}/#{fn}")
if File.extname(fn)==".rb" and File.basename(fn,".rb").match(/\w+(_spec?)/)
File.basename(fn,".rb").chomp("_spec")
end
end
end).flatten.compact.sort.each do |spec|
desc "Run the code examples in spec/#{sub}/#{spec}_spec.rb"
RSpec::Core::RakeTask.new(spec.gsub(/(_routing|_controller|_helper)$/,'') => spec_prereq) do |t|
t.pattern = "./spec/#{sub}/#{spec}_spec.rb"
end
end
sub_files.each do |fn|
if File.exists?("#{dn}/#{fn}") and File.directory?("#{dn}/#{fn}")
sub_sub_files = (Dir.open("#{dn}/#{fn}").map do |sub_fn|
if File.extname(sub_fn)==".rb" and File.basename(sub_fn,".rb").match(/\w+(_spec?)/)
File.basename(sub_fn,".rb").chomp("_spec")
end
end).flatten.compact.sort
if sub_sub_files.size > 0
desc "Run the code examples in spec/#{sub}/#{fn}"
RSpec::Core::RakeTask.new(fn => spec_prereq) do |t|
t.pattern = "./spec/#{sub}/#{fn}/*_spec.rb"
end
end
namespace fn.to_s.singularize.to_sym do
sub_sub_files.each do |spec|
desc "Run the code examples in spec/#{sub}/#{fn}/#{spec}_spec.rb"
RSpec::Core::RakeTask.new(spec.split(".")[0] => spec_prereq) do |t|
t.pattern = "./spec/#{sub}/#{fn}/#{spec}_spec.rb"
end
end
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment