2 # validate_absolute_path.rb
4 module Puppet::Parser::Functions
5 newfunction(:validate_absolute_path, :doc => <<-'DOC') do |args|
6 Validate the string represents an absolute path in the filesystem. This function works
7 for windows and unix style paths.
9 The following values will pass:
11 $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
12 validate_absolute_path($my_path)
13 $my_path2 = '/var/lib/puppet'
14 validate_absolute_path($my_path2)
15 $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet','C:/Program Files/Puppet Labs/Puppet']
16 validate_absolute_path($my_path3)
17 $my_path4 = ['/var/lib/puppet','/usr/share/puppet']
18 validate_absolute_path($my_path4)
20 The following values will fail, causing compilation to abort:
22 validate_absolute_path(true)
23 validate_absolute_path('../var/lib/puppet')
24 validate_absolute_path('var/lib/puppet')
25 validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
26 validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
28 validate_absolute_path($undefined)
35 raise Puppet::ParseError, "validate_absolute_path(): wrong number of arguments (#{args.length}; must be > 0)"
39 # put arg to candidate var to be able to replace it
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)
44 candidates = Array.new(1, arg)
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."