File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed
lib/puppet/functions/stdlib Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ # @summary
4
+ # Returns whether the Puppet runtime has access to a given function.
5
+ #
6
+ # @example Using stdlib::has_function()
7
+ # stdlib::has_function('stdlib::has_function') # true
8
+ # stdlib::has_function('not_a_function') # false
9
+ #
10
+ # Determines whether the Puppet runtime has access to a function by the
11
+ # name provided.
12
+ #
13
+ # @return
14
+ # Returns true if the provided function name is available, false otherwise.
15
+ #
16
+ Puppet ::Functions . create_function ( :'stdlib::has_function' , Puppet ::Functions ::InternalFunction ) do
17
+ dispatch :has_function do
18
+ scope_param
19
+ param 'String[1]' , :function_name
20
+ return_type 'Boolean'
21
+ end
22
+
23
+ def has_function ( scope , function_name ) # rubocop:disable Naming/PredicateName
24
+ loaders = scope . compiler . loaders
25
+ loader = loaders . private_environment_loader
26
+ return true unless loader &.load ( :function , function_name ) . nil?
27
+
28
+ # If the loader cannot find the function it might be
29
+ # a 3x-style function stubbed in on-the-fly for testing.
30
+ func_3x = Puppet ::Parser ::Functions . function ( function_name . to_sym )
31
+ func_3x . is_a? ( String ) && !func_3x . empty?
32
+ end
33
+ end
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'stdlib::has_function' do
6
+ let ( :pre_condition ) { 'function puppet_func {}' }
7
+
8
+ before ( :each ) do
9
+ Puppet ::Parser ::Functions . newfunction ( :test_3x_func ) do |_args |
10
+ true
11
+ end
12
+ end
13
+
14
+ it { is_expected . not_to be_nil }
15
+
16
+ # Itself, a namespaced function:
17
+ it { is_expected . to run . with_params ( 'stdlib::has_function' ) . and_return ( true ) }
18
+
19
+ # A namespaced function which does not exist:
20
+ it { is_expected . to run . with_params ( 'stdlib::not_a_function' ) . and_return ( false ) }
21
+
22
+ # A top-function which does not exist:
23
+ it { is_expected . to run . with_params ( 'not_a_function' ) . and_return ( false ) }
24
+
25
+ # A Puppet core function:
26
+ it { is_expected . to run . with_params ( 'assert_type' ) . and_return ( true ) }
27
+
28
+ # A Puppet function stubbed during testing:
29
+ it { is_expected . to run . with_params ( 'puppet_func' ) . and_return ( true ) }
30
+
31
+ # A file-loaded 3x style function in stdlib:
32
+ it { is_expected . to run . with_params ( 'validate_augeas' ) . and_return ( true ) }
33
+
34
+ # A stubbed 3x-style function:
35
+ it { is_expected . to run . with_params ( 'test_3x_func' ) . and_return ( true ) }
36
+ end
You can’t perform that action at this time.
0 commit comments