/
gzip_spec.rb
287 lines (248 loc) · 8.58 KB
/
gzip_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# encoding: utf-8
require File.expand_path('../../spec_helper.rb', __FILE__)
describe Backup::Compressor::Gzip do
before do
Backup::Compressor::Gzip.stubs(:utility).returns('gzip')
Backup::Compressor::Gzip.instance_variable_set(:@has_rsyncable, true)
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
it 'should be extended by Utilities::Helpers' do
Backup::Compressor::Gzip.instance_eval('class << self; self; end').
should include(Backup::Utilities::Helpers)
end
describe '.has_rsyncable?' do
before do
Backup::Compressor::Gzip.instance_variable_set(:@has_rsyncable, nil)
end
context 'when --rsyncable is available' do
before do
Backup::Compressor::Gzip.expects(:`).once.
with('gzip --rsyncable --version >/dev/null 2>&1; echo $?').
returns("0\n")
end
it 'returns true and caches the result' do
Backup::Compressor::Gzip.has_rsyncable?.should be(true)
Backup::Compressor::Gzip.has_rsyncable?.should be(true)
end
end
context 'when --rsyncable is not available' do
before do
Backup::Compressor::Gzip.expects(:`).once.
with('gzip --rsyncable --version >/dev/null 2>&1; echo $?').
returns("1\n")
end
it 'returns false and caches the result' do
Backup::Compressor::Gzip.has_rsyncable?.should be(false)
Backup::Compressor::Gzip.has_rsyncable?.should be(false)
end
end
end
describe '#initialize' do
let(:compressor) { Backup::Compressor::Gzip.new }
after { Backup::Compressor::Gzip.clear_defaults! }
context 'when no pre-configured defaults have been set' do
it 'should use default values' do
compressor.level.should be(false)
compressor.rsyncable.should be(false)
compressor.compress_with do |cmd, ext|
cmd.should == 'gzip'
ext.should == '.gz'
end
end
it 'should use the values given' do
compressor = Backup::Compressor::Gzip.new do |c|
c.level = 5
c.rsyncable = true
end
compressor.level.should == 5
compressor.rsyncable.should be(true)
compressor.compress_with do |cmd, ext|
cmd.should == 'gzip -5 --rsyncable'
ext.should == '.gz'
end
end
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
c.rsyncable = true
end
end
it 'should use pre-configured defaults' do
compressor.level.should == 7
compressor.rsyncable.should be(true)
compressor.compress_with do |cmd, ext|
cmd.should == 'gzip -7 --rsyncable'
ext.should == '.gz'
end
end
it 'should override pre-configured defaults' do
compressor = Backup::Compressor::Gzip.new do |c|
c.level = 6
c.rsyncable = false
end
compressor.level.should == 6
compressor.rsyncable.should be(false)
compressor.compress_with do |cmd, ext|
cmd.should == 'gzip -6'
ext.should == '.gz'
end
end
end # context 'when pre-configured defaults have been set'
it 'should ignore rsyncable option and warn user if not supported' do
Backup::Compressor::Gzip.instance_variable_set(:@has_rsyncable, false)
Backup::Logger.expects(:warn).with() do |err|
err.should be_a(Backup::Errors::Compressor::Gzip::RsyncableError)
err.message.should match(/'rsyncable' option ignored/)
end
compressor = Backup::Compressor::Gzip.new do |c|
c.level = 5
c.rsyncable = true
end
compressor.level.should == 5
compressor.rsyncable.should be(true)
compressor.compress_with do |cmd, ext|
cmd.should == 'gzip -5'
ext.should == '.gz'
end
end
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 {|err|
err.should be_an_instance_of Backup::Errors::ConfigurationError
err.message.should match(
/Use Gzip#level instead/
)
}
end
context 'when set to true' do
it 'should log a warning and set `level` to 1' do
compressor = Backup::Compressor::Gzip.new do |c|
c.fast = true
end
compressor.level.should == 1
end
end
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
context 'when only the best option is used' do
before do
Backup::Logger.expects(:warn).with {|err|
err.should be_an_instance_of Backup::Errors::ConfigurationError
err.message.should match(
/Use Gzip#level instead/
)
}
end
context 'when set to true' do
it 'should log a warning and set `level` to 1' do
compressor = Backup::Compressor::Gzip.new do |c|
c.best = true
end
compressor.level.should == 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
context 'when both fast and best options are used' do
before do
Backup::Logger.expects(:warn).twice.with {|err|
err.should be_an_instance_of Backup::Errors::ConfigurationError
err.message.should match(
/Use Gzip#level instead/
)
}
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
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
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
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
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
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
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