summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-09-22 22:50:38 -0700
committerJohn Keiser <john@johnkeiser.com>2015-09-22 22:50:38 -0700
commit5a4bf945406e80e107e934d1506bc61e42cf1675 (patch)
tree226bda12feeb960b147177a5150d1efee731bbc0
parent0d93133c31704711b2bb6a20db74a83dc1b23493 (diff)
parent71fc5c5650f8bd3ec819087d6a5ca7f4eaeb1158 (diff)
downloadchef-5a4bf945406e80e107e934d1506bc61e42cf1675.tar.gz
Merge branch 'jk/default-ignoring'
-rw-r--r--lib/chef/property.rb17
-rw-r--r--lib/chef/resource.rb2
-rw-r--r--spec/unit/property_spec.rb25
3 files changed, 38 insertions, 6 deletions
diff --git a/lib/chef/property.rb b/lib/chef/property.rb
index 09198d90f1..7f42616bdd 100644
--- a/lib/chef/property.rb
+++ b/lib/chef/property.rb
@@ -86,7 +86,24 @@ class Chef
#
def initialize(**options)
options.each { |k,v| options[k.to_sym] = v if k.is_a?(String) }
+
+ # Only pick the first of :default, :name_property and :name_attribute if
+ # more than one is specified.
+ found_defaults = []
+ options.reject! do |k,v|
+ if [ :name_property, :name_attribute, :default ].include?(k)
+ found_defaults << k
+ # Reject all but the first default key you find
+ found_defaults.size > 1
+ else
+ false
+ end
+ end
+ if found_defaults.size > 1
+ Chef::Log.deprecation("Cannot specify keys #{found_defaults.join(", ")} together on property #{options[:name]}--only the first one (#{found_defaults[0]}) will be obeyed. Please pick one.", caller(5..5)[0])
+ end
options[:name_property] = options.delete(:name_attribute) if options.has_key?(:name_attribute) && !options.has_key?(:name_property)
+
@options = options
options[:name] = options[:name].to_sym if options[:name]
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 6c2f91ed8b..9e40fdccfd 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -782,6 +782,8 @@ class Chef
def self.property(name, type=NOT_PASSED, **options)
name = name.to_sym
+ options.each { |k,v| options[k.to_sym] = v if k.is_a?(String) }
+
options[:instance_variable_name] = :"@#{name}" if !options.has_key?(:instance_variable_name)
options.merge!(name: name, declared_in: self)
diff --git a/spec/unit/property_spec.rb b/spec/unit/property_spec.rb
index f758b5f403..369c065aaa 100644
--- a/spec/unit/property_spec.rb
+++ b/spec/unit/property_spec.rb
@@ -946,14 +946,27 @@ describe "Chef::Resource.property" do
expect(resource.x).to eq 'blah'
end
end
- with_property ":x, default: 10, #{name}: true" do
- it "chooses default over #{name}" do
- expect(resource.x).to eq 10
+ context "default ordering deprecation warnings" do
+ it "emits a deprecation warning for property :x, default: 10, #{name}: true" do
+ expect { resource_class.property :x, :default => 10, name.to_sym => true }.to raise_error Chef::Exceptions::DeprecatedFeatureError,
+ /Cannot specify keys default, #{name} together on property x--only the first one \(default\) will be obeyed. Please pick one./
+ end
+ it "emits a deprecation warning for property :x, #{name}: true, default: 10" do
+ expect { resource_class.property :x, name.to_sym => true, :default => 10 }.to raise_error Chef::Exceptions::DeprecatedFeatureError,
+ /Cannot specify keys #{name}, default together on property x--only the first one \(#{name}\) will be obeyed. Please pick one./
end
end
- with_property ":x, #{name}: true, default: 10" do
- it "chooses default over #{name}" do
- expect(resource.x).to eq 10
+ context "default ordering" do
+ before { Chef::Config[:treat_deprecation_warnings_as_errors] = false }
+ with_property ":x, default: 10, #{name}: true" do
+ it "chooses default over #{name}" do
+ expect(resource.x).to eq 10
+ end
+ end
+ with_property ":x, #{name}: true, default: 10" do
+ it "chooses #{name} over default" do
+ expect(resource.x).to eq 'blah'
+ end
end
end
with_property ":x, #{name}: true, required: true" do