class Danger::DangerCobertura

Show code coverage of modified and added files. Add warnings if minimum file coverage is not achieved.

@example Warn on minimum file coverage of 30% and show all modified files coverage.

cobertura.report = "path/to/my/report.xml"
cobertura.warn_if_file_less_than(percentage: 30)
cobertura.show_coverage

@see Kyaak/danger-cobertura @tags cobertura, coverage

Constants

ERROR_FILE_NOT_FOUND
ERROR_FILE_NOT_SET
TABLE_COLUMN_LINE

Attributes

additional_headers[RW]

Array of symbols which allows to extend the markdown report columns. Allowed symbols: :branch, :line

@return [Array<Symbol>] Columns to add in the markdown report.

filename_prefix[RW]

Path prefix to be added to the cobertura class filename attribute.

@return [String] Prefix to add to filename path.

report[RW]

Path to the xml formatted cobertura report.

@return [String] Report file.

Public Instance Methods

fail_if_file_less_than(percentage:) click to toggle source

Fail if a modified file has a lower total coverage than defined.

@param percentage [Float] The minimum code coverage required for a file. @return [Array<String>] Fail warnings of files with a lower coverage.

# File lib/cobertura/plugin.rb, line 51
def fail_if_file_less_than(percentage:)
  filtered_items.each do |item|
    next unless item.total_percentage < percentage

    fail "#{item.name} has less than #{percentage}% coverage"
  end
end
show_coverage(*) click to toggle source

Show markdown table of modified and added files. TODO remove * wildcard to accept all parameter: `danger local` bug - github.com/danger/danger/issues/1041 @return [Array<String>] A markdown report of modified files and their coverage report.

# File lib/cobertura/plugin.rb, line 62
def show_coverage(*)
  return if filtered_items.empty?

  table = "## Code coverage\n".dup
  table << table_header
  table << table_separation

  filtered_items.each do |item|
    table << table_entry(item)
  end
  markdown table
end
warn_if_file_less_than(percentage:) click to toggle source

Warn if a modified file has a lower total coverage than defined.

@param percentage [Float] The minimum code coverage required for a file. @return [Array<String>] Warnings of files with a lower coverage.

# File lib/cobertura/plugin.rb, line 39
def warn_if_file_less_than(percentage:)
  filtered_items.each do |item|
    next unless item.total_percentage < percentage

    warn "#{item.name} has less than #{percentage}% coverage"
  end
end

Private Instance Methods

coverage_items() click to toggle source

Extract and create all class items from the xml report.

@return [Array<CoverageItem>] Items with cobertura class information.

# File lib/cobertura/plugin.rb, line 203
def coverage_items
  @coverage_items ||= xml_report.xpath("//class").map do |node|
    CoverageItem.new(node)
  end
end
filtered_items() click to toggle source

Getter for coverage items of targeted files. Only coverage items contained in the targeted files list will be returned.

@return [Array<CoverageItem>] Filtered array of items

# File lib/cobertura/plugin.rb, line 135
def filtered_items
  @filtered_items ||= coverage_items.select do |item|
    (include_item_prefix?(item) || include_target_prefix?(item)) && !item.name.include?("$")
  end
end
format_coverage(coverage) click to toggle source

Format coverage output to two decimals.

@param coverage [Float] Value to format. @return [String] Formatted coverage string.

# File lib/cobertura/plugin.rb, line 127
def format_coverage(coverage)
  format("%.2f", coverage)
end
header_branch_rate?() click to toggle source

Check if additional_headers includes symbol :branch

@return [Boolean] :branch header defined.

# File lib/cobertura/plugin.rb, line 119
def header_branch_rate?
  !additional_headers.nil? && additional_headers.include?(:branch)
end
header_line_rate?() click to toggle source

Check if additional_headers includes symbol :line

@return [Boolean] :line header defined.

# File lib/cobertura/plugin.rb, line 112
def header_line_rate?
  !additional_headers.nil? && additional_headers.include?(:line)
end
include_item_prefix?(item) click to toggle source

Combine item filename with prefix.

@param item [CoverageItem] Coverage item to create the full filename. @return [String] Combined filename.

# File lib/cobertura/plugin.rb, line 145
def include_item_prefix?(item)
  prefixed = "".dup
  if filename_prefix
    prefixed << filename_prefix
    prefixed << "/" unless filename_prefix.chars.last == "/"
  end
  prefixed << item.filename

  result = false
  target_files.each do |target_file|
    result = target_file.eql?(prefixed)
    break if result
  end
  result
end
include_target_prefix?(item) click to toggle source
# File lib/cobertura/plugin.rb, line 161
def include_target_prefix?(item)
  result = false
  target_files.each do |target_file|
    prefixed = "".dup
    if filename_prefix
      prefixed << filename_prefix
      prefixed << "/" unless filename_prefix.chars.last == "/"
    end
    prefixed << target_file
    result = prefixed.eql?(item.filename)
    break if result
  end
  result
end
parse() click to toggle source

Parse the defined coverage report file.

@return [Oga::XML::Document] The root xml object.

# File lib/cobertura/plugin.rb, line 186
def parse
  raise ERROR_FILE_NOT_SET if report.nil? || report.empty?
  raise format(ERROR_FILE_NOT_FOUND, report) unless File.exist?(report)

  Oga.parse_xml(File.read(report))
end
table_entry(item) click to toggle source

Create the show_coverage table rows.

@param item [CoverageItem] Coverage item to put information in the table row. @return [String] Markdown for table rows.

# File lib/cobertura/plugin.rb, line 101
def table_entry(item)
  line = item.name.dup
  line << "|#{format_coverage(item.total_percentage)}"
  line << "|#{format_coverage(item.line_rate)}" if header_line_rate?
  line << "|#{format_coverage(item.branch_rate)}" if header_branch_rate?
  line << "\n"
end
table_header() click to toggle source

Create the show_coverage column headers.

@return [String] Markdown for table headers.

# File lib/cobertura/plugin.rb, line 80
def table_header
  line = "File|Total".dup
  line << "|Line" if header_line_rate?
  line << "|Branch" if header_branch_rate?
  line << "\n"
end
table_separation() click to toggle source

Create the show_coverage table header separation line.

@return [String] Markdown for table header separation.

# File lib/cobertura/plugin.rb, line 90
def table_separation
  line = "#{TABLE_COLUMN_LINE}|#{TABLE_COLUMN_LINE}".dup
  line << "|#{TABLE_COLUMN_LINE}" if header_line_rate?
  line << "|#{TABLE_COLUMN_LINE}" if header_branch_rate?
  line << "\n"
end
target_files() click to toggle source

A getter for current modified and added files.

@return [Danger::FileList] Wrapper FileList object.

# File lib/cobertura/plugin.rb, line 179
def target_files
  @target_files ||= git.modified_files + git.added_files
end
xml_report() click to toggle source

Convenient method to not always parse the report but keep it in the memory.

@return [Oga::XML::Document] The root xml object.

# File lib/cobertura/plugin.rb, line 196
def xml_report
  @xml_report ||= parse
end