Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ string

- *Type*: rvalue

basename
-------
Returns the `basename` of a path.

*Examples:*

basename('/path/to/a/file.ext')

Would return: 'file.ext'

bool2num
--------
Converts a boolean to a number. Converts the values:
Expand Down
12 changes: 12 additions & 0 deletions lib/puppet/parser/functions/basename.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Puppet::Parser::Functions
newfunction(:basename, :type => :rvalue, :arity => 1, doc => <<-EOS
Returns the basename of a path.
EOS
) do |arguments|

path = arguments[0]
return File.basename(path)
end
end

# vim: set ts=2 sw=2 et :
42 changes: 42 additions & 0 deletions spec/acceptance/basename_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper_acceptance'

describe 'basename function', :unless => UNSUPPORTED_PLATFORMS.include?(fact('operatingsystem')) do
describe 'success' do
context 'absolute path' do
it 'returns the basename' do
pp = <<-EOS
$a = '/path/to/a/file.txt'
$b = 'file.txt'
$o = basename($a)
if $o == $b {
notify { 'output correct': }
}
EOS

apply_manifest(pp, :catch_failures => true) do |r|
expect(r.stdout).to match(/Notice: output correct/)
end
end
end
context 'relative path' do
it 'returns the basename' do
pp = <<-EOS
$a = 'path/to/a/file.txt'
$b = 'file.txt'
$o = basename($a)
if $o == $b {
notify { 'output correct': }
}
EOS

apply_manifest(pp, :catch_failures => true) do |r|
expect(r.stdout).to match(/Notice: output correct/)
end
end
end
end
describe 'failure' do
it 'handles improper argument counts'
end
end
24 changes: 24 additions & 0 deletions spec/functions/basename_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#! /usr/bin/env ruby -S rspec
require 'spec_helper'

describe "the basename function" do
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }

it "should exist" do
expect(Puppet::Parser::Functions.function("basename")).to eq("function_basename")
end

it "should raise a ParseError if there is less than 1 arguments" do
expect { scope.function_basename([]) }.to( raise_error(Puppet::ParseError))
end

it "should return basename for an absolute path" do
result = scope.function_basename(['/path/to/a/file.ext'])
expect(result).to(eq('file.ext'))
end

it "should return basename for a relative path" do
result = scope.function_basename(['path/to/a/file.ext'])
expect(result).to(eq('file.ext'))
end
end