Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Function extname() #949

Merged
merged 1 commit into from
Oct 11, 2018
Merged
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
21 changes: 18 additions & 3 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 Expand Up @@ -1969,12 +1984,12 @@ Arguments: A numeric or a string representing a number.

#### `num2bool`

Converts a number, or a string representation of a number, into a true Boolean.
Converts a number, or a string representation of a number, into a true Boolean.
Zero or anything non-numeric becomes `false`.
Numbers greater than zero become `true`.

Since Puppet 5.0.0, the same can be achieved with the Puppet type system.
See the [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
See the [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
function in Puppet for the many available type conversions.

Boolean(0) # false
Expand Down Expand Up @@ -2236,7 +2251,7 @@ Replaces consecutive repeats (such as 'aaaa') in a string with a single characte
Converts certain strings to a Boolean. This attempts to convert strings that contain the values '1', 'true', 't', 'y', or 'yes' to `true`. Strings that contain values '0', 'false', 'f', 'n', or 'no', or that are an empty string or undefined are converted to `false`. Any other value causes an error. These checks are case insensitive.

Since Puppet 5.0.0, the same can be achieved with the Puppet type system.
See the [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
See the [`Boolean.new`](https://puppet.com/docs/puppet/latest/function.html#conversion-to-boolean)
function in Puppet for the many available type conversions.

Boolean('false'), Boolean('n'), Boolean('no') # all false
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