Skip to content

Commit 0c68ff6

Browse files
committed
Merge pull request #150 from raphink/dev/dirname_function
Add a dirname function
2 parents 0aa7e94 + 2ba9e47 commit 0c68ff6

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.markdown

+10
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ Would return: ['a','c']
195195

196196
- *Type*: rvalue
197197

198+
dirname
199+
-------
200+
Returns the `dirname` of a path.
201+
202+
*Examples:*
203+
204+
dirname('/path/to/a/file.ext')
205+
206+
Would return: '/path/to/a'
207+
198208
downcase
199209
--------
200210
Converts the case of a string or all strings in an array to lower case.
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:dirname, :type => :rvalue, :doc => <<-EOS
3+
Returns the dirname of a path.
4+
EOS
5+
) do |arguments|
6+
7+
raise(Puppet::ParseError, "dirname(): Wrong number of arguments " +
8+
"given (#{arguments.size} for 1)") if arguments.size < 1
9+
10+
path = arguments[0]
11+
return File.dirname(path)
12+
end
13+
end
14+
15+
# vim: set ts=2 sw=2 et :
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the dirname function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
it "should exist" do
8+
Puppet::Parser::Functions.function("dirname").should == "function_dirname"
9+
end
10+
11+
it "should raise a ParseError if there is less than 1 arguments" do
12+
lambda { scope.function_dirname([]) }.should( raise_error(Puppet::ParseError))
13+
end
14+
15+
it "should return dirname for an absolute path" do
16+
result = scope.function_dirname(['/path/to/a/file.ext'])
17+
result.should(eq('/path/to/a'))
18+
end
19+
20+
it "should return dirname for a relative path" do
21+
result = scope.function_dirname(['path/to/a/file.ext'])
22+
result.should(eq('path/to/a'))
23+
end
24+
end

0 commit comments

Comments
 (0)