Class: Corundum::SimpleCov

Inherits:
TaskLib
  • Object
show all
Defined in:
lib/corundum/simplecov.rb

Instance Method Summary collapse

Instance Method Details

#config_file_contentsObject



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

def config_file_contents
  contents = ["SimpleCov.start do"]
  contents << "  coverage_dir \"#{report_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



33
34
35
36
37
38
# File 'lib/corundum/simplecov.rb', line 33

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

#defineObject



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
# File 'lib/corundum/simplecov.rb', line 68

def define
  in_namespace do
    file "Rakefile"

    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?(File::join(Rake::original_dir, ".simplecov")) or fail "No .simplecov (try: rake #{self[:example_config]})"
    end

    directory File::dirname(report_path)
    RSpecReportTask.new(@test_lib) do |t|
      t.task_name = report_path
      t.rspec_opts += %w{-r simplecov}
    end
    file report_path => all_files

    task :generate_report => [:preflight, report_path]

    desc "View coverage in browser"
    BrowserTask.new(self) do |t|
      t.index_html = report_path
    end

    task :verify_coverage => :generate_report do
      require 'nokogiri'

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

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

      raise "Coverage must be at least #{threshold} but was #{percentage}" if percentage < threshold
      puts "Coverage is #{percentage}% (required: #{threshold}%)"
    end

    task :find_stragglers => :generate_report do
      require 'nokogiri'

      doc = Nokogiri::parse(File::read(report_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)

      not_listed = covered_files - need_coverage
      not_covered = need_coverage - covered_files
      unless not_listed.empty? and not_covered.empty?
        raise ["Covered files and gemspec manifest don't match:",
          "Not in gemspec: #{not_listed.inspect}",
        "Not covered: #{not_covered.inspect}"].join("\n")
      end
    end
  end
  task :preflight => in_namespace(:config_exists)

  task :qa => in_namespace(:verify_coverage, :find_stragglers)
end

#filter_linesObject



45
46
47
48
49
# File 'lib/corundum/simplecov.rb', line 45

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

#group_linesObject



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

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

#resolve_configurationObject



40
41
42
43
# File 'lib/corundum/simplecov.rb', line 40

def resolve_configuration
  self.config_path ||= File::expand_path(config_file, Rake::original_dir)
  self.report_path ||= File::join(report_dir, "index.html")
end