Skip to content

Commit 696c89d

Browse files
author
Morgan Haskel
committed
Merge pull request #372 from poikilotherm/feature/master/validate_absolute_path_allow_arrays
Allow array of pathes in validate_absolute_path
2 parents 841b0df + 89995e4 commit 696c89d

File tree

3 files changed

+94
-54
lines changed

3 files changed

+94
-54
lines changed

README.markdown

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -470,27 +470,34 @@ You can also use this with arrays. For example, `unique(["a","a","b","b","c","c"
470470

471471
* `uriescape`: Urlencodes a string or array of strings. Requires either a single string or an array as an input. *Type*: rvalue
472472

473-
* `validate_absolute_path`: Validate that the string represents an absolute path in the filesystem. This function works for Windows and Unix-style paths.
474-
The following values will pass:
475-
476-
```
477-
$my_path = "C:/Program Files (x86)/Puppet Labs/Puppet"
478-
validate_absolute_path($my_path)
479-
$my_path2 = "/var/lib/puppet"
480-
validate_absolute_path($my_path2)
481-
```
482-
483-
The following values will fail, causing compilation to abort:
484-
485-
```
486-
validate_absolute_path(true)
487-
validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
488-
validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
489-
$undefined = undef
490-
validate_absolute_path($undefined)
491-
```
492-
493-
*Type*: statement
473+
* `validate_absolute_path`: Validate the string represents an absolute path in the filesystem. This function works for Windows and Unix style paths.
474+
475+
The following values will pass:
476+
477+
```
478+
$my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
479+
validate_absolute_path($my_path)
480+
$my_path2 = '/var/lib/puppet'
481+
validate_absolute_path($my_path2)
482+
$my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet','C:/Program Files/Puppet Labs/Puppet']
483+
validate_absolute_path($my_path3)
484+
$my_path4 = ['/var/lib/puppet','/usr/share/puppet']
485+
validate_absolute_path($my_path4)
486+
```
487+
488+
The following values will fail, causing compilation to abort:
489+
490+
```
491+
validate_absolute_path(true)
492+
validate_absolute_path('../var/lib/puppet')
493+
validate_absolute_path('var/lib/puppet')
494+
validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
495+
validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
496+
$undefined = undef
497+
validate_absolute_path($undefined)
498+
```
499+
500+
*Type*: statement
494501
495502
* `validate_array`: Validate that all passed values are array data structures. Abort catalog compilation if any value fails this check.
496503

lib/puppet/parser/functions/validate_absolute_path.rb

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ module Puppet::Parser::Functions
55
66
The following values will pass:
77
8-
$my_path = "C:/Program Files (x86)/Puppet Labs/Puppet"
8+
$my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
99
validate_absolute_path($my_path)
10-
$my_path2 = "/var/lib/puppet"
10+
$my_path2 = '/var/lib/puppet'
1111
validate_absolute_path($my_path2)
12-
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)
1316
1417
The following values will fail, causing compilation to abort:
1518
1619
validate_absolute_path(true)
20+
validate_absolute_path('../var/lib/puppet')
21+
validate_absolute_path('var/lib/puppet')
1722
validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
1823
validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
1924
$undefined = undef
@@ -28,28 +33,36 @@ module Puppet::Parser::Functions
2833
end
2934

3035
args.each do |arg|
31-
# This logic was borrowed from
32-
# [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
33-
34-
# Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
35-
if Puppet::Util.respond_to?(:absolute_path?) then
36-
unless Puppet::Util.absolute_path?(arg, :posix) or Puppet::Util.absolute_path?(arg, :windows)
37-
raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.")
36+
# put arg to candidate var to be able to replace it
37+
candidates = arg
38+
# if arg is just a string with a path to test, convert it to an array
39+
# to avoid test code duplication
40+
unless arg.is_a?(Array) then
41+
candidates = Array.new(1,arg)
42+
end
43+
# iterate over all pathes within the candidates array
44+
candidates.each do |path|
45+
# This logic was borrowed from
46+
# [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
47+
# Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
48+
if Puppet::Util.respond_to?(:absolute_path?) then
49+
unless Puppet::Util.absolute_path?(path, :posix) or Puppet::Util.absolute_path?(path, :windows)
50+
raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.")
51+
end
52+
else
53+
# This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
54+
# Determine in a platform-specific way whether a path is absolute. This
55+
# defaults to the local platform if none is specified.
56+
# Escape once for the string literal, and once for the regex.
57+
slash = '[\\\\/]'
58+
name = '[^\\\\/]+'
59+
regexes = {
60+
:windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
61+
:posix => %r!^/!,
62+
}
63+
rval = (!!(path =~ regexes[:posix])) || (!!(path =~ regexes[:windows]))
64+
rval or raise Puppet::ParseError, ("#{path.inspect} is not an absolute path.")
3865
end
39-
else
40-
# This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
41-
# Determine in a platform-specific way whether a path is absolute. This
42-
# defaults to the local platform if none is specified.
43-
# Escape once for the string literal, and once for the regex.
44-
slash = '[\\\\/]'
45-
name = '[^\\\\/]+'
46-
regexes = {
47-
:windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
48-
:posix => %r!^/!,
49-
}
50-
51-
rval = (!!(arg =~ regexes[:posix])) || (!!(arg =~ regexes[:windows]))
52-
rval or raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.")
5366
end
5467
end
5568
end

spec/functions/validate_absolute_path_spec.rb

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def self.valid_paths
3939
expect { subject.call [path] }.not_to raise_error
4040
end
4141
end
42+
valid_paths do
43+
it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do
44+
expect { subject.call [valid_paths] }.not_to raise_error
45+
end
46+
end
4247
end
4348

4449
context "Puppet without mocking" do
@@ -47,6 +52,11 @@ def self.valid_paths
4752
expect { subject.call [path] }.not_to raise_error
4853
end
4954
end
55+
valid_paths do
56+
it "validate_absolute_path(#{valid_paths.inspect}) should not fail" do
57+
expect { subject.call [valid_paths] }.not_to raise_error
58+
end
59+
end
5060
end
5161
end
5262

@@ -55,6 +65,7 @@ def self.valid_paths
5565
[
5666
nil,
5767
[ nil ],
68+
[ nil, nil ],
5869
{ 'foo' => 'bar' },
5970
{ },
6071
'',
@@ -66,19 +77,28 @@ def self.valid_paths
6677
end
6778

6879
context 'Relative paths' do
69-
%w{
70-
relative1
71-
.
72-
..
73-
./foo
74-
../foo
75-
etc/puppetlabs/puppet
76-
opt/puppet/bin
77-
}.each do |path|
80+
def self.rel_paths
81+
%w{
82+
relative1
83+
.
84+
..
85+
./foo
86+
../foo
87+
etc/puppetlabs/puppet
88+
opt/puppet/bin
89+
}
90+
end
91+
rel_paths.each do |path|
7892
it "validate_absolute_path(#{path.inspect}) should fail" do
7993
expect { subject.call [path] }.to raise_error Puppet::ParseError
8094
end
8195
end
96+
rel_paths do
97+
it "validate_absolute_path(#{rel_paths.inspect}) should fail" do
98+
expect { subject.call [rel_paths] }.to raise_error Puppet::ParseError
99+
end
100+
end
82101
end
83102
end
84103
end
104+

0 commit comments

Comments
 (0)