diff --git a/lib/brakeman.rb b/lib/brakeman.rb index 8afadc6a8aef29e4b9ca3a2d4730873983f0d80c..9601c8dd88260830965191c33e420729ed2fec24 100644 --- a/lib/brakeman.rb +++ b/lib/brakeman.rb @@ -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 {} diff --git a/test/tests/test_brakeman.rb b/test/tests/test_brakeman.rb index eab95f5b1dbc2057dd8cd8b2942f5c58d8a688c4..4aaebb2c69a8deeece332a43062ecdecae69b0ab 100644 --- a/test/tests/test_brakeman.rb +++ b/test/tests/test_brakeman.rb @@ -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