From e29d765bf6b6bae70ae332ae7c05318a8dec923e Mon Sep 17 00:00:00 2001 From: hukl Date: Fri, 27 Feb 2009 12:53:03 +0100 Subject: new coverage rake task - finally --- lib/tasks/coverage.rake | 86 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 68 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/tasks/coverage.rake b/lib/tasks/coverage.rake index b70f813..953cf7a 100644 --- a/lib/tasks/coverage.rake +++ b/lib/tasks/coverage.rake @@ -1,24 +1,74 @@ -require 'rcov/rcovtask' - -namespace :test do - namespace :coverage do - desc "Delete aggregate coverage data." - task(:clean) { rm_f "coverage.data" } +def run_coverage(files) + rm_f "coverage" + rm_f "coverage.data" + + # turn the files we want to run into a string + if files.empty? + puts "No files were specified for testing" + return end - - desc 'Aggregate code coverage for unit, functional and integration tests' - task :coverage => "test:coverage:clean" - %w[unit functional integration].each do |target| - namespace :coverage do - Rcov::RcovTask.new(target) do |t| - t.libs << "test" - t.test_files = FileList["test/#{target}/*_test.rb"] - t.output_dir = "test/coverage/#{target}" - t.verbose = true - t.rcov_opts << '--rails --aggregate coverage.data' + + files = files.join(" ") + + if PLATFORM =~ /darwin/ + exclude = '--exclude "gems/*" --exclude "Library/Frameworks/*"' + else + exclude = '--exclude "rubygems/*"' + end + # rake test:units:rcov SHOW_ONLY=models,controllers,lib,helpers + # rake test:units:rcov SHOW_ONLY=m,c,l,h + if ENV['SHOW_ONLY'] + params = String.new + show_only = ENV['SHOW_ONLY'].to_s.split(',').map{|x|x.strip} + if show_only.any? + reg_exp = [] + for show_type in show_only + reg_exp << case show_type + when 'm', 'models' : 'app\/models' + when 'c', 'controllers' : 'app\/controllers' + when 'h', 'helpers' : 'app\/helpers' + when 'l', 'lib' : 'lib' + else + show_type + end + end + reg_exp.map!{ |m| "(#{m})" } + params << " --exclude \"^(?!#{reg_exp.join('|')})\"" end + exclude = exclude + params + end + + + rcov = "rcov --rails -Ilib:test --sort coverage --text-report #{exclude} --no-validator-links" + puts + puts + puts "Running tests..." + cmd = "#{rcov} #{files}" + puts cmd + sh cmd +end + +namespace :test do + + desc "Measures unit, functional, and integration test coverage" + task :coverage do + run_coverage Dir["test/unit/**/*.rb", "test/functional/**/*.rb", "test/integration/**/*.rb"] end - task :coverage => "test:coverage:#{target}" + namespace :coverage do + desc "Runs coverage on unit tests" + task :units do + run_coverage Dir["test/unit/**/*.rb"] + end + + desc "Runs coverage on functional tests" + task :functionals do + run_coverage Dir["test/functional/**/*.rb"] + end + + desc "Runs coverage on integration tests" + task :integration do + run_coverage Dir["test/integration/**/*.rb"] + end end end \ No newline at end of file -- cgit v1.3