Permalink
Browse files

Compressor Updates

- Add a Custom Compressor
  - Deprecate the Lzma and Pbzip2 Compressor
- Add a #level setting for Gzip and Bzip2
  - Deprecate the #best and #fast settings
- Ensure Compressor is valid when initialized
  • Loading branch information...
1 parent 9a93acb commit 2c70c819f6e142a02230570042b252c9a21a7f2d Brian D. Burns committed Mar 18, 2012
View
1 lib/backup.rb
@@ -83,6 +83,7 @@ module Compressor
autoload :Base, File.join(COMPRESSOR_PATH, 'base')
autoload :Gzip, File.join(COMPRESSOR_PATH, 'gzip')
autoload :Bzip2, File.join(COMPRESSOR_PATH, 'bzip2')
+ autoload :Custom, File.join(COMPRESSOR_PATH, 'custom')
autoload :Pbzip2, File.join(COMPRESSOR_PATH, 'pbzip2')
autoload :Lzma, File.join(COMPRESSOR_PATH, 'lzma')
end
View
4 lib/backup/cli/helpers.rb
@@ -70,11 +70,9 @@ def utility(name)
path = UTILITY[name] || %x[which #{ name } 2>/dev/null].chomp
if path.empty?
raise Errors::CLI::UtilityNotFoundError, <<-EOS
- Path to '#{ name }' could not be found.
+ Could not locate '#{ name }'.
Make sure the specified utility is installed
and available in your system's $PATH.
- If this is a database utility, you may need to specify the full path
- using the Database's '<utility_name>_utility' configuration setting.
EOS
end
UTILITY[name] = path
View
14 lib/backup/compressor/base.rb
@@ -6,14 +6,17 @@ class Base
include Backup::CLI::Helpers
include Backup::Configuration::Helpers
- def initialize
- load_defaults!
+ ##
+ # Yields to the block the compressor command and filename extension.
+ def compress_with
+ log!
+ yield @cmd, @ext
end
private
##
- # Return the encryptor name, with Backup namespace removed
+ # Return the compressor name, with Backup namespace removed
def compressor_name
self.class.to_s.sub('Backup::', '')
end
@@ -22,8 +25,11 @@ def compressor_name
# Logs a message to the console and log file to inform
# the client that Backup is using the compressor
def log!
- Logger.message "Using #{ compressor_name } for compression."
+ Logger.message "Using #{ compressor_name } for compression.\n" +
+ " Command: '#{ @cmd }'\n" +
+ " Ext: '#{ @ext }'"
end
+
end
end
end
View
48 lib/backup/compressor/bzip2.rb
@@ -5,44 +5,40 @@ module Compressor
class Bzip2 < Base
##
- # Tells Backup::Compressor::Bzip2 to compress
- # better (-9) rather than faster when set to true
- attr_accessor :best
+ # Specify the level of compression to use.
+ #
+ # Values should be a single digit from 1 to 9.
+ # Note that setting the level to either extreme may or may not
+ # give the desired result. Be sure to check the documentation
+ # for the compressor being used.
+ #
+ # The default `level` is 9.
+ attr_accessor :level
+
+ attr_deprecate :fast, :version => '3.0.24',
+ :replacement => :level,
+ :value => lambda {|val| val ? 1 : nil }
+ attr_deprecate :best, :version => '3.0.24',
+ :replacement => :level,
+ :value => lambda {|val| val ? 9 : nil }
##
- # Tells Backup::Compressor::Bzip2 to compress
- # faster (-1) rather than better when set to true
- attr_accessor :fast
-
- ##
- # Creates a new instance of Backup::Compressor::Bzip2 and
- # configures it to either compress faster or better
- # bzip2 compresses by default with -9 (best compression)
- # and lower block sizes don't make things significantly faster
- # (according to official bzip2 docs)
+ # Creates a new instance of Backup::Compressor::Bzip2
def initialize(&block)
- super
+ load_defaults!
- @best ||= false
- @fast ||= false
+ @level ||= false
instance_eval(&block) if block_given?
- end
- ##
- # Yields to the block the compressor command with options
- # and it's filename extension.
- def compress_with
- log!
- yield "#{ utility(:bzip2) }#{ options }", '.bz2'
+ @cmd = "#{ utility(:bzip2) }#{ options }"
+ @ext = '.bz2'
end
private
- ##
- # Returns the option syntax for compressing
def options
- " #{ '--best ' if @best }#{ '--fast' if @fast }".rstrip
+ " -#{ @level }" if @level
end
end
View
53 lib/backup/compressor/custom.rb
@@ -0,0 +1,53 @@
+# encoding: utf-8
+
+module Backup
+ module Compressor
+ class Custom < Base
+
+ ##
+ # Specify the system command to invoke a compressor,
+ # including any command-line arguments.
+ # e.g. @compressor.command = 'pbzip2 -p2 -4'
+ #
+ # The data to be compressed will be piped to the command's STDIN,
+ # and it should write the compressed data to STDOUT.
+ # i.e. `cat file.tar | %command% > file.tar.%extension%`
+ attr_accessor :command
+
+ ##
+ # File extension to append to the compressed file's filename.
+ # e.g. @compressor.extension = '.bz2'
+ attr_accessor :extension
+
+ ##
+ # Initializes a new custom compressor.
+ def initialize(&block)
+ load_defaults!
+
+ instance_eval(&block) if block_given?
+
+ @cmd = set_cmd
+ @ext = set_ext
+ end
+
+ private
+
+ ##
+ # Return the command line using the full path.
+ # Ensures the command exists and is executable.
+ def set_cmd
+ parts = @command.to_s.split(' ')
+ parts[0] = utility(parts[0])
+ parts.join(' ')
+ end
+
+ ##
+ # Return the extension given without whitespace.
+ # If extension was not set, return an empty string
+ def set_ext
+ @extension.to_s.strip
+ end
+
+ end
+ end
+end
View
45 lib/backup/compressor/gzip.rb
@@ -5,41 +5,40 @@ module Compressor
class Gzip < Base
##
- # Tells Backup::Compressor::Gzip to compress
- # better rather than faster when set to true
- attr_accessor :best
+ # Specify the level of compression to use.
+ #
+ # Values should be a single digit from 1 to 9.
+ # Note that setting the level to either extreme may or may not
+ # give the desired result. Be sure to check the documentation
+ # for the compressor being used.
+ #
+ # The default `level` is 6.
+ attr_accessor :level
+
+ attr_deprecate :fast, :version => '3.0.24',
+ :replacement => :level,
+ :value => lambda {|val| val ? 1 : nil }
+ attr_deprecate :best, :version => '3.0.24',
+ :replacement => :level,
+ :value => lambda {|val| val ? 9 : nil }
##
- # Tells Backup::Compressor::Gzip to compress
- # faster rather than better when set to true
- attr_accessor :fast
-
- ##
- # Creates a new instance of Backup::Compressor::Gzip and
- # configures it to either compress faster or better
+ # Creates a new instance of Backup::Compressor::Gzip
def initialize(&block)
- super
+ load_defaults!
- @best ||= false
- @fast ||= false
+ @level ||= false
instance_eval(&block) if block_given?
- end
- ##
- # Yields to the block the compressor command with options
- # and it's filename extension.
- def compress_with
- log!
- yield "#{ utility(:gzip) }#{ options }", '.gz'
+ @cmd = "#{ utility(:gzip) }#{ options }"
+ @ext = '.gz'
end
private
- ##
- # Returns the gzip option syntax for compressing
def options
- " #{ '--best ' if @best }#{ '--fast' if @fast }".rstrip
+ " -#{ @level }" if @level
end
end
View
28 lib/backup/compressor/lzma.rb
@@ -15,34 +15,36 @@ class Lzma < Base
attr_accessor :fast
##
- # Creates a new instance of Backup::Compressor::Lzma and
- # configures it to either compress faster or better
- # Lzma compresses by default with -9 (best compression)
- # and lower block sizes don't make things significantly faster
- # (according to official bzip2 docs)
+ # Creates a new instance of Backup::Compressor::Lzma
def initialize(&block)
- super
+ load_defaults!
@best ||= false
@fast ||= false
instance_eval(&block) if block_given?
+
+ @cmd = "#{ utility(:lzma) }#{ options }"
+ @ext = '.lzma'
end
+
##
- # Yields to the block the compressor command with options
- # and it's filename extension.
+ # Yields to the block the compressor command and filename extension.
def compress_with
- log!
- yield "#{ utility(:lzma) }#{ options }", '.lzma'
+ Backup::Logger.warn(
+ "[DEPRECATION WARNING]\n" +
+ " Compressor::Lzma is being deprecated as of backup v.3.0.24\n" +
+ " and will soon be removed. Please see the Compressors wiki page at\n" +
+ " https://github.com/meskyanichi/backup/wiki/Compressors"
+ )
+ super
end
private
- ##
- # Returns the option syntax for compressing
def options
- " #{ '--best ' if @best }#{ '--fast' if @fast }".rstrip
+ (' --best' if @best) || (' --fast' if @fast)
end
end
View
37 lib/backup/compressor/pbzip2.rb
@@ -5,50 +5,53 @@ module Compressor
class Pbzip2 < Base
##
- # Tells Backup::Compressor::Pbzip2 to compress
+ # Tells Backup::Compressor::Lzma to compress
# better (-9) rather than faster when set to true
attr_accessor :best
##
- # Tells Backup::Compressor::Pbzip2 to compress
+ # Tells Backup::Compressor::Lzma to compress
# faster (-1) rather than better when set to true
attr_accessor :fast
##
- # Tells Backup::Compressor::Pbzip2 how many processors
- # use, by default autodetect is used
+ # Tells Backup::Compressor::Pbzip2 how many processors to use.
+ # Autodetects the number of active CPUs by default.
attr_accessor :processors
##
- # Creates a new instance of Backup::Compressor::Pbzip2 and
- # configures it to either compress faster or better
- # bzip2 compresses by default with -9 (best compression)
- # and lower block sizes don't make things significantly faster
- # (according to official bzip2 docs)
+ # Creates a new instance of Backup::Compressor::Pbzip2
def initialize(&block)
- super
+ load_defaults!
@best ||= false
@fast ||= false
@processors ||= false
instance_eval(&block) if block_given?
+
+ @cmd = "#{ utility(:pbzip2) }#{ options }"
+ @ext = '.bz2'
end
##
- # Yields to the block the compressor command with options
- # and it's filename extension.
+ # Yields to the block the compressor command and filename extension.
def compress_with
- log!
- yield "#{ utility(:pbzip2) }#{ options }", '.bz2'
+ Backup::Logger.warn(
+ "[DEPRECATION WARNING]\n" +
+ " Compressor::Pbzip2 is being deprecated as of backup v.3.0.24\n" +
+ " and will soon be removed. Please see the Compressors wiki page at\n" +
+ " https://github.com/meskyanichi/backup/wiki/Compressors"
+ )
+ super
end
private
- ##
- # Returns the gzip option syntax for compressing
def options
- " #{ '--best ' if @best }#{ '--fast ' if @fast }#{ "-p#{@processors}" if @processors }".rstrip
+ level = (' --best' if @best) || (' --fast' if @fast)
+ cpus = " -p#{ @processors }" if @processors
+ "#{ level }#{ cpus }"
end
end
View
2 lib/backup/config.rb
@@ -107,7 +107,7 @@ def add_dsl_constants!
['S3', 'CloudFiles', 'Ninefold', 'Dropbox', 'FTP',
'SFTP', 'SCP', 'RSync', 'Local'],
# Compressors
- ['Gzip', 'Bzip2', 'Pbzip2', 'Lzma'],
+ ['Gzip', 'Bzip2', 'Custom', 'Pbzip2', 'Lzma'],
# Encryptors
['OpenSSL', 'GPG'],
# Syncers
View
6 spec/cli/helpers_spec.rb
@@ -221,7 +221,7 @@
expect do
helpers.utility(:unknown)
end.to raise_error(Backup::Errors::CLI::UtilityNotFoundError) {|err|
- err.message.should match(/Path to 'unknown' could not be found/)
+ err.message.should match(/Could not locate 'unknown'/)
}
end
@@ -231,13 +231,13 @@
expect do
helpers.utility(:not_cached)
end.to raise_error(Backup::Errors::CLI::UtilityNotFoundError) {|err|
- err.message.should match(/Path to 'not_cached' could not be found/)
+ err.message.should match(/Could not locate 'not_cached'/)
}
expect do
helpers.utility(:not_cached)
end.to raise_error(Backup::Errors::CLI::UtilityNotFoundError) {|err|
- err.message.should match(/Path to 'not_cached' could not be found/)
+ err.message.should match(/Could not locate 'not_cached'/)
}
end
end
View
2 spec/cli/utility_spec.rb
@@ -194,7 +194,7 @@
[--storages=STORAGES] # (cloud_files, dropbox, ftp, local, ninefold, rsync, s3, scp, sftp)
[--syncers=SYNCERS] # (cloud_files, rsync_local, rsync_pull, rsync_push, s3)
[--encryptors=ENCRYPTORS] # (gpg, openssl)
- [--compressors=COMPRESSORS] # (bzip2, gzip, lzma, pbzip2)
+ [--compressors=COMPRESSORS] # (bzip2, custom, gzip, lzma, pbzip2)
[--notifiers=NOTIFIERS] # (campfire, hipchat, mail, presently, prowl, twitter)
[--archives]
[--splitter] # use `--no-splitter` to disable
View
31 spec/compressor/base_spec.rb
@@ -3,7 +3,7 @@
require File.expand_path('../../spec_helper.rb', __FILE__)
describe Backup::Compressor::Base do
- let(:base) { Backup::Compressor::Base.new }
+ let(:compressor) { Backup::Compressor::Base.new }
it 'should include CLI::Helpers' do
Backup::Compressor::Base.
@@ -15,27 +15,38 @@
include?(Backup::Configuration::Helpers).should be_true
end
- describe '#initialize' do
- it 'should load pre-configured defaults' do
- Backup::Compressor::Base.any_instance.expects(:load_defaults!)
- base
+ describe '#compress_with' do
+ it 'should yield the compressor command and extension' do
+ compressor.instance_variable_set(:@cmd, 'compressor command')
+ compressor.instance_variable_set(:@ext, 'compressor extension')
+
+ compressor.expects(:log!)
+
+ compressor.compress_with do |cmd, ext|
+ cmd.should == 'compressor command'
+ ext.should == 'compressor extension'
+ end
end
end
describe '#compressor_name' do
it 'should return class name with Backup namespace removed' do
- base.send(:compressor_name).should == 'Compressor::Base'
+ compressor.send(:compressor_name).should == 'Compressor::Base'
end
end
describe '#log!' do
it 'should log a message' do
- base.expects(:compressor_name).returns('Compressor Name')
+ compressor.instance_variable_set(:@cmd, 'compressor command')
+ compressor.instance_variable_set(:@ext, 'compressor extension')
+ compressor.expects(:compressor_name).returns('Compressor Name')
+
Backup::Logger.expects(:message).with(
- 'Using Compressor Name for compression.'
+ "Using Compressor Name for compression.\n" +
+ " Command: 'compressor command'\n" +
+ " Ext: 'compressor extension'"
)
- base.send(:log!)
+ compressor.send(:log!)
end
end
-
end
View
256 spec/compressor/bzip2_spec.rb
@@ -3,86 +3,236 @@
require File.expand_path('../../spec_helper.rb', __FILE__)
describe Backup::Compressor::Bzip2 do
- let(:compressor) { Backup::Compressor::Bzip2.new }
+ before do
+ Backup::Compressor::Bzip2.any_instance.stubs(:utility).returns('bzip2')
+ end
it 'should be a subclass of Compressor::Base' do
Backup::Compressor::Bzip2.
superclass.should == Backup::Compressor::Base
end
describe '#initialize' do
+ let(:compressor) { Backup::Compressor::Bzip2.new }
+
after { Backup::Compressor::Bzip2.clear_defaults! }
it 'should load pre-configured defaults' do
Backup::Compressor::Bzip2.any_instance.expects(:load_defaults!)
compressor
end
- it 'should use pre-configured defaults' do
- Backup::Compressor::Bzip2.defaults do |c|
- c.best = true
- c.fast = true
+ context 'when no pre-configured defaults have been set' do
+ it 'should use default values' do
+ compressor.level.should be_false
+
+ compressor.instance_variable_get(:@cmd).should == 'bzip2'
+ compressor.instance_variable_get(:@ext).should == '.bz2'
end
- compressor.best.should be_true
- compressor.fast.should be_true
- end
- it 'should set defaults when no pre-configured defaults are set' do
- compressor.best.should be_false
- compressor.fast.should be_false
- end
+ it 'should use the values given' do
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.level = 5
+ end
+ compressor.level.should == 5
- it 'should override pre-configured defaults' do
- Backup::Compressor::Bzip2.defaults do |c|
- c.best = true
- c.fast = true
+ compressor.instance_variable_get(:@cmd).should == 'bzip2 -5'
+ compressor.instance_variable_get(:@ext).should == '.bz2'
end
- compressor = Backup::Compressor::Bzip2.new do |c|
- c.best = false
- c.fast = false
+ end # context 'when no pre-configured defaults have been set'
+
+ context 'when pre-configured defaults have been set' do
+ before do
+ Backup::Compressor::Bzip2.defaults do |c|
+ c.level = 7
+ end
end
- compressor.best.should be_false
- compressor.fast.should be_false
- end
- end # describe '#initialize'
- describe '#compress_with' do
- before do
- compressor.expects(:log!)
- compressor.expects(:utility).with(:bzip2).returns('bzip2')
- end
+ it 'should use pre-configured defaults' do
+ compressor.level.should == 7
- it 'should yield with the --best option' do
- compressor.best = true
- compressor.compress_with do |cmd, ext|
- cmd.should == 'bzip2 --best'
- ext.should == '.bz2'
+ compressor.instance_variable_get(:@cmd).should == 'bzip2 -7'
+ compressor.instance_variable_get(:@ext).should == '.bz2'
end
- end
- it 'should yield with the --fast option' do
- compressor.fast = true
- compressor.compress_with do |cmd, ext|
- cmd.should == 'bzip2 --fast'
- ext.should == '.bz2'
+ it 'should override pre-configured defaults' do
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.level = 6
+ end
+ compressor.level.should == 6
+
+ compressor.instance_variable_get(:@cmd).should == 'bzip2 -6'
+ compressor.instance_variable_get(:@ext).should == '.bz2'
end
- end
+ end # context 'when pre-configured defaults have been set'
+ end # describe '#initialize'
+
+ describe 'deprecations' do
+ describe 'fast and best options' do
+ context 'when only the fast option is used' do
+ before do
+ Backup::Logger.expects(:warn).with(
+ instance_of(Backup::Errors::ConfigurationError)
+ )
+ end
- it 'should yield with the --best and --fast options' do
- compressor.best = true
- compressor.fast = true
- compressor.compress_with do |cmd, ext|
- cmd.should == 'bzip2 --best --fast'
- ext.should == '.bz2'
+ context 'when set to true' do
+ it 'should log a warning and set `level` to 1' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Bzip2.level is being set to '1'"
+ )
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.fast = true
+ end
+ compressor.level.should be(1)
+ end
+ end
+
+ context 'when set to false' do
+ it 'should only log a warning' do
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.fast = false
+ end
+ compressor.level.should be_false
+ end
+ end
end
- end
- it 'should yield with no options' do
- compressor.compress_with do |cmd, ext|
- cmd.should == 'bzip2'
- ext.should == '.bz2'
+ context 'when only the best option is used' do
+ before do
+ Backup::Logger.expects(:warn).with(
+ instance_of(Backup::Errors::ConfigurationError)
+ )
+ end
+
+ context 'when set to true' do
+ it 'should log a warning and set `level` to 1' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Bzip2.level is being set to '9'"
+ )
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.best = true
+ end
+ compressor.level.should be(9)
+ end
+ end
+
+ context 'when set to false' do
+ it 'should only log a warning' do
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.best = false
+ end
+ compressor.level.should be_false
+ end
+ end
+
end
- end
- end # describe '#compress_with'
+ context 'when both fast and best options are used' do
+ before do
+ Backup::Logger.expects(:warn).twice.with(
+ instance_of(Backup::Errors::ConfigurationError)
+ )
+ end
+
+ context 'when both are set true' do
+ context 'when fast is set first' do
+ it 'should cause the best option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Bzip2.level is being set to '1'"
+ )
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Bzip2.level is being set to '9'"
+ )
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.fast = true
+ c.best = true
+ end
+ compressor.level.should == 9
+ end
+ end
+
+ context 'when best is set first' do
+ it 'should cause the fast option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Bzip2.level is being set to '1'"
+ )
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Bzip2.level is being set to '9'"
+ )
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.best = true
+ c.fast = true
+ end
+ compressor.level.should == 1
+ end
+ end
+ end
+
+ context 'when only one is set true' do
+ context 'when fast is set true before best' do
+ it 'should cause the fast option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Bzip2.level is being set to '1'"
+ )
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.fast = true
+ c.best = false
+ end
+ compressor.level.should == 1
+ end
+ end
+
+ context 'when fast is set true after best' do
+ it 'should cause the fast option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Bzip2.level is being set to '1'"
+ )
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.best = false
+ c.fast = true
+ end
+ compressor.level.should == 1
+ end
+ end
+
+ context 'when best is set true before fast' do
+ it 'should cause the best option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Bzip2.level is being set to '9'"
+ )
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.best = true
+ c.fast = false
+ end
+ compressor.level.should == 9
+ end
+ end
+
+ context 'when best is set true after fast' do
+ it 'should cause the best option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Bzip2.level is being set to '9'"
+ )
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.fast = false
+ c.best = true
+ end
+ compressor.level.should == 9
+ end
+ end
+ end
+
+ context 'when both are set false' do
+ it 'should only issue the two warnings' do
+ compressor = Backup::Compressor::Bzip2.new do |c|
+ c.fast = false
+ c.best = false
+ end
+ compressor.level.should be_false
+ end
+ end
+ end
+ end # describe 'fast and best options'
+ end # describe 'deprecations'
end
View
106 spec/compressor/custom_spec.rb
@@ -0,0 +1,106 @@
+# encoding: utf-8
+
+require File.expand_path('../../spec_helper.rb', __FILE__)
+
+describe Backup::Compressor::Custom do
+ let(:compressor) { Backup::Compressor::Custom.new }
+
+ before(:all) do
+ # CLI::Helpers#utility will raise an error
+ # if the command is invalid or not set
+ Backup::Compressor::Custom.send(
+ :define_method, :utility,
+ lambda {|arg| arg.to_s.empty? ? 'error' : "/path/to/#{ arg }" }
+ )
+ end
+
+ it 'should be a subclass of Compressor::Base' do
+ Backup::Compressor::Custom.
+ superclass.should == Backup::Compressor::Base
+ end
+
+ describe '#initialize' do
+ let(:compressor) { Backup::Compressor::Custom.new }
+
+ after { Backup::Compressor::Custom.clear_defaults! }
+
+ it 'should load pre-configured defaults' do
+ Backup::Compressor::Custom.any_instance.expects(:load_defaults!)
+ compressor
+ end
+
+ it 'should call CLI::Helpers#utility to validate command' do
+ Backup::Compressor::Custom.any_instance.expects(:utility)
+ compressor
+ end
+
+ it 'should clean the command and extension for use with compress_with' do
+ compressor = Backup::Compressor::Custom.new do |c|
+ c.command = ' my_command --option foo '
+ c.extension = ' my_extension '
+ end
+
+ compressor.command.should == ' my_command --option foo '
+ compressor.extension.should == ' my_extension '
+
+ compressor.expects(:log!)
+ compressor.compress_with do |cmd, ext|
+ cmd.should == '/path/to/my_command --option foo'
+ ext.should == 'my_extension'
+ end
+ end
+
+ context 'when no pre-configured defaults have been set' do
+ it 'should use default values' do
+ compressor.command.should be_nil
+ compressor.extension.should be_nil
+
+ compressor.instance_variable_get(:@cmd).should == 'error'
+ compressor.instance_variable_get(:@ext).should == ''
+ end
+
+ it 'should use the values given' do
+ compressor = Backup::Compressor::Custom.new do |c|
+ c.command = 'my_command'
+ c.extension = 'my_extension'
+ end
+
+ compressor.command.should == 'my_command'
+ compressor.extension.should == 'my_extension'
+
+ compressor.instance_variable_get(:@cmd).should == '/path/to/my_command'
+ compressor.instance_variable_get(:@ext).should == 'my_extension'
+ end
+ end # context 'when no pre-configured defaults have been set'
+
+ context 'when pre-configured defaults have been set' do
+ before do
+ Backup::Compressor::Custom.defaults do |c|
+ c.command = 'default_command'
+ c.extension = 'default_extension'
+ end
+ end
+
+ it 'should use pre-configured defaults' do
+ compressor.command.should == 'default_command'
+ compressor.extension.should == 'default_extension'
+
+ compressor.instance_variable_get(:@cmd).should == '/path/to/default_command'
+ compressor.instance_variable_get(:@ext).should == 'default_extension'
+ end
+
+ it 'should override pre-configured defaults' do
+ compressor = Backup::Compressor::Custom.new do |c|
+ c.command = 'new_command'
+ c.extension = 'new_extension'
+ end
+
+ compressor.command.should == 'new_command'
+ compressor.extension.should == 'new_extension'
+
+ compressor.instance_variable_get(:@cmd).should == '/path/to/new_command'
+ compressor.instance_variable_get(:@ext).should == 'new_extension'
+ end
+ end # context 'when pre-configured defaults have been set'
+ end # describe '#initialize'
+end
View
263 spec/compressor/gzip_spec.rb
@@ -3,81 +3,236 @@
require File.expand_path('../../spec_helper.rb', __FILE__)
describe Backup::Compressor::Gzip do
- let(:compressor) { Backup::Compressor::Gzip.new }
+ before do
+ Backup::Compressor::Gzip.any_instance.stubs(:utility).returns('gzip')
+ end
+
+ it 'should be a subclass of Compressor::Base' do
+ Backup::Compressor::Gzip.
+ superclass.should == Backup::Compressor::Base
+ end
describe '#initialize' do
+ let(:compressor) { Backup::Compressor::Gzip.new }
+
after { Backup::Compressor::Gzip.clear_defaults! }
it 'should load pre-configured defaults' do
Backup::Compressor::Gzip.any_instance.expects(:load_defaults!)
- compressor # instantiate object
+ compressor
end
- it 'should use pre-configured defaults' do
- Backup::Compressor::Gzip.defaults do |c|
- c.best = true
- c.fast = true
+ context 'when no pre-configured defaults have been set' do
+ it 'should use default values' do
+ compressor.level.should be_false
+
+ compressor.instance_variable_get(:@cmd).should == 'gzip'
+ compressor.instance_variable_get(:@ext).should == '.gz'
end
- compressor.best.should be_true
- compressor.fast.should be_true
- end
- it 'should set defaults when no pre-configured defaults are set' do
- compressor.best.should be_false
- compressor.fast.should be_false
- end
+ it 'should use the values given' do
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.level = 5
+ end
+ compressor.level.should == 5
- it 'should override pre-configured defaults' do
- Backup::Compressor::Gzip.defaults do |c|
- c.best = true
- c.fast = true
+ compressor.instance_variable_get(:@cmd).should == 'gzip -5'
+ compressor.instance_variable_get(:@ext).should == '.gz'
end
- compressor = Backup::Compressor::Gzip.new do |c|
- c.best = false
- c.fast = false
+ end # context 'when no pre-configured defaults have been set'
+
+ context 'when pre-configured defaults have been set' do
+ before do
+ Backup::Compressor::Gzip.defaults do |c|
+ c.level = 7
+ end
end
- compressor.best.should be_false
- compressor.fast.should be_false
- end
- end # describe 'configuration defaults'
- describe '#compress_with' do
- before do
- compressor.expects(:log!)
- compressor.expects(:utility).with(:gzip).returns('gzip')
- end
+ it 'should use pre-configured defaults' do
+ compressor.level.should == 7
- it 'should yield with the --best option' do
- compressor.best = true
- compressor.compress_with do |cmd, ext|
- cmd.should == 'gzip --best'
- ext.should == '.gz'
+ compressor.instance_variable_get(:@cmd).should == 'gzip -7'
+ compressor.instance_variable_get(:@ext).should == '.gz'
end
- end
- it 'should yield with the --fast option' do
- compressor.fast = true
- compressor.compress_with do |cmd, ext|
- cmd.should == 'gzip --fast'
- ext.should == '.gz'
+ it 'should override pre-configured defaults' do
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.level = 6
+ end
+ compressor.level.should == 6
+
+ compressor.instance_variable_get(:@cmd).should == 'gzip -6'
+ compressor.instance_variable_get(:@ext).should == '.gz'
end
- end
+ end # context 'when pre-configured defaults have been set'
+ end # describe '#initialize'
+
+ describe 'deprecations' do
+ describe 'fast and best options' do
+ context 'when only the fast option is used' do
+ before do
+ Backup::Logger.expects(:warn).with(
+ instance_of(Backup::Errors::ConfigurationError)
+ )
+ end
+
+ context 'when set to true' do
+ it 'should log a warning and set `level` to 1' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Gzip.level is being set to '1'"
+ )
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.fast = true
+ end
+ compressor.level.should be(1)
+ end
+ end
- it 'should yield with the --best and --fast options' do
- compressor.best = true
- compressor.fast = true
- compressor.compress_with do |cmd, ext|
- cmd.should == 'gzip --best --fast'
- ext.should == '.gz'
+ context 'when set to false' do
+ it 'should only log a warning' do
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.fast = false
+ end
+ compressor.level.should be_false
+ end
+ end
end
- end
- it 'should yield with no options' do
- compressor.compress_with do |cmd, ext|
- cmd.should == 'gzip'
- ext.should == '.gz'
+ context 'when only the best option is used' do
+ before do
+ Backup::Logger.expects(:warn).with(
+ instance_of(Backup::Errors::ConfigurationError)
+ )
+ end
+
+ context 'when set to true' do
+ it 'should log a warning and set `level` to 1' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Gzip.level is being set to '9'"
+ )
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.best = true
+ end
+ compressor.level.should be(9)
+ end
+ end
+
+ context 'when set to false' do
+ it 'should only log a warning' do
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.best = false
+ end
+ compressor.level.should be_false
+ end
+ end
+
end
- end
- end # describe '#compress_with'
+ context 'when both fast and best options are used' do
+ before do
+ Backup::Logger.expects(:warn).twice.with(
+ instance_of(Backup::Errors::ConfigurationError)
+ )
+ end
+
+ context 'when both are set true' do
+ context 'when fast is set first' do
+ it 'should cause the best option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Gzip.level is being set to '1'"
+ )
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Gzip.level is being set to '9'"
+ )
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.fast = true
+ c.best = true
+ end
+ compressor.level.should == 9
+ end
+ end
+
+ context 'when best is set first' do
+ it 'should cause the fast option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Gzip.level is being set to '1'"
+ )
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Gzip.level is being set to '9'"
+ )
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.best = true
+ c.fast = true
+ end
+ compressor.level.should == 1
+ end
+ end
+ end
+
+ context 'when only one is set true' do
+ context 'when fast is set true before best' do
+ it 'should cause the fast option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Gzip.level is being set to '1'"
+ )
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.fast = true
+ c.best = false
+ end
+ compressor.level.should == 1
+ end
+ end
+
+ context 'when fast is set true after best' do
+ it 'should cause the fast option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Gzip.level is being set to '1'"
+ )
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.best = false
+ c.fast = true
+ end
+ compressor.level.should == 1
+ end
+ end
+
+ context 'when best is set true before fast' do
+ it 'should cause the best option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Gzip.level is being set to '9'"
+ )
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.best = true
+ c.fast = false
+ end
+ compressor.level.should == 9
+ end
+ end
+
+ context 'when best is set true after fast' do
+ it 'should cause the best option to be set' do
+ Backup::Logger.expects(:warn).with(
+ "Backup::Compressor::Gzip.level is being set to '9'"
+ )
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.fast = false
+ c.best = true
+ end
+ compressor.level.should == 9
+ end
+ end
+ end
+
+ context 'when both are set false' do
+ it 'should only issue the two warnings' do
+ compressor = Backup::Compressor::Gzip.new do |c|
+ c.fast = false
+ c.best = false
+ end
+ compressor.level.should be_false
+ end
+ end
+ end
+ end # describe 'fast and best options'
+ end # describe 'deprecations'
end
View
99 spec/compressor/lzma_spec.rb
@@ -3,86 +3,121 @@
require File.expand_path('../../spec_helper.rb', __FILE__)
describe Backup::Compressor::Lzma do
- let(:compressor) { Backup::Compressor::Lzma.new }
+ before do
+ Backup::Compressor::Lzma.any_instance.stubs(:utility).returns('lzma')
+ end
it 'should be a subclass of Compressor::Base' do
Backup::Compressor::Lzma.
superclass.should == Backup::Compressor::Base
end
describe '#initialize' do
+ let(:compressor) { Backup::Compressor::Lzma.new }
+
after { Backup::Compressor::Lzma.clear_defaults! }
it 'should load pre-configured defaults' do
Backup::Compressor::Lzma.any_instance.expects(:load_defaults!)
- compressor # instantiate object
+ compressor
end
- it 'should use pre-configured defaults' do
- Backup::Compressor::Lzma.defaults do |c|
- c.best = true
- c.fast = true
+ context 'when no pre-configured defaults have been set' do
+ it 'should use default values' do
+ compressor.best.should be_false
+ compressor.fast.should be_false
end
- compressor.best.should be_true
- compressor.fast.should be_true
- end
- it 'should set defaults when no pre-configured defaults are set' do
- compressor.best.should be_false
- compressor.fast.should be_false
- end
+ it 'should use the values given' do
+ compressor = Backup::Compressor::Lzma.new do |c|
+ c.best = true
+ c.fast = true
+ end
- it 'should override pre-configured defaults' do
- Backup::Compressor::Lzma.defaults do |c|
- c.best = true
- c.fast = true
+ compressor.best.should be_true
+ compressor.fast.should be_true
end
- compressor = Backup::Compressor::Lzma.new do |c|
- c.best = false
- c.fast = false
+ end # context 'when no pre-configured defaults have been set'
+
+ context 'when pre-configured defaults have been set' do
+ before do
+ Backup::Compressor::Lzma.defaults do |c|
+ c.best = true
+ c.fast = true
+ end
+ end
+
+ it 'should use pre-configured defaults' do
+ compressor.best.should be_true
+ compressor.fast.should be_true
end
- compressor.best.should be_false
- compressor.fast.should be_false
- end
- end # describe 'configuration defaults'
+
+ it 'should override pre-configured defaults' do
+ compressor = Backup::Compressor::Lzma.new do |c|
+ c.best = false
+ c.fast = false
+ end
+
+ compressor.best.should be_false
+ compressor.fast.should be_false
+ end
+ end # context 'when pre-configured defaults have been set'
+ end # describe '#initialize'
describe '#compress_with' do
before do
- compressor.expects(:log!)
- compressor.expects(:utility).with(:lzma).returns('lzma')
+ Backup::Compressor::Lzma.any_instance.expects(:log!)
+
+ Backup::Logger.expects(:warn).with do |msg|
+ msg.should match(
+ /\[DEPRECATION WARNING\]\n Compressor::Lzma is being deprecated/
+ )
+ end
end
it 'should yield with the --best option' do
- compressor.best = true
+ compressor = Backup::Compressor::Lzma.new do |c|
+ c.best = true
+ end
+
compressor.compress_with do |cmd, ext|
cmd.should == 'lzma --best'
ext.should == '.lzma'
end
end
it 'should yield with the --fast option' do
- compressor.fast = true
+ compressor = Backup::Compressor::Lzma.new do |c|
+ c.fast = true
+ end
+
compressor.compress_with do |cmd, ext|
cmd.should == 'lzma --fast'
ext.should == '.lzma'
end
end
- it 'should yield with the --best and --fast options' do
- compressor.best = true
- compressor.fast = true
+ it 'should prefer the --best option over --fast' do
+ compressor = Backup::Compressor::Lzma.new do |c|
+ c.best = true
+ c.fast = true
+ end
+
compressor.compress_with do |cmd, ext|
- cmd.should == 'lzma --best --fast'
+ cmd.should == 'lzma --best'
ext.should == '.lzma'
end
end
it 'should yield with no options' do
+ compressor = Backup::Compressor::Lzma.new
+
compressor.compress_with do |cmd, ext|
cmd.should == 'lzma'
ext.should == '.lzma'
end
end
+
end # describe '#compress_with'
end
View
133 spec/compressor/pbzip2_spec.rb
@@ -3,108 +3,157 @@
require File.expand_path('../../spec_helper.rb', __FILE__)
describe Backup::Compressor::Pbzip2 do
- let(:compressor) { Backup::Compressor::Pbzip2.new }
+ before do
+ Backup::Compressor::Pbzip2.any_instance.stubs(:utility).returns('pbzip2')
+ end
+
+ it 'should be a subclass of Compressor::Base' do
+ Backup::Compressor::Pbzip2.
+ superclass.should == Backup::Compressor::Base
+ end
describe '#initialize' do
+ let(:compressor) { Backup::Compressor::Pbzip2.new }
+
after { Backup::Compressor::Pbzip2.clear_defaults! }
it 'should load pre-configured defaults' do
Backup::Compressor::Pbzip2.any_instance.expects(:load_defaults!)
compressor
end
- it 'should use pre-configured defaults' do
- Backup::Compressor::Pbzip2.defaults do |c|
- c.best = true
- c.fast = true
- c.processors = 2
+ context 'when no pre-configured defaults have been set' do
+ it 'should use default values' do
+ compressor.best.should be_false
+ compressor.fast.should be_false
+ compressor.processors.should be_false
end
- compressor.best.should be_true
- compressor.fast.should be_true
- compressor.processors.should == 2
- end
- it 'should set defaults when no pre-configured defaults are set' do
- compressor.best.should be_false
- compressor.fast.should be_false
- compressor.processors.should be_false
- end
+ it 'should use the values given' do
+ compressor = Backup::Compressor::Pbzip2.new do |c|
+ c.best = true
+ c.fast = true
+ c.processors = 2
+ end
- it 'should override pre-configured defaults' do
- Backup::Compressor::Pbzip2.defaults do |c|
- c.best = true
- c.fast = true
- c.processors = 2
+ compressor.best.should be_true
+ compressor.fast.should be_true
+ compressor.processors.should == 2
end
- compressor = Backup::Compressor::Pbzip2.new do |c|
- c.best = false
- c.fast = false
- c.processors = 4
+ end # context 'when no pre-configured defaults have been set'
+
+ context 'when pre-configured defaults have been set' do
+ before do
+ Backup::Compressor::Pbzip2.defaults do |c|
+ c.best = true
+ c.fast = true
+ c.processors = 2
+ end
end
- compressor.best.should be_false
- compressor.fast.should be_false
- compressor.processors.should == 4
- end
- end # describe 'configuration defaults'
+
+ it 'should use pre-configured defaults' do
+ compressor.best.should be_true
+ compressor.fast.should be_true
+ compressor.processors.should == 2
+ end
+
+ it 'should override pre-configured defaults' do
+ compressor = Backup::Compressor::Pbzip2.new do |c|
+ c.best = false
+ c.fast = false
+ c.processors = 4
+ end
+
+ compressor.best.should be_false
+ compressor.fast.should be_false
+ compressor.processors.should == 4
+ end
+ end # context 'when pre-configured defaults have been set'
+ end # describe '#initialize'
describe '#compress_with' do
before do
- compressor.expects(:log!)
- compressor.expects(:utility).with(:pbzip2).returns('pbzip2')
+ Backup::Compressor::Pbzip2.any_instance.expects(:log!)
+
+ Backup::Logger.expects(:warn).with do |msg|
+ msg.should match(
+ /\[DEPRECATION WARNING\]\n Compressor::Pbzip2 is being deprecated/
+ )
+ end
end
it 'should yield with the --best option' do
- compressor.best = true
+ compressor = Backup::Compressor::Pbzip2.new do |c|
+ c.best = true
+ end
+
compressor.compress_with do |cmd, ext|
cmd.should == 'pbzip2 --best'
ext.should == '.bz2'
end
end
it 'should yield with the --fast option' do
- compressor.fast = true
+ compressor = Backup::Compressor::Pbzip2.new do |c|
+ c.fast = true
+ end
+
compressor.compress_with do |cmd, ext|
cmd.should == 'pbzip2 --fast'
ext.should == '.bz2'
end
end
it 'should yield with the -p option' do
- compressor.processors = 2
+ compressor = Backup::Compressor::Pbzip2.new do |c|
+ c.processors = 2
+ end
+
compressor.compress_with do |cmd, ext|
cmd.should == 'pbzip2 -p2'
ext.should == '.bz2'
end
end
- it 'should yield with the --best and --fast options' do
- compressor.best = true
- compressor.fast = true
+ it 'should prefer the --best option over --fast' do
+ compressor = Backup::Compressor::Pbzip2.new do |c|
+ c.best = true
+ c.fast = true
+ end
+
compressor.compress_with do |cmd, ext|
- cmd.should == 'pbzip2 --best --fast'
+ cmd.should == 'pbzip2 --best'
ext.should == '.bz2'
end
end
it 'should yield with the --best and -p options' do
- compressor.best = true
- compressor.processors = 2
+ compressor = Backup::Compressor::Pbzip2.new do |c|
+ c.best = true
+ c.processors = 2
+ end
+
compressor.compress_with do |cmd, ext|
cmd.should == 'pbzip2 --best -p2'
ext.should == '.bz2'
end
end
it 'should yield with the --fast and -p options' do
- compressor.fast = true
- compressor.processors = 2
+ compressor = Backup::Compressor::Pbzip2.new do |c|
+ c.fast = true
+ c.processors = 2
+ end
+
compressor.compress_with do |cmd, ext|
cmd.should == 'pbzip2 --fast -p2'
ext.should == '.bz2'
end
end
it 'should yield with no options' do
+ compressor = Backup::Compressor::Pbzip2.new
+
compressor.compress_with do |cmd, ext|
cmd.should == 'pbzip2'
ext.should == '.bz2'
View
5 templates/cli/utility/compressor/bzip2
@@ -1,7 +1,4 @@
##
# Bzip2 [Compressor]
#
- compress_with Bzip2 do |compression|
- compression.best = true
- compression.fast = false
- end
+ compress_with Bzip2
View
11 templates/cli/utility/compressor/custom
@@ -0,0 +1,11 @@
+ ##
+ # Custom [Compressor]
+ #
+ # For information on using a Custom Compressor,
+ # please see the following Wiki page:
+ # https://github.com/meskyanichi/backup/wiki/Compressors
+ #
+ compress_with Custom do |compressor|
+ compressor.command = 'gzip'
+ compressor.extension = '.gz'
+ end
View
5 templates/cli/utility/compressor/gzip
@@ -1,7 +1,4 @@
##
# Gzip [Compressor]
#
- compress_with Gzip do |compression|
- compression.best = true
- compression.fast = false
- end
+ compress_with Gzip
View
3 templates/cli/utility/compressor/lzma
@@ -1,6 +1,9 @@
##
# Lzma [Compressor]
#
+ # [DEPRECATED]
+ # See the Wiki for more info.
+ # https://github.com/meskyanichi/backup/wiki/Compressors
compress_with Lzma do |compression|
compression.best = true
compression.fast = false
View
3 templates/cli/utility/compressor/pbzip2
@@ -1,6 +1,9 @@
##
# Pbzip2 [Compressor]
#
+ # [DEPRECATED]
+ # See the Wiki for more info.
+ # https://github.com/meskyanichi/backup/wiki/Compressors
compress_with Pbzip2 do |compression|
compression.best = true
compression.fast = false

0 comments on commit 2c70c81

Please sign in to comment.