Class: SimpleCov::Formatter::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov-console/output/table.rb,
lib/simplecov-console/output/block.rb,
lib/simplecov-console.rb

Defined Under Namespace

Modules: BlockOutput, TableOutput

Constant Summary collapse

VERSION =
IO.read(File.expand_path("../../VERSION", __FILE__)).strip
ATTRIBUTES =
[
  :branch_missing_len,
  :max_lines,
  :max_rows,
  :missing_len,
  :output_filename,
  :output_style,
  :output_to_file,
  :show_covered,
  :sort,
  :table_options,
  :use_colors,
]

Instance Method Summary collapse

Instance Method Details

#branches_missed(missed_branches) ⇒ Object



152
153
154
155
156
# File 'lib/simplecov-console.rb', line 152

def branches_missed(missed_branches)
  missed_branches.group_by(&:start_line).map do |line_number, branches|
    "#{line_number}[#{branches.map(&:type).join(',')}]"
  end
end

#colorize(s) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/simplecov-console.rb', line 220

def colorize(s)
  return s if !use_colors?

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

#format(result) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/simplecov-console.rb', line 68

def format(result)
  include_output_style

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

  result_file_path = ''
  if SimpleCov::Formatter::Console.output_to_file
    result_file_path = File.join(SimpleCov.coverage_path, SimpleCov::Formatter::Console.output_filename)
    # Temporarily assign $stdout to the file
    file = File.open(result_file_path, 'w')
    $stdout = file
  end

  puts unless SimpleCov::Formatter::Console.output_to_file
  puts "COVERAGE: #{colorize(pct(result.covered_percent))} -- #{result.covered_lines}/#{result.total_lines} lines in #{result.files.size} files"
  show_branch_coverage = show_branch_coverage?(result)
  if show_branch_coverage
    puts "BRANCH COVERAGE: #{colorize(pct(result.coverage_statistics[:branch].percent))} -- #{result.covered_branches}/#{result.total_branches} branches in #{result.files.size} files"
  end
  puts

  if root.nil? then
    return
  end

  if SimpleCov::Formatter::Console.sort == 'coverage'
    if show_branch_coverage
      files = result.files.sort do |a,b|
        (a.covered_percent <=> b.covered_percent).nonzero? ||
          (a.coverage_statistics[:branch].percent <=> b.coverage_statistics[:branch].percent)
      end
    else
      files = result.files.sort_by(&:covered_percent)
    end
  else
    files = result.files.to_a
  end

  covered_files = 0

  unless SimpleCov::Formatter::Console.show_covered
    files.select!{ |file|
      if file.covered_percent == 100 && (!show_branch_coverage || file.coverage_statistics[:branch].percent == 100) then
        covered_files += 1
        false
      else
        true
      end
    }
    if files.nil? or files.empty? then
      return
    end
  end

  max_rows = SimpleCov::Formatter::Console.max_rows

  if ![-1, nil].include?(max_rows) && files.size > max_rows then
    puts "showing bottom (worst) #{max_rows} of #{files.size} files"
    files = files.slice(0, max_rows)
  end

  puts output(files, root, show_branch_coverage)

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

  if SimpleCov::Formatter::Console.output_to_file
    # Restore $stdout to its original state
    $stdout = STDOUT
    file.close
    puts "Coverage report generated for #{result.command_name} to #{result_file_path}"
  end
end

#include_output_styleObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/simplecov-console.rb', line 52

def include_output_style
  if SimpleCov::Formatter::Console.output_style == 'block' then
    require 'simplecov-console/output/block'
    extend BlockOutput
  else
    # default to table
    require 'simplecov-console/output/table'
    extend TableOutput
  end
end

#missed(missed_lines) ⇒ Array<String>

Group missed lines for better display

Parameters:

  • missed (Array<SimpleCov::SourceFile::Line>)

    array of missed lines reported by SimpleCov

Returns:

  • (Array<String>)

    Missing groups of lines



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/simplecov-console.rb', line 163

def missed(missed_lines)
  groups = {}
  base = nil
  previous = nil
  missed_lines.each do |src|
    ln = src.line_number
    if base && previous && (ln - 1) == previous
      groups[base] += 1
      previous = ln
    else
      base = ln
      groups[base] = 0
      previous = base
    end
  end

  group_str = []
  groups.map do |starting_line, length|
    if length > 0
      group_str << "#{starting_line}-#{starting_line + length}"
    else
      group_str << "#{starting_line}"
    end
  end

  max_lines = SimpleCov::Formatter::Console.max_lines
  if max_lines > 0 && group_str.size > max_lines then
    # Show at most N missing groups of lines
    group_str = group_str[0, SimpleCov::Formatter::Console.max_lines] << "..."
  end

  group_str
end

#pct(number) ⇒ Object



212
213
214
# File 'lib/simplecov-console.rb', line 212

def pct(number)
  sprintf("%6.2f%%", number)
end

#show_branch_coverage?(result) ⇒ Boolean

Returns:

  • (Boolean)


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

def show_branch_coverage?(result)
  Gem::Version.new(SimpleCov::VERSION) >= Gem::Version.new('0.18.5') &&
    result.coverage_statistics[:branch]
end

#trunc(str, type: :line) ⇒ Object

Truncate string to at most N chars (as defined by missing_len)



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/simplecov-console.rb', line 198

def trunc(str, type: :line)
  return str if str.include?("...") # already truncated, skip

  len = type == :branch ? SimpleCov::Formatter::Console.branch_missing_len : SimpleCov::Formatter::Console.missing_len
  if len > 0 && str.size > len
    truncated = str[0, len]
    if type == :branch && !truncated.end_with?(']')
      truncated = truncated.sub(/,?\s*\d+(\[[^\]]*)?$/, '') # removes partial branch symbols (3[then, 3[t, 3, etc.)
    end
    str = truncated.gsub(/,(\s+)?$/, '') + ' ...'
  end
  str
end

#use_colors?Boolean

Returns:

  • (Boolean)


216
217
218
# File 'lib/simplecov-console.rb', line 216

def use_colors?
  SimpleCov::Formatter::Console.use_colors
end