aef-bakker 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,497 @@
1
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
+ #
3
+ # This file is part of Bakker.
4
+ #
5
+ # Bakker is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>
17
+
18
+ require 'lib/bakker'
19
+
20
+ require 'tmpdir'
21
+ require 'fileutils'
22
+
23
+ require 'rubygems'
24
+ require 'sys/uname'
25
+
26
+ module BakkerSpecHelper
27
+ # If there is a way to get the executable path of the currently running ruby
28
+ # interpreter, please tell me how.
29
+ warn 'Attention: If the ruby interpreter to be tested with is not ruby in the ' +
30
+ "default path, you have to change this manually in #{__FILE__} line #{__LINE__ + 1}"
31
+ RUBY_PATH = 'ruby'
32
+
33
+ def tempfile_path(name, options = {})
34
+ path = File.join(@folder_path, name)
35
+
36
+ FileUtils.touch(path) if options[:create]
37
+
38
+ path
39
+ end
40
+
41
+ def executable_path
42
+ "#{RUBY_PATH} bin/bakker"
43
+ end
44
+
45
+ def windows?
46
+ Sys::Uname.sysname.downcase.include?('windows')
47
+ end
48
+ end
49
+
50
+ describe Aef::Bakker do
51
+ include BakkerSpecHelper
52
+
53
+ before(:each) do
54
+ # Before ruby 1.8.7, the tmpdir standard library had no method to create
55
+ # a temporary directory (mktmpdir).
56
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('1.8.7')
57
+ @folder_path = File.join(Dir.tmpdir, 'bakker_spec')
58
+ Dir.mkdir(@folder_path)
59
+ else
60
+ @folder_path = Dir.mktmpdir('bakker_spec')
61
+ end
62
+ end
63
+
64
+ after(:each) do
65
+ FileUtils.rm_rf(@folder_path)
66
+ end
67
+
68
+ context 'library' do
69
+ it "should throw an exception when source file does not exist" do
70
+ source_path = tempfile_path('abc')
71
+
72
+ lambda {
73
+ Aef::Bakker.process(source_path)
74
+ }.should raise_error(Aef::BakkerWarning)
75
+ end
76
+
77
+ it "should throw an exception when source and target file do exist" do
78
+ source_path = tempfile_path('abc', :create => true)
79
+ target_path = tempfile_path('abc.bak', :create => true)
80
+
81
+ lambda {
82
+ Aef::Bakker.process(source_path)
83
+ }.should raise_error(Aef::BakkerWarning)
84
+ end
85
+
86
+ it "should extend a non-extended file correctly" do
87
+ source_path = tempfile_path('def', :create => true)
88
+ target_path = tempfile_path('def.bak')
89
+
90
+ lambda {
91
+ Aef::Bakker.process(source_path, '.bak', :toggle, :move)
92
+ }.should change{ File.exist?(target_path) }.from(false).to(true) and
93
+ change{ File.exist?(source_path) }.from(true).to(false)
94
+ end
95
+
96
+ it "should extend a non-extended file correctly even if only the target is given" do
97
+ source_path = tempfile_path('def', :create => true)
98
+ target_path = tempfile_path('def.bak')
99
+
100
+ lambda {
101
+ Aef::Bakker.process(target_path, '.bak', :toggle, :move)
102
+ }.should change{ File.exist?(target_path) }.from(false).to(true) and
103
+ change{ File.exist?(source_path) }.from(true).to(false)
104
+ end
105
+
106
+ it "should not extend a non-extended file if mode is remove" do
107
+ source_path = tempfile_path('abc', :create => true)
108
+ target_path = tempfile_path('abc.ext')
109
+
110
+ lambda {
111
+ Aef::Bakker.process(source_path, '.ext', :remove, :move)
112
+ }.should_not change{ File.exist?(target_path) == false } and
113
+ change{ File.exist?(source_path) == true }
114
+ end
115
+
116
+ it "should not extend a non-extended file if mode is remove even if only the target is given" do
117
+ source_path = tempfile_path('abc', :create => true)
118
+ target_path = tempfile_path('abc.ext')
119
+
120
+ lambda {
121
+ Aef::Bakker.process(target_path, '.ext', :remove, :move)
122
+ }.should_not change{ File.exist?(target_path) == false } and
123
+ change{ File.exist?(source_path) == true }
124
+ end
125
+
126
+ it "should extend a non-extended file if mode is add" do
127
+ source_path = tempfile_path('abc', :create => true)
128
+ target_path = tempfile_path('abc.ext')
129
+
130
+ lambda {
131
+ Aef::Bakker.process(source_path, '.ext', :add, :move)
132
+ }.should change{ File.exist?(target_path) }.from(false).to(true) and
133
+ change{ File.exist?(source_path) }.from(true).to(false)
134
+ end
135
+
136
+ it "should extend a non-extended file if mode is add even if only the target is given" do
137
+ source_path = tempfile_path('abc', :create => true)
138
+ target_path = tempfile_path('abc.ext')
139
+
140
+ lambda {
141
+ Aef::Bakker.process(source_path, '.ext', :add, :move)
142
+ }.should change{ File.exist?(target_path) }.from(false).to(true) and
143
+ change{ File.exist?(source_path) }.from(true).to(false)
144
+ end
145
+
146
+ it "should create an extended copy of a non-extended file if action is copy" do
147
+ source_path = tempfile_path('xyz', :create => true)
148
+ target_path = tempfile_path('xyz.tar.gz')
149
+
150
+ lambda {
151
+ Aef::Bakker.process(source_path, '.tar.gz', :toggle, :copy)
152
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
153
+ not change{ File.exists?(source_path) == true }
154
+ end
155
+
156
+ it "should create an extended copy of a non-extended file if action is copy even if only the target is given" do
157
+ source_path = tempfile_path('xyz', :create => true)
158
+ target_path = tempfile_path('xyz.tar.gz')
159
+
160
+ lambda {
161
+ Aef::Bakker.process(target_path, '.tar.gz', :toggle, :copy)
162
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
163
+ not change{ File.exists?(source_path) == true }
164
+ end
165
+
166
+ it "should unextend an extended file correctly" do
167
+ source_path = tempfile_path('xyz.zirbel', :create => true)
168
+ target_path = tempfile_path('xyz')
169
+
170
+ lambda {
171
+ Aef::Bakker.process(source_path, '.zirbel', :toggle, :move)
172
+ }.should change{ File.exist?(target_path) }.from(false).to(true) and
173
+ change{ File.exist?(source_path) }.from(true).to(false)
174
+ end
175
+
176
+ it "should unextend an extended file correctly even if only the target is given" do
177
+ source_path = tempfile_path('xyz.zirbel', :create => true)
178
+ target_path = tempfile_path('xyz')
179
+
180
+ lambda {
181
+ Aef::Bakker.process(target_path, '.zirbel', :toggle, :move)
182
+ }.should change{ File.exist?(target_path) }.from(false).to(true) and
183
+ change{ File.exist?(source_path) }.from(true).to(false)
184
+ end
185
+
186
+ it "should not unextend an extended file if mode is add" do
187
+ source_path = tempfile_path('1234.bak', :create => true)
188
+ target_path = tempfile_path('1234')
189
+
190
+ lambda {
191
+ Aef::Bakker.process(source_path, '.bak', :add, :move)
192
+ }.should_not change{ File.exist?(target_path) == false } and
193
+ change{ File.exist?(source_path) == true }
194
+ end
195
+
196
+ it "should not unextend an extended file if mode is add even if only the target is given" do
197
+ source_path = tempfile_path('1234.bak', :create => true)
198
+ target_path = tempfile_path('1234')
199
+
200
+ lambda {
201
+ Aef::Bakker.process(target_path, '.bak', :add, :move)
202
+ }.should_not change{ File.exist?(target_path) == false } and
203
+ change{ File.exist?(source_path) == true }
204
+ end
205
+
206
+ it "should unextend an extended file if mode is remove" do
207
+ source_path = tempfile_path('testfile.1234', :create => true)
208
+ target_path = tempfile_path('testfile')
209
+
210
+ lambda {
211
+ Aef::Bakker.process(source_path, '.1234', :remove, :move)
212
+ }.should change{ File.exist?(target_path) }.from(false).to(true) and
213
+ change{ File.exist?(source_path) }.from(true).to(false)
214
+ end
215
+
216
+ it "should unextend an extended file if mode is remove even if only the target is given" do
217
+ source_path = tempfile_path('testfile.1234', :create => true)
218
+ target_path = tempfile_path('testfile')
219
+
220
+ lambda {
221
+ Aef::Bakker.process(target_path, '.1234', :remove, :move)
222
+ }.should change{ File.exist?(target_path) }.from(false).to(true) and
223
+ change{ File.exist?(source_path) }.from(true).to(false)
224
+ end
225
+
226
+ it "should create an unextended copy of an extended file if action is copy" do
227
+ source_path = tempfile_path('demo.exe', :create => true)
228
+ target_path = tempfile_path('demo')
229
+
230
+ lambda {
231
+ Aef::Bakker.process(source_path, '.exe', :toggle, :copy)
232
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
233
+ not change{ File.exists?(source_path) == true }
234
+ end
235
+
236
+ it "should create an unextended copy of an extended file if action is copy even if only the target is given" do
237
+ source_path = tempfile_path('demo.exe', :create => true)
238
+ target_path = tempfile_path('demo')
239
+
240
+ lambda {
241
+ Aef::Bakker.process(target_path, '.exe', :toggle, :copy)
242
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
243
+ not change{ File.exists?(source_path) == true }
244
+ end
245
+ end
246
+
247
+ context 'commandline tool' do
248
+ it "use action move, extension .bak and mode toggle by default" do
249
+ source_path = tempfile_path('test.bak', :create => true)
250
+ target_path = tempfile_path('test')
251
+
252
+ lambda {
253
+ `#{executable_path} #{source_path}`
254
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
255
+ change{ File.exists?(source_path) }.from(true).to(false)
256
+
257
+ lambda {
258
+ `#{executable_path} #{target_path}`
259
+ }.should change{ File.exists?(target_path) }.from(true).to(false) and
260
+ change{ File.exists?(source_path) }.from(false).to(true)
261
+ end
262
+
263
+ it "should use -a to select the action" do
264
+ source_path = tempfile_path('abc', :create => true)
265
+ target_path = tempfile_path('abc.bak')
266
+
267
+ lambda {
268
+ `#{executable_path} -a copy #{source_path}`
269
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
270
+ not change{ File.exists?(source_path) == true }
271
+ end
272
+
273
+ it "should use --action to select the action" do
274
+ source_path = tempfile_path('abc', :create => true)
275
+ target_path = tempfile_path('abc.bak')
276
+
277
+ lambda {
278
+ `#{executable_path} --action copy #{source_path}`
279
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
280
+ not change{ File.exists?(source_path) == true }
281
+ end
282
+
283
+ it "should use -e to select an extension" do
284
+ source_path = tempfile_path('abc', :create => true)
285
+ target_path = tempfile_path('abc.zirbel')
286
+
287
+ lambda {
288
+ `#{executable_path} -e .zirbel #{source_path}`
289
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
290
+ change{ File.exists?(source_path) }.from(true).to(false)
291
+ end
292
+
293
+ it "should use --extension to select an extension" do
294
+ source_path = tempfile_path('abc', :create => true)
295
+ target_path = tempfile_path('abc.zirbel')
296
+
297
+ lambda {
298
+ `#{executable_path} --extension .zirbel #{source_path}`
299
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
300
+ change{ File.exists?(source_path) }.from(true).to(false)
301
+ end
302
+
303
+ it "should use -m to select a processing mode" do
304
+ source_path = tempfile_path('abc', :create => true)
305
+ target_path = tempfile_path('abc.bak')
306
+
307
+ lambda {
308
+ `#{executable_path} -m remove #{source_path}`
309
+ }.should_not change{ File.exists?(target_path) == false } and
310
+ change{ File.exists?(source_path) == true }
311
+
312
+ lambda {
313
+ `#{executable_path} -m toggle #{source_path}`
314
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
315
+ change{ File.exists?(source_path) }.from(true).to(false)
316
+
317
+ lambda {
318
+ `#{executable_path} -m add #{source_path}`
319
+ }.should_not change{ File.exists?(target_path) == true } and
320
+ change{ File.exists?(source_path) == false }
321
+ end
322
+
323
+ it "should use --mode to select a processing mode" do
324
+ source_path = tempfile_path('abc', :create => true)
325
+ target_path = tempfile_path('abc.bak')
326
+
327
+ lambda {
328
+ `#{executable_path} --mode remove #{source_path}`
329
+ }.should_not change{ File.exists?(target_path) == false } and
330
+ change{ File.exists?(source_path) == true }
331
+
332
+ lambda {
333
+ `#{executable_path} --mode toggle #{source_path}`
334
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
335
+ change{ File.exists?(source_path) }.from(true).to(false)
336
+
337
+ lambda {
338
+ `#{executable_path} --mode add #{source_path}`
339
+ }.should_not change{ File.exists?(target_path) == true } and
340
+ change{ File.exists?(source_path) == false }
341
+ end
342
+
343
+ it "should allow multiple files as argument" do
344
+ source_path_a = tempfile_path('abc', :create => true)
345
+ target_path_a = tempfile_path('abc.bak')
346
+ source_path_b = tempfile_path('def.bak', :create => true)
347
+ target_path_b = tempfile_path('def')
348
+ source_path_c = tempfile_path('xyz', :create => true)
349
+ target_path_c = tempfile_path('xyz.bak')
350
+
351
+ lambda {
352
+ `#{executable_path} #{source_path_a} #{source_path_b} #{source_path_c}`
353
+ }.should change{ File.exists?(target_path_a) }.from(false).to(true) and
354
+ change{ File.exists?(source_path_a) }.from(true).to(false) and
355
+ change{ File.exists?(target_path_b) }.from(false).to(true) and
356
+ change{ File.exists?(source_path_b) }.from(true).to(false) and
357
+ change{ File.exists?(target_path_c) }.from(false).to(true) and
358
+ change{ File.exists?(source_path_c) }.from(true).to(false)
359
+ end
360
+
361
+ it "should accept action config via environment variables" do
362
+ source_path = tempfile_path('abc', :create => true)
363
+ target_path = tempfile_path('abc.bak')
364
+
365
+ lambda {
366
+ if windows?
367
+ `set BAKKER_ACTION=copy`
368
+ `#{executable_path} #{source_path}`
369
+ else
370
+ `env BAKKER_ACTION=copy #{executable_path} #{source_path}`
371
+ end
372
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
373
+ not change{ File.exists?(source_path) == true }
374
+ end
375
+
376
+ it "should prefer commandline setting over environment for action" do
377
+ source_path = tempfile_path('abc', :create => true)
378
+ target_path = tempfile_path('abc.bak')
379
+
380
+ lambda {
381
+ if windows?
382
+ `set BAKKER_ACTION=copy`
383
+ `#{executable_path} -a move #{source_path}`
384
+ else
385
+ `env BAKKER_ACTION=copy #{executable_path} -a move #{source_path}`
386
+ end
387
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
388
+ change{ File.exists?(source_path) }.from(true).to(false)
389
+ end
390
+
391
+ it "should accept extension config via environment variables" do
392
+ source_path = tempfile_path('abc', :create => true)
393
+ target_path = tempfile_path('abc.zirbel')
394
+
395
+ lambda {
396
+ if windows?
397
+ `set BAKKER_EXTENSION=.zirbel`
398
+ `#{executable_path} #{source_path}`
399
+ else
400
+ `env BAKKER_EXTENSION=.zirbel #{executable_path} #{source_path}`
401
+ end
402
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
403
+ change{ File.exists?(source_path) }.from(true).to(false)
404
+ end
405
+
406
+ it "should prefer commandline setting over environment for extension" do
407
+ source_path = tempfile_path('abc', :create => true)
408
+ target_path = tempfile_path('abc.zirbel')
409
+
410
+ lambda {
411
+ if windows?
412
+ `set BAKKER_EXTENSION=.1234`
413
+ `#{executable_path} -e .zirbel #{source_path}`
414
+ else
415
+ `env BAKKER_EXTENSION=.1234 #{executable_path} -e .zirbel #{source_path}`
416
+ end
417
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
418
+ change{ File.exists?(source_path) }.from(true).to(false)
419
+ end
420
+
421
+ it "should accept extension config via environment variables" do
422
+ source_path = tempfile_path('abc', :create => true)
423
+ target_path = tempfile_path('abc.bak')
424
+
425
+ lambda {
426
+ if windows?
427
+ `set BAKKER_MODE=remove`
428
+ `#{executable_path} #{source_path}`
429
+ else
430
+ `env BAKKER_MODE=remove #{executable_path} #{source_path}`
431
+ end
432
+ }.should_not change{ File.exists?(target_path) == false } and
433
+ change{ File.exists?(source_path) == true }
434
+
435
+ lambda {
436
+ if windows?
437
+ `set BAKKER_MODE=toggle`
438
+ `#{executable_path} #{source_path}`
439
+ else
440
+ `env BAKKER_MODE=toggle #{executable_path} #{source_path}`
441
+ end
442
+ }.should change{ File.exists?(target_path) }.from(false).to(true) and
443
+ change{ File.exists?(source_path) }.from(true).to(false)
444
+
445
+ lambda {
446
+ if windows?
447
+ `set BAKKER_MODE=add`
448
+ `#{executable_path} #{source_path}`
449
+ else
450
+ `env BAKKER_MODE=add #{executable_path} #{source_path}`
451
+ end
452
+ }.should_not change{ File.exists?(target_path) == true } and
453
+ change{ File.exists?(source_path) == false }
454
+ end
455
+
456
+ it "should prefer commandline setting over environment for mode" do
457
+ source_path = tempfile_path('abc', :create => true)
458
+ target_path = tempfile_path('abc.bak')
459
+
460
+ lambda {
461
+ if windows?
462
+ `set BAKKER_MODE=add`
463
+ `#{executable_path} -m remove #{source_path}`
464
+ else
465
+ `env BAKKER_MODE=add #{executable_path} -m remove #{source_path}`
466
+ end
467
+ }.should_not change{ File.exists?(target_path) == false } and
468
+ change{ File.exists?(source_path) == true }
469
+ end
470
+
471
+ it 'should display correct version and licensing information with the --version switch' do
472
+ message = <<-EOF
473
+ Bakker 1.1.0
474
+
475
+ Project: https://rubyforge.org/projects/aef/
476
+ RDoc: http://aef.rubyforge.org/bakker/
477
+ Github: http://github.com/aef/bakker/
478
+
479
+ Copyright 2009 Alexander E. Fischer <aef@raxys.net>
480
+
481
+ Bakker is free software: you can redistribute it and/or modify
482
+ it under the terms of the GNU General Public License as published by
483
+ the Free Software Foundation, either version 3 of the License, or
484
+ (at your option) any later version.
485
+
486
+ This program is distributed in the hope that it will be useful,
487
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
488
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
489
+ GNU General Public License for more details.
490
+
491
+ You should have received a copy of the GNU General Public License
492
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
493
+ EOF
494
+ `#{executable_path} --version`.should eql(message)
495
+ end
496
+ end
497
+ end