Class: SimpleCov::Formatter::Console

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

Constant Summary collapse

VERSION =
File.new(File.join(File.expand_path(File.dirname(__FILE__)), "../VERSION")).read.strip

Instance Method Summary collapse

Instance Method Details

#colorize(s) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/simplecov-console.rb', line 67

def colorize(s)
  s =~ /([\d.]+)/
  n = $1.to_f
  if n >= 90 then
    s.colorize(:green)
  elsif n >= 80 then
    s.colorize(:yellow)
  else
    s.colorize(:red)
  end
end

#format(result) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
# File 'lib/simplecov-console.rb', line 9

def format(result)

  root = nil
  if Module.const_defined? :ROOT then
    root = ROOT
  elsif Module.const_defined? :Rails then
    root = Rails.root.to_s
  elsif ENV["BUNDLE_GEMFILE"] then
    root = File.dirname(ENV["BUNDLE_GEMFILE"])
  else
    root = Dir.pwd
  end

  puts
  puts "COVERAGE: #{colorize(pct(result))} -- #{result.covered_lines}/#{result.total_lines} lines in #{result.files.size} files"
  puts

  if root.nil? then
    return
  end

  files = result.files.sort{ |a,b| a.covered_percent <=> b.covered_percent }

  covered_files = 0
  files.select!{ |file|
    if file.covered_percent == 100 then
      covered_files += 1
      false
    else
      true
    end
  }

  if files.nil? or files.empty? then
    return
  end

  table = files.map{ |f| { :file => f.filename.gsub(root + "/", ''), :coverage => pct(f) } }

  if table.size > 15 then
    puts "showing bottom (worst) 15 of #{table.size} files"
    table = table.slice(0, 15)
  end

  s = Hirb::Helpers::Table.render(table).split(/\n/)
  s.pop
  puts s.join("\n").gsub(/\d+\.\d+%/) { |m| colorize(m) }

  if covered_files > 0 then
    puts "#{covered_files} file(s) with 100% coverage not shown"
  end

end

#pct(obj) ⇒ Object



63
64
65
# File 'lib/simplecov-console.rb', line 63

def pct(obj)
  sprintf("%6.2f%%", obj.covered_percent)
end