Skip to content

(#13610) Add function_available to stdlib #59

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

Closed
wants to merge 3 commits into from
Closed
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
23 changes: 23 additions & 0 deletions lib/puppet/parser/functions/is_function_available.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#
# is_function_available.rb
#

module Puppet::Parser::Functions
newfunction(:is_function_available, :type => :rvalue, :doc => <<-EOS
This function accepts a string as an argument, determines whether the
Puppet runtime has access to a function by that name. It returns a
true if the function exists, false if not.
EOS
) do |arguments|

if (arguments.size != 1) then
raise(Puppet::ParseError, "is_function_available?(): Wrong number of arguments "+
"given #{arguments.size} for 1")
end

function = Puppet::Parser::Functions.function(arguments[0].to_sym)
function.is_a?(String) and not function.empty?
end
end

# vim: set ts=2 sw=2 et :
31 changes: 31 additions & 0 deletions spec/unit/puppet/parser/functions/is_function_available.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env rspec
require 'spec_helper'

describe "the is_function_available function" do
before :all do
Puppet::Parser::Functions.autoloader.loadall
end

before :each do
@scope = Puppet::Parser::Scope.new
end

it "should exist" do
Puppet::Parser::Functions.function("is_function_available").should == "function_is_function_available"
end

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

it "should return false if a nonexistent function is passed" do
result = @scope.function_is_function_available(['jeff_mccunes_left_sock'])
result.should(eq(false))
end

it "should return true if an available function is passed" do
result = @scope.function_is_function_available(['require'])
result.should(eq(true))
end

end