提交 23c35630 编写于 作者: S soffolk

refactor get_options and get_output_format

上级 1fe36bc4
......@@ -62,16 +62,10 @@ module Brakeman
if options.is_a? String
options = { :app_path => options }
end
options = default_options.merge(load_options(options[:config_file])).merge(options)
options[:app_path] = File.expand_path(options[:app_path])
file_options = load_options(options[:config_file])
options = file_options.merge options
options[:quiet] = true if options[:quiet].nil? && file_options[:quiet]
options = get_defaults.merge! options
options[:output_formats] = get_output_formats options
options
......@@ -102,7 +96,7 @@ module Brakeman
end
#Default set of options
def self.get_defaults
def self.default_options
{ :assume_all_routes => true,
:skip_checks => Set.new,
:check_arguments => true,
......@@ -130,42 +124,51 @@ module Brakeman
raise ArgumentError, "Cannot specify output format if multiple output files specified"
end
if options[:output_format]
[
case options[:output_format]
when :html, :to_html
:to_html
when :csv, :to_csv
:to_csv
when :pdf, :to_pdf
:to_pdf
when :tabs, :to_tabs
:to_tabs
when :json, :to_json
:to_json
else
:to_s
end
]
get_formats_from_output_format options[:output_format]
elsif options[:output_files]
get_formats_from_output_files options[:output_files]
else
return [:to_s] unless options[:output_files]
options[:output_files].map do |output_file|
case output_file
when /\.html$/i
:to_html
when /\.csv$/i
:to_csv
when /\.pdf$/i
:to_pdf
when /\.tabs$/i
:to_tabs
when /\.json$/i
:to_json
else
:to_s
end
return [:to_s]
end
end
def self.get_formats_from_output_format output_format
case output_format
when :html, :to_html
[:to_html]
when :csv, :to_csv
[:to_csv]
when :pdf, :to_pdf
[:to_pdf]
when :tabs, :to_tabs
[:to_tabs]
when :json, :to_json
[:to_json]
else
[:to_s]
end
end
private_class_method :get_formats_from_output_format
def self.get_formats_from_output_files output_files
output_files.map do |output_file|
case output_file
when /\.html$/i
:to_html
when /\.csv$/i
:to_csv
when /\.pdf$/i
:to_pdf
when /\.tabs$/i
:to_tabs
when /\.json$/i
:to_json
else
:to_s
end
end
end
private_class_method :get_formats_from_output_files
#Output list of checks (for `-k` option)
def self.list_checks
......
......@@ -97,4 +97,41 @@ class ConfigTests < Test::Unit::TestCase
assert_nil final_options[:quiet]
end
def test_output_format_with_default
options = {}
output_format = Brakeman.get_output_formats(options)
assert_equal [:to_s], output_format
end
def output_format_tester options, expected_options
output_formats = Brakeman.get_output_formats(options)
assert_equal expected_options, output_formats
end
def test_output_format
output_format_tester({}, [:to_s])
output_format_tester({:output_format => :html}, [:to_html])
output_format_tester({:output_format => :to_html}, [:to_html])
output_format_tester({:output_format => :csv}, [:to_csv])
output_format_tester({:output_format => :to_csv}, [:to_csv])
output_format_tester({:output_format => :pdf}, [:to_pdf])
output_format_tester({:output_format => :to_pdf}, [:to_pdf])
output_format_tester({:output_format => :json}, [:to_json])
output_format_tester({:output_format => :to_json}, [:to_json])
output_format_tester({:output_format => :tabs}, [:to_tabs])
output_format_tester({:output_format => :to_tabs}, [:to_tabs])
output_format_tester({:output_format => :others}, [:to_s])
output_format_tester({:output_files => ['xx.html', 'xx.pdf']}, [:to_html, :to_pdf])
output_format_tester({:output_files => ['xx.pdf', 'xx.json']}, [:to_pdf, :to_json])
output_format_tester({:output_files => ['xx.json', 'xx.tabs']}, [:to_json, :to_tabs])
output_format_tester({:output_files => ['xx.tabs', 'xx.csv']}, [:to_tabs, :to_csv])
output_format_tester({:output_files => ['xx.csv', 'xx.xxx']}, [:to_csv, :to_s])
output_format_tester({:output_files => ['xx.xx', 'xx.xx']}, [:to_s, :to_s])
output_format_tester({:output_files => ['xx.html', 'xx.pdf', 'xx.csv', 'xx.tabs', 'xx.json']}, [:to_html, :to_pdf, :to_csv, :to_tabs, :to_json])
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册