Class: CodeClimate::TestReporter::Formatter
- Inherits:
-
Object
- Object
- CodeClimate::TestReporter::Formatter
- Defined in:
- lib/code_climate/test_reporter/formatter.rb
Instance Method Summary collapse
- #calculate_blob_id(path) ⇒ Object
- #ci_service_data ⇒ Object
- #committed_at ⇒ Object
- #compute_branch(payload) ⇒ Object
- #format(result) ⇒ Object
- #git_branch ⇒ Object
- #partial? ⇒ Boolean
- #short_filename(filename) ⇒ Object
- #tddium? ⇒ Boolean
- #to_payload(result) ⇒ Object
Instance Method Details
#calculate_blob_id(path) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/code_climate/test_reporter/formatter.rb', line 122 def calculate_blob_id(path) content = File.open(path, "rb") {|f| f.read } header = "blob #{content.length}\0" store = header + content Digest::SHA1.hexdigest(store) end |
#ci_service_data ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/code_climate/test_reporter/formatter.rb', line 82 def ci_service_data if ENV['TRAVIS'] { name: "travis-ci", branch: ENV['TRAVIS_BRANCH'], build_identifier: ENV['TRAVIS_JOB_ID'], pull_request: ENV['TRAVIS_PULL_REQUEST'] } elsif ENV['CIRCLECI'] { name: "circlci", build_identifier: ENV['CIRCLE_BUILD_NUM'], branch: ENV['CIRCLE_BRANCH'], commit_sha: ENV['CIRCLE_SHA1'] } elsif ENV['SEMAPHORE'] { name: "semaphore", branch: ENV['BRANCH_NAME'], build_identifier: ENV['SEMAPHORE_BUILD_NUMBER'] } elsif ENV['JENKINS_URL'] { name: "jenkins", build_identifier: ENV['BUILD_NUMBER'], build_url: ENV['BUILD_URL'], branch: ENV['GIT_BRANCH'], commit_sha: ENV['GIT_COMMIT'] } elsif ENV['TDDIUM'] { name: "tddium", build_identifier: ENV['TDDIUM_SESSION_ID'], worker_id: ENV['TDDIUM_TID'] } else {} end end |
#committed_at ⇒ Object
134 135 136 137 |
# File 'lib/code_climate/test_reporter/formatter.rb', line 134 def committed_at committed_at = `git log -1 --pretty=format:'%ct'` committed_at.to_i.zero? ? nil : committed_at.to_i end |
#compute_branch(payload) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/code_climate/test_reporter/formatter.rb', line 149 def compute_branch(payload) git_branch = payload[:git][:branch] ci_branch = payload[:ci_service][:branch] if ci_branch.to_s.strip.size > 0 ci_branch.sub(/^origin\//, "") elsif git_branch.to_s.strip.size > 0 && !git_branch.to_s.strip.start_with?("(") git_branch.sub(/^origin\//, "") else "master" end end |
#format(result) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/code_climate/test_reporter/formatter.rb', line 11 def format(result) print "Coverage = #{result.covered_percent.round(2)}%. " payload = to_payload(result) if tddium? || ENV["TO_FILE"] file_path = File.join(Dir.tmpdir, "codeclimate-test-coverage-#{SecureRandom.uuid}.json") print "Coverage results saved to #{file_path}... " File.open(file_path, "w") { |file| file.write(payload.to_json) } else client = Client.new computed_branch = compute_branch(payload) print "Sending report to #{client.host} for branch #{computed_branch}... " client.post_results(payload) end puts "done." true rescue => ex puts ExceptionMessage.new(ex). false end |
#git_branch ⇒ Object
139 140 141 142 143 |
# File 'lib/code_climate/test_reporter/formatter.rb', line 139 def git_branch branch = `git branch`.split("\n").delete_if { |i| i[0] != "*" } branch = [branch].flatten.first branch ? branch.gsub("* ","") : nil end |
#partial? ⇒ Boolean
33 34 35 |
# File 'lib/code_climate/test_reporter/formatter.rb', line 33 def partial? tddium? end |
#short_filename(filename) ⇒ Object
129 130 131 132 |
# File 'lib/code_climate/test_reporter/formatter.rb', line 129 def short_filename(filename) return filename unless ::SimpleCov.root filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '') end |
#tddium? ⇒ Boolean
145 146 147 |
# File 'lib/code_climate/test_reporter/formatter.rb', line 145 def tddium? ci_service_data && ci_service_data[:name] == "tddium" end |
#to_payload(result) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/code_climate/test_reporter/formatter.rb', line 37 def to_payload(result) totals = Hash.new(0) source_files = result.files.map do |file| totals[:total] += file.lines.count totals[:covered] += file.covered_lines.count totals[:missed] += file.missed_lines.count { name: short_filename(file.filename), blob_id: calculate_blob_id(file.filename), coverage: file.coverage.to_json, covered_percent: file.covered_percent.round(2), covered_strength: file.covered_strength.round(2), line_counts: { total: file.lines.count, covered: file.covered_lines.count, missed: file.missed_lines.count } } end { repo_token: ENV["CODECLIMATE_REPO_TOKEN"], source_files: source_files, run_at: result.created_at, covered_percent: result.covered_percent.round(2), covered_strength: result.covered_strength.round(2), line_counts: totals, partial: partial?, git: { head: `git log -1 --pretty=format:'%H'`, committed_at: committed_at, branch: git_branch, }, 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_service: ci_service_data } end |