spec and implement coquelicot-collect-garbage
authorLunar <lunar@anargeek.net>
Fri, 9 Mar 2012 14:18:07 +0000 (15:18 +0100)
committerLunar <lunar@anargeek.net>
Thu, 14 Mar 2013 09:12:08 +0000 (10:12 +0100)
It is located in the bin/ directory and replaces the old ext/coquelicot_gc.rb.

README
bin/coquelicot-collect-garbage [moved from ext/coquelicot_gc.rb with 79% similarity]
lib/coquelicot/app.rb
spec/coquelicot/app_spec.rb
spec/spec_helper.rb

diff --git a/README b/README
index 71395c8..2f0c13c 100644 (file)
--- a/README
+++ b/README
@@ -157,8 +157,8 @@ them according to your environment.
 
 To cleanup files automatically when they expired, coquelicot comes with
 a cleanup script, that does the garbage collection for you. The easiest
-way is to add `ext/coquelicot_gc.rb` as a cron job that runs every 5
-minutes (or so).
+way is to run `bin/coquelicot-collect-gabage` as a cron job that runs
+every 5 minutes (or so).
 
 ### Migrate from Jyraphe
 
similarity index 79%
rename from ext/coquelicot_gc.rb
rename to bin/coquelicot-collect-garbage
index 8461164..dee0737 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/env ruby
+#!/usr/bin/env ruby
 # Coquelicot: "one-click" file sharing with a focus on users' privacy.
 # Copyright © 2012 potager.org <jardiniers@potager.org>
 #           © 2011 mh / immerda.ch <mh+coquelicot@immerda.ch>
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-$: << File.expand_path(File.join(File.dirname(__FILE__),'..','lib')) << File.expand_path(File.join(File.dirname(__FILE__),'..'))
-require 'bundler/setup'
+require 'rubygems'
+require 'bundler'
+Bundler.setup
+
 require 'coquelicot'
 
-Coquelicot::Depot.new(Coquelicot.settings.depot_path).gc!
+Coquelicot.collect_garbage!(ARGV)
index f8ab01d..e0b55c7 100644 (file)
@@ -26,6 +26,7 @@ require 'fast_gettext'
 require 'upr'
 require 'moneta/memory'
 require 'rainbows'
+require 'optparse'
 
 module Coquelicot
   class << self
@@ -36,6 +37,25 @@ module Coquelicot
       @depot = Depot.new(settings.depot_path) if @depot.nil? || settings.depot_path != @depot.path
       @depot
     end
+    # Called by +coquelicot-collect-garbage+ script.
+    def collect_garbage!(args = [])
+      parser ||= OptionParser.new do |opts|
+        opts.banner = "Usage: #{$0} [options]"
+
+        opts.separator ""
+        opts.separator "Options:"
+
+        opts.on "-c", "--config FILE", "read settings from FILE" do |file|
+          settings.config_file file
+        end
+        opts.on_tail("-h", "--help", "show this message") do
+          $stderr.puts opts.to_s
+          exit
+        end
+      end
+      parser.parse!(args)
+      depot.gc!
+    end
   end
 
   class Application < Sinatra::Base
index 834fd78..9429897 100644 (file)
@@ -16,6 +16,7 @@
 
 require 'spec_helper'
 require 'capybara/dsl'
+require 'tempfile'
 
 describe Coquelicot::Application do
   include Rack::Test::Methods
@@ -57,3 +58,63 @@ describe Coquelicot::Application do
     end
   end
 end
+
+describe Coquelicot, '.collect_garbage!' do
+  context 'when given no option' do
+    include_context 'with Coquelicot::Application'
+
+    it 'should use the default depot path' do
+      Coquelicot::Depot.should_receive(:new).
+        with(@depot_path).
+        and_return(double.as_null_object)
+      Coquelicot.collect_garbage!
+    end
+    it 'should call gc!' do
+      depot = double('Depot').as_null_object
+      depot.should_receive(:gc!)
+      Coquelicot::Depot.stub(:new).and_return(depot)
+      Coquelicot.collect_garbage!
+    end
+  end
+  context 'when using "-c <settings.yml>"' do
+    around(:each) do |example|
+      settings = Tempfile.new('coquelicot')
+      begin
+        settings.write(YAML.dump({ 'depot_path' => '/nonexistent' }))
+        settings.close
+        @settings_path = settings.path
+        example.run
+      ensure
+        settings.unlink
+      end
+    end
+    it 'should use the depot path defined in the given settings' do
+      Coquelicot::Depot.should_receive(:new).
+        with('/nonexistent').
+        and_return(double.as_null_object)
+      Coquelicot.collect_garbage! ['-c', @settings_path]
+    end
+    it 'should call gc!' do
+      depot = double('Depot').as_null_object
+      depot.should_receive(:gc!)
+      Coquelicot::Depot.stub(:new).and_return(depot)
+      Coquelicot.collect_garbage! ['-c', @settings_path]
+    end
+  end
+  context 'when using "-h"' do
+    it 'should display help and exit' do
+      stderr = capture(:stderr) do
+        expect { Coquelicot.collect_garbage! ['-h'] }.to raise_error(SystemExit)
+      end
+      stderr.should =~ /Usage:/
+    end
+    it 'should not call gc!' do
+      depot = double('Depot').as_null_object
+      depot.should_not_receive(:gc!)
+      Coquelicot::Depot.stub(:new).and_return(depot)
+      capture(:stderr) do
+        expect { Coquelicot.collect_garbage! ['-h'] }.to raise_error(SystemExit)
+      end
+    end
+  end
+end
index 9e30e28..c394b23 100644 (file)
@@ -39,6 +39,7 @@ shared_context 'with Coquelicot::Application' do
   around(:each) do |example|
     path = Dir.mktmpdir('coquelicot')
     begin
+      @depot_path = path
       app.set :depot_path, path
       example.run
     ensure