Class: CoverageReporter
- Inherits:
-
Object
- Object
- CoverageReporter
- 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
-
#target_branch ⇒ Object
readonly
Returns the value of attribute target_branch.
Instance Method Summary collapse
- #check_and_fix_result_time(result, index) ⇒ Object
- #current_branch ⇒ Object
- #current_node ⇒ Object
- #download_files ⇒ Object
-
#initialize(target_branch) ⇒ CoverageReporter
constructor
A new instance of CoverageReporter.
- #merge_files(json_files) ⇒ Object
- #output_result_html(merged_result) ⇒ Object
-
#run ⇒ Object
Returns false if unsuccessful Returns true if successful.
-
#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.
- #upload_result_file(merged_result) ⇒ Object
Constructor Details
#initialize(target_branch) ⇒ CoverageReporter
Returns a new instance of CoverageReporter.
19 20 21 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 19 def initialize(target_branch) @target_branch = target_branch end |
Instance Attribute Details
#target_branch ⇒ Object (readonly)
Returns the value of attribute target_branch.
17 18 19 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 17 def target_branch @target_branch end |
Instance Method Details
#check_and_fix_result_time(result, index) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 79 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_branch ⇒ Object
23 24 25 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 23 def current_branch ENV['CIRCLE_BRANCH'] end |
#current_node ⇒ Object
27 28 29 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 27 def current_node ENV['CIRCLE_NODE_INDEX'].to_i end |
#download_files ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 54 def download_files if ENV["CIRCLE_JOB"].nil? CircleCi1.new.download_files else CircleCi2.new.download_files end end |
#merge_files(json_files) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 62 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
91 92 93 94 95 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 91 def output_result_html(merged_result) # Format merged result with html html_formatter = SimpleCov::Formatter::HTMLFormatter.new html_formatter.format(merged_result) end |
#run ⇒ Object
Returns false if unsuccessful Returns true if successful
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 33 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.
106 107 108 109 110 111 112 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 106 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
97 98 99 100 101 102 |
# File 'lib/codeclimate_circle_ci_coverage/coverage_reporter.rb', line 97 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 |