From 36082a2da225cd0f213a193dfdfc4c7d618c3640 Mon Sep 17 00:00:00 2001 From: Andrew Bloomgarden Date: Tue, 7 Sep 2010 21:52:40 -0700 Subject: [PATCH] Futureport RSpex 1.x's Rcov::VerifyTask. --- Rakefile | 16 +++++++------- spec/verify_rcov.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 spec/verify_rcov.rb diff --git a/Rakefile b/Rakefile index d0c745f..d82b624 100644 --- a/Rakefile +++ b/Rakefile @@ -3,7 +3,7 @@ rspec_base = File.expand_path(File.dirname(__FILE__) + '/../rspec/lib') $LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base) and !$LOAD_PATH.include?(rspec_base) require 'rspec/core/rake_task' -#require 'spec/rake/verify_rcov' +require 'spec/verify_rcov' # a local file future-ported from RSpec 1.x plugin_name = 'resources_controller' @@ -22,13 +22,13 @@ RSpec::Core::RakeTask.new(:rcov) do |t| t.rcov_opts = ['--output', 'doc/coverage','--text-report', '--exclude', "gems/,spec/,rcov.rb,#{File.expand_path(File.join(File.dirname(__FILE__),'../../..'))}"] end -# namespace :rcov do -# desc "Verify RCov threshold for #{plugin_name}" -# RCov::VerifyTask.new(:verify => :rcov) do |t| -# t.threshold = 100.0 -# t.index_html = File.join(File.dirname(__FILE__), 'doc/coverage/index.html') -# end -# end +namespace :rcov do + desc "Verify RCov threshold for #{plugin_name}" + RCov::VerifyTask.new(:verify => :rcov) do |t| + t.threshold = 100.0 + t.index_html = File.join(File.dirname(__FILE__), 'doc/coverage/index.html') + end +end # load up the tasks for testing rspec generators against RC require File.join(File.dirname(__FILE__), 'spec/generate_rake_task') diff --git a/spec/verify_rcov.rb b/spec/verify_rcov.rb new file mode 100644 index 0000000..9423b47 --- /dev/null +++ b/spec/verify_rcov.rb @@ -0,0 +1,52 @@ +module RCov + # A task that can verify that the RCov coverage doesn't + # drop below a certain threshold. It should be run after + # running Spec::Rake::SpecTask. + class VerifyTask < Rake::TaskLib + # Name of the task. Defaults to :verify_rcov + attr_accessor :name + + # Path to the index.html file generated by RCov, which + # is the file containing the total coverage. + # Defaults to 'coverage/index.html' + attr_accessor :index_html + + # Whether or not to output details. Defaults to true. + attr_accessor :verbose + + # The threshold value (in percent) for coverage. If the + # actual coverage is not equal to this value, the task will raise an + # exception. + attr_accessor :threshold + + # Require the threshold value be met exactly. This is the default. + 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 =~ /\s*(\d+\.\d+)%\s*<\/tt>/ + total_coverage = $1.to_f + break + end + end + puts "Coverage: #{total_coverage}% (threshold: #{threshold}%)" if verbose + raise "Coverage must be at least #{threshold}% but was #{total_coverage}%" if total_coverage < threshold + raise "Coverage has increased above the threshold of #{threshold}% to #{total_coverage}%. You should update your threshold value." if (total_coverage > threshold) and require_exact_threshold + end + end + end +end \ No newline at end of file