class CodeClimate::TestReporter::Formatter

Public Instance Methods

format(result) click to toggle source
# File lib/code_climate/test_reporter/formatter.rb, line 14
def format(result)
  return true unless CodeClimate::TestReporter.run?

  print "Coverage = #{result.source_files.covered_percent.round(2)}%. "

  payload = to_payload(result)
  PayloadValidator.validate(payload)
  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
    print "Sending report to #{client.host} for branch #{Git.branch_from_git_or_ci}... "
    client.post_results(payload)
  end

  puts "done."
  true
rescue => ex
  puts ExceptionMessage.new(ex).message
  false
end
partial?() click to toggle source
# File lib/code_climate/test_reporter/formatter.rb, line 38
def partial?
  tddium?
end
round(numeric, precision) click to toggle source

Convert to Float before rounding. Fixes [#7] possible segmentation fault when calling round on a Rational

# File lib/code_climate/test_reporter/formatter.rb, line 96
def round(numeric, precision)
  Float(numeric).round(precision)
end
short_filename(filename) click to toggle source
# File lib/code_climate/test_reporter/formatter.rb, line 84
def short_filename(filename)
  return filename unless ::SimpleCov.root
  filename = filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '')
  apply_prefix filename
end
tddium?() click to toggle source
# File lib/code_climate/test_reporter/formatter.rb, line 90
def tddium?
  ci_service_data && ci_service_data[:name] == "tddium"
end
to_payload(result) click to toggle source
# File lib/code_climate/test_reporter/formatter.rb, line 42
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:          CalculateBlob.new(file.filename).blob_id,
      coverage:         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

  {
    repo_token:       ENV["CODECLIMATE_REPO_TOKEN"],
    source_files:     source_files,
    run_at:           result.created_at.to_i,
    covered_percent:  result.source_files.covered_percent.round(2),
    covered_strength: result.source_files.covered_strength.round(2),
    line_counts:      totals,
    partial:          partial?,
    git: Git.info,
    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

Private Instance Methods

apply_prefix(filename) click to toggle source
# File lib/code_climate/test_reporter/formatter.rb, line 102
def apply_prefix filename
  prefix = CodeClimate::TestReporter.configuration.path_prefix
  return filename if prefix.nil?
  "#{prefix}/#{filename}"
end
ci_service_data() click to toggle source
# File lib/code_climate/test_reporter/formatter.rb, line 108
def ci_service_data
  @ci_service_data ||= Ci.service_data
end