Class: Cyclid::API::Plugins::Cobertura

Inherits:
Action
  • Object
show all
Defined in:
app/cyclid/plugins/action/cobertura.rb

Overview

Cobertura (& compatible) coverage reader plugin

Instance Method Summary collapse

Methods inherited from Action

human_name, #prepare

Methods inherited from Base

config?, config_schema, default_config, get_config, human_name, register_plugin, set_config, update_config

Constructor Details

#initialize(args = {}) ⇒ Cobertura

Returns a new instance of Cobertura.



26
27
28
29
30
31
32
33
# File 'app/cyclid/plugins/action/cobertura.rb', line 26

def initialize(args = {})
  args.symbolize_keys!

  # There must be the path to the coverage report..
  raise 'a Cobertura action requires a path' unless args.include? :path

  @path = args[:path]
end

Instance Method Details

#perform(log) ⇒ Object



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
# File 'app/cyclid/plugins/action/cobertura.rb', line 35

def perform(log)
  # Retrieve the Cobertura XML report
  report = StringIO.new
  @transport.download(report, @path ** @ctx)

  # Parse the report and extract the line & branch coverage.
  xml = Nokogiri.parse(report.string)
  coverage = xml.xpath('//coverage')
  line_rate = coverage.attr('line-rate').value.to_f
  branch_rate = coverage.attr('branch-rate').value.to_f

  # Coverage is given as a fraction, so convert it to a percentage.
  #
  # Cobertura can produce oddly specific coverage metrics, so round it
  # to only 2 decimal points...
  line_rate_pct = (line_rate * 100).round(2)
  branch_rate_pct = (branch_rate * 100).round(2)

  log.write "Cobertura coverage line rate is #{line_rate_pct}%, " \
            "branch rate is #{branch_rate_pct}%\n"

  @ctx[:cobertura_line_rate] = "#{line_rate_pct}%"
  @ctx[:cobertura_branch_rate] = "#{branch_rate_pct}%"

  return [true, 0]
rescue StandardError => ex
  log.write "Failed to read Cobertura coverage report: #{ex}"

  return [false, 0]
end