From 941177a4902f4bfe7b8b3f528ce6648752f5409c Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Mon, 7 Apr 2008 23:54:58 -0500 Subject: Changing how destroy works, just a bit -- it now accepts the name of the instance to be destroyed, rather than the instance itself. --- lib/puppet/indirector/file.rb | 13 ++++++++----- lib/puppet/indirector/indirection.rb | 2 ++ lib/puppet/indirector/memory.rb | 6 +++--- spec/unit/indirector/file.rb | 20 ++++++++------------ spec/unit/indirector/memory.rb | 2 +- 5 files changed, 22 insertions(+), 21 deletions(-) diff --git a/lib/puppet/indirector/file.rb b/lib/puppet/indirector/file.rb index c2d36c46b..8c984154b 100644 --- a/lib/puppet/indirector/file.rb +++ b/lib/puppet/indirector/file.rb @@ -2,21 +2,23 @@ require 'puppet/indirector/terminus' # An empty terminus type, meant to just return empty objects. class Puppet::Indirector::File < Puppet::Indirector::Terminus - def destroy(file) + # Remove files on disk. + def destroy(name) if respond_to?(:path) - path = path(file.name) + path = path(name) else - path = file.path + path = name end - raise Puppet::Error.new("File %s does not exist; cannot destroy" % [file]) unless File.exist?(path) + raise Puppet::Error.new("File %s does not exist; cannot destroy" % [name]) unless File.exist?(path) begin File.unlink(path) rescue => detail - raise Puppet::Error, "Could not remove %s: %s" % [file, detail] + raise Puppet::Error, "Could not remove %s: %s" % [name, detail] end end + # Return a model instance for a given file on disk. def find(name) if respond_to?(:path) path = path(name) @@ -35,6 +37,7 @@ class Puppet::Indirector::File < Puppet::Indirector::Terminus return model.new(content) end + # Save a new file to disk. def save(file) if respond_to?(:path) path = path(file.name) diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 91cc42b17..6f1497319 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -211,6 +211,7 @@ class Puppet::Indirector::Indirection return nil end + # Remove something via the terminus. def destroy(key, *args) check_authorization(:destroy, terminus_class, ([key] + args)) @@ -223,6 +224,7 @@ class Puppet::Indirector::Indirection nil end + # Search for more than one instance. Should always return an array. def search(*args) check_authorization(:search, terminus_class, args) diff --git a/lib/puppet/indirector/memory.rb b/lib/puppet/indirector/memory.rb index 5bfcec95d..b97e6ffb6 100644 --- a/lib/puppet/indirector/memory.rb +++ b/lib/puppet/indirector/memory.rb @@ -6,9 +6,9 @@ class Puppet::Indirector::Memory < Puppet::Indirector::Terminus @instances = {} end - def destroy(instance) - raise ArgumentError.new("Could not find %s to destroy" % instance) unless @instances.include?(instance.name) - @instances.delete(instance.name) + def destroy(name) + raise ArgumentError.new("Could not find %s to destroy" % name) unless @instances.include?(name) + @instances.delete(name) end def find(name) diff --git a/spec/unit/indirector/file.rb b/spec/unit/indirector/file.rb index a2aa4502d..fa10743ef 100755 --- a/spec/unit/indirector/file.rb +++ b/spec/unit/indirector/file.rb @@ -121,38 +121,34 @@ describe Puppet::Indirector::File do describe Puppet::Indirector::File, " when removing files" do it "should provide a method to remove files at a specified path" do - file = stub 'file', :path => @path, :name => @path File.expects(:exist?).with(@path).returns(true) File.expects(:unlink).with(@path) - @searcher.destroy(file) + @searcher.destroy(@path) end it "should throw an exception if the file is not found" do - file = stub 'file', :path => @path, :name => @path File.expects(:exist?).with(@path).returns(false) - proc { @searcher.destroy(file) }.should raise_error(Puppet::Error) + proc { @searcher.destroy(@path) }.should raise_error(Puppet::Error) end it "should fail intelligently if the file cannot be removed" do - file = stub 'file', :path => @path, :name => @path File.expects(:exist?).with(@path).returns(true) File.expects(:unlink).with(@path).raises(ArgumentError) - proc { @searcher.destroy(file) }.should raise_error(Puppet::Error) + proc { @searcher.destroy(@path) }.should raise_error(Puppet::Error) end it "should use the path() method to calculate the path if it exists" do - @searcher.meta_def(:path) do |name| - name.upcase + @searcher.meta_def(:path) do |thing| + thing.to_s.upcase end - file = stub 'file', :name => "/yay" - File.expects(:exist?).with("/YAY").returns(true) - File.expects(:unlink).with("/YAY") + File.expects(:exist?).with("/MY/FILE").returns(true) + File.expects(:unlink).with("/MY/FILE") - @searcher.destroy(file) + @searcher.destroy(@path) end end end diff --git a/spec/unit/indirector/memory.rb b/spec/unit/indirector/memory.rb index c0fca6bd9..2e9a7f8f3 100755 --- a/spec/unit/indirector/memory.rb +++ b/spec/unit/indirector/memory.rb @@ -22,7 +22,7 @@ describe "A Memory Terminus", :shared => true do it "should be able to remove previously saved instances" do @searcher.save(@instance) - @searcher.destroy(@instance) + @searcher.destroy(@instance.name) @searcher.find(@name).should be_nil end -- cgit