Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_absolute_path.rb
1 #
2 # validate_absolute_path.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:validate_absolute_path, :doc => <<-DOC) do |args|
6     @summary
7       Validate the string represents an absolute path in the filesystem.  This function works
8       for windows and unix style paths.
9
10     @return
11       passes when the string is an absolute path or raise an error when it is not and fails compilation
12
13     @example **Usage**
14
15       The following values will pass:
16
17           $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
18           validate_absolute_path($my_path)
19           $my_path2 = '/var/lib/puppet'
20           validate_absolute_path($my_path2)
21           $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet','C:/Program Files/Puppet Labs/Puppet']
22           validate_absolute_path($my_path3)
23           $my_path4 = ['/var/lib/puppet','/usr/share/puppet']
24           validate_absolute_path($my_path4)
25
26       The following values will fail, causing compilation to abort:
27
28           validate_absolute_path(true)
29           validate_absolute_path('../var/lib/puppet')
30           validate_absolute_path('var/lib/puppet')
31           validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
32           validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
33           $undefined = undef
34           validate_absolute_path($undefined)
35     DOC
36
37     require 'puppet/util'
38
39     if args.empty?
40       raise Puppet::ParseError, "validate_absolute_path(): wrong number of arguments (#{args.length}; must be > 0)"
41     end
42
43     args.each do |arg|
44       # put arg to candidate var to be able to replace it
45       candidates = arg
46       # if arg is just a string with a path to test, convert it to an array
47       # to avoid test code duplication
48       unless arg.is_a?(Array)
49         candidates = Array.new(1, arg)
50       end
51       # iterate over all paths within the candidates array
52       candidates.each do |path|
53         unless function_is_absolute_path([path])
54           raise Puppet::ParseError, "#{path.inspect} is not an absolute path."
55         end
56       end
57     end
58   end
59 end