lib/covered/summary.rb in covered-0.20.1 vs lib/covered/summary.rb in covered-0.20.2

- old
+ new

@@ -27,10 +27,11 @@ terminal[:uncovered_code] ||= terminal.style(:red) terminal[:covered_code] ||= terminal.style(:green) terminal[:ignored_code] ||= terminal.style(nil, nil, :faint) terminal[:annotations] ||= terminal.style(:blue) + terminal[:error] ||= terminal.style(:red) end end def each(wrapper) statistics = Statistics.new @@ -80,40 +81,53 @@ unless line.end_with? $/ terminal.puts end end + def print_coverage(terminal, coverage) + line_offset = 1 + counts = coverage.counts + + coverage.read do |file| + print_line_header(terminal) + + file.each_line do |line| + count = counts[line_offset] + + print_annotations(terminal, coverage, line, line_offset) + + print_line(terminal, line, line_offset, count) + + line_offset += 1 + end + end + end + + def print_error(terminal, error) + terminal.puts "Error: #{error.message}", style: :error + terminal.puts error.backtrace + end + # A coverage array gives, for each line, the number of line execution by the interpreter. A nil value means coverage is finishd for this line (lines like else and end). - def call(wrapper, output = $stdout) + def call(wrapper, output = $stdout, **options) terminal = self.terminal(output) statistics = self.each(wrapper) do |coverage| - line_offset = 1 - path = wrapper.relative_path(coverage.path) terminal.puts "" terminal.puts path, style: :path - counts = coverage.counts - - coverage.read do |file| - print_line_header(terminal) - - file.each_line do |line| - count = counts[line_offset] - - print_annotations(terminal, coverage, line, line_offset) - - print_line(terminal, line, line_offset, count) - - line_offset += 1 - end + begin + print_coverage(terminal, coverage, **options) + rescue => error + print_error(terminal, error) end coverage.print(output) end + terminal.puts statistics.print(output) end end class FullSummary < Summary @@ -148,52 +162,38 @@ end end end class PartialSummary < Summary - def call(wrapper, output = $stdout, before: 4, after: 4) - terminal = self.terminal(output) + def print_coverage(terminal, coverage, before: 4, after: 4) + return if coverage.zero? - statistics = self.each(wrapper) do |coverage| - line_offset = 1 + line_offset = 1 + counts = coverage.counts + last_line = nil + + coverage.read do |file| + print_line_header(terminal) - path = wrapper.relative_path(coverage.path) - terminal.puts "" - terminal.puts path, style: :path - - counts = coverage.counts - last_line = nil - - unless coverage.zero? - print_line_header(terminal) + file.each_line do |line| + range = Range.new([line_offset - before, 0].max, line_offset+after) - coverage.read do |file| - file.each_line do |line| - range = Range.new([line_offset - before, 0].max, line_offset+after) - - if counts[range]&.include?(0) - count = counts[line_offset] - - if last_line and last_line != line_offset-1 - terminal.puts ":".rjust(16) - end - - print_annotations(terminal, coverage, line, line_offset) - print_line(terminal, line, line_offset, count) - - last_line = line_offset - end - - line_offset += 1 + if counts[range]&.include?(0) + count = counts[line_offset] + + if last_line and last_line != line_offset-1 + terminal.puts ":".rjust(16) end + + print_annotations(terminal, coverage, line, line_offset) + print_line(terminal, line, line_offset, count) + + last_line = line_offset end + + line_offset += 1 end - - coverage.print(output) end - - terminal.puts - statistics.print(output) end end class Quiet def call(wrapper, output = $stdout)