diff --git a/lib/figaro/application.rb b/lib/figaro/application.rb index 1608ae1..08ebba9 100644 --- a/lib/figaro/application.rb +++ b/lib/figaro/application.rb @@ -4,6 +4,7 @@ module Figaro class Application FIGARO_ENV_PREFIX = "_FIGARO_" + SILENCE_STRING_WARNINGS_KEY = :FIGARO_SILENCE_STRING_WARNINGS include Enumerable @@ -69,8 +70,10 @@ def environment_configuration end def set(key, value) - non_string_configuration!(key) unless key.is_a?(String) - non_string_configuration!(value) unless value.is_a?(String) || value.nil? + unless non_string_warnings_silenced? + non_string_configuration!(key) unless key.is_a?(String) + non_string_configuration!(value) unless value.is_a?(String) || value.nil? + end ::ENV[key.to_s] = value.nil? ? nil : value.to_s ::ENV[FIGARO_ENV_PREFIX + key.to_s] = value.nil? ? nil: value.to_s @@ -80,6 +83,13 @@ def skip?(key) ::ENV.key?(key.to_s) && !::ENV.key?(FIGARO_ENV_PREFIX + key.to_s) end + def non_string_warnings_silenced? + key = SILENCE_STRING_WARNINGS_KEY + + # Allow the silence configuration itself to use non-string keys/values. + configuration.values_at(key.to_s, key).any? { |cv| cv.to_s == 'true' } + end + def non_string_configuration!(value) warn "WARNING: Use strings for Figaro configuration. #{value.inspect} was converted to #{value.to_s.inspect}." end diff --git a/spec/figaro/application_spec.rb b/spec/figaro/application_spec.rb index d9e8d7c..ed7dc32 100644 --- a/spec/figaro/application_spec.rb +++ b/spec/figaro/application_spec.rb @@ -232,12 +232,49 @@ def yaml_to_path(yaml) }.from("bar").to("baz") end - it "warns when a key isn't a string" do - allow(application).to receive(:configuration) { { foo: "bar" } } + shared_examples 'correct warning with and without silence override' do + [ + { FIGARO_SILENCE_STRING_WARNINGS: true }, + { 'FIGARO_SILENCE_STRING_WARNINGS' => true }, + { FIGARO_SILENCE_STRING_WARNINGS: 'true' }, + { 'FIGARO_SILENCE_STRING_WARNINGS' => 'true' }, + ].each do |override| + it "does not warn with override #{override.inspect}" do + allow(application).to receive(:configuration) { config.merge(override) } + + expect(application).to_not receive(:warn) + + application.load + end + end - expect(application).to receive(:warn) + [ + [{ }, 1], + [{ 'FIGARO_SILENCE_STRING_WARNINGS' => false }, 2], + [{ FIGARO_SILENCE_STRING_WARNINGS: false }, 3], + [{ 'FIGARO_SILENCE_STRING_WARNINGS' => 'false' }, 1], + [{ FIGARO_SILENCE_STRING_WARNINGS: 'false' }, 2], + ].each do |override, expected_warning_count| + it "warns #{expected_warning_count} times with override #{override.inspect}" do + allow(application).to receive(:configuration) { config.merge(override) } - application.load + expect(application).to receive(:warn).exactly(expected_warning_count).times + + application.load + end + end + end + + context "warning when a key isn't a string" do + let(:config) { { SYMBOL_KEY: 'string value' } } + + include_examples "correct warning with and without silence override" + end + + context "warning when a value isn't a string" do + let(:config) { { 'string key' => :SYMBOL_VALUE } } + + include_examples "correct warning with and without silence override" end it "warns when a value isn't a string" do