Permalink
Browse files

Merge branch 'cleanup_output'

  • Loading branch information...
2 parents 90988d0 + 2789296 commit c5da1278bc064373c2563d1bd3d93bff3ea7effd @oreoshake oreoshake committed Oct 8, 2012
Showing with 93 additions and 54 deletions.
  1. +4 −1 HISTORY.md
  2. +1 −2 README.md
  3. +3 −3 guard-brakeman.gemspec
  4. +72 −35 lib/guard/brakeman.rb
  5. +12 −12 spec/guard/brakeman_spec.rb
  6. +1 −1 tmp/aruba/default_app/Guardfile
View
@@ -1,4 +1,7 @@
-# 0.5.0
+# 0.5.1
+- Cleanup the output and clean up some code
+
+# 0.5.0
- Support for >= guard-1.1.0
# 0.4.0
View
@@ -27,7 +27,7 @@ $ guard init brakeman
## Use sublime Text 2?
-Check out [sublime_guard](https://github.com/cyphactor/sublime_guard)! It gives you control Guard without leaving the editor. This is even more powerful with Growl notifications. Enter distraction-free mode and never leave!
+Check out [sublime_guard](https://github.com/cyphactor/sublime_guard)! It gives you control Guard without leaving the editor. This is even more powerful with Growl notifications. Enter distraction-free mode and never leave!
## Usage
@@ -85,7 +85,6 @@ Pull requests are very welcome! Please try to follow these simple rules if appli
* Please create a topic branch for every separate change you make.
* Make sure your patches are well tested.
-* Update the [Yard](http://yardoc.org/) documentation.
* Update the README.
* Update the CHANGELOG for noteworthy changes.
* Please **do not change** the version number.
@@ -3,7 +3,7 @@ $:.push File.expand_path('../lib', __FILE__)
Gem::Specification.new do |s|
s.name = 'guard-brakeman'
- s.version = '0.5.0'
+ s.version = '0.5.1'
s.platform = Gem::Platform::RUBY
s.authors = ['Neil Matatall', 'Justin Collins']
s.homepage = 'http://rubygems.org/gems/guard-brakeman'
@@ -13,10 +13,10 @@ Gem::Specification.new do |s|
s.rubyforge_project = 'guard-brakeman'
s.add_dependency 'guard', '>= 1.1.0'
- s.add_dependency 'brakeman', '>= 1.5.3'
+ s.add_dependency 'brakeman', '~> 1.7.0'
s.files = Dir.glob('{lib}/**/*') + %w[LICENSE README.md]
s.require_path = 'lib'
- s.rdoc_options = ["--charset=UTF-8", "--main=README.md", "--exclude='(lib|test|spec)|(Gem|Guard|Rake)file'"]
+ s.rdoc_options = ["--charset=UTF-8", "--main=README.md", "--exclude='(test|spec)|(Gem|Guard|Rake)file'"]
end
View
@@ -12,8 +12,10 @@ class Brakeman < Guard
def initialize(watchers = [], options = { })
super
+ ::Brakeman.instance_variable_set(:@quiet, options[:quiet])
+
if options[:skip_checks]
- options[:skip_checks] = options[:skip_checks].map do |val|
+ options[:skip_checks] = options[:skip_checks].map do |val|
# mimic Brakeman::set_options behavior
val[0,5] == "Check" ? val : "Check" << val
end
@@ -24,10 +26,10 @@ def initialize(watchers = [], options = { })
# TODO mixing the use of this attr, good to match? Bad to couple?
@options = {
- :notifications => true,
- :run_on_start => false,
- :chatty => false,
- :min_confidence => 1
+ :notifications => true,
+ :run_on_start => false,
+ :chatty => false,
+ :min_confidence => 1
}.update(options)
end
@@ -38,11 +40,10 @@ def initialize(watchers = [], options = { })
def start
@scanner_opts = ::Brakeman::set_options({:app_path => '.'}.merge(@options))
@options.merge!(@scanner_opts)
-
@tracker = ::Brakeman::Scanner.new(@scanner_opts).process
if @options[:run_on_start]
- run_all
+ run_all
elsif @options[:chatty]
::Guard::Notifier.notify("Brakeman is ready to work!", :title => "Brakeman started", :image => :pending)
end
@@ -53,7 +54,6 @@ def start
# @raise [:task_has_failed] when stop has failed
#
def run_all
- UI.info 'running all'
@tracker.run_checks
print_failed(@tracker.checks)
throw :task_has_failed if @tracker.checks.all_warnings.any?
@@ -82,71 +82,76 @@ def print_failed report
all_warnings = report.all_warnings
- puts all_warnings.sort_by { |w| w.confidence }
-
message = "#{all_warnings.count} brakeman findings"
if @options[:output_files]
write_report
- message += "\nResults written to #{@options[:output_files]}"
+ message += "\nResults written to #{@options[:output_files]}"
end
if @options[:chatty] && all_warnings.any?
- ::Guard::Notifier.notify(message, :title => "Full Brakeman results", :image => icon)
+ ::Guard::Notifier.notify(message, :title => "Full Brakeman results", :image => icon)
end
+
+ info(message, 'yellow')
+ warning_info(all_warnings.sort_by { |w| w.confidence })
end
def print_changed report
UI.info "\n------ brakeman warnings --------\n"
-
- message = ""
+
+ message = []
should_alert = false
fixed_warnings = report.fixed_warnings
if fixed_warnings.any?
- icon = :success
results_notification = pluralize(fixed_warnings.length, "fixed warning")
- UI.info(UI.send(:color, results_notification, 'green')) # janky
-
- should_alert = true
- message += results_notification
+ info(results_notification, 'green')
+ warning_info(fixed_warnings.sort_by { |w| w.confidence })
- puts fixed_warnings.sort_by { |w| w.confidence }
- puts ""
+ message << results_notification
+ should_alert = true
+ icon = :success
end
new_warnings = report.new_warnings
if new_warnings.any?
new_warning_message = pluralize(new_warnings.length, "new warning")
- UI.info(UI.send(:color, new_warning_message, 'red')) # janky
+ info(new_warning_message, 'red')
+ warning_info(new_warnings.sort_by { |w| w.confidence })
- message += new_warning_message
+ message << new_warning_message
should_alert = true
icon = :failed
-
- puts new_warnings.sort_by { |w| w.confidence }
- puts ""
end
existing_warnings = report.existing_warnings
if existing_warnings.any?
- should_alert = true if @options[:chatty]
- icon ||= :pending
-
existing_warning_message = pluralize(existing_warnings.length, "previous warning")
- UI.warning existing_warning_message
- message += existing_warning_message
+ info(existing_warning_message, 'yellow')
+ warning_info(existing_warnings.sort_by { |w| w.confidence })
- puts existing_warnings.sort_by { |w| w.confidence }
+ message << existing_warning_message
+ should_alert = true if @options[:chatty]
+ icon ||= :pending
end
if @options[:output_files]
write_report
- message += "\nResults written to #{@options[:output_files]}"
+ message << "\nResults written to #{@options[:output_files]}"
+ end
+
+ title = case icon
+ when :success
+ pluralize(fixed_warnings.length, "Warning") + " fixed."
+ when :pending
+ pluralize(existing_warnings.length, "Warning") + " left to fix."
+ when :failed
+ pluralize(new_warnings.length, "Warning") + " introduced."
end
if @options[:notifications] && should_alert
- ::Guard::Notifier.notify(message.chomp, :title => "Brakeman results", :image => icon)
+ ::Guard::Notifier.notify(message.join(", ").chomp, :title => title, :image => icon)
end
end
@@ -158,9 +163,41 @@ def write_report
end
end
- # stolen from rails
+ # stolen from ActiveSupport
def pluralize(count, singular, plural = nil)
"#{count || 0} " + ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize))
end
+
+ def info(message, color = :white)
+ UI.info(UI.send(:color, message, color))
+ end
+
+ def warning_info(warnings, color = :white)
+ warnings.each do |warning|
+ info(decorate_warning(warning))
+ end
+ end
+
+ def decorate_warning(warning)
+ color = case warning.confidence
+ when 0
+ :red
+ when 1
+ :yellow
+ when 2
+ :white
+ end
+
+ output = UI.send(:color, ::Brakeman::Warning::TEXT_CONFIDENCE[warning.confidence], color)
+ output << " - #{warning.warning_type} - #{warning.message}"
+ output << " near line #{warning.line}" if warning.line
+ if warning.file
+ # fix this ish or wait for brakeman to be fixed
+ filename = warning.file.gsub(@options[:app_path], '')
+ output << " in #{filename}"
+ end
+ output << ": #{warning.format_code}" if warning.code
+ output
+ end
end
end
@@ -5,12 +5,12 @@
# Pending tests
describe Guard::Brakeman do
let(:default_options) { {:cli => '--stuff'} }
- let(:tracker) { double().as_null_object }
+ let(:tracker) { double().as_null_object }
let(:report) { double().as_null_object }
before(:each) do
@guard = Guard::Brakeman.new
-
+ @guard.stub(:decorate_warning)
@guard.instance_variable_set(:@tracker, tracker)
@guard.instance_variable_set(:@options, {:notifications => false, :app_path => 'tmp/aruba/default_app'})
end
@@ -28,7 +28,7 @@
before(:each) do
@guard.instance_variable_set(:@options, @guard.instance_variable_get(:@options).merge({:run_on_start => true}))
end
-
+
it 'runs all checks' do
scanner.stub(:process).and_return(tracker)
@guard.should_receive(:run_all)
@@ -92,14 +92,14 @@
it 'writes the brakeman report to disk' do
@guard.should_receive(:write_report)
@guard.send :print_failed, report
- end
+ end
it 'adds the report filename to the growl' do
@guard.stub(:write_report)
@guard.instance_variable_set(:@options, @guard.instance_variable_get(:@options).merge({:chatty => true}))
::Guard::Notifier.should_receive(:notify).with(/test\.csv/, anything)
@guard.send :print_failed, report
- end
+ end
end
context 'with notifications disabled' do
@@ -112,8 +112,8 @@
@guard.send :print_failed, report
end
end
- end
-
+ end
+
describe '#print_changed' do
before(:each) do
report.stub(:all_warnings).and_return [double(:confidence => 3)]
@@ -124,7 +124,7 @@
before(:each) do
@guard.instance_variable_set(:@options, @guard.instance_variable_get(:@options).merge(options))
end
-
+
it 'does not alert on warnings below the threshold' do
::Guard::Notifier.should_not_receive :notify
@guard.send :print_changed, report
@@ -139,9 +139,9 @@
it 'notifies the user' do
::Guard::Notifier.should_receive :notify
@guard.send :print_changed, report
- end
+ end
end
-
+
context 'with notifications disabled' do
before(:each) do
@guard.instance_variable_set(:@options, {:notifications => false})
@@ -161,14 +161,14 @@
it 'writes the brakeman report to disk' do
File.should_receive(:open).with('test.csv', 'w')
@guard.send :print_changed, report
- end
+ end
it 'adds the report filename to the growl' do
@guard.stub(:write_report)
@guard.instance_variable_set(:@options, @guard.instance_variable_get(:@options).merge({:notifications => true}))
::Guard::Notifier.should_receive(:notify).with(/test\.csv/, anything)
@guard.send :print_changed, report
- end
+ end
end
end
@@ -1,4 +1,4 @@
-guard 'brakeman', :chatty => true, :notifications => true, :run_on_start => true, :min_confidence => 0 do
+guard 'brakeman', :chatty => false,:notifications => true, :run_on_start => true, :min_confidence => 0 do
watch(%r{^app/.+\.(erb|haml|rhtml|rb)$})
watch(%r{^config/.+\.rb$})
watch(%r{^lib/.+\.rb$})

0 comments on commit c5da127

Please sign in to comment.