rake/verifytask.rb
changeset 9 143e61e24c08
parent 8 308f7dc97753
child 10 389e66b0d38e
equal deleted inserted replaced
8:308f7dc97753 9:143e61e24c08
     1 #####################################################################
       
     2 ###	S U B V E R S I O N   T A S K S   A N D   H E L P E R S
       
     3 #####################################################################
       
     4 
       
     5 require 'rake/tasklib'
       
     6 
       
     7 #
       
     8 # Work around the inexplicable behaviour of the original RDoc::VerifyTask, which 
       
     9 # errors if your coverage isn't *exactly* the threshold.
       
    10 # 
       
    11 
       
    12 # A task that can verify that the RCov coverage doesn't
       
    13 # drop below a certain threshold. It should be run after
       
    14 # running Spec::Rake::SpecTask.
       
    15 class VerifyTask < Rake::TaskLib
       
    16 	
       
    17 	COVERAGE_PERCENTAGE_PATTERN = 
       
    18 		%r{<tt class='coverage_code'>(\d+\.\d+)%</tt>}
       
    19 	
       
    20 	# Name of the task. Defaults to :verify_rcov
       
    21 	attr_accessor :name
       
    22 
       
    23 	# Path to the index.html file generated by RCov, which
       
    24 	# is the file containing the total coverage.
       
    25 	# Defaults to 'coverage/index.html'
       
    26 	attr_accessor :index_html
       
    27 
       
    28 	# Whether or not to output details. Defaults to true.
       
    29 	attr_accessor :verbose
       
    30 
       
    31 	# The threshold value (in percent) for coverage. If the 
       
    32 	# actual coverage is not equal to this value, the task will raise an 
       
    33 	# exception. 
       
    34 	attr_accessor :threshold
       
    35 
       
    36 	def initialize( name=:verify )
       
    37 		@name = name
       
    38 		@index_html = 'coverage/index.html'
       
    39 		@verbose = true
       
    40 		yield self if block_given?
       
    41 		raise "Threshold must be set" if @threshold.nil?
       
    42 		define
       
    43 	end
       
    44 
       
    45 	def define
       
    46 		desc "Verify that rcov coverage is at least #{threshold}%"
       
    47 
       
    48 		task @name do
       
    49 			total_coverage = nil
       
    50 			if match = File.read( index_html ).match( COVERAGE_PERCENTAGE_PATTERN )
       
    51 				total_coverage = Float( match[1] )
       
    52 			else
       
    53 				raise "Couldn't find the coverage percentage in #{index_html}"
       
    54 			end
       
    55 
       
    56 			puts "Coverage: #{total_coverage}% (threshold: #{threshold}%)" if verbose
       
    57 			if total_coverage < threshold
       
    58 				raise "Coverage must be at least #{threshold}% but was #{total_coverage}%"
       
    59 			end
       
    60 		end
       
    61 	end
       
    62 end
       
    63 
       
    64 # vim: set nosta noet ts=4 sw=4: