Rename aggregate to concatenate
[buildr.git] / spec / packaging / archive_spec.rb
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with this
3 # work for additional information regarding copyright ownership. The ASF
4 # licenses this file to you under the Apache License, Version 2.0 (the
5 # "License"); you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations under
14 # the License.
15
16
17 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helpers'))
18
19
20 module ArchiveTaskHelpers
21 # Not too smart, we just create some content based on file name to make sure you read what you write.
22 def content_for(file)
23 "Content for #{File.basename(file)}"
24 end
25
26 # Qualify a filename
27 #
28 # e.g. qualify("file.zip", "src") => "file-src.zip"
29 def qualify(filename, qualifier)
30 ext = (filename =~ /\.$/) ? "." : File.extname(filename)
31 base = filename[0..0-ext.size-1]
32 base + "-" + qualifier + ext
33 end
34
35 # Create an archive not using the archive task, this way we do have a file in existence, but we don't
36 # have an already invoked task. Yield an archive task to the block which can use it to include files,
37 # set options, etc.
38 def create_without_task
39 archive(qualify(@archive, "tmp")).tap do |task|
40 yield task if block_given?
41 task.invoke
42 mv task.name, @archive
43 end
44 end
45
46 def create_for_merge
47 zip(qualify(@archive, "src")).include(@files).tap do |task|
48 yield task
49 end
50 end
51
52 def init_dir
53 unless @dir
54 @dir = File.expand_path('test')
55 @files = %w{Test1.txt Text2.html}.map { |file| File.expand_path(file, @dir) }.
56 each { |file| write file, content_for(file) }
57 @empty_dirs = %w{EmptyDir1 EmptyDir2}.map { |file| File.expand_path(file, @dir) }.
58 each { |file| mkdir file }
59 end
60 end
61 end
62
63 shared_examples_for 'ArchiveTask' do
64 include ArchiveTaskHelpers
65
66 before(:each) do
67 init_dir
68 end
69
70 it 'should point to archive file' do
71 archive(@archive).name.should eql(@archive)
72 end
73
74 it 'should create file' do
75 lambda { archive(@archive).invoke }.should change { File.exist?(@archive) }.to(true)
76 end
77
78 it 'should create empty archive if no files included' do
79 archive(@archive).invoke
80 inspect_archive { |archive| archive.should be_empty }
81 end
82
83 it 'should raise error when include() is called with nil values' do
84 lambda { archive(@archive).include(nil) }.should raise_error
85 lambda { archive(@archive).include([nil]) }.should raise_error
86 end
87
88 it 'should create empty archive if called #clean method' do
89 archive(@archive).include(@files).clean.invoke
90 inspect_archive { |archive| archive.should be_empty }
91 end
92
93 it 'should archive all included files' do
94 archive(@archive).include(@files).invoke
95 inspect_archive { |archive| @files.each { |f| archive[File.basename(f)].should eql(content_for(f)) } }
96 inspect_archive.size.should eql(@files.size)
97 end
98
99 it 'should archive file tasks' do
100 tasks = @files.map { |fn| file(fn) }
101 archive(@archive).include(tasks).invoke
102 inspect_archive { |archive| @files.each { |f| archive[File.basename(f)].should eql(content_for(f)) } }
103 inspect_archive.size.should eql(@files.size)
104 end
105
106 it 'should invoke and archive file tasks' do
107 file = file('included') { write 'included' }
108 lambda { archive(@archive).include(file).invoke }.should change { File.exist?(file.to_s) }.to(true)
109 inspect_archive.keys.should include('included')
110 end
111
112 it 'should archive artifacts' do
113 write 'library-1.0.txt', 'library-1.0'
114 artifact("org.example:library:txt:1.0").from 'library-1.0.txt'
115 archive(@archive).include("org.example:library:txt:1.0").invoke
116 inspect_archive.keys.should include('library-1.0.txt')
117 end
118
119 it 'should archive project artifacts' do
120 define 'p1' do
121 project.version = '1.0'
122 package(:zip)
123 end
124 archive(@archive).include(project('p1')).invoke
125 inspect_archive.keys.should include('p1-1.0.zip')
126 end
127
128 it 'should include entry for directory' do
129 archive(@archive).include(@dir).invoke
130 inspect_archive { |archive| @files.each { |f| archive['test/' + File.basename(f)].should eql(content_for(f)) } }
131 end
132
133 it 'should not archive any excluded files' do
134 archive(@archive).include(@files).exclude(@files.last).invoke
135 inspect_archive do |archive|
136 archive.keys.should include(File.basename(@files.first))
137 archive.keys.should_not include(File.basename(@files.last))
138 end
139 end
140
141 it 'should not archive any excluded files in included directories' do
142 archive(@archive).include(@dir).exclude(@files.last).invoke
143 inspect_archive do |archive|
144 archive.keys.should include('test/' + File.basename(@files.first))
145 archive.keys.should_not include('test/' + File.basename(@files.last))
146 end
147 end
148
149 it 'should not archive any excluded files when using :from/:as' do
150 archive(@archive).include(:from=>@dir).exclude(@files.last).invoke
151 inspect_archive do |archive|
152 archive.keys.should include(File.basename(@files.first))
153 archive.keys.should_not include(File.basename(@files.last))
154 end
155 end
156
157 it 'should raise error when using :from with nil value' do
158 lambda {
159 archive(@archive).include(:from=>nil)
160 }.should raise_error
161 end
162
163 it 'should exclude entire directory and all its children' do
164 mkpath "#{@dir}/sub"
165 write "#{@dir}/sub/test"
166 archive(@archive).include(@dir).exclude("#{@dir}/sub").invoke
167 inspect_archive do |archive|
168 archive.keys.select { |file| file =~ /sub/ }.should be_empty
169 end
170 end
171
172 it 'should not archive any excluded files when pattern is *.ext' do
173 write "test/file.txt"
174 write "test/file.swf"
175 archive(@archive).include(@dir).exclude('**/*.swf').invoke
176 inspect_archive do |archive|
177 archive.keys.should include('test/file.txt')
178 archive.keys.should_not include('test/file.swf')
179 end
180 end
181
182 it 'should archive files into specified path' do
183 archive(@archive).include(@files, :path=>'code').invoke
184 inspect_archive { |archive| @files.each { |f| archive['code/' + File.basename(f)].should eql(content_for(f)) } }
185 end
186
187 it 'should include entry for directory' do
188 archive(@archive).include(@dir).invoke
189 inspect_archive { |archive| @files.each { |f| archive['test/' + File.basename(f)].should eql(content_for(f)) } }
190 end
191
192 it 'should archive files into specified path' do
193 archive(@archive).include(@files, :path=>'code').invoke
194 inspect_archive { |archive| @files.each { |f| archive['code/' + File.basename(f)].should eql(content_for(f)) } }
195 end
196
197 it 'should archive directories into specified path' do
198 archive(@archive).include(@dir, :path=>'code').invoke
199 inspect_archive { |archive| @files.each { |f| archive['code/test/' + File.basename(f)].should eql(content_for(f)) } }
200 end
201
202 it 'should understand . in path' do
203 archive(@archive).path('.').should == archive(@archive).path('')
204 archive(@archive).path('foo').path('.').should == archive(@archive).path('foo')
205 end
206
207 it 'should understand .. in path' do
208 archive(@archive).path('..').should == archive(@archive).path('')
209 archive(@archive).path('foo').path('..').should == archive(@archive).path('')
210 archive(@archive).path('foo/bar').path('..').should == archive(@archive).path('foo')
211 end
212
213 it 'should understand leading / in path' do
214 archive(@archive).path('/').should == archive(@archive).path('')
215 archive(@archive).path('foo/bar').path('/').should == archive(@archive).path('')
216 end
217
218 it 'should archive file into specified name' do
219 archive(@archive).include(@files.first, :as=>'test/sample').invoke
220 inspect_archive { |archive| @files.each { |f| archive['test/sample'].should eql(content_for(@files.first)) } }
221 end
222
223 it 'should archive directory into specified alias, without using "."' do
224 archive(@archive).include(@dir, :as=>'.').invoke
225 inspect_archive { |archive| archive.keys.should_not include(".") }
226 end
227
228 it 'should archive directories into specified alias, even if it has the same name' do
229 archive(@archive).include(@dir, :as=>File.basename(@dir)).invoke
230 inspect_archive { |archive|
231 archive.keys.should_not include "#{File.basename(@dir)}"
232 }
233 end
234
235 it 'should archive file into specified name/path' do
236 archive(@archive).include(@files.first, :as=>'test/sample', :path=>'path').invoke
237 inspect_archive { |archive| @files.each { |f| archive['path/test/sample'].should eql(content_for(@files.first)) } }
238 end
239
240 it 'should archive files starting with dot' do
241 write 'test/.config', '# configuration'
242 archive(@archive).include('test').invoke
243 inspect_archive { |archive| @files.each { |f| archive['test/.config'].should eql('# configuration') } }
244 end
245
246 it 'should archive directory into specified name' do
247 archive(@archive).include(@dir, :as=>'code').invoke
248 inspect_archive { |archive| @files.each { |f| archive['code/' + File.basename(f)].should eql(content_for(f)) } }
249 end
250
251 it 'should archive directory into specified name/path' do
252 archive(@archive).include(@dir, :as=>'code', :path=>'path').invoke
253 inspect_archive { |archive| @files.each { |f| archive['path/code/' + File.basename(f)].should eql(content_for(f)) } }
254 end
255
256 it 'should archive directory contents' do
257 archive(@archive).include(@dir, :as=>'.').invoke
258 inspect_archive { |archive| @files.each { |f| archive[File.basename(f)].should eql(content_for(f)) } }
259 end
260
261 it 'should archive directory contents into specified path' do
262 archive(@archive).include(@dir, :as=>'.', :path=>'path').invoke
263 inspect_archive { |archive| @files.each { |f| archive['path/' + File.basename(f)].should eql(content_for(f)) } }
264 end
265
266 it 'should not allow two files with the :as argument' do
267 lambda { archive(@archive).include(@files.first, @files.last, :as=>'test/sample') }.should raise_error(RuntimeError, /one file/)
268 end
269
270 it 'should expand another archive file' do
271 create_for_merge do |src|
272 archive(@archive).merge(src)
273 archive(@archive).invoke
274 inspect_archive { |archive| @files.each { |f| archive[File.basename(f)].should eql(content_for(f)) } }
275 end
276 end
277
278 it 'should expand another archive file with include pattern' do
279 create_for_merge do |src|
280 archive(@archive).merge(src).include(File.basename(@files.first))
281 archive(@archive).invoke
282 inspect_archive do |archive|
283 archive[File.basename(@files.first)].should eql(content_for(@files.first))
284 archive[File.basename(@files.last)].should be_nil
285 end
286 end
287 end
288
289 it 'should expand another archive file with exclude pattern' do
290 create_for_merge do |src|
291 archive(@archive).merge(src).exclude(File.basename(@files.first))
292 archive(@archive).invoke
293 inspect_archive do |archive|
294 @files[1..-1].each { |f| archive[File.basename(f)].should eql(content_for(f)) }
295 archive[File.basename(@files.first)].should be_nil
296 end
297 end
298 end
299
300 it 'should expand another archive file with nested exclude pattern' do
301 @files = %w{Test1.txt Text2.html}.map { |file| File.join(@dir, "foo", file) }.
302 each { |file| write file, content_for(file) }
303 zip(qualify(@archive, "src")).include(@dir).tap do |task|
304 archive(@archive).merge(task).exclude('test/*')
305 archive(@archive).invoke
306 inspect_archive.should be_empty
307 end
308 end
309
310 it 'should merge archives, concatenate file contents' do
311 @files = %w{foo1 foo2}.map { |folder| File.join(@dir, folder) }.
312 map do |dir|
313 txt_file = File.join(dir, 'test1.txt')
314 write txt_file, content_for('test1.txt')
315 zip(File.join(dir, 'test1.zip')).include(txt_file)
316 end
317 archive(@archive).merge(@files).concatenate("test1.txt")
318 archive(@archive).invoke
319 inspect_archive do |archive|
320 archive['test1.txt'].should eql(content_for('test1.txt') * @files.size)
321 end
322 end
323
324 it 'should expand another archive file into path' do
325 create_for_merge do |src|
326 archive(@archive).path('test').merge(src)
327 archive(@archive).invoke
328 inspect_archive { |archive| @files.each { |f| archive['test/' + File.basename(f)].should eql(content_for(f)) } }
329 end
330 end
331
332 it 'should expand another archive file into path with :path option' do
333 create_for_merge do |src|
334 archive(@archive).merge(src, :path=>'test')
335 archive(@archive).invoke
336 inspect_archive { |archive| @files.each { |f| archive['test/' + File.basename(f)].should eql(content_for(f)) } }
337 end
338 end
339
340 it "should expand another archive file into path with :path=>'/'" do
341 create_for_merge do |src|
342 archive(@archive).merge(src, :path=>'/')
343 archive(@archive).invoke
344 inspect_archive { |archive| @files.each { |f| archive[File.basename(f)].should eql(content_for(f)) } }
345 end
346 end
347
348 it 'should expand another archive file into path with merge option' do
349 create_for_merge do |src|
350 archive(@archive).include(src, :merge=>true)
351 archive(@archive).invoke
352 inspect_archive { |archive| @files.each { |f| archive[File.basename(f)].should eql(content_for(f)) } }
353 end
354 end
355
356 it 'should update if one of the files is recent' do
357 create_without_task { |archive| archive.include(@files) }
358 # Touch archive file to some point in the past. This effectively makes
359 # all included files newer.
360 File.utime Time.now - 100, Time.now - 100, @archive
361 archive(@archive).include(@files).invoke
362 File.stat(@archive).mtime.should be_within(10).of(Time.now)
363 end
364
365 it 'should update if a file in a subdir is more recent' do
366 subdir = File.expand_path("subdir", @dir)
367 test3 = File.expand_path("test3.css", subdir)
368
369 mkdir_p subdir
370 write test3, '/* Original */'
371
372 create_without_task { |archive| archive.include(:from => @dir) }
373 inspect_archive { |archive| archive["subdir/test3.css"].should eql('/* Original */') }
374
375 write test3, '/* Refreshed */'
376 File.utime(Time.now + 100, Time.now + 100, test3)
377 archive(@archive).include(:from => @dir).invoke
378 inspect_archive { |archive| archive["subdir/test3.css"].should eql('/* Refreshed */') }
379 end
380
381 it 'should do nothing if all files are uptodate' do
382 create_without_task { |archive| archive.include(@files) }
383 # By touching all files in the past, there's nothing new to update.
384 (@files + [@archive]).each { |f| File.utime Time.now - 100, Time.now - 100, f }
385 archive(@archive).include(@files).invoke
386 File.stat(@archive).mtime.should be_within(10).of(Time.now - 100)
387 end
388
389 it 'should update if one of the files is recent' do
390 create_without_task { |archive| archive.include(@files) }
391 # Change files, we expect to see new content.
392 write @files.first, '/* Refreshed */'
393 File.utime(Time.now - 100, Time.now - 100, @archive) # Touch archive file to some point in the past.
394 archive(@archive).include(@files).invoke
395 inspect_archive { |archive| archive[File.basename(@files.first)].should eql('/* Refreshed */') }
396 end
397
398 it 'should create new archive when updating' do
399 create_without_task { |archive| archive.include(@files) }
400 File.utime(Time.now - 100, Time.now - 100, @archive) # Touch archive file to some point in the past.
401 archive(@archive).include(@files[1..-1]).invoke
402 inspect_archive.size.should be(@files.size - 1)
403 end
404
405 it 'should not accept invalid options' do
406 archive(@archive).include(@files)
407 lambda { archive(@archive).with :option=>true }.should raise_error
408 end
409
410 it 'should invoke paths supplied in from parameters' do
411 included_file = File.expand_path("somefile.myext")
412 write included_file, content_for(included_file)
413 archive2_filename = File.expand_path("somebug.zip")
414 a2 = zip(archive2_filename).
415 include(included_file, :as => 'folder1/somefile1.ext').
416 include(included_file, :as => 'folder2/somefile2.ext').
417 invoke
418 a = archive(@archive)
419 f1 = unzip('target/folder1' => archive2_filename).from_path("folder1/*").root
420 f2 = unzip('target/folder2' => archive2_filename).from_path("folder2/*").root
421 a.include(:from => f1)
422 a.include(:from => f2)
423 a.invoke
424 contents = inspect_archive
425 contents["folder1/somefile1.ext"].should_not be_nil
426 contents["folder2/somefile2.ext"].should_not be_nil
427 end
428 end
429
430 describe TarTask do
431 it_should_behave_like 'ArchiveTask'
432
433 before(:each) do
434 @archive = File.expand_path('test.tar')
435 end
436
437 define_method(:archive) { |file| tar(file) }
438
439 def inspect_archive
440 entries = {}
441 Archive::Tar::Minitar.open @archive, 'r' do |reader|
442 reader.each { |entry| entries[entry.directory ? "#{entry.name}/" : entry.name] = entry.read }
443 end
444 yield entries if block_given?
445 entries
446 end
447
448 # chmod is not reliable on Windows
449 unless Buildr::Util.win_os?
450 it 'should preserve file permissions' do
451 # with JRuby it's important to use absolute paths with File.chmod()
452 # http://jira.codehaus.org/browse/JRUBY-3300
453 hello = File.expand_path('src/main/bin/hello')
454 write hello, 'echo hi'
455 File.chmod(0777, hello)
456 fail("Failed to set permission on #{hello}") unless (File.stat(hello).mode & 0777) == 0777
457
458 tar('foo.tgz').include('src/main/bin/*').invoke
459 unzip('target' => 'foo.tgz').extract
460 (File.stat('target/hello').mode & 0777).should == 0777
461 end
462
463 it 'should preserve file permissions when merging zip files' do
464 # with JRuby it's important to use absolute paths with File.chmod()
465 # http://jira.codehaus.org/browse/JRUBY-3300
466 hello = File.expand_path('src/main/bin/hello')
467 write hello, 'echo hi'
468 File.chmod(0777, hello)
469 fail("Failed to set permission on #{hello}") unless (File.stat(hello).mode & 0777) == 0777
470
471 foo = zip('foo.zip')
472 foo.include('src/main/bin/*').invoke
473 bar = tar('bar.tgz')
474 bar.merge(foo)
475 bar.invoke
476 unzip('target' => 'bar.tgz').extract
477 (File.stat('target/hello').mode & 0777).should == 0777
478 end
479 end
480
481 end
482
483
484 describe TarTask, ' gzipped' do
485 it_should_behave_like 'ArchiveTask'
486
487 before(:each) do
488 @archive = File.expand_path('test.tgz')
489 end
490
491 define_method(:archive) { |file| tar(file) }
492
493 def inspect_archive
494 entries = {}
495 Zlib::GzipReader.open @archive do |gzip|
496 Archive::Tar::Minitar.open gzip, 'r' do |reader|
497 reader.each { |entry| entries[entry.directory ? "#{entry.name}/" : entry.name] = entry.read }
498 end
499 end
500 yield entries if block_given?
501 entries
502 end
503 end
504
505 describe "ZipTask" do
506 include ArchiveTaskHelpers
507
508 it_should_behave_like 'ArchiveTask'
509
510 before(:each) do
511 init_dir
512 @archive = File.expand_path('test.zip')
513 end
514
515 define_method(:archive) { |file| zip(file) }
516
517 after(:each) do
518 checkZip(@archive)
519 end
520
521 # Check for possible corruption using Java's ZipInputStream and Java's "jar" command since
522 # they are stricter than rubyzip
523 def checkZip(file)
524 return unless File.exist?(file)
525 zip = Java.java.util.zip.ZipInputStream.new(Java.java.io.FileInputStream.new(file))
526 zip_entry_count = 0
527 while entry = zip.getNextEntry do
528 # just iterate over all entries
529 zip_entry_count = zip_entry_count + 1
530 end
531 zip.close()
532
533 # jar tool fails with "ZipException: error in opening zip file" if empty
534 if zip_entry_count > 0
535 sh "#{File.join(ENV['JAVA_HOME'], 'bin', 'jar')} tvf #{file}"
536 end
537 end
538
539 def inspect_archive
540 entries = {}
541 Zip::File.open @archive do |zip|
542 zip.entries.each do |entry|
543 if entry.directory?
544 # Ignore the / directory created for empty ZIPs when using java.util.zip.
545 if entry.name.to_s != '/'
546 entries[entry.name.to_s] = nil
547 end
548 else
549 entries[entry.name.to_s] = zip.read(entry.name)
550 end
551 end
552 end
553 yield entries if block_given?
554 entries
555 end
556
557 it 'should include empty dirs' do
558 archive(@archive).include(@dir)
559 archive(@archive).invoke
560 inspect_archive do |archive|
561 archive.keys.should include('test/EmptyDir1/')
562 end
563 end
564
565 it 'should include empty dirs from Dir' do
566 archive(@archive).include(Dir["#{@dir}/*"])
567 archive(@archive).invoke
568 inspect_archive do |archive|
569 archive.keys.should include('EmptyDir1/')
570 end
571 end
572
573 it 'should work with path object' do
574 archive(@archive).path('code').include(@files)
575 archive(@archive).invoke
576 inspect_archive { |archive| archive.keys.should include('code/') }
577 end
578
579 it 'should have path object that includes empty dirs' do
580 archive(@archive).path('code').include(Dir["#{@dir}/*"])
581 archive(@archive).invoke
582 inspect_archive do |archive|
583 archive.keys.should include('code/EmptyDir1/')
584 end
585 end
586
587 # chmod is not reliable on Windows
588 unless Buildr::Util.win_os?
589 it 'should preserve file permissions' do
590 # with JRuby it's important to use absolute paths with File.chmod()
591 # http://jira.codehaus.org/browse/JRUBY-3300
592 hello = File.expand_path('src/main/bin/hello')
593 write hello, 'echo hi'
594 File.chmod(0777, hello)
595 fail("Failed to set permission on #{hello}") unless (File.stat(hello).mode & 0777) == 0777
596
597 zip('foo.zip').include('src/main/bin/*').invoke
598 unzip('target' => 'foo.zip').extract
599 (File.stat('target/hello').mode & 0777).should == 0777
600 end
601
602 it 'should preserve file permissions when merging zip files' do
603 # with JRuby it's important to use absolute paths with File.chmod()
604 # http://jira.codehaus.org/browse/JRUBY-3300
605 hello = File.expand_path('src/main/bin/hello')
606 write hello, 'echo hi'
607 File.chmod(0777, hello)
608 fail("Failed to set permission on #{hello}") unless (File.stat(hello).mode & 0777) == 0777
609
610 foo = zip('foo.zip')
611 foo.include('src/main/bin/*').invoke
612 bar = zip('bar.zip')
613 bar.merge(foo)
614 bar.invoke
615 unzip('target' => 'bar.zip').extract
616 (File.stat('target/hello').mode & 0777).should == 0777
617 end
618 end
619
620 end
621
622 describe Unzip do
623 before(:each) do
624 @zip = File.expand_path('test.zip')
625 @dir = File.expand_path('test')
626 @files = %w{Test1.txt Text2.html}.map { |file| File.join(@dir, file) }.
627 each { |file| write file, content_for(file) }
628 @target = File.expand_path('target')
629 @targz = File.expand_path('test.tar.gz')
630 @targz2 = File.expand_path('test.tgz')
631 end
632
633 # Not too smart, we just create some content based on file name to
634 # make sure you read what you write.
635 def content_for(file)
636 "Content for #{File.basename(file)}"
637 end
638
639 def with_tar(*args)
640 tar(@targz).include(*args.empty? ? @files : args).invoke
641 yield
642 end
643
644 def with_tar_too(*args)
645 tar(@targz2).include(*args.empty? ? @files : args).invoke
646 yield
647 end
648
649 def with_zip(*args)
650 zip(@zip).include(*args.empty? ? @files : args).invoke
651 yield
652 end
653
654 it 'should touch target directory' do
655 with_zip do
656 mkdir @target
657 File.utime(Time.now - 10, Time.now - 10, @target)
658 unzip(@target=>@zip).target.invoke
659 end
660 File.stat(@target).mtime.should be_within(2).of(Time.now)
661 end
662
663 it 'should expand files' do
664 with_zip do
665 unzip(@target=>@zip).target.invoke
666 @files.each { |f| File.read(File.join(@target, File.basename(f))).should eql(content_for(f)) }
667 end
668 end
669
670 it 'should expand files from a tar.gz file' do
671 with_tar do
672 unzip(@target=>@targz).target.invoke
673 @files.each { |f| File.read(File.join(@target, File.basename(f))).should eql(content_for(f)) }
674 end
675 end
676
677 it 'should expand files from a .tgz file' do
678 with_tar_too do
679 unzip(@target=>@targz2).target.invoke
680 @files.each { |f| File.read(File.join(@target, File.basename(f))).should eql(content_for(f)) }
681 end
682 end
683
684 it 'should expand all files' do
685 with_zip do
686 unzip(@target=>@zip).target.invoke
687 FileList[File.join(@target, '*')].size.should be(@files.size)
688 end
689 end
690
691 it 'should expand all files from a .tar.gz file' do
692 with_tar do
693 unzip(@target=>@targz).target.invoke
694 FileList[File.join(@target, '*')].size.should be(@files.size)
695 end
696 end
697
698 it 'should expand only included files' do
699 with_zip do
700 only = File.basename(@files.first)
701 unzip(@target=>@zip).include(only).target.invoke
702 FileList[File.join(@target, '*')].should include(File.expand_path(only, @target))
703 FileList[File.join(@target, '*')].size.should be(1)
704 end
705 end
706
707 it 'should expand only included files from a .tar.gz file' do
708 with_tar do
709 only = File.basename(@files.first)
710 unzip(@target=>@targz).include(only).target.invoke
711 FileList[File.join(@target, '*')].should include(File.expand_path(only, @target))
712 FileList[File.join(@target, '*')].size.should be(1)
713 end
714 end
715
716 it 'should expand all but excluded files' do
717 with_zip do
718 except = File.basename(@files.first)
719 unzip(@target=>@zip).exclude(except).target.invoke
720 FileList[File.join(@target, '*')].should_not include(File.expand_path(except, @target))
721 FileList[File.join(@target, '*')].size.should be(@files.size - 1)
722 end
723 end
724
725 it 'should expand all but excluded files with a .tar.gz file' do
726 with_tar do
727 except = File.basename(@files.first)
728 unzip(@target=>@targz).exclude(except).target.invoke
729 FileList[File.join(@target, '*')].should_not include(File.expand_path(except, @target))
730 FileList[File.join(@target, '*')].size.should be(@files.size - 1)
731 end
732 end
733
734 it 'should include with nested path patterns' do
735 with_zip @files, :path=>'test/path' do
736 only = File.basename(@files.first)
737 unzip(@target=>@zip).include(only).target.invoke
738 FileList[File.join(@target, '*')].should be_empty
739
740 Rake::Task.clear ; rm_rf @target
741 unzip(@target=>@zip).include('test/path/' + only).target.invoke
742 FileList[File.join(@target, 'test/path/*')].size.should be(1)
743
744 Rake::Task.clear ; rm_rf @target
745 unzip(@target=>@zip).include('test/**/*').target.invoke
746 FileList[File.join(@target, 'test/path/*')].size.should be(2)
747
748 Rake::Task.clear ; rm_rf @target
749 unzip(@target=>@zip).include('test/*').target.invoke
750 FileList[File.join(@target, 'test/path/*')].size.should be(2)
751 end
752 end
753
754 it 'should include with nested path patterns with a .tar.gz file' do
755 with_tar @files, :path=>'test/path' do
756 only = File.basename(@files.first)
757 unzip(@target=>@targz).include(only).target.invoke
758 FileList[File.join(@target, '*')].should be_empty
759
760 Rake::Task.clear ; rm_rf @target
761 unzip(@target=>@targz).include('test/path/' + only).target.invoke
762 FileList[File.join(@target, 'test/path/*')].size.should be(1)
763
764 Rake::Task.clear ; rm_rf @target
765 unzip(@target=>@targz).include('test/**/*').target.invoke
766 FileList[File.join(@target, 'test/path/*')].size.should be(2)
767 end
768 end
769
770 it 'should include with relative path' do
771 with_zip @files, :path=>'test/path' do
772 only = File.basename(@files.first)
773 unzip(@target=>@zip).tap { |unzip| unzip.from_path('test').include(only) }.target.invoke
774 FileList[File.join(@target, '*')].should be_empty
775
776 Rake::Task.clear ; rm_rf @target
777 unzip(@target=>@zip).tap { |unzip| unzip.from_path('test').include('test/*') }.target.invoke
778 FileList[File.join(@target, 'path/*')].should be_empty
779
780 Rake::Task.clear ; rm_rf @target
781 unzip(@target=>@zip).tap { |unzip| unzip.from_path('test').include('path/*' + only) }.target.invoke
782 FileList[File.join(@target, 'path/*')].size.should be(1)
783
784 Rake::Task.clear ; rm_rf @target
785 unzip(@target=>@zip).tap { |unzip| unzip.from_path('test').include('path/*') }.target.invoke
786 FileList[File.join(@target, 'path/*')].size.should be(2)
787 end
788 end
789
790 it 'should include with relative path with a .tar.gz file' do
791 with_tar @files, :path=>'test/path' do
792 only = File.basename(@files.first)
793 unzip(@target=>@targz).tap { |unzip| unzip.from_path('test').include(only) }.target.invoke
794 FileList[File.join(@target, '*')].should be_empty
795
796 Rake::Task.clear ; rm_rf @target
797 unzip(@target=>@targz).tap { |unzip| unzip.from_path('test').include('test/*') }.target.invoke
798 FileList[File.join(@target, 'path/*')].should be_empty
799
800 Rake::Task.clear ; rm_rf @target
801 unzip(@target=>@targz).tap { |unzip| unzip.from_path('test').include('path/*' + only) }.target.invoke
802 FileList[File.join(@target, 'path/*')].size.should be(1)
803
804 Rake::Task.clear ; rm_rf @target
805 unzip(@target=>@targz).tap { |unzip| unzip.from_path('test').include('path/*') }.target.invoke
806 FileList[File.join(@target, 'path/*')].size.should be(2)
807 end
808 end
809
810 it 'should exclude with relative path' do
811 with_zip @files, :path=>'test' do
812 except = File.basename(@files.first)
813 unzip(@target=>@zip).tap { |unzip| unzip.from_path('test').exclude(except) }.target.invoke
814 FileList[File.join(@target, '*')].should include(File.join(@target, File.basename(@files[1])))
815 FileList[File.join(@target, '*')].size.should be(@files.size - 1)
816 end
817 end
818
819 it 'should exclude with relative path on a tar.gz file' do
820 with_tar @files, :path=>'test' do
821 except = File.basename(@files.first)
822 unzip(@target=>@targz).tap { |unzip| unzip.from_path('test').exclude(except) }.target.invoke
823 FileList[File.join(@target, '*')].should include(File.join(@target, File.basename(@files[1])))
824 FileList[File.join(@target, '*')].size.should be(@files.size - 1)
825 end
826 end
827
828 it "should handle relative paths without any includes or excludes" do
829 lib_files = %w{Test3.so Test4.rb}.
830 map { |file| File.join(@dir, file) }.
831 each { |file| write file, content_for(file) }
832 zip(@zip).include(@files, :path => 'src').include(lib_files, :path => 'lib').invoke
833
834 unzip(@target=>@zip).tap { |unzip| unzip.from_path('lib') }.target.invoke
835 FileList[File.join(@target, '**/*')].should have(2).files
836 end
837
838 it "should handle relative paths without any includes or excludes with a tar.gz file" do
839 lib_files = %w{Test3.so Test4.rb}.
840 map { |file| File.join(@dir, file) }.
841 each { |file| write file, content_for(file) }
842 tar(@targz).include(@files, :path => 'src').include(lib_files, :path => 'lib').invoke
843
844 unzip(@target=>@targz).tap { |unzip| unzip.from_path('lib') }.target.invoke
845 FileList[File.join(@target, '**/*')].should have(2).files
846 end
847
848 it 'should return itself from root method' do
849 task = unzip(@target=>@zip)
850 task.root.should be(task)
851 task.from_path('foo').root.should be(task)
852 end
853
854 it 'should return target task from target method' do
855 task = unzip(@target=>@zip)
856 task.target.should be(file(@target))
857 task.from_path('foo').target.should be(file(@target))
858 end
859
860 it 'should alias from_path as path' do
861 task = unzip(@target=>@zip)
862 task.from_path('foo').should be(task.path('foo'))
863 end
864
865 end