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

Use Ruby 2.7 compatible string matching #2074

Merged
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
10 changes: 8 additions & 2 deletions lib/puppet/functions/apache/bool2httpd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
# 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
2 changes: 2 additions & 0 deletions spec/functions/bool2httpd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
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
Expand Down