2 # A task that can verify that the RCov coverage doesn't
3 # drop below a certain threshold. It should be run after
4 # running Spec::Rake::SpecTask.
5 class VerifyTask < Rake::TaskLib
6 # Name of the task. Defaults to :verify_rcov
9 # Path to the index.html file generated by RCov, which
10 # is the file containing the total coverage.
11 # Defaults to 'coverage/index.html'
12 attr_accessor :index_html
14 # Whether or not to output details. Defaults to true.
15 attr_accessor :verbose
17 # The threshold value (in percent) for coverage. If the
18 # actual coverage is not equal to this value, the task will raise an
20 attr_accessor :threshold
22 # Require the threshold value be met exactly. This is the default.
23 attr_accessor :require_exact_threshold
25 def initialize(name=:verify_rcov)
27 @index_html = 'coverage/index.html'
29 @require_exact_threshold = true
30 yield self if block_given?
31 raise "Threshold must be set" if @threshold.nil?
36 desc "Verify that rcov coverage is at least #{threshold}%"
40 File.open(index_html).each_line do |line|
41 if line =~ /<tt class='coverage_total'>(\d+\.\d+)%<\/tt>/
42 total_coverage = eval($1)
46 puts "Coverage: #{total_coverage}% (threshold: #{threshold}%)" if verbose
47 raise "Coverage must be at least #{threshold}% but was #{total_coverage}%" if total_coverage < threshold
48 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