提交 5fb2d315 编写于 作者: S soffolk

add test to reproduce #300 and fixed with change load_config method

上级 24ed73a8
......@@ -63,7 +63,7 @@ module Brakeman
options = { :app_path => options }
end
options = default_options.merge(load_options(options[:config_file])).merge(options)
options = default_options.merge(load_options(options[:config_file], options[:quiet])).merge(options)
options[:app_path] = File.expand_path(options[:app_path])
options[:output_formats] = get_output_formats options
......@@ -78,12 +78,19 @@ module Brakeman
]
#Load options from YAML file
def self.load_options custom_location
def self.load_options custom_location, quiet
#Load configuration file
if config = config_file(custom_location)
options = YAML.load_file config
options.each { |k, v| options[k] = Set.new v if v.is_a? Array }
notify "[Notice] Using configuration in #{config}" unless options[:quiet]
# convert to hash with sym keys, ref: http://api.rubyonrails.org/classes/Hash.html#method-i-symbolize_keys-21
options.keys.each do |key|
options[(key.to_sym rescue key) || key] = options.delete(key)
end
# notify if options[:quiet] and quiet is nil||false
notify "[Notice] Using configuration in #{config}" unless (options[:quiet] || quiet)
options
else
{}
......
......@@ -55,6 +55,27 @@ class BaseCheckTests < Test::Unit::TestCase
end
class ConfigTests < Test::Unit::TestCase
def setup
Brakeman.instance_variable_set(:@quiet, false)
end
# method from test-unit: http://test-unit.rubyforge.org/test-unit/en/Test/Unit/Util/Output.html#capture_output-instance_method
def capture_output
require 'stringio'
output = StringIO.new
error = StringIO.new
stdout_save, stderr_save = $stdout, $stderr
$stdout, $stderr = output, error
begin
yield
[output.string, error.string]
ensure
$stdout, $stderr = stdout_save, stderr_save
end
end
def test_quiet_option_from_file
config = Tempfile.new("config")
......@@ -70,11 +91,54 @@ class ConfigTests < Test::Unit::TestCase
:app_path => "/tmp" #doesn't need to be real
}
final_options = Brakeman.set_options(options)
assert_equal "", capture_output {
final_options = Brakeman.set_options(options)
config.unlink
config.unlink
assert final_options[:quiet], "Expected quiet option to be true, but was #{final_options[:quiet]}"
assert final_options[:quiet], "Expected quiet option to be true, but was #{final_options[:quiet]}"
}[1]
end
def test_quiet_option_from_file_2
config = Tempfile.new("config")
config.write <<-YAML.strip
---
quiet: true
YAML
config.close
options = {
:config_file => config.path,
:app_path => "/tmp" #doesn't need to be real
}
assert_equal "", capture_output {
final_options = Brakeman.set_options(options)
}[1]
end
def test_quiet_option_from_commandline
config = Tempfile.new("config")
config.write <<-YAML.strip
---
app_path: "/tmp"
YAML
config.close
options = {
:config_file => config.path,
:quiet => true,
:app_path => "/tmp" #doesn't need to be real
}
assert_equal "", capture_output {
final_options = Brakeman.set_options(options)
}[1]
end
def test_quiet_option_default
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册