Class: PullReview::Coverage::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/pullreview/coverage/formatter.rb

Overview

A simplecov Formatter implementation see README.md for usage.

Instance Method Summary collapse

Instance Method Details

#apiObject

return based on config a remote api client or a local file implementation.



20
21
22
# File 'lib/pullreview/coverage/formatter.rb', line 20

def api
  config.api_to_file? ? PullReview::Coverage::LocalFileApi.new(config) : PullReview::Coverage::ClientApi.new(config)
end

#configObject



15
16
17
# File 'lib/pullreview/coverage/formatter.rb', line 15

def config
  @config ||= PullReview::Coverage::Config.new
end

#format(result) ⇒ Object

Simplecov callback to format report



7
8
9
10
11
12
13
# File 'lib/pullreview/coverage/formatter.rb', line 7

def format(result)
  return unless config.should_run?
  response = api.publish(to_payload(result))
  PullReview::Coverage.log(:info, "Coverage report ok #{api} : #{response}")
rescue => e
  PullReview::Coverage.log(:error, "Coverage report submission failed #{api}", e)
end

#prefix(filename) ⇒ Object

add a prefix to a filename



79
80
81
82
# File 'lib/pullreview/coverage/formatter.rb', line 79

def prefix(filename)
  return filename unless @config.prefix_filename
  "#{config.prefix_filename}/#{filename}"
end

#round(numeric, precision) ⇒ Object

wrap Float.round



85
86
87
# File 'lib/pullreview/coverage/formatter.rb', line 85

def round(numeric, precision)
  Float(numeric).round(precision)
end

#short_filename(filename) ⇒ Object

handle correctly filename considering root of the project and prefix



72
73
74
75
76
# File 'lib/pullreview/coverage/formatter.rb', line 72

def short_filename(filename)
  return prefix(filename) unless ::SimpleCov.root
  filename = filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '')
  prefix(filename)
end

#sources_coverage(result, totals) ⇒ Object

return array of hash with coverage details for each files a side effect is calculating the coverage’s totals



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pullreview/coverage/formatter.rb', line 49

def sources_coverage(result, totals)
  sources = result.files.map do |file|
    file_name = short_filename(file.filename)
    next if file_name.start_with?('vendor')
    totals[:total] += file.lines.count
    totals[:covered] += file.covered_lines.count
    totals[:missed] += file.missed_lines.count
    {
      name: file_name,
      coverage_details: file.coverage.to_json,
      covered_percent: round(file.covered_percent, 2),
      covered_strength: round(file.covered_strength, 2),
      line_counts: {
        total: file.lines.count,
        covered: file.covered_lines.count,
        missed: file.missed_lines.count
      }
    }
  end
  sources
end

#to_payload(result) ⇒ Object

Transform simplecov result to huge hash.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pullreview/coverage/formatter.rb', line 25

def to_payload(result)
  totals = Hash.new(0)
  sources = sources_coverage(result, totals)
  {
    repo_token: config.repo_token,
    files_coverage: sources,
    run_at: result.created_at.to_i,
    covered_percent: round(result.covered_percent, 2),
    covered_strength: round(result.covered_strength, 2),
    line_counts: totals,
    git_info: Git.new.infos,
    environment: {
      test_framework: result.command_name.downcase,
      pwd: Dir.pwd,
      rails_root: (Rails.root.to_s rescue nil),
      simplecov_root: ::SimpleCov.root,
      gem_version: VERSION
    },
    ci_info: ContinousBuild.infos
  }
end