To not blindly remove files and therefore maybe getting an exception
we check first on the existance of the file.

Signed-off-by: Peter Meier <[email protected]>
---
 lib/puppet/indirector/yaml.rb     |    3 ++-
 spec/unit/indirector/yaml_spec.rb |   16 +++++++++++-----
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/lib/puppet/indirector/yaml.rb b/lib/puppet/indirector/yaml.rb
index 46fdc24..7b12d25 100644
--- a/lib/puppet/indirector/yaml.rb
+++ b/lib/puppet/indirector/yaml.rb
@@ -48,7 +48,8 @@ class Puppet::Indirector::Yaml < Puppet::Indirector::Terminus
   end
 
   def destroy(request)
-    File.unlink(path(request.key))
+    file_path = path(request.key)
+    File.unlink(file_path) if File.exists?(file_path)
   end
 
   def search(request)
diff --git a/spec/unit/indirector/yaml_spec.rb 
b/spec/unit/indirector/yaml_spec.rb
index 605385e..38fa602 100755
--- a/spec/unit/indirector/yaml_spec.rb
+++ b/spec/unit/indirector/yaml_spec.rb
@@ -157,15 +157,21 @@ describe Puppet::Indirector::Yaml, " when choosing file 
location" do
 
 
     describe Puppet::Indirector::Yaml, " when destroying" do
-      it "should unlink the right yaml file" do
-        @request = stub 'request', :key => "one*", :instance => @subject
-        @one = mock 'one'
-        @store.stubs(:base).returns "/my/yaml/dir"
+      it "should unlink the right yaml file if it exists" do
+        path = File.join("/what/ever", @store.class.indirection_name.to_s, 
@request.key.to_s + ".yaml")
+        File.expects(:exists?).with(path).returns true
+        File.expects(:unlink).with(path)
 
-        File.expects(:unlink).with(File.join("/my/yaml/dir", 
@store.class.indirection_name.to_s, @request.key + ".yaml"))
+        @store.destroy(@request)
+      end
+      it "should not unlink the yaml file if it does not exists" do
+        path = File.join("/what/ever", @store.class.indirection_name.to_s, 
@request.key.to_s + ".yaml")
+        File.expects(:exists?).with(path).returns false
+        File.expects(:unlink).with(path).never
 
         @store.destroy(@request)
       end
+
     end
 
   end
-- 
1.7.2.3

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to