Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow silencing of non-string configuration warning #226 #264

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/figaro/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Figaro
class Application
FIGARO_ENV_PREFIX = "_FIGARO_"
SILENCE_STRING_WARNINGS_KEY = :FIGARO_SILENCE_STRING_WARNINGS

include Enumerable

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
45 changes: 41 additions & 4 deletions spec/figaro/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down