remove links in Depot#gc! when there is a link but no associated file
authorLunar <lunar@anargeek.net>
Tue, 6 Mar 2012 16:03:47 +0000 (17:03 +0100)
committerLunar <lunar@anargeek.net>
Fri, 23 Mar 2012 17:11:29 +0000 (18:11 +0100)
lib/coquelicot/depot.rb
spec/coquelicot/depot_spec.rb

index 4c0dbaf..9809101 100644 (file)
@@ -70,6 +70,10 @@ module Coquelicot
     def gc!
       files.each do |name|
         path = full_path(name)
+        unless File.exists?(path)
+          remove_from_links { |l| l.strip.end_with? " #{name}" }
+          next
+        end
         if File.lstat(path).size > 0
           file = StoredFile::open path
           file.empty! if file.expired?
index 6b67f10..ffa5e55 100644 (file)
@@ -237,6 +237,17 @@ module Coquelicot
           end
         end
       end
+      context 'when there is a link but no associated file' do
+        before(:each) do
+          depot.should_receive(:gen_random_file_name).
+            and_return('file', 'link')
+          add_file
+          File.unlink File.expand_path('file', @tmpdir)
+        end
+        it 'should remove the link' do
+          expect { depot.gc! }.to change { depot.size }.from(1).to(0)
+        end
+      end
     end
 
     describe '#size' do