diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2015-02-09 14:47:48 -0800 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2015-02-09 14:47:48 -0800 |
commit | 11140c2cbd6f675c1bc0c3f5697f23d6404886ba (patch) | |
tree | d54623e143871639b72d75977adaabf0635a86a4 | |
parent | 878ecf09a8945235637e3f761626f1e3c9e86ca1 (diff) | |
download | chef-11140c2cbd6f675c1bc0c3f5697f23d6404886ba.tar.gz |
Add Chef::Log.deprecation and associated wiring
-rw-r--r-- | lib/chef/deprecation/warnings.rb | 5 | ||||
-rw-r--r-- | lib/chef/exceptions.rb | 6 | ||||
-rw-r--r-- | lib/chef/log.rb | 11 | ||||
-rw-r--r-- | spec/unit/deprecation_spec.rb | 38 |
4 files changed, 43 insertions, 17 deletions
diff --git a/lib/chef/deprecation/warnings.rb b/lib/chef/deprecation/warnings.rb index 22b28f93b0..68b1d0e202 100644 --- a/lib/chef/deprecation/warnings.rb +++ b/lib/chef/deprecation/warnings.rb @@ -24,8 +24,8 @@ class Chef method_names.each do |name| m = instance_method(name) define_method(name) do |*args| - Chef::Log.warn "Method '#{name}' of '#{self.class}' is deprecated. It will be removed in Chef 12." - Chef::Log.warn "Please update your cookbooks accordingly. Accessed from:" + Chef::Log.deprecation "Method '#{name}' of '#{self.class}' is deprecated. It will be removed in Chef 12." + Chef::Log.deprecation "Please update your cookbooks accordingly. Accessed from:" caller[0..3].each {|l| Chef::Log.warn l} super(*args) end @@ -35,4 +35,3 @@ class Chef end end end - diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index 78d77e3778..498970468a 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -212,7 +212,11 @@ class Chef class NoProviderAvailable < RuntimeError; end - class DeprecatedFeatureError < RuntimeError; end + class DeprecatedFeatureError < RuntimeError; + def initalize + super("raising deprecation error due to treat_deprecation_warnings_as_errors being set") + end + end class MissingRole < RuntimeError NULL = Object.new diff --git a/lib/chef/log.rb b/lib/chef/log.rb index 131d706a5e..56c0ef022c 100644 --- a/lib/chef/log.rb +++ b/lib/chef/log.rb @@ -19,6 +19,7 @@ require 'logger' require 'chef/monologger' +require 'chef/exceptions' require 'mixlib/log' class Chef @@ -34,6 +35,14 @@ class Chef end end + def self.deprecation(msg=nil, &block) + if Chef::Config[:treat_deprecation_warnings_as_errors] + error(msg, *block) + raise Chef::Exceptions::DeprecatedFeatureError + else + warn(msg, *block) + end + end + end end - diff --git a/spec/unit/deprecation_spec.rb b/spec/unit/deprecation_spec.rb index 9bd081a6c4..f0f01db567 100644 --- a/spec/unit/deprecation_spec.rb +++ b/spec/unit/deprecation_spec.rb @@ -59,27 +59,41 @@ describe Chef::Deprecation do end end - context 'deprecation warning messages' do - before(:each) do - @warning_output = [ ] - allow(Chef::Log).to receive(:warn) { |msg| @warning_output << msg } + context 'when Chef::Config[:treat_deprecation_warnings_as_errors] is off' do + before do + Chef::Config[:treat_deprecation_warnings_as_errors] = false end - it 'should be enabled for deprecated methods' do - TestClass.new.deprecated_method(10) - expect(@warning_output).not_to be_empty + context 'deprecation warning messages' do + before(:each) do + @warning_output = [ ] + allow(Chef::Log).to receive(:warn) { |msg| @warning_output << msg } + end + + it 'should be enabled for deprecated methods' do + TestClass.new.deprecated_method(10) + expect(@warning_output).not_to be_empty + end + + it 'should contain stack trace' do + TestClass.new.deprecated_method(10) + expect(@warning_output.join("").include?(".rb")).to be_truthy + end end - it 'should contain stack trace' do - TestClass.new.deprecated_method(10) - expect(@warning_output.join("").include?(".rb")).to be_truthy + it 'deprecated methods should still be called' do + test_instance = TestClass.new + test_instance.deprecated_method(10) + expect(test_instance.get_value).to eq(10) end end - it 'deprecated methods should still be called' do + it 'should raise when deprecation warnings are treated as errors' do + # rspec should set this + expect(Chef::Config[:treat_deprecation_warnings_as_errors]).to be true test_instance = TestClass.new test_instance.deprecated_method(10) - expect(test_instance.get_value).to eq(10) + expect { test_instance.get_value }.to raise_error(Chef::Exceptions::DeprecatedFeatureError) end end |