Class: CodeClimate::TestReporter::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/code_climate/test_reporter/formatter.rb

Instance Method Summary collapse

Instance Method Details

#calculate_blob_id(path) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/code_climate/test_reporter/formatter.rb', line 84

def calculate_blob_id(path)
  File.open(path, "rb") do |file|
    header  = "blob #{file.size}\0"
    content = file.read.force_encoding("iso-8859-1").encode("utf-8", replace: nil)
    store   = header + content

    return Digest::SHA1.hexdigest(store)
  end
end

#format(result) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/code_climate/test_reporter/formatter.rb', line 15

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?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/code_climate/test_reporter/formatter.rb', line 39

def partial?
  tddium?
end

#round(numeric, precision) ⇒ Object

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



106
107
108
# File 'lib/code_climate/test_reporter/formatter.rb', line 106

def round(numeric, precision)
  Float(numeric).round(precision)
end

#short_filename(filename) ⇒ Object



94
95
96
97
98
# File 'lib/code_climate/test_reporter/formatter.rb', line 94

def short_filename(filename)
  return filename unless ::SimpleCov.root
  filename = filename.gsub(::SimpleCov.root, '.').gsub(/^\.\//, '')
  apply_prefix filename
end

#tddium?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/code_climate/test_reporter/formatter.rb', line 100

def tddium?
  ci_service_data && ci_service_data[:name] == "tddium"
end

#to_payload(result) ⇒ Object



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
81
82
# File 'lib/code_climate/test_reporter/formatter.rb', line 43

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