Skip to content

Instantly share code, notes, and snippets.

@roovo
Created December 13, 2010 12:38
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 roovo/738952 to your computer and use it in GitHub Desktop.
Save roovo/738952 to your computer and use it in GitHub Desktop.
for generating coverage with rspec 2 in ruby 1.9.2 using Simplecov
Rake.application.instance_variable_get('@tasks').delete('spec:rcov')
module SimpleCov
class RakeTask < ::Rake::TaskLib
def initialize(*args)
desc "Run RSpec code examples"
task(args.shift || :simplecov) do
RakeFileUtils.send(:verbose, true) do
if files_to_run.empty?
puts "No examples matching #{pattern} could be found"
else
begin
ruby(spec_command_with_simplecov)
rescue
raise("ruby #{spec_command_with_simplecov} failed")
end
end
end
end
end
private
def files_to_run # :nodoc:
if ENV['SPEC']
FileList[ ENV['SPEC'] ]
else
FileList[ './spec/**/*_spec.rb' ].map { |f| %["#{f}"] }
end
end
def spec_command_with_simplecov
cmd_parts = []
cmd_parts << "-S"
cmd_parts << "bundle exec"
cmd_parts << 'script/simplecov'
cmd_parts << files_to_run
cmd_parts.flatten.compact.reject(&blank).join(" ")
end
def blank
lambda {|s| s == ""}
end
end
end
namespace :spec do
SimpleCov::RakeTask.new(:coverage => 'db:test:prepare')
end
#!/usr/bin/env ruby
# put this in your rails app script directory and make it executable
require 'rspec/core'
require 'simplecov'
module RSpec
module Core
class Runner
def self.autorun_with_simplecov
return if autorun_disabled? || installed_at_exit? || running_in_drb?
@installed_at_exit = true
at_exit { run_with_simplecov(ARGV, $stderr, $stdout) ? exit(0) : exit(1) }
end
def self.run_with_simplecov(args, err, out)
SimpleCov.use_merging false
SimpleCov.start do
add_filter "/autotest/"
add_filter "/config/"
add_filter "/db/"
add_filter "/fixtures/"
add_filter "/lib/"
add_filter "/script/"
add_filter "/spec/"
add_filter "/vendor/"
add_group 'Controllers', 'app/controllers'
add_group 'Models', 'app/models'
add_group 'Helpers', 'app/helpers'
add_group 'Mailers', 'app/mailers'
add_group 'Observers', 'app/observers'
end
run(args, err, out)
end
end
end
end
RSpec::Core::Runner.autorun_with_simplecov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment