Skip to content

Commit

Permalink
Merge pull request #949 from cocker-cc/feature/master/add_function_ex…
Browse files Browse the repository at this point in the history
…tname

add Function extname()
  • Loading branch information
david22swan authored Oct 11, 2018
2 parents e180d71 + f5b650e commit b9caf4d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,21 @@ userlist:
ensure_resources('user', hiera_hash('userlist'), {'ensure' => 'present'})
```
#### `stdlib::extname`
Returns the Extension (the Portion of Filename in Path starting from the last Period).
Example usage:
```puppet
stdlib::extname('test.rb') => '.rb'
stdlib::extname('a/b/d/test.rb') => '.rb'
stdlib::extname('test') => ''
stdlib::extname('.profile') => ''
```
*Type*: rvalue.
#### `fact`
Return the value of a given fact. Supports the use of dot-notation for referring to structured facts. If a fact requested does not exist, returns Undef.
Expand Down
26 changes: 26 additions & 0 deletions lib/puppet/functions/stdlib/extname.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Returns the Extension (the Portion of Filename in Path starting from the
# last Period).
#
# If Path is a Dotfile, or starts with a Period, then the starting Dot is not
# dealt with the Start of the Extension.
#
# An empty String will also be returned, when the Period is the last Character
# in Path.

Puppet::Functions.create_function(:'stdlib::extname') do
# @param filename The Filename
# @return [String] The Extension starting from the last Period
# @example Determining the Extension of a Filename
# stdlib::extname('test.rb') => '.rb'
# stdlib::extname('a/b/d/test.rb') => '.rb'
# stdlib::extname('test') => ''
# stdlib::extname('.profile') => ''
dispatch :extname do
param 'String', :filename
return_type 'String'
end

def extname(filename)
File.extname(filename)
end
end
16 changes: 16 additions & 0 deletions spec/functions/extname_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'spec_helper'

describe 'stdlib::extname' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{'stdlib::extname' expects 1 argument, got none}) }
it { is_expected.to run.with_params('one', 'two').and_raise_error(ArgumentError, %r{'stdlib::extname' expects 1 argument, got 2}) }
it { is_expected.to run.with_params([]).and_raise_error(ArgumentError, %r{'stdlib::extname' parameter 'filename' expects a String value, got Array}) }
it { is_expected.to run.with_params('test.rb').and_return('.rb') }
it { is_expected.to run.with_params('a/b/d/test.rb').and_return('.rb') }
it { is_expected.to run.with_params('test').and_return('') }
it { is_expected.to run.with_params('.profile').and_return('') }

context 'with UTF8 and double byte characters' do
it { is_expected.to run.with_params('file_√ạĺűē/竹.rb').and_return('.rb') }
end
end

0 comments on commit b9caf4d

Please sign in to comment.