From c97046186d528adb43730a6806a6c1c781dc1b72 Mon Sep 17 00:00:00 2001 From: Serdar Sutay Date: Wed, 13 Aug 2014 14:22:08 -0700 Subject: Merge pull request #1812 from opscode/sersut/chef-1773 Name :freebsd_package resource `package` to provide compat with ChefSpec. --- lib/chef/dsl/recipe.rb | 14 ++++++++++++++ lib/chef/resource/freebsd_package.rb | 12 ++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/chef/dsl/recipe.rb b/lib/chef/dsl/recipe.rb index 6846703f09..46788c046b 100644 --- a/lib/chef/dsl/recipe.rb +++ b/lib/chef/dsl/recipe.rb @@ -84,6 +84,20 @@ class Chef resource = build_resource(type, name, created_at, &resource_attrs_block) + # Some resources (freebsd_package) can be invoked with multiple names + # (package || freebsd_package). + # https://github.com/opscode/chef/issues/1773 + # For these resources we want to make sure + # their key in resource collection is same as the name they are declared + # as. Since this might be a breaking change for resources that define + # customer to_s methods, we are working around the issue by letting + # resources know of their created_as_type until this issue is fixed in + # Chef 12: + # https://github.com/opscode/chef/issues/1817 + if resource.respond_to?(:created_as_type=) + resource.created_as_type = type + end + run_context.resource_collection.insert(resource) resource end diff --git a/lib/chef/resource/freebsd_package.rb b/lib/chef/resource/freebsd_package.rb index c7a8d44181..70ef62ae8a 100644 --- a/lib/chef/resource/freebsd_package.rb +++ b/lib/chef/resource/freebsd_package.rb @@ -31,17 +31,26 @@ class Chef provides :package, :on_platforms => ["freebsd"] + attr_accessor :created_as_type def initialize(name, run_context=nil) super @resource_name = :freebsd_package + @created_as_type = "freebsd_package" end def after_created assign_provider end - + # This resource can be invoked with multiple names package & freebsd_package. + # We override the to_s method to ensure the key in resource collection + # matches the type resource is declared as using created_as_type. This + # logic can be removed once Chef does this for all resource in Chef 12: + # https://github.com/opscode/chef/issues/1817 + def to_s + "#{created_as_type}[#{name}]" + end private @@ -68,4 +77,3 @@ class Chef end end end - -- cgit v1.2.1