Skip to content

Commit ff30448

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

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

lib/puppet/functions/deprecation.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Function to print deprecation warnings, it will display a provided message as well as the calling function.
2+
# The deprecation message will only be displayed once per caller.
3+
# It is affected by the puppet setting 'strict', which can be set to :error, :off and :warning (default)
4+
#
5+
require "puppet_x/puppetlabs/stdlib/deprecation_state"
6+
7+
Puppet::Functions.create_function(:deprecation) do
8+
dispatch :deprecation do
9+
param 'String', :message
10+
end
11+
12+
def deprecation(message)
13+
called_by = Kernel.caller.first
14+
# we only want to display a deprecation message once per caller.
15+
unless PuppetX::Puppetlabs::Stdlib::DeprecationState.deprecation_warning_said(called_by)
16+
# depending on configuration setting of strict
17+
case Puppet.settings[:strict]
18+
when :off
19+
# do nothing
20+
when :error
21+
Puppet.err("#{message}, called by #{called_by}")
22+
else
23+
Puppet.warning("#{message}, called by #{called_by}")
24+
end
25+
PuppetX::Puppetlabs::Stdlib::DeprecationState.add_deprecation_warning_said(called_by)
26+
end
27+
end
28+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module PuppetX
2+
module Puppetlabs
3+
module Stdlib
4+
class DeprecationState
5+
# This is so we display one message per caller
6+
@@deprecation_warning_said = []
7+
def self.deprecation_warning_said(calling_method)
8+
@@deprecation_warning_said.include?(calling_method)
9+
end
10+
def self.add_deprecation_warning_said(calling_method)
11+
@@deprecation_warning_said.push(calling_method) unless @@deprecation_warning_said.include?(calling_method)
12+
end
13+
end
14+
end
15+
end
16+
end

spec/functions/deprecation_spec.rb

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

0 commit comments

Comments
 (0)