Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConsoleFormatter #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cover_me.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |s|
s.description = %q{CoverMe - Code Coverage for Ruby 1.9}
s.email = %q{[email protected]}
s.extra_rdoc_files = ["LICENSE"]
s.files = ["lib/cover_me/config.rb", "lib/cover_me/directory_report.rb", "lib/cover_me/emma_formatter.rb", "lib/cover_me/formatter.rb", "lib/cover_me/global_report.rb", "lib/cover_me/hash.rb", "lib/cover_me/html_formatter.rb", "lib/cover_me/index.rb", "lib/cover_me/processor.rb", "lib/cover_me/report.rb", "lib/cover_me/results.rb", "lib/cover_me/templates/emma.xml.erb", "lib/cover_me/templates/index.css", "lib/cover_me/templates/index.html.erb", "lib/cover_me/templates/jquery.js", "lib/cover_me/templates/jquery.tablesorter.js", "lib/cover_me/templates/report.css", "lib/cover_me/templates/report.html.erb", "lib/cover_me.rb", "lib/generators/cover_me/install/install_generator.rb", "lib/generators/cover_me/install/templates/cover_me.rake", "LICENSE"]
s.files = ["lib/cover_me/config.rb", "lib/cover_me/console_formatter.rb", "lib/cover_me/directory_report.rb", "lib/cover_me/emma_formatter.rb", "lib/cover_me/formatter.rb", "lib/cover_me/global_report.rb", "lib/cover_me/hash.rb", "lib/cover_me/html_formatter.rb", "lib/cover_me/index.rb", "lib/cover_me/processor.rb", "lib/cover_me/report.rb", "lib/cover_me/results.rb", "lib/cover_me/templates/console.erb", "lib/cover_me/templates/console.file.erb", "lib/cover_me/templates/console.line.erb", "lib/cover_me/templates/emma.xml.erb", "lib/cover_me/templates/index.css", "lib/cover_me/templates/index.html.erb", "lib/cover_me/templates/jquery.js", "lib/cover_me/templates/jquery.tablesorter.js", "lib/cover_me/templates/report.css", "lib/cover_me/templates/report.html.erb", "lib/cover_me.rb", "lib/generators/cover_me/install/install_generator.rb", "lib/generators/cover_me/install/templates/cover_me.rake", "LICENSE"]
s.homepage = %q{http://www.metabates.com}
s.post_install_message = %q{Thank you for installing CoverMe!

Expand Down
4 changes: 2 additions & 2 deletions lib/cover_me.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def complete!

CoverMe.set_defaults

%w{index report formatter processor html_formatter emma_formatter results directory_report global_report}.each do |file|
%w{index report formatter processor html_formatter emma_formatter console_formatter results directory_report global_report}.each do |file|
require File.expand_path(File.join(path, file))
end

Expand All @@ -42,4 +42,4 @@ def complete!

at_exit do
CoverMe::Results.merge_results!(Coverage.result)
end
end
2 changes: 2 additions & 0 deletions lib/cover_me/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def set_defaults # :nodoc:
c.html_formatter.set_default(:finalizer_files, {'report.css' => 'report.css', 'index.css' => 'index.css',
'jquery.js' => 'jquery.js', 'jquery.tablesorter.js' => 'jquery.tablesorter.js'})
c.emma_formatter.set_default(:output_path, Configatron::Delayed.new {File.join(CoverMe.config.project.root, 'coverage')})
c.console_formatter.set_default(:verbose, true)
c.console_formatter.set_default(:use_color, true)
end
end

Expand Down
66 changes: 66 additions & 0 deletions lib/cover_me/console_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class CoverMe::ConsoleFormatter < CoverMe::Formatter

COLOR_CODE = {
reset: '0',

foreground: '3',
background: '4',

black: '0',
red: '1',
green: '2',
yellow: '3',
blue: '4',
magenta: '5',
cyan: '6',
white: '7',

bold: '1',
italic: '3',
underline: '4'
}

COLOR = {
'reset' => "\e[" + COLOR_CODE[:reset] + 'm',
'hit' => "\e[" + COLOR_CODE[:foreground] + COLOR_CODE[:white] +
';' + COLOR_CODE[:bold] +
';' + COLOR_CODE[:background] + COLOR_CODE[:green] + 'm',
'near' => "\e[" + COLOR_CODE[:foreground] + COLOR_CODE[:yellow] +
';' + COLOR_CODE[:bold] +
';' + COLOR_CODE[:background] + COLOR_CODE[:black] + 'm',
'miss' => "\e[" + COLOR_CODE[:foreground] + COLOR_CODE[:white] +
';' + COLOR_CODE[:bold] +
';' + COLOR_CODE[:background] + COLOR_CODE[:red] + 'm'
}

def format_report(report)
end

def format_index(index)
template_file = File.join(File.dirname(__FILE__), 'templates', 'console.erb')
config = CoverMe.config.console_formatter
color = config.use_color ? COLOR : {}

template('console.erb', '-').run(binding)
return if index.percent_tested == 100

index.reports.sort.each do |report|
template('console.file.erb', '-').run(binding)
next unless config.verbose

$stdout.puts ' Untested line(s):' unless report.executed_percent == 100.0
num_width = (report.source.length).to_s.length
last_rendered = nil
report.coverage.each_with_index do |count, line|
next unless report.coverage[line - 1] == 0 ||
count == 0 ||
report.coverage[line + 1] == 0

status = count == 0 ? 'miss' : 'hit'
template('console.line.erb', '-').run(binding)
$stdout.puts '' unless count == 0 || report.coverage[line + 1] == 0
end
$stdout.puts '' if config.verbose && index.percent_tested == 100
end
end
end
8 changes: 5 additions & 3 deletions lib/cover_me/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ def finalize

# Returns an ERB object based on the template file requested.
# Template files are expected to live in the _templates_ directory.
# Can receive trim_mode of ERB as second argument.
#
# Example:
# template('index.html.erb') # => ERB object
# templaet('console.erb', '-') # => ERB object with trim_mode "-"
#
# The ERB object return still needs to be bound and processed.
def template(file)
ERB.new(File.read(File.join(File.dirname(__FILE__), 'templates', file)))
def template(file, trim_mode = nil)
ERB.new(File.read(File.join(File.dirname(__FILE__), 'templates', file)), nil, trim_mode)
end

end
end
2 changes: 2 additions & 0 deletions lib/cover_me/templates/console.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

<%= index.percent_tested < 100 ? color['miss'] : color['hit'] %><%= index.percent_tested %>% tested.<%= color['reset'] %>
1 change: 1 addition & 0 deletions lib/cover_me/templates/console.file.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= color[report.proximity] %><%= report.filename %> - <%= report.executed_percent %>%(<%= report.lines_executed %>/<%= report.lines_of_code %> lines) tested.<%= color['reset'] %>
1 change: 1 addition & 0 deletions lib/cover_me/templates/console.line.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= color[status] %><%= sprintf "%0#{num_width}d", line + 1 %>:<%= report.source[line].chomp %><%= color['reset'] %>
29 changes: 29 additions & 0 deletions spec/cover_me/console_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'

describe CoverMe::ConsoleFormatter do

before(:each) do
@formatter = CoverMe::ConsoleFormatter.new
$stdout = @output = StringIO.new
end

describe "format_report" do

it "should exist" do
@formatter.should respond_to(:format_report)
end

end

describe "format_index" do

it "should output to $stdout" do
index = CoverMe::Index.new
index.reports << CoverMe::Report.new(File.join(CoverMe.config.project.root, 'app', 'models', 'user.rb'), [1])
@formatter.format(index)
@output.string.length.should > 0
end

end

end
4 changes: 4 additions & 0 deletions spec/cover_me/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
@formatter.template('index.html.erb').should be_kind_of(ERB)
end

it "should receive trim_mode as second argument" do
lambda {@formatter.template('console.erb', '-')}.should_not raise_error
end

end

end