Skip to content

Commit

Permalink
Allow 1 as true value in environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc committed Mar 21, 2023
1 parent 35bf597 commit 84b3af3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
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',
'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

0 comments on commit 84b3af3

Please sign in to comment.