Class: CoverageReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/codeclimate_circle_ci_coverage/coverage_reporter.rb

Overview

This packages up the code coverage metrics from multiple servers and sends them to CodeClimate via the CodeClimate Test Reporter Gem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_branch) ⇒ CoverageReporter

Returns a new instance of CoverageReporter.



17
18
19
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 17

def initialize(target_branch)
  @target_branch = target_branch
end

Instance Attribute Details

#target_branchObject (readonly)

Returns the value of attribute target_branch.



15
16
17
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 15

def target_branch
  @target_branch
end

Instance Method Details

#check_and_fix_result_time(result, index) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 77

def check_and_fix_result_time(result, index)
  if Time.now - result.created_at >= SimpleCov.merge_timeout
    puts "result #{index} has a created_at from #{result.created_at} (current time #{Time.now})"
    puts "This will prevent coverage from being combined. Please check your tests for Stub-Time-related issues"
    puts "Setting result created_at to current time to avoid this issue"
    # If the result is timestamped old, it is ignored by SimpleCov
    # So we always set the created_at to Time.now so that the ResultMerger
    # doesn't discard any results
    result.created_at = Time.now
  end
end

#current_branchObject



21
22
23
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 21

def current_branch
  ENV['CIRCLE_BRANCH']
end

#current_nodeObject



25
26
27
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 25

def current_node
  ENV['CIRCLE_NODE_INDEX'].to_i
end

#download_filesObject



52
53
54
55
56
57
58
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 52

def download_files
  if ENV["CIRCLE_JOB"].nil?
    CircleCi1.new.download_files
  else
    CircleCi2.new.download_files
  end
end

#merge_files(json_files) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 60

def merge_files(json_files)
  SimpleCov.coverage_dir('/tmp/coverage')

  # Merge coverage results from all nodes
  json_files.each_with_index do |resultset, i|
    resultset.each do |_command_name, data|
      result = SimpleCov::Result.from_hash(['command', i].join => data)
      check_and_fix_result_time(result, i)
      SimpleCov::ResultMerger.store_result(result)
    end
  end

  merged_result = SimpleCov::ResultMerger.merged_result
  merged_result.command_name = 'RSpec'
  merged_result
end

#output_result_html(merged_result) ⇒ Object



89
90
91
92
93
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 89

def output_result_html(merged_result)
  # Format merged result with html
  html_formatter = SimpleCov::Formatter::HTMLFormatter.new
  html_formatter.format(merged_result)
end

#runObject

Returns false if unsuccessful Returns true if successful



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 31

def run
  # Only submit coverage to codeclimate on target branch
  if current_branch != target_branch
    puts "Current branch #{current_branch} is not the target branch #{target_branch}"
    puts "No Coverage will be reported"
    return true
  end

  # Only run on node0
  return true unless current_node.zero?

  json_result_files = download_files
  merged_result = merge_files(json_result_files)

  output_result_html(merged_result)
  upload_result_file(merged_result)
  store_code_climate_payload(merged_result)
  puts "Reporting Complete."
  true
end

#store_code_climate_payload(merged_result) ⇒ Object

Internal: Debug function, in use to log the exact file which is sent to codeclimate for use when troubleshooting.



104
105
106
107
108
109
110
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 104

def store_code_climate_payload(merged_result)
  ENV["CODECLIMATE_TO_FILE"] = "true"
  codeclimate_formatter = CodeClimate::TestReporter::Formatter.new
  codeclimate_formatter.format(merged_result.to_hash)
ensure
  ENV["CODECLIMATE_TO_FILE"] = nil
end

#upload_result_file(merged_result) ⇒ Object



95
96
97
98
99
100
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 95

def upload_result_file(merged_result)
  # Post merged coverage result to codeclimate
  codeclimate_formatter = CodeClimate::TestReporter::Formatter.new
  formatted_results = codeclimate_formatter.format(merged_result.to_hash)
  CodeClimate::TestReporter::PostResults.new(formatted_results).post
end