Browse files

add html reports

  • Loading branch information...
1 parent b44cc11 commit 3abffb4ef4432a59ac4245de9e5e405462e28e50 @calavera calavera committed Jun 30, 2011
View
1 .gitignore
@@ -3,3 +3,4 @@
Gemfile.lock
pkg/*
tmp
+lint-spec-report.html
View
12 lib/jruby/lint/cli.rb
@@ -23,6 +23,18 @@ def process_options(args)
@options.eval << v
end
+ opts.on('--text', 'print report as text') do
+ @options.text = true
+ end
+
+ opts.on('--ansi', 'print report as ansi text') do
+ @options.ansi = true
+ end
+
+ opts.on('--html [REPORT_FILE]', 'print report as html file') do |file|
+ @options.html = file || 'jruby-lint.html'
+ end
+
opts.on_tail("-v", "--version", "Print version and exit") do
require 'jruby/lint/version'
puts "JRuby-Lint version #{VERSION}"
View
8 lib/jruby/lint/finding.rb
@@ -8,6 +8,14 @@ def initialize(*args)
super
end
+ def error?
+ tags.include?('error')
+ end
+
+ def warning?
+ tags.include?('warning')
+ end
+
def to_s
"#{file}:#{line}: [#{tags.join(', ')}] #{message}"
end
View
9 lib/jruby/lint/project.rb
@@ -19,7 +19,7 @@ def initialize(options = OpenStruct.new)
@sources = options.files || (options.eval ? [] : Dir['./**/*'])
load_collectors
- load_reporters
+ load_reporters(options)
load_libraries
end
@@ -45,8 +45,11 @@ def load_collectors
end
end
- def load_reporters
- @reporters = [(STDOUT.tty? ? Reporters::ANSIColor : Reporters::Text).new(self, STDOUT)]
+ def load_reporters(options)
+ @reporters = []
+ @reporters << Reporters::Html.new(self, options.html) if options.html
+ @reporters << Reporters::ANSIColor.new(self, STDOUT) if options.ansi || STDOUT.tty?
+ @reporters << Reporters::Text.new(self, STDOUT) if options.text || @reporters.empty?
end
def load_libraries
View
34 lib/jruby/lint/reporters.rb
@@ -1,35 +1,7 @@
-require 'term/ansicolor'
-
module JRuby::Lint
module Reporters
- class Text
- def initialize(project, output)
- @tags, @output = project.tags, output
- end
-
- def report(findings)
- findings.each do |finding|
- puts finding unless (@tags & finding.tags).empty?
- end
- end
-
- def puts(finding)
- @output.puts finding.to_s
- end
- end
-
- class ANSIColor < Text
- include Term::ANSIColor
- def puts(finding)
- msg = if finding.tags.include?("error")
- red(finding.to_s)
- elsif finding.tags.include?("warning")
- yellow(finding.to_s)
- else
- finding.to_s
- end
- @output.puts msg
- end
- end
end
end
+
+require 'jruby/lint/reporters/text'
+require 'jruby/lint/reporters/html'
View
20 lib/jruby/lint/reporters/html.rb
@@ -0,0 +1,20 @@
+module JRuby::Lint
+ module Reporters
+ class Html
+ require 'erb'
+
+ def initialize(project, output)
+ @tags = project.tags
+ @output = output
+ @template = ERB.new(File.read(File.expand_path('../jruby-lint.html.erb', __FILE__)))
+ end
+
+ def report(findings)
+ @findings = findings
+ File.open(@output, 'w') do |file|
+ file.write @template.result(binding)
+ end
+ end
+ end
+ end
+end
View
40 lib/jruby/lint/reporters/jruby-lint.html.erb
@@ -0,0 +1,40 @@
+<html>
+ <head>
+ <title>JRuby-lint diagnostics report</title>
+ <style>
+ul { padding: 0px; }
+li { list-style: none; margin: 0px; padding: 0px; display: list-item; }
+body { font-family: 'Lucida Grande', Verdana, Arial, Sans-Serif; font-size:10.5pt; margin: 0; }
+
+h1 { color: #383838; font-size: 4em; }
+.site { width: 70em; margin: 0 auto; padding: .1em 5em; }
+nav { position: relative; }
+#findings li { position: relative; padding: .5em 1em; margin-top: .3em; }
+.message { border-left: 5px solid silver; background: #EEE; }
+.error { border-left: 5px solid #b22222; background: #E6BCB5; }
+.warning { border-left: 5px solid #FF9900; background: #FFFF99; }
+ </style>
+ </head>
+ <body>
+ <div class="site">
+ <nav>
+ <h1>JRuby diagnostics</h1>
+ </nav>
+ <div id="findings">
+ <% unless @findings.empty? %>
+ <ul>
+ <% @findings.each do |finding| %>
+ <% if finding.error? %>
+ <li class="error"><%= finding %></li>
+ <% elsif finding.warning? %>
+ <li class="warning"><%= finding %></li>
+ <% else %>
+ <li class="message"><%= finding %></li>
+ <% end %>
+ <% end %>
+ </ul>
+ <% end %>
+ </div> <!-- div#findings -->
+ </div> <!-- div.site -->
+ </body>
+</html>
View
34 lib/jruby/lint/reporters/text.rb
@@ -0,0 +1,34 @@
+module JRuby::Lint
+ module Reporters
+ class Text
+ def initialize(project, output)
+ @tags, @output = project.tags, output
+ end
+
+ def report(findings)
+ findings.each do |finding|
+ puts finding unless (@tags & finding.tags).empty?
+ end
+ end
+
+ def puts(finding)
+ @output.puts finding.to_s
+ end
+ end
+
+ class ANSIColor < Text
+ require 'term/ansicolor'
+ include Term::ANSIColor
+ def puts(finding)
+ msg = if finding.error?
+ red(finding.to_s)
+ elsif finding.warning?
+ yellow(finding.to_s)
+ else
+ finding.to_s
+ end
+ @output.puts msg
+ end
+ end
+ end
+end
View
12 spec/jruby/lint/finding_spec.rb
@@ -28,4 +28,16 @@
When { @finding = JRuby::Lint::Finding.new(message, tags, file, line) }
Then { @finding.tags.should == ["1", "two", "3.0"] }
end
+
+ it "is an error when it includes the tag `error'" do
+ Given(:tags) { [:error] }
+ When { @finding = JRuby::Lint::Finding.new(message, tags, file, line) }
+ Then { @finding.should be_error }
+ end
+
+ it "is a warning when it includes the tag `warning'" do
+ Given(:tags) { [:warning] }
+ When { @finding = JRuby::Lint::Finding.new(message, tags, file, line) }
+ Then { @finding.should be_warning }
+ end
end
View
44 spec/jruby/lint/project_spec.rb
@@ -74,4 +74,48 @@
Then { project.run }
end
+
+ context 'loading reporters' do
+ context 'with html option' do
+ Given(:options) { OpenStruct.new(:html => 'report.html') }
+ Given(:project) { in_current_dir { JRuby::Lint::Project.new(options) } }
+ Then { project.reporters.should have(1).reporter }
+ Then { project.reporters.first.should be_an_instance_of(JRuby::Lint::Reporters::Html) }
+ end
+
+ context 'with ansi option' do
+ Given(:options) { OpenStruct.new(:ansi => true) }
+ Given(:project) { in_current_dir { JRuby::Lint::Project.new(options) } }
+ Then { project.reporters.should have(1).reporter }
+ Then { project.reporters.first.should be_an_instance_of(JRuby::Lint::Reporters::ANSIColor) }
+ end
+
+ context 'with text option' do
+ Given(:options) { OpenStruct.new(:text => true) }
+ Given(:project) { in_current_dir { JRuby::Lint::Project.new(options) } }
+ Then { project.reporters.should have(1).reporter }
+ Then { project.reporters.first.should be_an_instance_of(JRuby::Lint::Reporters::Text) }
+ end
+
+ context 'with tty' do
+ Given { STDOUT.stub(:tty?).and_return(true) }
+ Given(:project) { in_current_dir { JRuby::Lint::Project.new } }
+ Then { project.reporters.should have(1).reporter }
+ Then { project.reporters.first.should be_an_instance_of(JRuby::Lint::Reporters::ANSIColor) }
+ end
+
+ context 'without any option' do
+ Given(:project) { in_current_dir { JRuby::Lint::Project.new } }
+ Then { project.reporters.should have(1).reporter }
+ Then { project.reporters.first.should be_an_instance_of(JRuby::Lint::Reporters::Text) }
+ end
+
+ context 'with several options' do
+ Given(:options) { OpenStruct.new(:ansi => true, :html => 'report.html') }
+ Given(:project) { in_current_dir { JRuby::Lint::Project.new(options) } }
+ Then { project.reporters.should have(2).reporter }
+ Then { project.reporters.map(&:class).should include(JRuby::Lint::Reporters::ANSIColor) }
+ Then { project.reporters.map(&:class).should include(JRuby::Lint::Reporters::Html) }
+ end
+ end
end
View
20 spec/jruby/lint/reporters_spec.rb
@@ -26,17 +26,33 @@
Given(:reporter) { JRuby::Lint::Reporters::ANSIColor.new(project, output) }
context "shows a finding tagged 'error' in red" do
- Given(:finding) { double "finding", :to_s => "hello", :tags => %w(error) }
+ Given(:finding) { double "finding", :to_s => "hello", :tags => %w(error), :error? => true }
Given(:output) { double("output").tap {|o| o.should_receive(:puts).with(red("hello")) } }
Then { reporter.report [finding] }
end
context "shows a finding tagged 'warning' in yellow" do
- Given(:finding) { double "finding", :to_s => "hello", :tags => %w(warning) }
+ Given(:finding) { double "finding", :to_s => "hello", :tags => %w(warning), :error? => false, :warning? => true }
Given(:output) { double("output").tap {|o| o.should_receive(:puts).with(yellow("hello")) } }
Then { reporter.report [finding] }
end
end
+
+ context "Html reporter" do
+ Given(:reporter) { JRuby::Lint::Reporters::Html.new(project, 'lint-spec-report.html') }
+
+ context "shows a finding tagged 'error' in red" do
+ Given(:finding) { double "finding", :to_s => "hello", :tags => %w(error), :error? => true }
+ Then { reporter.report [finding] }
+ Then { File.read('lint-spec-report.html').should include('<li class="error">hello</li>') }
+ end
+
+ context "shows a finding tagged 'warning' in yellow" do
+ Given(:finding) { double "finding", :to_s => "hello", :tags => %w(warning), :error? => false, :warning? => true }
+ Then { reporter.report [finding] }
+ Then { File.read('lint-spec-report.html').should include('<li class="warning">hello</li>') }
+ end
+ end
end

0 comments on commit 3abffb4

Please sign in to comment.