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 1 as true value in environment variables #2710

Merged
merged 3 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions lib/datadog/core/environment/variable_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Datadog
module Core
# Namespace for handling application environment
Expand All @@ -14,11 +16,17 @@ module VariableHelpers
# @param [Boolean] default the default value if the keys in `var` are not present in the environment
# @param [Boolean] deprecation_warning when `var` is a list, record a deprecation log when
# the first key in `var` is not used.
# @return [Boolean] if the environment value is the string `true`
# @return [Boolean] if the environment value is the string `true` or `1`
# @return [default] if the environment value is not found
def env_to_bool(var, default = nil, deprecation_warning: true)
var = decode_array(var, deprecation_warning)
var && ENV.key?(var) ? ENV[var].to_s.strip.downcase == 'true' : default
if var && ENV.key?(var)
value = ENV[var].to_s.strip
value.downcase!
value == 'true' || value == '1' # rubocop:disable Style/MultipleComparison
else
default
end
end

# Reads an environment variable as an Integer.
Expand Down
9 changes: 5 additions & 4 deletions spec/datadog/core/environment/variable_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@
%w[
true
TRUE
1
].each do |value|
context value.to_s do
let(:env_value) { value.to_s }
let(:env_value) { value }

it { is_expected.to be true }
end
Expand All @@ -85,11 +86,11 @@
'',
'false',
'FALSE',
0,
1
'0',
marcotc marked this conversation as resolved.
Show resolved Hide resolved
'arbitrary string',
].each do |value|
context value.to_s do
let(:env_value) { value.to_s }
let(:env_value) { value }

it { is_expected.to be false }
end
Expand Down