From 369e8ce0b06cd156769fe301760ff753188cc84b Mon Sep 17 00:00:00 2001 From: Justin Collins Date: Sun, 9 Jun 2013 17:19:38 -0700 Subject: [PATCH] Fix output format detection to be more strict again and also -f should override detection --- lib/brakeman.rb | 2 +- lib/brakeman/report.rb | 23 +++++++---------------- test/tests/report_generation.rb | 6 ++++++ 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/lib/brakeman.rb b/lib/brakeman.rb index cd7bd2e4..4cc81f5c 100644 --- a/lib/brakeman.rb +++ b/lib/brakeman.rb @@ -296,7 +296,7 @@ module Brakeman def self.write_report_to_files tracker, output_files output_files.each_with_index do |output_file, idx| File.open output_file, "w" do |f| - f.write tracker.report.format(output_file) + f.write tracker.report.format(tracker.options[:output_formats][idx]) end notify "Report saved in '#{output_file}'" end diff --git a/lib/brakeman/report.rb b/lib/brakeman/report.rb index 4bd7c00b..d9ea7524 100644 --- a/lib/brakeman/report.rb +++ b/lib/brakeman/report.rb @@ -283,23 +283,14 @@ class Brakeman::Report end end - # format output from filename or format - def format(filename_or_format) - case filename_or_format - when /\.html/, :to_html - to_html - when /\.pdf/, :to_pdf - to_pdf - when /\.csv/, :to_csv - to_csv - when /\.json/, :to_json - to_json - when /\.tabs/, :to_tabs - to_tabs - when :to_test - to_test + # format output + def format format + valid_formats = [:to_html, :to_pdf, :to_csv, :to_json, :to_tabs, :to_test, :to_s] + + if valid_formats.include? format + self.send(format) else - to_s + raise "Invalid format: #{format}. Should be one of #{valid_formats.inspect}" end end diff --git a/test/tests/report_generation.rb b/test/tests/report_generation.rb index c750a7a1..9c102f79 100644 --- a/test/tests/report_generation.rb +++ b/test/tests/report_generation.rb @@ -39,4 +39,10 @@ class TestReportGeneration < Test::Unit::TestCase assert report.is_a? String end + + def test_bad_format_type + assert_raises RuntimeError do + Report.format(:to_something_else) + end + end end -- GitLab