diff --git a/lib/brakeman.rb b/lib/brakeman.rb index cd7bd2e46a1fa7d9ce33f78bb3b052bfad3e774f..4cc81f5c115c357cfcfae81b2e831678c040bf95 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 4bd7c00bda53d18927407595d4f9ce39bbe2f63a..d9ea75246e42d73c7d680daf65e2ef052c98e656 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 c750a7a16ccf0a195accffa7ae6aac149f015bb5..9c102f79f2cb7ebdfcd45c1de10641c55928f8c3 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