Permalink
Browse files

Added new rcov tasks, made spec_and_verify_coverage the default rake …

…task.

Coverage set to current coverage.
  • Loading branch information...
justinfrench committed Mar 28, 2011
1 parent 1aea99c commit 050ef12645560e4fa179a9ef161b3434dc2fea81
Showing with 59 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +13 −3 Rakefile
  3. +2 −0 formtastic.gemspec
  4. +42 −0 lib/tasks/verify_rcov.rb
  5. +1 −0 spec/support/test_environment.rb
View
@@ -1,6 +1,7 @@
.DS_Store
.rvmrc
coverage
+coverage.data
pkg
*~
*watchr.rb
View
@@ -3,9 +3,10 @@ require 'rubygems'
require 'rake'
require 'rake/rdoctask'
require 'rspec/core/rake_task'
+require 'tasks/verify_rcov'
desc 'Default: run unit specs.'
-task :default => :spec
+task :default => :spec_and_verify_coverage
desc 'Generate documentation for the formtastic plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
@@ -32,8 +33,17 @@ RSpec::Core::RakeTask.new('specdoc') do |t|
end
desc 'Run all examples with RCov'
-RSpec::Core::RakeTask.new('examples_with_rcov') do |t|
+RSpec::Core::RakeTask.new('rcov') do |t|
t.pattern = FileList['spec/**/*_spec.rb']
t.rcov = true
- t.rcov_opts = ['--exclude', 'spec,Library']
+ t.rcov_opts = %w(--exclude gems/*,spec/*,.bundle/*, --aggregate coverage.data)
+end
+
+RCov::VerifyTask.new(:verify_coverage) do |t|
+ t.require_exact_threshold = false
+ t.threshold = 94.98
+end
+
+desc "Run all examples and verify coverage"
+task :spec_and_verify_coverage => [:rcov, :verify_coverage] do
end
View
@@ -41,5 +41,7 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<hpricot>, ["~> 0.8.3"])
s.add_development_dependency(%q<BlueCloth>) # for YARD
s.add_development_dependency(%q<yard>, ["~> 0.6"])
+ s.add_development_dependency(%q<rcov>, ["~> 0.9.9"])
+ s.add_development_dependency(%q<colored>)
end
View
@@ -0,0 +1,42 @@
+require 'colored'
+module RCov
+
+ class VerifyTask < Rake::TaskLib
+
+ attr_accessor :name
+ attr_accessor :index_html
+ attr_accessor :verbose
+ attr_accessor :threshold
+ attr_accessor :require_exact_threshold
+
+ def initialize(name=:verify_rcov)
+ @name = name
+ @index_html = 'coverage/index.html'
+ @verbose = true
+ @require_exact_threshold = true
+ yield self if block_given?
+ raise "Threshold must be set" if @threshold.nil?
+ define
+ end
+
+ def define
+ desc "Verify that rcov coverage is at least #{threshold}%"
+ task @name do
+ total_coverage = 0
+ File.open(index_html).each_line do |line|
+ if line =~ /<tt class='coverage_total'>\s*(\d+\.\d+)%\s*<\/tt>/
+ total_coverage = $1.to_f
+ break
+ end
+ end
+ output_coverage(total_coverage)
+ end
+ end
+
+ def output_coverage(total_coverage)
+ puts "Coverage: #{total_coverage}% (threshold: #{threshold}%)".green if verbose && total_coverage >= threshold
+ raise "Coverage must be at least #{threshold}% but was #{total_coverage}%".red if total_coverage < threshold
+ raise "Coverage has increased above the threshold of #{threshold}% to #{total_coverage}%. You should update your threshold value.".red if (total_coverage > threshold) and require_exact_threshold
+ end
+ end
+end
@@ -1,4 +1,5 @@
# encoding: utf-8
+require 'rspec'
require 'rspec_tag_matchers'
RSpec.configure do |config|

0 comments on commit 050ef12

Please sign in to comment.