Skip to content

Commit

Permalink
Merge pull request #782 from igalic/bool2httpd-function
Browse files Browse the repository at this point in the history
function to munge booleans to httpd's On/Off
  • Loading branch information
Morgan Haskel committed Aug 28, 2014
2 parents 2425abf + d2ad8ec commit c220ddd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/puppet/parser/functions/bool2httpd.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Puppet::Parser::Functions::newfunction(:bool2httpd, :type => :rvalue, :doc => <<-EOS
Transform a supposed boolean to On or Off. Pass all other values through.
Given a nil value (undef), bool2httpd will return 'Off'
Example:
$trace_enable = false
$server_signature = 'mail'
bool2httpd($trace_enable)
# => 'Off'
bool2httpd($server_signature)
# => 'mail'
bool2httpd(undef)
# => 'Off'
EOS
) do |args|
raise(Puppet::ParseError, "bool2httpd() wrong number of arguments. Given: #{args.size} for 1)") if args.size != 1

arg = args[0]

if arg.nil? or arg == false or arg =~ /false/i or arg == :undef
return 'Off'
elsif arg == true or arg =~ /true/i
return 'On'
end

return arg.to_s
end
54 changes: 54 additions & 0 deletions spec/unit/puppet/parser/functions/bool2httpd_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'

describe "the bool2httpd function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

it "should exist" do
expect(Puppet::Parser::Functions.function("bool2httpd")).to eq("function_bool2httpd")
end

it "should raise a ParseError if there is less than 1 arguments" do
expect { scope.function_bool2httpd([]) }.to( raise_error(Puppet::ParseError))
end

it "should convert true to 'On'" do
result = scope.function_bool2httpd([true])
expect(result).to(eq('On'))
end

it "should convert true to a string" do
result = scope.function_bool2httpd([true])
expect(result.class).to(eq(String))
end

it "should convert false to 'Off'" do
result = scope.function_bool2httpd([false])
expect(result).to(eq('Off'))
end

it "should convert false to a string" do
result = scope.function_bool2httpd([false])
expect(result.class).to(eq(String))
end

it "should accept (and return) any string" do
result = scope.function_bool2httpd(["mail"])
expect(result).to(eq('mail'))
end

it "should accept a nil value (and return Off)" do
result = scope.function_bool2httpd([nil])
expect(result).to(eq('Off'))
end

it "should accept an undef value (and return 'Off')" do
result = scope.function_bool2httpd([:undef])
expect(result).to(eq('Off'))
end

it "should return a default value on non-matches" do
result = scope.function_bool2httpd(['foo'])
expect(result).to(eq('foo'))
end
end

0 comments on commit c220ddd

Please sign in to comment.