diff options
author | John Keiser <john@johnkeiser.com> | 2015-05-27 10:40:55 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-05-28 13:27:44 -0700 |
commit | 81e4fa499bb792ca53ffd362a88067c858c33211 (patch) | |
tree | de9275d089daf0da6dca4d97bde0bb299b01b253 | |
parent | 6d1b736f6d581fb6a63f5b51708b0a75c566321e (diff) | |
download | chef-81e4fa499bb792ca53ffd362a88067c858c33211.tar.gz |
Fix up deprecated_attr_*, add tests
-rw-r--r-- | lib/chef/mixin/deprecation.rb | 36 | ||||
-rw-r--r-- | spec/unit/deprecation_spec.rb | 55 |
2 files changed, 71 insertions, 20 deletions
diff --git a/lib/chef/mixin/deprecation.rb b/lib/chef/mixin/deprecation.rb index c7923a5efb..a3eacf75cb 100644 --- a/lib/chef/mixin/deprecation.rb +++ b/lib/chef/mixin/deprecation.rb @@ -100,28 +100,24 @@ class Chef deprecated_attr_writer(name, alternative) end - def deprecated_attr_reader(name, alternative) - instance_eval <<-EOM - def #{name}(value) - Chef::Log.deprecation("#{self}.#{name} is deprecated. Support will be removed in a future release.") - Chef::Log.deprecation(#{alternative.inspect}) - Chef::Log.deprecation("Called from:") - caller[0..3].each {|l| log(l)} - @#{name} - end - EOM + def deprecated_attr_reader(name, alternative, level=:warn) + define_method(name) do + Chef::Log.deprecation("#{self.class}.#{name} is deprecated. Support will be removed in a future release.") + Chef::Log.deprecation(alternative) + Chef::Log.deprecation("Called from:") + caller[0..3].each {|c| Chef::Log.deprecation(c)} + instance_variable_get("@#{name}") + end end - def deprecated_attr_writer(name, alternative) - instance_eval <<-EOM - def #{name}=(value) - Chef::Log.deprecation("Writing to #{self}.#{name}= is deprecated. Support will be removed in a future release.") - Chef::Log.deprecation(#{alternative.inspect}) - Chef::Log.deprecation("Called from:") - caller[0..3].each {|l| log(l)} - @#{name} = value - end - EOM + def deprecated_attr_writer(name, alternative, level=:warn) + define_method("#{name}=") do |value| + Chef::Log.deprecation("Writing to #{self.class}.#{name} with #{name}= is deprecated. Support will be removed in a future release.") + Chef::Log.deprecation(alternative) + Chef::Log.deprecation("Called from:") + caller[0..3].each {|c| Chef::Log.deprecation(c)} + instance_variable_set("@#{name}", value) + end end end end diff --git a/spec/unit/deprecation_spec.rb b/spec/unit/deprecation_spec.rb index f824cb7c76..2e1f3c39f3 100644 --- a/spec/unit/deprecation_spec.rb +++ b/spec/unit/deprecation_spec.rb @@ -95,4 +95,59 @@ describe Chef::Deprecation do expect { test_instance.deprecated_method(10) }.to raise_error(Chef::Exceptions::DeprecatedFeatureError) end + context "When a class has deprecated_attr, _reader and _writer" do + before(:context) do + class DeprecatedAttrTest + extend Chef::Mixin::Deprecation + def initialize + @a = @r = @w = 1 + end + deprecated_attr :a, "a" + deprecated_attr_reader :r, "r" + deprecated_attr_writer :w, "w" + end + end + + it "The deprecated_attr emits warnings" do + test = DeprecatedAttrTest.new + expect { test.a = 10 }.to raise_error(Chef::Exceptions::DeprecatedFeatureError) + expect { test.a }.to raise_error(Chef::Exceptions::DeprecatedFeatureError) + end + + it "The deprecated_attr_writer emits warnings, and does not create a reader" do + test = DeprecatedAttrTest.new + expect { test.w = 10 }.to raise_error(Chef::Exceptions::DeprecatedFeatureError) + expect { test.w }.to raise_error(NoMethodError) + end + + it "The deprecated_attr_reader emits warnings, and does not create a writer" do + test = DeprecatedAttrTest.new + expect { test.r = 10 }.to raise_error(NoMethodError) + expect { test.r }.to raise_error(Chef::Exceptions::DeprecatedFeatureError) + end + + context "With deprecation warnings not throwing exceptions" do + before do + Chef::Config[:treat_deprecation_warnings_as_errors] = false + end + + it "The deprecated_attr can be written to and read from" do + test = DeprecatedAttrTest.new + test.a = 10 + expect(test.a).to eq 10 + end + + it "The deprecated_attr_reader can be read from" do + test = DeprecatedAttrTest.new + expect(test.r).to eq 1 + end + + it "The deprecated_attr_writer can be written to" do + test = DeprecatedAttrTest.new + test.w = 10 + expect(test.instance_eval { @w }).to eq 10 + end + end + end + end |