-
Notifications
You must be signed in to change notification settings - Fork 583
(MODULES-3529) add deprecation function #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Function to print deprecation warnings, Logs a warning once for a given key. The uniqueness key - can appear once. The msg is the message text including any positional information that is formatted by the user/caller of the method It is affected by the puppet setting 'strict', which can be set to :error (outputs as an error message), :off (no message / error is displayed) and :warning (default, outputs a warning) *Type*: String, String. | ||
# | ||
|
||
Puppet::Functions.create_function(:deprecation) do | ||
dispatch :deprecation do | ||
param 'String', :key | ||
param 'String', :message | ||
end | ||
|
||
def deprecation(key, message) | ||
# depending on configuration setting of strict | ||
case Puppet.settings[:strict] | ||
when :off | ||
# do nothing | ||
when :error | ||
fail("deprecation. #{key}. #{message}") | ||
else | ||
Puppet.warn_once('deprecation', key, message) | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#! /usr/bin/env ruby -S rspec | ||
require 'spec_helper_acceptance' | ||
require 'shellwords' | ||
|
||
describe 'deprecation function' do | ||
before :each do | ||
FileUtils.rm_rf '/tmp/deprecation' | ||
end | ||
|
||
context 'with --strict=error', if: get_puppet_version =~ /^4/ do | ||
before :all do | ||
pp = <<-EOS | ||
deprecation('key', 'message') | ||
file { '/tmp/deprecation': ensure => present } | ||
EOS | ||
@result = on(default, puppet('apply', '--strict=error', '-e', Shellwords.shellescape(pp)), acceptable_exit_codes: (0...256)) | ||
end | ||
|
||
it "should return an error" do | ||
expect(@result.exit_code).to eq(1) | ||
end | ||
|
||
it "should show the error message" do | ||
expect(@result.stderr).to match(/deprecation. key. message/) | ||
end | ||
|
||
describe file('/tmp/deprecation') do | ||
it { is_expected.not_to exist } | ||
end | ||
end | ||
|
||
context 'with --strict=warning', if: get_puppet_version =~ /^4/ do | ||
before :all do | ||
pp = <<-EOS | ||
deprecation('key', 'message') | ||
file { '/tmp/deprecation': ensure => present } | ||
EOS | ||
@result = on(default, puppet('apply', '--strict=warning', '-e', Shellwords.shellescape(pp)), acceptable_exit_codes: (0...256)) | ||
end | ||
|
||
it "should not return an error" do | ||
expect(@result.exit_code).to eq(0) | ||
end | ||
|
||
it "should show the error message" do | ||
expect(@result.stderr).to match(/Warning: message/) | ||
end | ||
|
||
describe file('/tmp/deprecation') do | ||
it { is_expected.to exist } | ||
end | ||
end | ||
|
||
context 'with --strict=off', if: get_puppet_version =~ /^4/ do | ||
before :all do | ||
pp = <<-EOS | ||
deprecation('key', 'message') | ||
file { '/tmp/deprecation': ensure => present } | ||
EOS | ||
@result = on(default, puppet('apply', '--strict=off', '-e', Shellwords.shellescape(pp)), acceptable_exit_codes: (0...256)) | ||
end | ||
|
||
it "should not return an error" do | ||
expect(@result.exit_code).to eq(0) | ||
end | ||
|
||
it "should not show the error message" do | ||
expect(@result.stderr).not_to match(/Warning: message/) | ||
end | ||
|
||
describe file('/tmp/deprecation') do | ||
it { is_expected.to exist } | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'spec_helper' | ||
|
||
if ENV["FUTURE_PARSER"] == 'yes' | ||
describe 'deprecation' do | ||
pending 'teach rspec-puppet to load future-only functions under 3.7.5' do | ||
it { is_expected.not_to eq(nil) } | ||
end | ||
end | ||
end | ||
|
||
if Puppet.version.to_f >= 4.0 | ||
describe 'deprecation' do | ||
before(:each) { | ||
# this is to reset the strict variable to default | ||
Puppet.settings[:strict] = :warning | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Affecting global state should be reversed in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resolved |
||
} | ||
|
||
it { is_expected.not_to eq(nil) } | ||
it { is_expected.to run.with_params().and_raise_error(ArgumentError) } | ||
|
||
it 'should display a single warning' do | ||
Puppet.expects(:warning).with(includes('heelo')) | ||
is_expected.to run.with_params('key', 'heelo') | ||
end | ||
|
||
it 'should display a single warning, despite multiple calls' do | ||
Puppet.expects(:warning).with(includes('heelo')).once | ||
is_expected.to run.with_params('key', 'heelo') | ||
is_expected.to run.with_params('key', 'heelo') | ||
end | ||
|
||
it 'should fail twice with message, with multiple calls. when strict= :error' do | ||
Puppet.settings[:strict] = :error | ||
Puppet.expects(:warning).with(includes('heelo')).never | ||
is_expected.to run.with_params('key', 'heelo').and_raise_error(RuntimeError, /deprecation. key. heelo/) | ||
is_expected.to run.with_params('key', 'heelo').and_raise_error(RuntimeError, /deprecation. key. heelo/) | ||
end | ||
|
||
it 'should display nothing, despite multiple calls. strict= :off' do | ||
Puppet.settings[:strict] = :off | ||
Puppet.expects(:warning).with(includes('heelo')).never | ||
is_expected.to run.with_params('key', 'heelo') | ||
is_expected.to run.with_params('key', 'heelo') | ||
end | ||
|
||
after(:all) { | ||
# this is to reset the strict variable to default | ||
Puppet.settings[:strict] = :warning | ||
} | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding to disabled_warnings requires an update to puppet as it lists all keys that can be disabled - so that part will not work without a change to that setting in puppet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is very unfortunate, as we hoped to expose this functionality to the user. Specifically, allowing them(us) to specify finegrained deprecations, at least at the module level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually what I said is misleading, the first parameter
kind
to warn once, is the key todisabled_warnings
. So,kind
can be set to'deprecation'
which means it can be disabled. Here is an example from puppet source:Puppet.warn_once('deprecation', 'symbol_comparison', 'Comparing Symbols to non-Symbol values is deprecated')
. If a new kind is wanted and that it is also wanted to be able to disable that kind then puppet must be updated. (It would not be unreasonable to support any kind in the disable_warnings (with the only downside that typos will not be caught)).