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 warnings #8

Merged
merged 1 commit into from
Nov 30, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

- Ensure that Figjam.adapter is set to Figjam::Rails::Application before the figjam Railtie is loaded [#7](https://github.com/hlascelles/figjam/pull/7)
- Allow silencing of non-string configuration warnings [#8](https://github.com/hlascelles/figjam/pull/8)

## 1.4.0 (2022-11-14)

Expand Down
17 changes: 15 additions & 2 deletions lib/figjam/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
module Figjam
class Application
FIGARO_ENV_PREFIX = "_FIGARO_"
SILENCE_STRING_WARNINGS_KEYS = %i[
FIGARO_SILENCE_STRING_WARNINGS
FIGJAM_SILENCE_STRING_WARNINGS
]

include Enumerable

Expand Down Expand Up @@ -70,8 +74,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 @@ -81,6 +87,13 @@ def skip?(key)
::ENV.key?(key.to_s) && !::ENV.key?(FIGARO_ENV_PREFIX + key.to_s)
end

def non_string_warnings_silenced?
SILENCE_STRING_WARNINGS_KEYS.any? { |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 Figjam configuration. #{value.inspect} was converted to #{value.to_s.inspect}."
end
Expand Down
55 changes: 46 additions & 9 deletions spec/figjam/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,57 @@ 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" } }

expect(application).to receive(:warn)
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' },
{ FIGJAM_SILENCE_STRING_WARNINGS: true },
{ 'FIGJAM_SILENCE_STRING_WARNINGS' => true },
{ FIGJAM_SILENCE_STRING_WARNINGS: 'true' },
{ 'FIGJAM_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

application.load
[
[{ }, 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],
[{ 'FIGJAM_SILENCE_STRING_WARNINGS' => false }, 2],
[{ FIGJAM_SILENCE_STRING_WARNINGS: false }, 3],
[{ 'FIGJAM_SILENCE_STRING_WARNINGS' => 'false' }, 1],
[{ FIGJAM_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) }

expect(application).to receive(:warn).exactly(expected_warning_count).times

application.load
end
end
end

it "warns when a value isn't a string" do
allow(application).to receive(:configuration) { { "foo" => ["bar"] } }
context "warning when a key isn't a string" do
let(:config) { { SYMBOL_KEY: 'string value' } }

expect(application).to receive(:warn)
include_examples "correct warning with and without silence override"
end

application.load
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 "allows nil values" do
Expand Down