Skip to content

Commit 72d2365

Browse files
author
tphoney
committed
(MODULES-3529)add deprecation function
1 parent 9465eea commit 72d2365

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 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.
2+
#
3+
4+
Puppet::Functions.create_function(:deprecation) do
5+
dispatch :deprecation do
6+
param 'String', :key
7+
param 'String', :message
8+
end
9+
10+
def deprecation(key, message)
11+
# depending on configuration setting of strict
12+
case Puppet.settings[:strict]
13+
when :off
14+
# do nothing
15+
when :error
16+
fail("deprecation. #{key}. #{message}")
17+
else
18+
Puppet.warn_once('deprecation', key, message)
19+
end
20+
end
21+
end

spec/acceptance/deprecation_spec.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper_acceptance'
3+
require 'shellwords'
4+
5+
describe 'deprecation function' do
6+
before :each do
7+
FileUtils.rm_rf '/tmp/deprecation'
8+
end
9+
10+
context 'with --strict=error', if: get_puppet_version =~ /^4/ do
11+
before :all do
12+
pp = <<-EOS
13+
deprecation('key', 'message')
14+
file { '/tmp/deprecation': ensure => present }
15+
EOS
16+
@result = on(default, puppet('apply', '--strict=error', '-e', Shellwords.shellescape(pp)), acceptable_exit_codes: (0...256))
17+
end
18+
19+
it "should return an error" do
20+
expect(@result.exit_code).to eq(1)
21+
end
22+
23+
it "should show the error message" do
24+
expect(@result.stderr).to match(/deprecation. key. message/)
25+
end
26+
27+
describe file('/tmp/deprecation') do
28+
it { is_expected.not_to exist }
29+
end
30+
end
31+
32+
context 'with --strict=warning', if: get_puppet_version =~ /^4/ do
33+
before :all do
34+
pp = <<-EOS
35+
deprecation('key', 'message')
36+
file { '/tmp/deprecation': ensure => present }
37+
EOS
38+
@result = on(default, puppet('apply', '--strict=warning', '-e', Shellwords.shellescape(pp)), acceptable_exit_codes: (0...256))
39+
end
40+
41+
it "should not return an error" do
42+
expect(@result.exit_code).to eq(0)
43+
end
44+
45+
it "should show the error message" do
46+
expect(@result.stderr).to match(/Warning: message/)
47+
end
48+
49+
describe file('/tmp/deprecation') do
50+
it { is_expected.to exist }
51+
end
52+
end
53+
54+
context 'with --strict=off', if: get_puppet_version =~ /^4/ do
55+
before :all do
56+
pp = <<-EOS
57+
deprecation('key', 'message')
58+
file { '/tmp/deprecation': ensure => present }
59+
EOS
60+
@result = on(default, puppet('apply', '--strict=off', '-e', Shellwords.shellescape(pp)), acceptable_exit_codes: (0...256))
61+
end
62+
63+
it "should not return an error" do
64+
expect(@result.exit_code).to eq(0)
65+
end
66+
67+
it "should not show the error message" do
68+
expect(@result.stderr).not_to match(/Warning: message/)
69+
end
70+
71+
describe file('/tmp/deprecation') do
72+
it { is_expected.to exist }
73+
end
74+
end
75+
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(:warning).with(includes('heelo')).never
35+
is_expected.to run.with_params('key', 'heelo').and_raise_error(RuntimeError, /deprecation. key. heelo/)
36+
is_expected.to run.with_params('key', 'heelo').and_raise_error(RuntimeError, /deprecation. key. heelo/)
37+
end
38+
39+
it 'should display nothing, despite multiple calls. strict= :off' do
40+
Puppet.settings[:strict] = :off
41+
Puppet.expects(:warning).with(includes('heelo')).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)