Skip to content

Commit 3797e92

Browse files
committed
Ignore Puppet's strict setting when calling function without namespace
Previously, when a user had the Puppet setting `strict` set to `error` (which is the default in Puppet 8), a call to one of stdlib's functions via the deprecated non-namespaced function would cause a hard failure instead of just logging a warning and calling the real namespaced function. In this change, an optional third parameter is added to the `deprecation` function. The default behaviour remains the same, but when the third parameter is set to `false`, the `strict` setting is ignored. Finally, all of our shims have been updated to call `deprecation` with this parameter set to `false`. This change will make it much easier for users to migrate to stdlib 9 (and to upgrade modules that now depend on stdlib 9) Fixes #1373
1 parent 3df8f0c commit 3797e92

23 files changed

+97
-98
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ task :regenerate_unamespaced_shims do
115115
repeated_param 'Any', :args
116116
end
117117
def deprecation_gen(*args)
118-
call_function('deprecation', '#{function_name}', 'This function is deprecated, please use stdlib::#{function_name} instead.')
118+
call_function('deprecation', '#{function_name}', 'This function is deprecated, please use stdlib::#{function_name} instead.', false)
119119
call_function('stdlib::#{function_name}', *args)
120120
end
121121
end

lib/puppet/functions/batch_escape.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'batch_escape', 'This function is deprecated, please use stdlib::batch_escape instead.')
11+
call_function('deprecation', 'batch_escape', 'This function is deprecated, please use stdlib::batch_escape instead.', false)
1212
call_function('stdlib::batch_escape', *args)
1313
end
1414
end

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

lib/puppet/functions/ensure_packages.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
repeated_param 'Any', :args
88
end
99
def deprecation_gen(scope, *args)
10-
call_function('deprecation', 'ensure_packages', 'This function is deprecated, please use stdlib::ensure_packages instead.')
10+
call_function('deprecation', 'ensure_packages', 'This function is deprecated, please use stdlib::ensure_packages instead.', false)
1111
scope.call_function('stdlib::ensure_packages', args)
1212
end
1313
end

lib/puppet/functions/fqdn_rand_string.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'fqdn_rand_string', 'This function is deprecated, please use stdlib::fqdn_rand_string instead.')
11+
call_function('deprecation', 'fqdn_rand_string', 'This function is deprecated, please use stdlib::fqdn_rand_string instead.', false)
1212
call_function('stdlib::fqdn_rand_string', *args)
1313
end
1414
end

lib/puppet/functions/has_interface_with.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'has_interface_with', 'This function is deprecated, please use stdlib::has_interface_with instead.')
11+
call_function('deprecation', 'has_interface_with', 'This function is deprecated, please use stdlib::has_interface_with instead.', false)
1212
call_function('stdlib::has_interface_with', *args)
1313
end
1414
end

lib/puppet/functions/merge.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'merge', 'This function is deprecated, please use stdlib::merge instead.')
11+
call_function('deprecation', 'merge', 'This function is deprecated, please use stdlib::merge instead.', false)
1212
call_function('stdlib::merge', *args)
1313
end
1414
end

lib/puppet/functions/os_version_gte.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'os_version_gte', 'This function is deprecated, please use stdlib::os_version_gte instead.')
11+
call_function('deprecation', 'os_version_gte', 'This function is deprecated, please use stdlib::os_version_gte instead.', false)
1212
call_function('stdlib::os_version_gte', *args)
1313
end
1414
end

lib/puppet/functions/parsehocon.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'parsehocon', 'This function is deprecated, please use stdlib::parsehocon instead.')
11+
call_function('deprecation', 'parsehocon', 'This function is deprecated, please use stdlib::parsehocon instead.', false)
1212
call_function('stdlib::parsehocon', *args)
1313
end
1414
end

lib/puppet/functions/powershell_escape.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'powershell_escape', 'This function is deprecated, please use stdlib::powershell_escape instead.')
11+
call_function('deprecation', 'powershell_escape', 'This function is deprecated, please use stdlib::powershell_escape instead.', false)
1212
call_function('stdlib::powershell_escape', *args)
1313
end
1414
end

lib/puppet/functions/seeded_rand.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'seeded_rand', 'This function is deprecated, please use stdlib::seeded_rand instead.')
11+
call_function('deprecation', 'seeded_rand', 'This function is deprecated, please use stdlib::seeded_rand instead.', false)
1212
call_function('stdlib::seeded_rand', *args)
1313
end
1414
end

lib/puppet/functions/seeded_rand_string.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'seeded_rand_string', 'This function is deprecated, please use stdlib::seeded_rand_string instead.')
11+
call_function('deprecation', 'seeded_rand_string', 'This function is deprecated, please use stdlib::seeded_rand_string instead.', false)
1212
call_function('stdlib::seeded_rand_string', *args)
1313
end
1414
end

lib/puppet/functions/shell_escape.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'shell_escape', 'This function is deprecated, please use stdlib::shell_escape instead.')
11+
call_function('deprecation', 'shell_escape', 'This function is deprecated, please use stdlib::shell_escape instead.', false)
1212
call_function('stdlib::shell_escape', *args)
1313
end
1414
end

lib/puppet/functions/to_json.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'to_json', 'This function is deprecated, please use stdlib::to_json instead.')
11+
call_function('deprecation', 'to_json', 'This function is deprecated, please use stdlib::to_json instead.', false)
1212
call_function('stdlib::to_json', *args)
1313
end
1414
end

lib/puppet/functions/to_json_pretty.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'to_json_pretty', 'This function is deprecated, please use stdlib::to_json_pretty instead.')
11+
call_function('deprecation', 'to_json_pretty', 'This function is deprecated, please use stdlib::to_json_pretty instead.', false)
1212
call_function('stdlib::to_json_pretty', *args)
1313
end
1414
end

lib/puppet/functions/to_python.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'to_python', 'This function is deprecated, please use stdlib::to_python instead.')
11+
call_function('deprecation', 'to_python', 'This function is deprecated, please use stdlib::to_python instead.', false)
1212
call_function('stdlib::to_python', *args)
1313
end
1414
end

lib/puppet/functions/to_ruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'to_ruby', 'This function is deprecated, please use stdlib::to_ruby instead.')
11+
call_function('deprecation', 'to_ruby', 'This function is deprecated, please use stdlib::to_ruby instead.', false)
1212
call_function('stdlib::to_ruby', *args)
1313
end
1414
end

lib/puppet/functions/to_toml.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'to_toml', 'This function is deprecated, please use stdlib::to_toml instead.')
11+
call_function('deprecation', 'to_toml', 'This function is deprecated, please use stdlib::to_toml instead.', false)
1212
call_function('stdlib::to_toml', *args)
1313
end
1414
end

lib/puppet/functions/to_yaml.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'to_yaml', 'This function is deprecated, please use stdlib::to_yaml instead.')
11+
call_function('deprecation', 'to_yaml', 'This function is deprecated, please use stdlib::to_yaml instead.', false)
1212
call_function('stdlib::to_yaml', *args)
1313
end
1414
end

lib/puppet/functions/type_of.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'type_of', 'This function is deprecated, please use stdlib::type_of instead.')
11+
call_function('deprecation', 'type_of', 'This function is deprecated, please use stdlib::type_of instead.', false)
1212
call_function('stdlib::type_of', *args)
1313
end
1414
end

lib/puppet/functions/validate_domain_name.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'validate_domain_name', 'This function is deprecated, please use stdlib::validate_domain_name instead.')
11+
call_function('deprecation', 'validate_domain_name', 'This function is deprecated, please use stdlib::validate_domain_name instead.', false)
1212
call_function('stdlib::validate_domain_name', *args)
1313
end
1414
end

lib/puppet/functions/validate_email_address.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
repeated_param 'Any', :args
99
end
1010
def deprecation_gen(*args)
11-
call_function('deprecation', 'validate_email_address', 'This function is deprecated, please use stdlib::validate_email_address instead.')
11+
call_function('deprecation', 'validate_email_address', 'This function is deprecated, please use stdlib::validate_email_address instead.', false)
1212
call_function('stdlib::validate_email_address', *args)
1313
end
1414
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)