Skip to content

Commit 4b64d06

Browse files
authored
Merge pull request #1378 from alexjfisher/strict_deprecation
2 parents 3df8f0c + f7dd14a commit 4b64d06

File tree

2 files changed

+76
-77
lines changed

2 files changed

+76
-77
lines changed

lib/puppet/functions/deprecation.rb

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
# frozen_string_literal: true
22

3-
# Function to print deprecation warnings, Logs a warning once for a given key.
4-
#
5-
# The uniqueness key - can appear once.
6-
# The msg is the message text including any positional information that is formatted by the
7-
# user/caller of the method.
8-
# It is affected by the puppet setting 'strict', which can be set to :error
9-
# (outputs as an error message), :off (no message / error is displayed) and :warning
10-
# (default, outputs a warning) *Type*: String, String.
11-
#
3+
# @summary Function to print deprecation warnings, Logs a warning once for a given key.
124
Puppet::Functions.create_function(:deprecation) do
135
# @param key
14-
# @param message
15-
# @return deprecated warnings
6+
# The uniqueness key. This function logs once for any given key.
7+
# @param message
8+
# Is the message text including any positional information that is formatted by the user/caller of the function.
9+
# @param use_strict_setting
10+
# When `true`, (the default), the function is affected by the puppet setting 'strict', which can be set to :error
11+
# (outputs as an error message), :off (no message / error is displayed) and :warning
12+
# (default, outputs a warning).
1613
dispatch :deprecation do
1714
param 'String', :key
1815
param 'String', :message
16+
optional_param 'Boolean', :use_strict_setting
1917
end
2018

21-
def deprecation(key, message)
19+
def deprecation(key, message, use_strict_setting = true) # rubocop:disable Style/OptionalBooleanParameter
2220
if defined? Puppet::Pops::PuppetStack.stacktrace
2321
stacktrace = Puppet::Pops::PuppetStack.stacktrace
2422
file = stacktrace[0]
2523
line = stacktrace[1]
2624
message = "#{message} at #{file}:#{line}"
2725
end
28-
# depending on configuration setting of strict
29-
case Puppet.settings[:strict]
30-
when :off
31-
# do nothing
32-
when :error
33-
raise("deprecation. #{key}. #{message}")
34-
else
35-
Puppet.deprecation_warning(message, key) unless ENV['STDLIB_LOG_DEPRECATIONS'] == 'false'
36-
end
26+
27+
# Do nothing if using strict setting and strict is set to `off`
28+
return if use_strict_setting && Puppet.settings[:strict] == :off
29+
30+
# Fail hard if using strict setting and strict is set to `error`
31+
raise("deprecation. #{key}. #{message}") if use_strict_setting && Puppet.settings[:strict] == :error
32+
33+
# Otherwise raise a soft warning
34+
# (unless the STDLIB_LOG_DEPRECATIONS has been set to `false`. This is mainly for use in rspec-puppet testing to suppress noise in logs)
35+
Puppet.deprecation_warning(message, key) unless ENV['STDLIB_LOG_DEPRECATIONS'] == 'false'
36+
nil
3737
end
3838
end

spec/functions/deprecation_spec.rb

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,75 @@
22

33
require 'spec_helper'
44

5-
if Puppet::Util::Package.versioncmp(Puppet.version, '4.5.0') >= 0
6-
describe 'deprecation' do
7-
before(:each) do
8-
# this is to reset the strict variable to default
9-
Puppet.settings[:strict] = :warning
10-
end
5+
describe 'deprecation' do
6+
before(:each) do
7+
# this is to reset the strict variable to default
8+
Puppet.settings[:strict] = :warning
9+
end
1110

12-
after(:each) do
13-
# this is to reset the strict variable to default
14-
Puppet.settings[:strict] = :warning
15-
end
11+
after(:each) do
12+
# this is to reset the strict variable to default
13+
Puppet.settings[:strict] = :warning
14+
end
1615

17-
it { is_expected.not_to be_nil }
18-
it { is_expected.to run.with_params.and_raise_error(ArgumentError) }
16+
it { is_expected.not_to be_nil }
17+
it { is_expected.to run.with_params.and_raise_error(ArgumentError) }
1918

20-
it 'displays a single warning' do
21-
if Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0 && Puppet::Util::Package.versioncmp(Puppet.version, '5.5.7') < 0
22-
expect(Puppet).to receive(:deprecation_warning).with('heelo at :', 'key')
23-
expect(Puppet).to receive(:deprecation_warning).with("Modifying 'autosign' as a setting is deprecated.")
24-
else
25-
expect(Puppet).to receive(:warning).with(include('heelo')).once
26-
end
27-
expect(subject).to run.with_params('key', 'heelo')
19+
it 'displays a single warning' do
20+
if Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0 && Puppet::Util::Package.versioncmp(Puppet.version, '5.5.7') < 0
21+
expect(Puppet).to receive(:deprecation_warning).with('heelo at :', 'key')
22+
expect(Puppet).to receive(:deprecation_warning).with("Modifying 'autosign' as a setting is deprecated.")
23+
else
24+
expect(Puppet).to receive(:warning).with(include('heelo')).once
2825
end
26+
expect(subject).to run.with_params('key', 'heelo')
27+
end
2928

30-
it 'displays a single warning, despite multiple calls' do
31-
if Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0 && Puppet::Util::Package.versioncmp(Puppet.version, '5.5.7') < 0
32-
expect(Puppet).to receive(:deprecation_warning).with('heelo at :', 'key').twice
33-
expect(Puppet).to receive(:deprecation_warning).with("Modifying 'autosign' as a setting is deprecated.")
34-
else
35-
expect(Puppet).to receive(:warning).with(include('heelo')).once
36-
end
37-
2.times do |_i|
38-
expect(subject).to run.with_params('key', 'heelo')
39-
end
29+
it 'displays a single warning, despite multiple calls' do
30+
if Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0 && Puppet::Util::Package.versioncmp(Puppet.version, '5.5.7') < 0
31+
expect(Puppet).to receive(:deprecation_warning).with('heelo at :', 'key').twice
32+
expect(Puppet).to receive(:deprecation_warning).with("Modifying 'autosign' as a setting is deprecated.")
33+
else
34+
expect(Puppet).to receive(:warning).with(include('heelo')).once
4035
end
41-
42-
it 'fails twice with message, with multiple calls. when strict= :error' do
43-
Puppet.settings[:strict] = :error
44-
expect(Puppet).not_to receive(:warning).with(include('heelo'))
45-
2.times do |_i|
46-
expect(subject).to run.with_params('key', 'heelo').and_raise_error(RuntimeError, %r{deprecation. key. heelo})
47-
end
36+
2.times do |_i|
37+
expect(subject).to run.with_params('key', 'heelo')
4838
end
39+
end
4940

50-
it 'displays nothing, despite multiple calls. strict= :off' do
51-
Puppet.settings[:strict] = :off
52-
expect(Puppet).not_to receive(:warning).with(include('heelo'))
53-
2.times do |_i|
54-
expect(subject).to run.with_params('key', 'heelo')
55-
end
41+
it 'fails twice with message, with multiple calls. when strict= :error' do
42+
Puppet.settings[:strict] = :error
43+
expect(Puppet).not_to receive(:warning).with(include('heelo'))
44+
2.times do |_i|
45+
expect(subject).to run.with_params('key', 'heelo').and_raise_error(RuntimeError, %r{deprecation. key. heelo})
5646
end
5747
end
58-
elsif Puppet.version.to_f < 4.0
59-
# Puppet version < 4 will use these tests.
60-
describe 'deprecation' do
61-
after(:each) do
62-
ENV.delete('STDLIB_LOG_DEPRECATIONS')
63-
end
6448

65-
before(:each) do
66-
ENV['STDLIB_LOG_DEPRECATIONS'] = 'true'
49+
it 'displays nothing, despite multiple calls. strict= :off' do
50+
Puppet.settings[:strict] = :off
51+
expect(Puppet).not_to receive(:warning).with(include('heelo'))
52+
2.times do |_i|
53+
expect(subject).to run.with_params('key', 'heelo')
6754
end
55+
end
6856

69-
it { is_expected.not_to be_nil }
70-
it { is_expected.to run.with_params.and_raise_error(Puppet::ParseError, %r{wrong number of arguments}i) }
57+
context 'with `use_strict_setting` `false`' do
58+
let(:params) { ['key', 'heelo', false] }
7159

72-
it 'displays a single warning' do
73-
expect(scope).to receive(:warning).with(include('heelo'))
74-
expect(subject).to run.with_params('key', 'heelo')
60+
context 'and `strict` setting set to `error`' do
61+
it 'displays a warning' do
62+
Puppet.settings[:strict] = :error
63+
expect(Puppet).to receive(:warning).with(include('heelo')).once
64+
expect(subject).to run.with_params(*params)
65+
end
66+
end
67+
68+
context 'and `strict` setting set to `off`' do
69+
it 'displays a warning' do
70+
Puppet.settings[:strict] = :off
71+
expect(Puppet).to receive(:warning).with(include('heelo')).once
72+
expect(subject).to run.with_params(*params)
73+
end
7574
end
7675
end
7776
end

0 commit comments

Comments
 (0)