Skip to content

Commit

Permalink
Merge branch 'gh-1-output'
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Matatall committed Feb 13, 2012
2 parents 18496a5 + 1930cf8 commit 404cf4c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/guard/brakeman.rb
Expand Up @@ -36,8 +36,8 @@ def initialize(watchers = [], options = { })
# @raise [:task_has_failed] when stop has failed
#
def start
scanner_opts = ::Brakeman::set_options({:app_path => '.'}.merge(@options))
@scanner = ::Brakeman::Scanner.new(scanner_opts)
@scanner_opts = ::Brakeman::set_options({:app_path => '.'}.merge(@options))
@scanner = ::Brakeman::Scanner.new(@scanner_opts)
@tracker = @scanner.process

if @options[:run_on_start]
Expand Down Expand Up @@ -83,6 +83,13 @@ def print_failed report

puts all_warnings.sort_by { |w| w.confidence }

message = "#{all_warnings.count} brakeman findings"

if @options[:output_file]
write_report
message += "\nResults written to #{@options[:output_file]}"
end

if @options[:chatty] && all_warnings.any?
::Guard::Notifier.notify(message, :title => "Full Brakeman results", :image => icon)
end
Expand Down Expand Up @@ -132,6 +139,13 @@ def print_changed report
puts existing_warnings.sort_by { |w| w.confidence }
end



if @options[:output_file]
write_report
message += "\nResults written to #{@options[:output_file]}"
end

if @options[:notifications] && should_alert
::Guard::Notifier.notify(message.chomp, :title => "Brakeman results", :image => icon)
end
Expand All @@ -140,5 +154,11 @@ def print_changed report
def reject_below_threshold(warnings)
warnings.reject {|w| w.confidence > (3 - @options[:min_confidence].to_i)}
end

def write_report
File.open @options[:output_file], "w" do |f|
f.puts @tracker.report.send(@scanner_opts[:output_format])
end
end
end
end
45 changes: 45 additions & 0 deletions spec/guard/brakeman_spec.rb
Expand Up @@ -87,6 +87,24 @@
end
end

context 'with the output option' do
before(:each) do
@guard.instance_variable_set(:@options, {:output_file => 'test.csv'})
end

it 'writes the brakeman report to disk' do
@guard.should_receive(:write_report)
@guard.send :print_failed, report
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

context 'with notifications disabled' do
before(:each) do
@guard.instance_variable_set(:@options, {:chatty => false})
Expand Down Expand Up @@ -137,5 +155,32 @@
@guard.send :print_changed, report
end
end

context 'with the output option' do
before(:each) do
@guard.instance_variable_set(:@options, {:output_file => 'test.csv'})
end

it 'writes the brakeman report to disk' do
File.should_receive(:open).with('test.csv', 'w')
@guard.send :print_changed, report
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

describe "#write_report" do
it 'writes the report to disk' do
@guard.instance_variable_set(:@options, {:output_file => 'test.csv'})

File.should_receive(:open).with('test.csv', 'w')
@guard.send(:write_report)
end
end
end

0 comments on commit 404cf4c

Please sign in to comment.