Skip to content

Commit e72e310

Browse files
committed
adds new parser called is_absolute_path
* is_absolute_path returns boolean true if the given path is absolute, returns false otherwise. * works for windows and unix
1 parent 88a9a31 commit e72e310

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:is_absolute_path, :type => :rvalue, :arity => 1, :doc => <<-'ENDHEREDOC') do |args|
3+
Returns boolean true if 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 return true:
7+
8+
$my_path = 'C:/Program Files (x86)/Puppet Labs/Puppet'
9+
is_absolute_path($my_path)
10+
$my_path2 = '/var/lib/puppet'
11+
is_absolute_path($my_path2)
12+
$my_path3 = ['C:/Program Files (x86)/Puppet Labs/Puppet']
13+
is_absolute_path($my_path3)
14+
$my_path4 = ['/var/lib/puppet']
15+
is_absolute_path($my_path4)
16+
17+
The following values will return false:
18+
19+
is_absolute_path(true)
20+
is_absolute_path('../var/lib/puppet')
21+
is_absolute_path('var/lib/puppet')
22+
$undefined = undef
23+
is_absolute_path($undefined)
24+
25+
ENDHEREDOC
26+
27+
require 'puppet/util'
28+
29+
path = args[0]
30+
# This logic was borrowed from
31+
# [lib/puppet/file_serving/base.rb](https://github.com/puppetlabs/puppet/blob/master/lib/puppet/file_serving/base.rb)
32+
# Puppet 2.7 and beyond will have Puppet::Util.absolute_path? Fall back to a back-ported implementation otherwise.
33+
if Puppet::Util.respond_to?(:absolute_path?) then
34+
value = (Puppet::Util.absolute_path?(path, :posix) or Puppet::Util.absolute_path?(path, :windows))
35+
else
36+
# This code back-ported from 2.7.x's lib/puppet/util.rb Puppet::Util.absolute_path?
37+
# Determine in a platform-specific way whether a path is absolute. This
38+
# defaults to the local platform if none is specified.
39+
# Escape once for the string literal, and once for the regex.
40+
slash = '[\\\\/]'
41+
name = '[^\\\\/]+'
42+
regexes = {
43+
:windows => %r!^(([A-Z]:#{slash})|(#{slash}#{slash}#{name}#{slash}#{name})|(#{slash}#{slash}\?#{slash}#{name}))!i,
44+
:posix => %r!^/!
45+
}
46+
value = (!!(path =~ regexes[:posix])) || (!!(path =~ regexes[:windows]))
47+
end
48+
value
49+
end
50+
end
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
require 'spec_helper'
2+
3+
describe :is_absolute_path do
4+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
5+
6+
let(:function_args) do
7+
[]
8+
end
9+
10+
let(:function) do
11+
scope.function_is_absolute_path(function_args)
12+
end
13+
14+
15+
describe 'validate arity' do
16+
let(:function_args) do
17+
[1,2]
18+
end
19+
it "should raise a ParseError if there is more than 1 arguments" do
20+
lambda { function }.should( raise_error(ArgumentError))
21+
end
22+
23+
end
24+
25+
it "should exist" do
26+
Puppet::Parser::Functions.function(subject).should == "function_#{subject}"
27+
end
28+
29+
# help enforce good function defination
30+
it 'should contain arity' do
31+
32+
end
33+
34+
it "should raise a ParseError if there is less than 1 arguments" do
35+
lambda { function }.should( raise_error(ArgumentError))
36+
end
37+
38+
39+
describe 'should retrun true' do
40+
let(:return_value) do
41+
true
42+
end
43+
44+
describe 'windows' do
45+
let(:function_args) do
46+
['c:\temp\test.txt']
47+
end
48+
it 'should return data' do
49+
function.should eq(return_value)
50+
end
51+
end
52+
53+
describe 'non-windows' do
54+
let(:function_args) do
55+
['/temp/test.txt']
56+
end
57+
58+
it 'should return data' do
59+
function.should eq(return_value)
60+
end
61+
end
62+
end
63+
64+
describe 'should return false' do
65+
let(:return_value) do
66+
false
67+
end
68+
describe 'windows' do
69+
let(:function_args) do
70+
['..\temp\test.txt']
71+
end
72+
it 'should return data' do
73+
function.should eq(return_value)
74+
end
75+
end
76+
77+
describe 'non-windows' do
78+
let(:function_args) do
79+
['../var/lib/puppet']
80+
end
81+
it 'should return data' do
82+
function.should eq(return_value)
83+
end
84+
end
85+
end
86+
end

0 commit comments

Comments
 (0)