summaryrefslogtreecommitdiff
blob: bb602e455ec110976edebb1fa64c48d8243a8da1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
  
  files = files.join(" ")

  if RUBY_PLATFORM =~ /darwin/
    exclude = '--exclude "gems/*" --exclude "Library/Frameworks/*"'
  elsif RUBY_PLATFORM =~ /java/
    exclude = '--exclude "rubygems/*,jruby/*,parser*,gemspec*,_DELEGATION*,eval*,recognize_optimized*,yaml,yaml/*,fcntl"'
  else
    exclude = '--exclude "gems/*"'
  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' then 'app\/models'
        when 'c', 'controllers' then 'app\/controllers'
        when 'h', 'helpers' then 'app\/helpers'
        when 'l', 'lib' then 'lib'
        else
          show_type
        end
      end
      reg_exp.map!{ |m| "(#{m})" }
      params << " --exclude \"^(?!#{reg_exp.join('|')})\""
    end
    exclude = exclude + params
  end

  rcov_bin = RUBY_PLATFORM =~ /java/ ? "jruby -S rcov" : "rcov"
  rcov = "#{rcov_bin} --rails -Ilib:test --sort coverage --text-report #{exclude}"
  puts
  puts
  puts "Running tests..."
  cmd = "#{rcov} #{files}"
  puts cmd
  begin
    sh cmd
  rescue Exception => e
    $stderr.puts "#{e.message}"
    $stderr.puts "The tests likely failed. Not aborting to not break Hudson builds"
  end
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
  
  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