Class: SimpleCov::Formatter::BadgeFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov-formatter-badge.rb

Constant Summary collapse

RESULT_FILE_NAME =
'coverage.svg'

Instance Method Summary collapse

Instance Method Details

#color_by(persent) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/simplecov-formatter-badge.rb', line 38

def color_by(persent)
  if persent > 90
    '#4c1'
  elsif persent > 80
    '#dfb317'
  else
    '#e05d44'
  end
end

#format(result) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/simplecov-formatter-badge.rb', line 6

def format(result)
  persent = result.covered_percent.to_i
  color = color_by(persent)
  File.open(result_file_path, "w" ) do |file|
    file.write <<~XML
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="94" height="20">
      <linearGradient id="b" x2="0" y2="100%">
        <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
        <stop offset="1" stop-opacity=".1"/>
      </linearGradient>
      <clipPath id="a">
        <rect width="94" height="20" rx="3" fill="#fff"/>
      </clipPath>
      <g clip-path="url(#a)">
        <path fill="#555" d="M0 0h59v20H0z"/>
        <path fill="#{color}" d="M59 0h35v20H59z"/>
        <path fill="url(#b)" d="M0 0h94v20H0z"/>
      </g>
      <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110">
        <text x="305" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="490">coverage</text>
        <text x="305" y="140" transform="scale(.1)" textLength="490">coverage</text>
        <text x="755" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">#{persent}%</text>
        <text x="755" y="140" transform="scale(.1)" textLength="250">#{persent}%</text></g>
      </svg>
    XML
  end
end

#result_file_pathObject



34
35
36
# File 'lib/simplecov-formatter-badge.rb', line 34

def result_file_path
  File.join(SimpleCov.coverage_path, RESULT_FILE_NAME)
end