diff options
author | Noah Kantrowitz <noah@coderanger.net> | 2016-07-28 22:09:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-28 22:09:05 -0700 |
commit | 23de6b876497a1a881d836700513eda3e85a1b59 (patch) | |
tree | 5099f7a12eee0a21afccfa7a4583e591df59c102 | |
parent | c5dc0ead11b566031e2183f549a74882e53610f3 (diff) | |
parent | 6a5ad935e3790a2098107594715d075f2fd39d9b (diff) | |
download | chef-23de6b876497a1a881d836700513eda3e85a1b59.tar.gz |
Merge pull request #5146 from coderanger/delete-notifications
Also clear notifications when deleting a resource.
-rw-r--r-- | lib/chef/dsl/declare_resource.rb | 10 | ||||
-rw-r--r-- | spec/unit/dsl/declare_resource_spec.rb | 30 |
2 files changed, 39 insertions, 1 deletions
diff --git a/lib/chef/dsl/declare_resource.rb b/lib/chef/dsl/declare_resource.rb index 8d76ddfb31..86227a0f9d 100644 --- a/lib/chef/dsl/declare_resource.rb +++ b/lib/chef/dsl/declare_resource.rb @@ -71,7 +71,15 @@ class Chef # delete_resource!(:template, '/x/y.txy') # def delete_resource!(type, name, run_context: self.run_context) - run_context.resource_collection.delete("#{type}[#{name}]") + run_context.resource_collection.delete("#{type}[#{name}]").tap do |resource| + # Purge any pending notifications too. This will not raise an exception + # if there are no notifications. + if resource + run_context.before_notification_collection.delete(resource.declared_key) + run_context.immediate_notification_collection.delete(resource.declared_key) + run_context.delayed_notification_collection.delete(resource.declared_key) + end + end end # Lookup a resource in the resource collection by name and delete it. Returns diff --git a/spec/unit/dsl/declare_resource_spec.rb b/spec/unit/dsl/declare_resource_spec.rb index 6dd9317c21..692e57d2e1 100644 --- a/spec/unit/dsl/declare_resource_spec.rb +++ b/spec/unit/dsl/declare_resource_spec.rb @@ -235,6 +235,36 @@ describe Chef::ResourceCollection do ).to eql(resource) expect(run_context.resource_collection.all_resources.size).to eql(0) end + + it "removes pending delayed notifications" do + recipe.declare_resource(:zen_master, "one") + recipe.declare_resource(:zen_master, "two") do + notifies :win, "zen_master[one]" + end + recipe.delete_resource(:zen_master, "two") + resource = recipe.declare_resource(:zen_master, "two") + expect(resource.delayed_notifications).to eql([]) + end + + it "removes pending immediate notifications" do + recipe.declare_resource(:zen_master, "one") + recipe.declare_resource(:zen_master, "two") do + notifies :win, "zen_master[one]", :immediate + end + recipe.delete_resource(:zen_master, "two") + resource = recipe.declare_resource(:zen_master, "two") + expect(resource.immediate_notifications).to eql([]) + end + + it "removes pending before notifications" do + recipe.declare_resource(:zen_master, "one") + recipe.declare_resource(:zen_master, "two") do + notifies :win, "zen_master[one]", :before + end + recipe.delete_resource(:zen_master, "two") + resource = recipe.declare_resource(:zen_master, "two") + expect(resource.before_notifications).to eql([]) + end end describe "run_context helpers" do |