|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
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. |
12 | 4 | Puppet::Functions.create_function(:deprecation) do |
13 | 5 | # @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). |
16 | 13 | dispatch :deprecation do |
17 | 14 | param 'String', :key |
18 | 15 | param 'String', :message |
| 16 | + optional_param 'Boolean', :use_strict_setting |
19 | 17 | end |
20 | 18 |
|
21 | | - def deprecation(key, message) |
| 19 | + def deprecation(key, message, use_strict_setting = true) # rubocop:disable Style/OptionalBooleanParameter |
22 | 20 | if defined? Puppet::Pops::PuppetStack.stacktrace |
23 | 21 | stacktrace = Puppet::Pops::PuppetStack.stacktrace |
24 | 22 | file = stacktrace[0] |
25 | 23 | line = stacktrace[1] |
26 | 24 | message = "#{message} at #{file}:#{line}" |
27 | 25 | 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 supress noise in logs) |
| 35 | + Puppet.deprecation_warning(message, key) unless ENV['STDLIB_LOG_DEPRECATIONS'] == 'false' |
| 36 | + nil |
37 | 37 | end |
38 | 38 | end |
0 commit comments