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

0 commit comments

Comments
 (0)