Class: Corundum::SimpleCov

Inherits:
DocumentationTask show all
Defined in:
lib/corundum/simplecov.rb

Instance Method Summary collapse

Methods inherited from DocumentationTask

#entry_point, title

Instance Method Details

#config_file_contentsObject



57
58
59
60
61
62
63
64
# File 'lib/corundum/simplecov.rb', line 57

def config_file_contents
  contents = ["SimpleCov.start do"]
  contents << "  coverage_dir \"#{target_dir}\""
  contents += filter_lines.map{|line| "  " + line}
  contents += group_lines.map{|line| "  " + line}
  contents << "end"
  return contents.join("\n")
end

#default_configuration(toolkit, testlib) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/corundum/simplecov.rb', line 30

def default_configuration(toolkit, testlib)
  super(toolkit)
  self.test_lib = testlib
  self.code_files = toolkit.files.code
  self.all_files =  toolkit.file_lists.project + toolkit.file_lists.code + toolkit.file_lists.test
  self.qa_rejections = toolkit.qa_rejections
end

#defineObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/corundum/simplecov.rb', line 66

def define
  super
  in_namespace do
    task :example_config do
      $stderr.puts "Try this in #{config_path}"
      $stderr.puts "(You can just do #$0 > #{config_path})"
      $stderr.puts
      puts config_file_contents
    end

    task :config_exists do
      File::exists?(config_path) or fail "No .simplecov (try: rake #{self[:example_config]})"
      File::read(config_path) =~ /coverage_dir.*#{target_dir}/ or fail ".simplecov doesn't refer to #{target_dir}"
    end

    @test_lib.doc_task(:report => [:config_exists] + all_files) do |t|
      t.rspec_opts = %w{-r simplecov -f progress} + test_options + t.rspec_opts
    end
    file entry_path => :report

    task :generate_report => [:config_exists, entry_path]

    task :verify_coverage => :generate_report do
      require 'nokogiri'
      require 'corundum/qa-report'

      doc = Nokogiri::parse(File::read(entry_path))

      coverage_total_xpath = "//span[@class='covered_percent']/span"
      percentage = doc.xpath(coverage_total_xpath).first.content.to_f

      report = QA::Report.new("Coverage")
      report.summary_counts = false
      report.add("percentage", entry_path, nil, percentage)
      report.add("threshold", entry_path, nil, threshold)
      qa_rejections << report

      if percentage < threshold
        report.fail "Coverage below threshold"
      end
    end

    task :find_stragglers => :generate_report do
      require 'nokogiri'
      require 'corundum/qa-report'

      doc = Nokogiri::parse(File::read(entry_path))

      covered_files = doc.xpath(
        "//table[@class='file_list']//td//a[@class='src_link']").map do |link|
        link.content
        end
      need_coverage = code_files.find_all(&coverage_filter)

      report = QA::Report.new("Stragglers")
      qa_rejections << report
      (covered_files - need_coverage).each do |file|
        report.add("Not in gemspec", file)
      end

      (need_coverage - covered_files).each do |file|
        report.add("Not covered", file)
      end

      unless report.empty?
        report.fail "Covered files and gemspec manifest don't match"
      end
    end
  end

  task :preflight => in_namespace(:config_exists)
  task :run_quality_assurance => in_namespace(:verify_coverage, :find_stragglers)
  task :run_continuous_integration => in_namespace(:verify_coverage, :find_stragglers)
end

#filter_linesObject



43
44
45
46
47
# File 'lib/corundum/simplecov.rb', line 43

def filter_lines
  return filters.map do |pattern|
    "add_filter \"#{pattern}\""
  end
end

#group_linesObject



49
50
51
52
53
54
55
# File 'lib/corundum/simplecov.rb', line 49

def group_lines
  lines = []
  groups.each_pair do |group, pattern|
    lines << "add_group \"#{group}\", \"#{pattern}\""
  end
  lines
end

#resolve_configurationObject



38
39
40
41
# File 'lib/corundum/simplecov.rb', line 38

def resolve_configuration
  self.config_path ||= File::expand_path(config_file, Rake::original_dir)
  super
end