Class: Danger::DangerSimpleCovJson

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

Overview

Report your Ruby app test suite code coverage.

You can use [simplecov](github.com/colszowka/simplecov) to gather code coverage data and a [json formatter](github.com/vicentllongo/simplecov-json) so this plugin can parse it.

Examples:

Report code coverage


simplecov.report('coverage/coverage.json')
simplecov.individual_report('coverage/coverage.json', Dir.pwd)

See Also:

  • marcelofabri/danger-simplecov_json

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instance_nameObject



79
80
81
# File 'lib/simplecov_json/plugin.rb', line 79

def self.instance_name
  'simplecov'
end

Instance Method Details

#individual_coverage_message(covered_files) ⇒ String

Builds the markdown table displaying coverage on individual files

Parameters:

  • covered_files (Array)

Returns:

  • (String)

    Markdown table



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/simplecov_json/plugin.rb', line 65

def individual_coverage_message(covered_files)
  require 'terminal-table'

  message = "### Code Coverage\n\n"
  table = Terminal::Table.new(
    headings: %w(File Coverage),
    style: { border_i: '|' },
    rows: covered_files.map do |file|
      [file[:filename], "#{format('%.02f', file[:covered_percent])}%"]
    end
  ).to_s
  message + table.split("\n")[1..-2].join("\n")
end

#individual_report(coverage_path, current_project_path) ⇒ void

This method returns an undefined value.

Parse a JSON code coverage file and report on the files that you have added or modified in git



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/simplecov_json/plugin.rb', line 41

def individual_report(coverage_path, current_project_path)
  if File.exist? coverage_path
    committed_files = git.modified_files + git.added_files

    unless current_project_path.nil?
      committed_files = committed_files.map do |s|
        current_project_path + '/' + s
      end
    end

    covered_files = JSON.parse(File.read(coverage_path), symbolize_names: true)[:files]
                        .select { |f| committed_files.include?(f[:filename]) }

    return if covered_files.nil? || covered_files.empty?
    markdown individual_coverage_message(covered_files)
  else
    fail('Code coverage data not found')
  end
end

#report(coverage_path, sticky: true) ⇒ void

This method returns an undefined value.

Parse a JSON code coverage file and report that information as a message in Danger.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simplecov_json/plugin.rb', line 22

def report(coverage_path, sticky: true)
  if File.exist? coverage_path
    coverage_json = JSON.parse(File.read(coverage_path), symbolize_names: true)
    metrics = coverage_json[:metrics]
    percentage = metrics[:covered_percent]
    lines = metrics[:covered_lines]
    total_lines = metrics[:total_lines]

    formatted_percentage = format('%.02f', percentage)
    message("Code coverage is now at #{formatted_percentage}% (#{lines}/#{total_lines} lines)", sticky: sticky)
  else
    fail('Code coverage data not found')
  end
end