Class: Coveralls::SimpleCov::Formatter

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

Instance Method Summary collapse

Instance Method Details

#display_error(e) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/coveralls/simplecov.rb', line 81

def display_error(e)
  Coveralls::Output.puts "Coveralls encountered an exception:", :color => "red"
  Coveralls::Output.puts e.class.to_s, :color => "red"
  Coveralls::Output.puts e.message, :color => "red"
  e.backtrace.each do |line|
    Coveralls::Output.puts line, :color => "red"
  end if e.backtrace
  if e.respond_to?(:response) && e.response
    Coveralls::Output.puts e.response.to_s, :color => "red"
  end
  false
end

#display_result(result) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/coveralls/simplecov.rb', line 5

def display_result(result)
  # Log which files would be submitted.
  if result.files.length > 0
    Coveralls::Output.puts "[Coveralls] Some handy coverage stats:"
  else
    Coveralls::Output.puts "[Coveralls] There are no covered files.", :color => "yellow"
  end
  result.files.each do |f|
    Coveralls::Output.print "  * "
    Coveralls::Output.print short_filename(f.filename).to_s, :color => "cyan"
    Coveralls::Output.print " => ", :color => "white"
    cov = "#{f.covered_percent.round}%"
    if f.covered_percent > 90
      Coveralls::Output.print cov, :color => "green"
    elsif f.covered_percent > 80
      Coveralls::Output.print cov, :color => "yellow"
    else
      Coveralls::Output.print cov, :color => "red"
    end
    Coveralls::Output.puts ""
  end
  true
end

#format(result) ⇒ Object



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/coveralls/simplecov.rb', line 29

def format(result)

  unless Coveralls.should_run?
    if Coveralls.noisy?
      display_result result
    end
    return
  end

  # Gather the source files.
  source_files = []
  result.files.each do |file|
    properties = {}

    # Get Source
    properties[:source] = File.open(file.filename, "rb:utf-8").read

    # Get the root-relative filename
    properties[:name] = short_filename(file.filename)

    # Get the coverage
    properties[:coverage] = file.coverage

    # Get the group
    # puts "result groups: #{result.groups}"
    # result.groups.each do |group_name, grouped_files|
    #   puts "Checking #{grouped_files.map(&:filename)} for #{file.filename}"
    #   if grouped_files.map(&:filename).include?(file.filename)
    #     properties[:group] = group_name
    #     break
    #   end
    # end

    source_files << properties
  end

  # Log which files are being submitted.
  # puts "Submitting #{source_files.length} file#{source_files.length == 1 ? '' : 's'}:"
  # source_files.each do |f|
  #   puts "  => #{f[:name]}"
  # end

  # Post to Coveralls.
  API.post_json "jobs", {:source_files => source_files, :test_framework => result.command_name.downcase, :run_at => result.created_at}
  Coveralls::Output.puts output_message result

  true

rescue Exception => e
  display_error e
end

#output_message(result) ⇒ Object



94
95
96
# File 'lib/coveralls/simplecov.rb', line 94

def output_message(result)
  "Coverage is at #{result.covered_percent.round(2) rescue result.covered_percent.round}%.\nCoverage report sent to Coveralls."
end

#short_filename(filename) ⇒ Object



98
99
100
101
# File 'lib/coveralls/simplecov.rb', line 98

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