Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_absolute_path.rb
1 module Puppet::Parser::Functions
2   newfunction(:validate_absolute_path, :doc => <<-'ENDHEREDOC') do |args|
3     Validate the string represents an absolute path in the filesystem.  This function works
4     for windows and unix style paths.
5
6     The following values will pass:
7
8         $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
9         validate_absolute_path($my_path)
10         $my_path2 = '/var/lib/puppet'
11         validate_absolute_path($my_path2)
12         $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet','C:/Program Files/Puppet Labs/Puppet']
13         validate_absolute_path($my_path3)
14         $my_path4 = ['/var/lib/puppet','/usr/share/puppet']
15         validate_absolute_path($my_path4)
16
17     The following values will fail, causing compilation to abort:
18
19         validate_absolute_path(true)
20         validate_absolute_path('../var/lib/puppet')
21         validate_absolute_path('var/lib/puppet')
22         validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
23         validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
24         $undefined = undef
25         validate_absolute_path($undefined)
26
27     ENDHEREDOC
28
29     # The deprecation function was being called twice, as validate_absolute_path calls is_absolute_path. I have removed it from here so it only calls deprecation once within is_absolute_path.
30     # function_deprecation([:validate_absolute_path, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Absolute_path. There is further documentation for validate_legacy function in the README.'])
31
32     require 'puppet/util'
33
34     unless args.length > 0 then
35       raise Puppet::ParseError, ("validate_absolute_path(): wrong number of arguments (#{args.length}; must be > 0)")
36     end
37
38     args.each do |arg|
39       # put arg to candidate var to be able to replace it
40       candidates = arg
41       # if arg is just a string with a path to test, convert it to an array
42       # to avoid test code duplication
43       unless arg.is_a?(Array) then
44         candidates = Array.new(1,arg)
45       end
46       # iterate over all paths within the candidates array
47       candidates.each do |path|
48         unless function_is_absolute_path([path])
49           raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.")
50         end
51       end
52     end
53   end
54 end