Skip to content

Commit 80a7374

Browse files
author
tphoney
committed
add deprecation function
1 parent 9465eea commit 80a7374

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

README.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,10 @@ Deletes all instances of a given value from a hash. For example, `delete_values(
289289

290290
Deletes all instances of the undef value from an array or hash. For example, `$hash = delete_undef_values({a=>'A', b=>'', c=>undef, d => false})` returns {a => 'A', b => '', d => false}. *Type*: rvalue.
291291

292+
#### `deprecation`
293+
294+
Function to print deprecation warnings, Logs a (non deprecation) warning once for a given key. A key, that can be used to denote this particular type of deprecation (for instance also adding it to disabled_warnings). The second is the uniqueness key - in the example, the deprecation can appear once per module name. The msg is the message text including any positional information that is formatted by the user/caller of the method It is affected by the puppet setting 'strict', which can be set to :error (outputs as an error message), :off (no message / error is displayed) and :warning (default, outputs a warning) *Type*: String, String, String.
295+
292296
#### `difference`
293297

294298
Returns the difference between two arrays. The returned array is a copy of the original array, removing any items that also appear in the second array. For example, `difference(["a","b","c"],["b","c","d"])` returns ["a"]. *Type*: rvalue.

lib/puppet/functions/deprecation.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Function to print deprecation warnings, Logs a (non deprecation) warning once for a given key
2+
# It is affected by the puppet setting 'strict', which can be set to :error, :off and :warning (default)
3+
#
4+
5+
Puppet::Functions.create_function(:deprecation) do
6+
dispatch :deprecation do
7+
param 'String', :kind
8+
param 'String', :key
9+
param 'String', :message
10+
end
11+
12+
def deprecation(kind, key, message)
13+
# depending on configuration setting of strict
14+
case Puppet.settings[:strict]
15+
when :off
16+
# do nothing
17+
when :error
18+
Puppet.fail("#{kind}. #{key}. #{message}")
19+
else
20+
Puppet.warn_once(kind, key, message)
21+
end
22+
end
23+
end

spec/functions/deprecation_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'spec_helper'
2+
3+
if ENV["FUTURE_PARSER"] == 'yes'
4+
describe 'deprecation' do
5+
pending 'teach rspec-puppet to load future-only functions under 3.7.5' do
6+
it { is_expected.not_to eq(nil) }
7+
end
8+
end
9+
end
10+
11+
if Puppet.version.to_f >= 4.0
12+
describe 'deprecation' do
13+
before(:each) {
14+
# this is to reset the strict variable to default
15+
Puppet.settings[:strict] = :warning
16+
}
17+
18+
it { is_expected.not_to eq(nil) }
19+
it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
20+
21+
it 'should display a single warning' do
22+
Puppet.expects(:warning).with(includes('heelo'))
23+
is_expected.to run.with_params('kind', 'key', 'heelo')
24+
end
25+
26+
it 'should display a single warning, despite multiple calls' do
27+
Puppet.expects(:warning).with(includes('heelo')).once
28+
is_expected.to run.with_params('kind', 'key', 'heelo')
29+
is_expected.to run.with_params('kind', 'key', 'heelo')
30+
end
31+
32+
it 'should fail twice with message, with multiple calls. when strict= :error' do
33+
Puppet.settings[:strict] = :error
34+
Puppet.expects(:fail).with(includes('heelo')).twice
35+
is_expected.to run.with_params('kind', 'key', 'heelo')
36+
is_expected.to run.with_params('kind', 'key', 'heelo')
37+
end
38+
39+
it 'should display nothing, despite multiple calls. strict= :off' do
40+
Puppet.settings[:strict] = :off
41+
Puppet.expects(:fail).with(any_parameters).never
42+
is_expected.to run.with_params('kind', 'key', 'heelo')
43+
is_expected.to run.with_params('kind', 'key', 'heelo')
44+
end
45+
46+
after(:all) {
47+
# this is to reset the strict variable to default
48+
Puppet.settings[:strict] = :warning
49+
}
50+
end
51+
end

0 commit comments

Comments
 (0)