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) ⇒ Object



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

def check_and_fix_result_time(result)
  if Time.now - result.created_at >= SimpleCov.merge_timeout
    puts "result #{i} 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"
    put "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



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

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

#merge_files(json_files) ⇒ Object



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

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)
      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



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

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
# 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)
  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.



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

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



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

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