forked from redhat-openstack/openstack-puppet-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basename() based on Ruby's File.basename
Based on dirname code. Includes RSpec tests and docs.
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Puppet::Parser::Functions | ||
newfunction(:basename, :type => :rvalue, :doc => <<-EOS | ||
Strips directory (and optional suffix) from a filename | ||
EOS | ||
) do |arguments| | ||
|
||
if arguments.size < 1 then | ||
raise(Puppet::ParseError, "basename(): No arguments given") | ||
elsif arguments.size > 2 then | ||
raise(Puppet::ParseError, "basename(): Too many arguments given (#{arguments.size})") | ||
else | ||
|
||
unless arguments[0].is_a?(String) | ||
raise(Puppet::ParseError, 'basename(): Requires string as first argument') | ||
end | ||
|
||
if arguments.size == 1 then | ||
rv = File.basename(arguments[0]) | ||
elsif arguments.size == 2 then | ||
|
||
unless arguments[1].is_a?(String) | ||
raise(Puppet::ParseError, 'basename(): Requires string as second argument') | ||
end | ||
|
||
rv = File.basename(arguments[0], arguments[1]) | ||
end | ||
|
||
end | ||
|
||
return rv | ||
end | ||
end | ||
|
||
# vim: set ts=2 sw=2 et : |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#! /usr/bin/env ruby -S rspec | ||
require 'spec_helper' | ||
|
||
describe "the basename function" do | ||
let(:scope) { PuppetlabsSpec::PuppetInternals.scope } | ||
|
||
it "should exist" do | ||
Puppet::Parser::Functions.function("basename").should == "function_basename" | ||
end | ||
|
||
it "should raise a ParseError if there is less than 1 argument" do | ||
lambda { scope.function_basename([]) }.should( raise_error(Puppet::ParseError)) | ||
end | ||
|
||
it "should raise a ParseError if there are more than 2 arguments" do | ||
lambda { scope.function_basename(['a', 'b', 'c']) }.should( raise_error(Puppet::ParseError)) | ||
end | ||
|
||
it "should return basename for an absolute path" do | ||
result = scope.function_basename(['/path/to/a/file.ext']) | ||
result.should(eq('file.ext')) | ||
end | ||
|
||
it "should return basename for a relative path" do | ||
result = scope.function_basename(['path/to/a/file.ext']) | ||
result.should(eq('file.ext')) | ||
end | ||
|
||
it "should strip extention when extension specified (absolute path)" do | ||
result = scope.function_basename(['/path/to/a/file.ext', '.ext']) | ||
result.should(eq('file')) | ||
end | ||
|
||
it "should strip extention when extension specified (relative path)" do | ||
result = scope.function_basename(['path/to/a/file.ext', '.ext']) | ||
result.should(eq('file')) | ||
end | ||
|
||
it "should complain about non-string first argument" do | ||
lambda { scope.function_basename([[]]) }.should( raise_error(Puppet::ParseError)) | ||
end | ||
|
||
it "should complain about non-string second argument" do | ||
lambda { scope.function_basename(['/path/to/a/file.ext', []]) }.should( raise_error(Puppet::ParseError)) | ||
end | ||
end |