Skip to content

Commit 841b0df

Browse files
author
Morgan Haskel
committed
Merge pull request #368 from rfugina/basename
Basename implementation
2 parents 8726caf + ef3d42f commit 841b0df

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

README.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ If you want to use a standardized set of run stages for Puppet, `include stdlib`
8686
Requires an action ('encode', 'decode') and either a plain or base64-encoded
8787
string. *Type*: rvalue
8888

89+
* `basename`: Returns the `basename` of a path (optionally stripping an extension). For example:
90+
* ('/path/to/a/file.ext') returns 'file.ext'
91+
* ('relative/path/file.ext') returns 'file.ext'
92+
* ('/path/to/a/file.ext', '.ext') returns 'file'
93+
94+
*Type*: rvalue
95+
8996
* `bool2num`: Converts a boolean to a number. Converts values:
9097
* 'false', 'f', '0', 'n', and 'no' to 0.
9198
* 'true', 't', '1', 'y', and 'yes' to 1.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:basename, :type => :rvalue, :doc => <<-EOS
3+
Strips directory (and optional suffix) from a filename
4+
EOS
5+
) do |arguments|
6+
7+
if arguments.size < 1 then
8+
raise(Puppet::ParseError, "basename(): No arguments given")
9+
elsif arguments.size > 2 then
10+
raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})")
11+
else
12+
13+
unless arguments[0].is_a?(String)
14+
raise(Puppet::ParseError, 'basename(): Requires string as first argument')
15+
end
16+
17+
if arguments.size == 1 then
18+
rv = File.basename(arguments[0])
19+
elsif arguments.size == 2 then
20+
21+
unless arguments[1].is_a?(String)
22+
raise(Puppet::ParseError, 'basename(): Requires string as second argument')
23+
end
24+
25+
rv = File.basename(arguments[0], arguments[1])
26+
end
27+
28+
end
29+
30+
return rv
31+
end
32+
end
33+
34+
# vim: set ts=2 sw=2 et :
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the basename function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
it "should exist" do
8+
Puppet::Parser::Functions.function("basename").should == "function_basename"
9+
end
10+
11+
it "should raise a ParseError if there is less than 1 argument" do
12+
lambda { scope.function_basename([]) }.should( raise_error(Puppet::ParseError))
13+
end
14+
15+
it "should raise a ParseError if there are more than 2 arguments" do
16+
lambda { scope.function_basename(['a', 'b', 'c']) }.should( raise_error(Puppet::ParseError))
17+
end
18+
19+
it "should return basename for an absolute path" do
20+
result = scope.function_basename(['/path/to/a/file.ext'])
21+
result.should(eq('file.ext'))
22+
end
23+
24+
it "should return basename for a relative path" do
25+
result = scope.function_basename(['path/to/a/file.ext'])
26+
result.should(eq('file.ext'))
27+
end
28+
29+
it "should strip extention when extension specified (absolute path)" do
30+
result = scope.function_basename(['/path/to/a/file.ext', '.ext'])
31+
result.should(eq('file'))
32+
end
33+
34+
it "should strip extention when extension specified (relative path)" do
35+
result = scope.function_basename(['path/to/a/file.ext', '.ext'])
36+
result.should(eq('file'))
37+
end
38+
39+
it "should complain about non-string first argument" do
40+
lambda { scope.function_basename([[]]) }.should( raise_error(Puppet::ParseError))
41+
end
42+
43+
it "should complain about non-string second argument" do
44+
lambda { scope.function_basename(['/path/to/a/file.ext', []]) }.should( raise_error(Puppet::ParseError))
45+
end
46+
end

0 commit comments

Comments
 (0)