Class: Danger::DangerCobertura

Inherits:
Plugin
  • Object
show all
Defined in:
lib/cobertura/plugin.rb

Overview

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

Examples:

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 Also:

  • Kyaak/danger-cobertura

Constant Summary collapse

ERROR_FILE_NOT_SET =
"Cobertura file not set. Use 'cobertura.file = \"path/to/my/report.xml\"'.".freeze
ERROR_FILE_NOT_FOUND =
"No file found at %s".freeze
TABLE_COLUMN_LINE =
"-----".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#additional_headersArray<Symbol>

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

Returns:

  • (Array<Symbol>)

    Columns to add in the markdown report.



29
30
31
# File 'lib/cobertura/plugin.rb', line 29

def additional_headers
  @additional_headers
end

#filename_prefixString

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

Returns:

  • (String)

    Prefix to add to filename path.



33
34
35
# File 'lib/cobertura/plugin.rb', line 33

def filename_prefix
  @filename_prefix
end

#reportString

Path to the xml formatted cobertura report.

Returns:

  • (String)

    Report file.



24
25
26
# File 'lib/cobertura/plugin.rb', line 24

def report
  @report
end

Instance Method Details

#fail_if_file_less_than(percentage:) ⇒ Array<String>

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

Parameters:

  • percentage (Float)

    The minimum code coverage required for a file.

Returns:

  • (Array<String>)

    Fail warnings of files with a lower coverage.



51
52
53
54
55
56
57
# 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_coverageArray<String>

Show markdown table of modified and added files. TODO remove * wildcard to accept all parameter: ‘danger local` bug - github.com/danger/danger/issues/1041

Returns:

  • (Array<String>)

    A markdown report of modified files and their coverage report.



62
63
64
65
66
67
68
69
70
71
72
73
# 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:) ⇒ Array<String>

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

Parameters:

  • percentage (Float)

    The minimum code coverage required for a file.

Returns:

  • (Array<String>)

    Warnings of files with a lower coverage.



39
40
41
42
43
44
45
# 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