Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / is_absolute_path.rb
1 #
2 # is_absolute_path.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:is_absolute_path, :type => :rvalue, :arity => 1, :doc => <<-'DOC') do |args|
6     @summary
7       **Deprecated:** Returns boolean true if the string represents an absolute path in the filesystem.
8
9     This function works for windows and unix style paths.
10
11     @example The following values will return true:
12       $my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
13       is_absolute_path($my_path)
14       $my_path2 = '/var/lib/puppet'
15       is_absolute_path($my_path2)
16       $my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet']
17       is_absolute_path($my_path3)
18       $my_path4 = ['/var/lib/puppet']
19       is_absolute_path($my_path4)
20
21     @example The following values will return false:
22       is_absolute_path(true)
23       is_absolute_path('../var/lib/puppet')
24       is_absolute_path('var/lib/puppet')
25       $undefined = undef
26       is_absolute_path($undefined)
27
28     @return [Boolean]
29       Returns `true` or `false`
30
31     > **Note:* **Deprecated** Will be removed in a future version of stdlib. See
32     [`validate_legacy`](#validate_legacy).
33   DOC
34     function_deprecation([:is_absolute_path, 'This method is deprecated, please use the stdlib validate_legacy function,
35                            with Stdlib::Compat::Absolute_path. There is further documentation for validate_legacy function in the README.'])
36     require 'puppet/util'
37
38     path = args[0]
39     # This logic was borrowed from
40     # [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
41     # Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
42     if Puppet::Util.respond_to?(:absolute_path?)
43       value = (Puppet::Util.absolute_path?(path, :posix) || Puppet::Util.absolute_path?(path, :windows))
44     else
45       # This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
46       # Determine in a platform-specific way whether a path is absolute. This
47       # defaults to the local platform if none is specified.
48       # Escape once for the string literal, and once for the regex.
49       slash = '[\\\\/]'
50       name = '[^\\\\/]+'
51       regexes = {
52         :windows => %r{^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))}i,
53         :posix => %r{^/},
54       }
55       value = !!(path =~ regexes[:posix]) || !!(path =~ regexes[:windows]) # rubocop:disable Style/DoubleNegation : No alternative known
56     end
57     value
58   end
59 end