Skip to content

Commit d222e4c

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

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-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 warning once for a given key. The uniqueness key - can appear once. 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.
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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', :key
8+
param 'String', :message
9+
end
10+
11+
def deprecation(key, message)
12+
# depending on configuration setting of strict
13+
case Puppet.settings[:strict]
14+
when :off
15+
# do nothing
16+
when :error
17+
Puppet.fail("deprecation. #{key}. #{message}")
18+
else
19+
Puppet.warn_once('deprecation', key, message)
20+
end
21+
end
22+
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('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('key', 'heelo')
29+
is_expected.to run.with_params('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('key', 'heelo')
36+
is_expected.to run.with_params('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('key', 'heelo')
43+
is_expected.to run.with_params('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)