diff --git a/lib/puppet/functions/apache/bool2httpd.rb b/lib/puppet/functions/apache/bool2httpd.rb index 99121640..511adba6 100644 --- a/lib/puppet/functions/apache/bool2httpd.rb +++ b/lib/puppet/functions/apache/bool2httpd.rb @@ -1,24 +1,30 @@ # @summary # Transform a supposed boolean to On or Off. Passes all other values through. # Puppet::Functions.create_function(:'apache::bool2httpd') do # @param arg # The value to be converted into a string. # # @return # Will return either `On` or `Off` if given a boolean value. Returns a string of any # other given value. # @example # $trace_enable = false # $server_signature = 'mail' # # apache::bool2httpd($trace_enable) # returns 'Off' # apache::bool2httpd($server_signature) # returns 'mail' # apache::bool2httpd(undef) # returns 'Off' # def bool2httpd(arg) - return 'Off' if arg.nil? || arg == false || arg =~ %r{false}i || arg == :undef - return 'On' if arg == true || arg =~ %r{true}i + return 'Off' if arg.nil? || arg == false || matches_string?(arg, %r{false}i) || arg == :undef + return 'On' if arg == true || matches_string?(arg, %r{true}i) arg.to_s end + + private + + def matches_string?(value, matcher) + value.is_a?(String) && value.match?(matcher) + end end diff --git a/spec/functions/bool2httpd_spec.rb b/spec/functions/bool2httpd_spec.rb index e436e0e3..fabcf274 100644 --- a/spec/functions/bool2httpd_spec.rb +++ b/spec/functions/bool2httpd_spec.rb @@ -1,29 +1,31 @@ require 'spec_helper' shared_examples 'apache::bool2httpd function' do it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(ArgumentError) } it { is_expected.to run.with_params('1', '2').and_raise_error(ArgumentError) } it { is_expected.to run.with_params(true).and_return('On') } + it { is_expected.to run.with_params('true').and_return('On') } it 'expected to return a string "On"' do expect(subject.execute(true)).to be_an_instance_of(String) end it { is_expected.to run.with_params(false).and_return('Off') } + it { is_expected.to run.with_params('false').and_return('Off') } it 'expected to return a string "Off"' do expect(subject.execute(false)).to be_an_instance_of(String) end it { is_expected.to run.with_params('mail').and_return('mail') } it { is_expected.to run.with_params(nil).and_return('Off') } it { is_expected.to run.with_params(:undef).and_return('Off') } it { is_expected.to run.with_params('foo').and_return('foo') } end describe 'apache::bool2httpd' do it_behaves_like 'apache::bool2httpd function' describe 'deprecated non-namespaced shim' do describe 'bool2httpd', type: :puppet_function do it_behaves_like 'apache::bool2httpd function' end end end