issue a warning when Depot.gc! is unable to read a file
authorLunar <lunar@anargeek.net>
Fri, 9 Mar 2012 16:36:22 +0000 (17:36 +0100)
committerLunar <lunar@anargeek.net>
Thu, 14 Mar 2013 09:12:08 +0000 (10:12 +0100)
lib/coquelicot/depot.rb
lib/coquelicot/stored_file.rb
spec/coquelicot/depot_spec.rb
spec/coquelicot/stored_file_spec.rb

index a78be56..61f868b 100644 (file)
@@ -75,7 +75,12 @@ module Coquelicot
           next
         end
         if File.lstat(path).size > 0
-          file = StoredFile::open path
+          begin
+            file = StoredFile::open path
+          rescue ArgumentError
+            $stderr.puts "W: #{path} is not a Coquelicot file. Skipping."
+            next
+          end
           file.empty! if file.expired?
         end
         if Time.now - File.lstat(path).mtime > (Coquelicot.settings.gone_period * 60)
index c3820a6..7b53864 100644 (file)
@@ -166,7 +166,7 @@ module Coquelicot
       end
 
       if YAML_START != (buf = @file.read(YAML_START.length)) then
-        raise "unknown file, read #{buf.inspect}"
+        raise ArgumentError.new("unknown file, read #{buf.inspect}")
       end
       parse_clear_meta
       return if pass.nil?
@@ -185,7 +185,7 @@ module Coquelicot
       @meta = YAML.load(meta)
       @features = COQUELICOT_FEATURES[@meta['Coquelicot']]
       unless @features
-        raise 'unknown file'
+        raise ArgumentError.new('unknown file')
       end
       @expire_at = Time.at(@meta['Expire-at'])
     end
index 44f3379..cca3abe 100644 (file)
@@ -248,6 +248,28 @@ module Coquelicot
           expect { depot.gc! }.to change { depot.size }.from(1).to(0)
         end
       end
+      context 'when there is a corrupted file' do
+        before(:each) do
+          depot.should_receive(:gen_random_file_name).
+            and_return('file', 'link')
+          add_file
+          @file_path = File.expand_path('file', @tmpdir)
+          File.open(@file_path, 'w') do |f|
+            f.write('gibberish')
+          end
+        end
+        it 'should print a warning on stderr' do
+          stderr = capture(:stderr) do
+            depot.gc!
+          end
+          stderr.should =~ /^W: #{@file_path} is not a Coquelicot file\. Skipping\./
+        end
+        it 'should not remove files' do
+          capture(:stderr) do
+            expect { depot.gc! }.to_not change { Dir.entries(@tmpdir) }
+          end
+        end
+      end
     end
 
     describe '#size' do
index 2a7c460..fbd199a 100644 (file)
@@ -113,8 +113,7 @@ module Coquelicot
         it 'should raise an error' do
           expect {
             StoredFile.open(__FILE__)
-          }.to raise_error
-          # XXX: make this an ArgumentError
+          }.to raise_error(ArgumentError)
         end
       end
       context 'when giving no pass' do