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



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

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



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

def default_configuration(toolkit, testlib)
  super(toolkit)
  target_dir.relative_path = "coverage"
  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



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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/corundum/simplecov.rb', line 69

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

    config_exists = task :config_exists do
      problems = []
      File::exists?(config_path) or problems << "No .simplecov (try: rake #{self[:example_config]})"
      config_string = File.read(config_path)
      unless config_string =~ /coverage_dir.*#{target_dir.pathname.relative_path_from(Pathname.pwd)}/
        problems << ".simplecov doesn't refer to #{target_dir}"
      end
      unless config_string =~ /SimpleCov::Formatter::JSONFormatter/
        problems << ".simplecov doesn't refer to SimpleCov::Formatter::JSONFormatter"
        problems << "in your .simplecov file, either: "
        problems << "    add 'formatter SimpleCov::Formatter::JSONFormatter'"
        problems << "  or"
        problems << "    add 'SimpleCov::Formatter::JSONFormatter' to an existing"
        problems << "    'formatter SimpleCov::Formatter::MultiFormatter'"
      end
      fail problems.join("\n") unless problems.empty?
    end

    class << config_exists
      attr_accessor :config_path

      def timestamp
        if File.exist?(config_path)
          File.mtime(config_path.to_s)
        else
          Rake::EARLY
        end
      end
    end
    config_exists.config_path = config_path

    @test_lib.report_task.rspec_opts << "-r simplecov"
    file entry_point => @test_lib.report_task.name
    file coverage_json => @test_lib.report_task.name
    @test_lib.report_task.extra_products << coverage_json

    task :verify_coverage => coverage_json do
      require 'json'
      require 'corundum/qa-report'

      doc = JSON::parse(File::read(coverage_json.to_s))

      percentage = doc["metrics"]["covered_percent"]

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

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

    task :find_stragglers => coverage_json do
      require 'json'
      require 'corundum/qa-report'

      doc = JSON::parse(File::read(coverage_json.to_s))

      pwd = Pathname.pwd
      covered_files = doc["files"].map{|f| Pathname.new(f["filename"]).relative_path_from(pwd).to_s}
      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 @test_lib.report_task.name => in_namespace(:config_exists)
  task @test_lib.report_task.name => all_files

  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



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

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

#group_linesObject



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

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

#resolve_configurationObject



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

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