Skip to content

Commit 898ff80

Browse files
committed
Merge pull request #46 from jeffmccune/feature/2.3.x/validate_absolute_path_function
(#12357) Add validate_absolute_path() function
2 parents 6383435 + 2ce669c commit 898ff80

3 files changed

Lines changed: 114 additions & 2 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
13+
14+
The following values will fail, causing compilation to abort:
15+
16+
validate_absolute_path(true)
17+
validate_absolute_path([ 'var/lib/puppet', '/var/foo' ])
18+
validate_absolute_path([ '/var/lib/puppet', 'var/foo' ])
19+
$undefined = undef
20+
validate_absolute_path($undefined)
21+
22+
ENDHEREDOC
23+
24+
require 'puppet/util'
25+
26+
unless args.length > 0 then
27+
raise Puppet::ParseError, ("validate_absolute_path(): wrong number of arguments (#{args.length}; must be > 0)")
28+
end
29+
30+
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+
unless Puppet::Util.absolute_path?(arg, :posix) or Puppet::Util.absolute_path?(arg, :windows)
34+
raise Puppet::ParseError, ("#{arg.inspect} is not an absolute path.")
35+
end
36+
end
37+
end
38+
end

spec/spec_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
dir = File.expand_path(File.dirname(__FILE__))
22
$LOAD_PATH.unshift File.join(dir, 'lib')
33

4-
p dir
5-
64
# Don't want puppet getting the command line arguments for rake or autotest
75
ARGV.clear
86

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
require 'spec_helper'
2+
3+
describe Puppet::Parser::Functions.function(:validate_absolute_path) do
4+
before :all do
5+
Puppet::Parser::Functions.autoloader.loadall
6+
end
7+
8+
let(:scope) do
9+
scope = Puppet::Parser::Scope.new
10+
end
11+
12+
# The subject of these examplres is the method itself.
13+
subject do
14+
scope.method :function_validate_absolute_path
15+
end
16+
17+
context 'Using Puppet::Parser::Scope.new' do
18+
19+
describe 'Garbage inputs' do
20+
paths = [
21+
nil,
22+
[ nil ],
23+
{ 'foo' => 'bar' },
24+
{ },
25+
'',
26+
]
27+
28+
paths.each do |path|
29+
it "validate_absolute_path(#{path.inspect}) should fail" do
30+
expect { subject.call [path] }.to raise_error Puppet::ParseError
31+
end
32+
end
33+
end
34+
describe 'relative paths' do
35+
paths = %w{
36+
relative1
37+
.
38+
..
39+
./foo
40+
../foo
41+
etc/puppetlabs/puppet
42+
opt/puppet/bin
43+
}
44+
45+
paths.each do |path|
46+
it "validate_absolute_path(#{path.inspect}) should fail" do
47+
expect { subject.call [path] }.to raise_error Puppet::ParseError
48+
end
49+
end
50+
end
51+
describe 'absolute paths' do
52+
paths = %w{
53+
C:/
54+
C:\\
55+
C:\\WINDOWS\\System32
56+
C:/windows/system32
57+
X:/foo/bar
58+
X:\\foo\\bar
59+
/var/tmp
60+
/var/lib/puppet
61+
/var/opt/../lib/puppet
62+
}
63+
64+
paths = paths + [
65+
'C:\\Program Files (x86)\\Puppet Labs\\Puppet Enterprise',
66+
'C:/Program Files (x86)/Puppet Labs/Puppet Enterprise',
67+
]
68+
69+
paths.each do |path|
70+
it "validate_absolute_path(#{path.inspect}) should not fail" do
71+
expect { subject.call [path] }.not_to raise_error
72+
end
73+
end
74+
end
75+
end
76+
end

0 commit comments

Comments
 (0)