Skip to content

Commit fb3a5a6

Browse files
b4ldrekohl
andcommitted
stdlib::ensure: update function to support the generic case
Often custom resources only support present/absent for the ensure parameter and often the inclusion of theses resources are enabled via some boolean value as such i often find my self using the following pattern $ensure_feature = $enable_feature.bool2str('ensure', 'present') This patch updates the stdlib::ensure function so we can simplify this to $ensure_feature = $enable_feature.stdlib::ensure # or ... $ensure_feature = stdlib::ensure($enable_feature) Update spec/functions/stdlib_ensure_spec.rb Co-authored-by: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl> Update spec/functions/stdlib_ensure_spec.rb Co-authored-by: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>
1 parent fd418ae commit fb3a5a6

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

functions/ensure.pp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# @return [String]
44
function stdlib::ensure(
55
Variant[Boolean, Enum['present', 'absent']] $ensure,
6-
Enum['directory', 'link', 'mounted', 'service', 'file', 'package'] $resource,
6+
Optional[Enum['directory', 'link', 'mounted', 'service', 'file', 'package']] $resource = undef,
77
) >> String {
88
$_ensure = $ensure ? {
99
Boolean => $ensure.bool2str('present', 'absent'),
@@ -22,6 +22,7 @@ function stdlib::ensure(
2222
default => 'stopped',
2323
}
2424
}
25+
undef: { $_ensure }
2526
default: {
2627
$_ensure ? {
2728
'present' => $resource,

spec/functions/stdlib_ensure_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
require 'spec_helper'
44

55
describe 'stdlib::ensure' do
6+
context 'work without resource' do
7+
it { is_expected.to run.with_params(true).and_return('present') }
8+
it { is_expected.to run.with_params(false).and_return('absent') }
9+
end
610
context 'work with service resource' do
711
it { is_expected.to run.with_params('present', 'service').and_return('running') }
812
it { is_expected.to run.with_params(true, 'service').and_return('running') }

0 commit comments

Comments
 (0)