Fix failure in Archive::Tar tests when perl is built
[p5sagit/p5-mst-13.2.git] / lib / Archive / Tar / t / 02_methods.t
1 BEGIN {
2     if( $ENV{PERL_CORE} ) {
3         chdir '../lib/Archive/Tar' if -d '../lib/Archive/Tar';
4     }       
5     use lib '../../..';
6 }
7
8 BEGIN { chdir 't' if -d 't' }
9
10 use Test::More 'no_plan';
11 use strict;
12 use lib '../lib';
13
14 use Cwd;
15 use IO::File;
16 use File::Copy;
17 use File::Path;
18 use File::Spec          ();
19 use File::Spec::Unix    ();
20 use File::Basename      ();
21 use Data::Dumper;
22
23 use Archive::Tar;
24 use Archive::Tar::Constant;
25
26 ### XXX TODO:
27 ### * change to fullname
28 ### * add tests for global variables
29
30 ### set up the environment ###
31 my @EXPECT_NORMAL = (
32     ### dirs        filename    contents
33     [   [],         'c',        qr/^iiiiiiiiiiii\s*$/ ],
34     [   [],         'd',        qr/^uuuuuuuu\s*$/ ],
35 );
36
37 ### includes binary data
38 my $ALL_CHARS = join '', "\r\n", map( chr, 1..255 ), "zzz\n\r";
39
40 ### @EXPECTBIN is used to ensure that $tarbin is written in the right
41 ### order and that the contents and order match exactly when extracted
42 my @EXPECTBIN = (
43     ###  dirs   filename      contents       ###
44     [    [],    'bIn11',      $ALL_CHARS x 11 ],
45     [    [],    'bIn3',       $ALL_CHARS x  3 ],
46     [    [],    'bIn4',       $ALL_CHARS x  4 ],
47     [    [],    'bIn1',       $ALL_CHARS      ],
48     [    [],    'bIn2',       $ALL_CHARS x  2 ],
49 );
50
51 ### @EXPECTX is used to ensure that $tarx is written in the right
52 ### order and that the contents and order match exactly when extracted
53 ### the 'x/x' extraction used to fail before A::T 1.08
54 my @EXPECTX = (
55     ###  dirs       filename    contents
56     [    [ 'x' ],   'k',        '',     ],
57     [    [ 'x' ],   'x',        'j',    ],   # failed before A::T 1.08
58 );
59
60 my $LONG_FILE = qq[directory/really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-really-long-directory-name/myfile];
61
62 ### wintendo can't deal with too long paths, so we might have to skip tests ###
63 my $TOO_LONG    =   ($^O eq 'MSWin32' or $^O eq 'cygwin' or $^O eq 'VMS')
64                     && length( cwd(). $LONG_FILE ) > 247;
65
66 ### warn if we are going to skip long file names
67 $TOO_LONG ? diag("No long filename support - long filename extraction disabled")
68           : ( push @EXPECT_NORMAL, [ [], $LONG_FILE, qr/^hello\s*$/] ) ;
69
70 my @ROOT        = grep { length }   'src', $TOO_LONG ? 'short' : 'long';
71
72 my $ZLIB        = eval { require IO::Zlib; 1 } ? 1 : 0;
73 my $NO_UNLINK   = $ARGV[0] ? 1 : 0;
74
75 ### enable debugging?
76 $Archive::Tar::DEBUG = 1 if $ARGV[1];
77
78 ### tests for binary and x/x files
79 my $TARBIN      = Archive::Tar->new;
80 my $TARX        = Archive::Tar->new;
81
82 ### paths to a .tar and .tgz file to use for tests
83 my $TAR_FILE        = File::Spec->catfile( @ROOT, 'bar.tar' );
84 my $TGZ_FILE        = File::Spec->catfile( @ROOT, 'foo.tgz' );
85 my $OUT_TAR_FILE    = File::Spec->catfile( @ROOT, 'out.tar' );
86 my $OUT_TGZ_FILE    = File::Spec->catfile( @ROOT, 'out.tgz' );
87
88 my $COMPRESS_FILE = 'copy';
89 $^O eq 'VMS' and $COMPRESS_FILE .= '.';
90 copy( File::Basename::basename($0), $COMPRESS_FILE );
91 chmod 0644, $COMPRESS_FILE;
92
93 ### done setting up environment ###
94
95
96 ### did we probe IO::Zlib support ok? ###
97 {   is( Archive::Tar->can_handle_compressed_files, $ZLIB,
98                                     "Proper IO::Zlib support detected" );
99 }
100
101
102 ### tar error tests
103 {   my $tar     = Archive::Tar->new;
104
105     ok( $tar,                       "Object created" );
106     isa_ok( $tar,                   'Archive::Tar');
107
108     local $Archive::Tar::WARN  = 0;
109
110     ### should be empty to begin with
111     is( $tar->error, '',            "The error string is empty" );
112
113     ### try a read on nothing
114     my @list = $tar->read();
115
116     ok(!(scalar @list),             "Function read returns 0 files on error" );
117     ok( $tar->error,                "   error string is non empty" );
118     like( $tar->error, qr/No file to read from/,
119                                     "   error string from create()" );
120     unlike( $tar->error, qr/add/,   "   error string does not contain add" );
121
122     ### now, add empty data
123     my $obj = $tar->add_data( '' );
124
125     ok( !$obj,                      "'add_data' returns undef on error" );
126     ok( $tar->error,                "   error string is non empty" );
127     like( $tar->error, qr/add/,     "   error string contains add" );
128     unlike( $tar->error, qr/create/,"   error string does not contain create" );
129
130     ### check if ->error eq $error
131     is( $tar->error, $Archive::Tar::error,
132                                     '$error matches error() method' );
133 }
134
135 ### read tests ###
136 {   ### normal tar + gz compressed file
137     my $archive         = $TAR_FILE;
138     my $compressed      = $TGZ_FILE;
139     my $tar             = Archive::Tar->new;
140     my $gzip            = 0;
141
142     ### check we got the object
143     ok( $tar,                       "Object created" );
144     isa_ok( $tar,                   'Archive::Tar');
145
146     for my $type( $archive, $compressed ) {
147         my $state = $gzip ? 'compressed' : 'uncompressed';
148
149         SKIP: {
150
151             ### skip gz compressed archives wihtout IO::Zlib
152             skip(   "No IO::Zlib - cannot read compressed archives",
153                     4 + 2 * (scalar @EXPECT_NORMAL)
154             ) if( $gzip and !$ZLIB);
155
156             ### ->read test
157             {   my @list    = $tar->read( $type );
158                 my $cnt     = scalar @list;
159                 my $expect  = scalar __PACKAGE__->get_expect();
160
161                 ok( $cnt,           "Reading $state file using 'read()'" );
162                 is( $cnt, $expect,  "   All files accounted for" );
163
164                 for my $file ( @list ) {
165                     ok( $file,      "Got File object" );
166                     isa_ok( $file,  "Archive::Tar::File" );
167
168                     next unless $file->is_file;
169
170                     my $name = $file->full_path;
171                     my($expect_name, $expect_content) =
172                         get_expect_name_and_contents( $name, \@EXPECT_NORMAL );
173
174                     ### ->fullname!
175                     ok($expect_name,"   Found expected file '$name'" );
176
177                     like($tar->get_content($name), $expect_content,
178                                     "   Content OK" );
179                 }
180             }
181
182
183             ### list_archive test
184             {   my @list    = Archive::Tar->list_archive( $archive );
185                 my $cnt     = scalar @list;
186                 my $expect  = scalar __PACKAGE__->get_expect();
187
188                 ok( $cnt,           "Reading $state file using 'list_archive'");
189                 is( $cnt, $expect,  "   All files accounted for" );
190
191                 for my $file ( @list ) {
192                     next if __PACKAGE__->is_dir( $file ); # directories
193
194                     my($expect_name, $expect_content) =
195                         get_expect_name_and_contents( $file, \@EXPECT_NORMAL );
196
197                     ok( $expect_name,
198                                     "   Found expected file '$file'" );
199                 }
200             }
201         }
202
203         ### now we try gz compressed archives
204         $gzip++;
205     }
206 }
207
208 ### add files tests ###
209 {   my @add     = map { File::Spec->catfile( @ROOT, @$_ ) } ['b'];
210     my @addunix = map { File::Spec::Unix->catfile( @ROOT, @$_ ) } ['b'];
211     my $tar     = Archive::Tar->new;
212
213     ### check we got the object
214     ok( $tar,                       "Object created" );
215     isa_ok( $tar,                   'Archive::Tar');
216
217     ### add the files
218     {   my @files = $tar->add_files( @add );
219
220         is( scalar @files, scalar @add,
221                                     "Adding files");
222         is( $files[0]->name, 'b',   "   Proper name" );
223         use Config;
224         if ($ENV{PERL_CORE} and $Config{config_args} =~/Dmksymlinks/) {
225             ok( !$files[0]->is_file,  "   Proper type" );
226         }
227         else {
228             is( $files[0]->is_file, 1,  "   Proper type" );
229         }
230         like( $files[0]->get_content, qr/^bbbbbbbbbbb\s*$/,
231                                     "   Content OK" );
232
233         ### check if we have then in our tar object
234         for my $file ( @addunix ) {
235             ok( $tar->contains_file($file),
236                                     "   File found in archive" );
237         }
238     }
239
240     ### check adding files doesn't conflict with a secondary archive
241     ### old A::T bug, we should keep testing for it
242     {   my $tar2    = Archive::Tar->new;
243         my @added   = $tar2->add_files( $COMPRESS_FILE );
244         my @count   = $tar2->list_files;
245
246         is( scalar @added, 1,       "Added files to secondary archive" );
247         is( scalar @added, scalar @count,
248                                     "   Does not conflict with first archive" );
249
250         ### check the adding of directories
251         my @add_dirs  = File::Spec->catfile( @ROOT );
252         my @dirs      = $tar2->add_files( @add_dirs );
253         is( scalar @dirs, scalar @add_dirs,
254                                     "Adding dirs");
255         ok( $dirs[0]->is_dir,       "   Proper type" );
256     }
257 }
258
259 ### add data tests ###
260 {
261     {   ### standard data ###
262         my @to_add  = ( 'a', 'aaaaa' );
263         my $tar     = Archive::Tar->new;
264
265         ### check we got the object
266         ok( $tar,                   "Object created" );
267         isa_ok( $tar,               'Archive::Tar');
268
269         ### add a new file item as data
270         my $obj = $tar->add_data( @to_add );
271
272         ok( $obj,                   "Adding data" );
273         is( $obj->name, $to_add[0], "   Proper name" );
274         is( $obj->is_file, 1,       "   Proper type" );
275         like( $obj->get_content, qr/^$to_add[1]\s*$/,
276                                     "   Content OK" );
277     }
278
279     {   ### binary data +
280         ### dir/file structure -- x/y always went ok, x/x used to extract
281         ### in the wrong way -- this test catches that
282         for my $list (  [$TARBIN,   \@EXPECTBIN],
283                         [$TARX,     \@EXPECTX],
284         ) {
285             ### XXX GLOBAL! changes may affect other tests!
286             my($tar,$struct) = @$list;
287
288             for my $aref ( @$struct ) {
289                 my ($dirs,$file,$data) = @$aref;
290
291                 my $path = File::Spec::Unix->catfile(
292                                 grep { length } @$dirs, $file );
293
294                 my $obj = $tar->add_data( $path, $data );
295
296                 ok( $obj,               "Adding data '$file'" );
297                 is( $obj->full_path, $path,
298                                         "   Proper name" );
299                 ok( $obj->is_file,      "   Proper type" );
300                 is( $obj->get_content, $data,
301                                         "   Content OK" );
302             }
303         }
304     }
305 }
306
307 ### rename/replace_content tests ###
308 {   my $tar     = Archive::Tar->new;
309     my $from    = 'c';
310     my $to      = 'e';
311
312     ### read in the file, check the proper files are there
313     ok( $tar->read( $TAR_FILE ),    "Read in '$TAR_FILE'" );
314     ok( $tar->get_files($from),     "   Found file '$from'" );
315     {   local $Archive::Tar::WARN = 0;
316         ok(!$tar->get_files($to),   "   File '$to' not yet found" );
317     }
318
319     ### rename an entry, check the rename has happened
320     ok( $tar->rename( $from, $to ), "   Renamed '$from' to '$to'" );
321     ok( $tar->get_files($to),       "   File '$to' now found" );
322     {   local $Archive::Tar::WARN = 0;
323         ok(!$tar->get_files($from), "   File '$from' no longer found'");
324     }
325
326     ### now, replace the content
327     my($expect_name, $expect_content) =
328                         get_expect_name_and_contents( $from, \@EXPECT_NORMAL );
329
330     like( $tar->get_content($to), $expect_content,
331                                     "Original content of '$from' in '$to'" );
332     ok( $tar->replace_content( $to, $from ),
333                                     "   Set content for '$to' to '$from'" );
334     is( $tar->get_content($to), $from,
335                                     "   Content for '$to' is indeed '$from'" );
336 }
337
338 ### remove tests ###
339 {   my $remove  = 'c';
340     my $tar     = Archive::Tar->new;
341
342     ok( $tar->read( $TAR_FILE ),    "Read in '$TAR_FILE'" );
343
344     ### remove returns the files left, which should be equal to list_files
345     is( scalar($tar->remove($remove)), scalar($tar->list_files),
346                                     "Removing file '$remove'" );
347
348     ### so what's left should be all expected files minus 1
349     is( scalar($tar->list_files), scalar(__PACKAGE__->get_expect) - 1,
350                                     "   Proper files remaining" );
351 }
352
353 ### write + read + extract tests ###
354 SKIP: {
355     skip('no IO::String', 326) if   !$Archive::Tar::HAS_PERLIO && 
356                                     !$Archive::Tar::HAS_IO_STRING;
357                                     
358     my $tar = Archive::Tar->new;
359     my $new = Archive::Tar->new;
360     ok( $tar->read( $TAR_FILE ),    "Read in '$TAR_FILE'" );
361
362     for my $aref (  [$tar,    \@EXPECT_NORMAL],
363                     [$TARBIN, \@EXPECTBIN],
364                     [$TARX,   \@EXPECTX]
365     ) {
366         my($obj,$struct) = @$aref;
367
368         ### check if we stringify it ok
369         {   my $string = $obj->write;
370             ok( $string,           "Stringified tar file has size" );
371             cmp_ok( length($string) % BLOCK, '==', 0,
372                                     "Tar archive stringified" );
373         }
374
375         ### write tar tests
376         {   my $out = $OUT_TAR_FILE;
377
378             {   ### write()
379                 ok( $obj->write($out),
380                                     "Wrote tarfile using 'write'" );
381                 check_tar_file( $out );
382                 check_tar_object( $obj, $struct );
383
384                 ### now read it in again
385                 ok( $new->read( $out ),
386                                     "Read '$out' in again" );
387
388                 check_tar_object( $new, $struct );
389
390                 ### now extract it again
391                 ok( $new->extract,  "Extracted '$out' with 'extract'" );
392                 check_tar_extract( $new, $struct );
393
394                 rm( $out ) unless $NO_UNLINK;
395             }
396
397
398             {   ### create_archive()
399                 ok( Archive::Tar->create_archive( $out, 0, $COMPRESS_FILE ),
400                                     "Wrote tarfile using 'create_archive'" );
401                 check_tar_file( $out );
402
403                 ### now extract it again
404                 ok( Archive::Tar->extract_archive( $out ),
405                                     "Extracted file using 'extract_archive'");
406                 rm( $out ) unless $NO_UNLINK;
407             }
408         }
409
410         ## write tgz tests
411         {   my $out = $OUT_TGZ_FILE;
412
413             SKIP: {
414
415                 ### weird errors from scalar(@x,@y,@z), dot it this way...
416                 my $file_cnt;
417                 map { $file_cnt += scalar @$_ } \@EXPECT_NORMAL, \@EXPECTBIN,
418                                                 \@EXPECTX;
419
420                 my $cnt =   5 +                 # the tests below
421                             (5*3*2) +           # check_tgz_file
422                                                 # check_tar_object fixed tests
423                             (3 * 2 * (2 + $file_cnt)) +
424                             ((4*$file_cnt) + 1);# check_tar_extract tests
425
426                 skip( "No IO::Zlib - cannot write compressed archives", $cnt )
427                     unless $ZLIB;
428
429                 {   ### write()
430                     ok($obj->write($out, 1),
431                                     "Writing compressed file using 'write'" );
432                     check_tgz_file( $out );
433                     check_tar_object( $obj, $struct );
434
435                     ### now read it in again
436                     ok( $new->read( $out ),
437                                     "Read '$out' in again" );
438                     check_tar_object( $new, $struct );
439
440                     ### now extract it again
441                     ok( $new->extract,
442                                     "Extracted '$out' again" );
443                     check_tar_extract( $new, $struct );
444
445                     rm( $out ) unless $NO_UNLINK;
446                 }
447
448                 {   ### create_archive()
449                     ok( Archive::Tar->create_archive( $out, 1, $COMPRESS_FILE ),
450                                     "Wrote gzip file using 'create_archive'" );
451                     check_tgz_file( $out );
452
453                     ### now extract it again
454                     ok( Archive::Tar->extract_archive( $out, 1 ),
455                                     "Extracted file using 'extract_archive'");
456                     rm( $out ) unless $NO_UNLINK;
457                 }
458             }
459         }
460     }
461 }
462
463
464 ### limited read + extract tests ###
465 {   my $tar     = Archive::Tar->new;
466     my @files   = $tar->read( $TAR_FILE, 0, { limit => 1 } );
467     my $obj     = $files[0];
468
469     is( scalar @files, 1,           "Limited read" );
470
471     my ($name,$content) = get_expect_name_and_contents(
472                                 $obj->full_path, \@EXPECT_NORMAL );
473
474     is( $obj->name, $name,          "   Expected file found" );
475
476     ### extract this single file to cwd()
477     for my $meth (qw[extract extract_file]) {
478         ok( $tar->$meth( $obj->full_path ),
479                                     "Extracted '$name' to cwd() with $meth" );
480         ok( -e $obj->full_path,     "   Extracted file exists" );
481         rm( $obj->full_path ) unless $NO_UNLINK;
482     }
483
484     ### extract this file to @ROOT
485     ### can only do that with 'extract_file', not with 'extract'
486     for my $meth (qw[extract_file]) {
487         my $outpath = File::Spec->catdir( @ROOT );
488         my $outfile = File::Spec->catfile( $outpath, $$ ); #$obj->full_path );
489
490         ok( $tar->$meth( $obj->full_path, $outfile ),
491                                     "Extracted file '$name' to $outpath with $meth" );
492         ok( -e $outfile,            "   Extracted file '$outfile' exists" );
493         rm( $outfile ) unless $NO_UNLINK;
494     }
495
496 }
497
498
499 ### clear tests ###
500 {   my $tar     = Archive::Tar->new;
501     my @files   = $tar->read( $TAR_FILE );
502
503     my $cnt = $tar->list_files();
504     ok( $cnt,                       "Found old data" );
505     ok( $tar->clear,                "   Clearing old data" );
506
507     my $new_cnt = $tar->list_files;
508     ok( !$new_cnt,                  "   Old data cleared" );
509 }
510
511 ### $DO_NOT_USE_PREFIX tests
512 {   my $tar     = Archive::Tar->new;
513
514
515     ### first write a tar file without prefix
516     {   my ($obj)   = $tar->add_files( $COMPRESS_FILE );
517         my $dir     = '';   # dir is empty!
518         my $file    = File::Basename::basename( $COMPRESS_FILE );
519
520         ok( $obj,                   "File added" );
521         isa_ok( $obj,               "Archive::Tar::File" );
522
523         ### internal storage ###
524         is( $obj->name, $file,      "   Name set to '$file'" );
525         is( $obj->prefix, $dir,     "   Prefix set to '$dir'" );
526
527         ### write the tar file without a prefix in it
528         local $Archive::Tar::DO_NOT_USE_PREFIX = 1;
529         ok( $tar->write( $OUT_TAR_FILE ),
530                                     "   Tar file written" );
531
532         ### and forget all about it...
533         $tar->clear;
534     }
535
536     ### now read it back in, there should be no prefix
537     {   ok( $tar->read( $OUT_TAR_FILE ),
538                                     "Tar file read in again" );
539
540         my ($obj) = $tar->get_files;
541         ok( $obj,                   "   File retrieved" );
542         isa_ok( $obj,               "Archive::Tar::File" );
543
544         is( $obj->name, $COMPRESS_FILE,
545                                     "   Name now set to '$COMPRESS_FILE'" );
546         is( $obj->prefix, '',       "   Prefix now empty" );
547
548         my $re = quotemeta $COMPRESS_FILE;
549         like( $obj->raw, qr/^$re/,  "   Prefix + name in name slot of header" );
550     }
551
552     rm( $OUT_TAR_FILE ) unless $NO_UNLINK;
553 }
554
555 ### clean up stuff
556 END {
557     for my $struct ( \@EXPECT_NORMAL, \@EXPECTBIN, \@EXPECTX ) {
558         for my $aref (@$struct) {
559
560             my $dir = $aref->[0]->[0];
561             rmtree $dir if $dir && -d $dir && not $NO_UNLINK;
562         }
563     }
564
565     my ($dir) = File::Spec::Unix->splitdir( $LONG_FILE );
566     rmtree $dir if $dir && -d $dir && not $NO_UNLINK;
567     1 while unlink $COMPRESS_FILE;
568 }
569
570 ###########################
571 ###     helper subs     ###
572 ###########################
573 sub get_expect {
574     return  map {
575                 split '/', $_
576             } map {
577                 File::Spec::Unix->catfile(
578                     grep { defined } @{$_->[0]}, $_->[1]
579                 )
580             } @EXPECT_NORMAL;
581 }
582
583 sub is_dir {
584     my $file = pop();
585     return $file =~ m|/$| ? 1 : 0;
586 }
587
588 sub rm {
589     my $x = shift;
590     if  ( is_dir($x) ) {
591          rmtree($x);
592     } else {
593          1 while unlink $x;
594     }
595 }
596
597 sub check_tar_file {
598     my $file        = shift;
599     my $filesize    = -s $file;
600     my $contents    = slurp_binfile( $file );
601
602     ok( defined( $contents ),   "   File read" );
603     ok( $filesize,              "   File written size=$filesize" );
604
605     cmp_ok( $filesize % BLOCK,     '==', 0,
606                         "   File size is a multiple of 512" );
607
608     cmp_ok( length($contents), '==', $filesize,
609                         "   File contents match size" );
610
611     is( TAR_END x 2, substr( $contents, -(BLOCK*2) ),
612                         "   Ends with 1024 null bytes" );
613
614     return $contents;
615 }
616
617 sub check_tgz_file {
618     my $file                = shift;
619     my $filesize            = -s $file;
620     my $contents            = slurp_gzfile( $file );
621     my $uncompressedsize    = length $contents;
622
623     ok( defined( $contents ),   "   File read and uncompressed" );
624     ok( $filesize,              "   File written size=$filesize uncompressed size=$uncompressedsize" );
625
626     cmp_ok( $uncompressedsize % BLOCK, '==', 0,
627                                 "   Uncompressed size is a multiple of 512" );
628
629     is( TAR_END x 2, substr($contents, -(BLOCK*2)),
630                                 "   Ends with 1024 null bytes" );
631
632     cmp_ok( $filesize, '<',  $uncompressedsize,
633                                 "   Compressed size < uncompressed size" );
634
635     return $contents;
636 }
637
638 sub check_tar_object {
639     my $obj     = shift;
640     my $struct  = shift or return;
641
642     ### amount of files (not dirs!) there should be in the object
643     my $expect  = scalar @$struct;
644     my @files   = grep { $_->is_file } $obj->get_files;
645
646     ### count how many files there are in the object
647     ok( scalar @files,          "   Found some files in the archive" );
648     is( scalar @files, $expect, "   Found expected number of files" );
649
650     for my $file (@files) {
651
652         ### XXX ->fullname
653         #my $path = File::Spec::Unix->catfile(
654         #            grep { length } $file->prefix, $file->name );
655         my($ename,$econtent) =
656             get_expect_name_and_contents( $file->full_path, $struct );
657
658         ok( $file->is_file,     "   It is a file" );
659         is( $file->full_path, $ename,
660                                 "   Name matches expected name" );
661         like( $file->get_content, $econtent,
662                                 "   Content as expected" );
663     }
664 }
665
666 sub check_tar_extract {
667     my $tar     = shift;
668     my $struct  = shift;
669
670     my @dirs;
671     for my $file ($tar->get_files) {
672         push @dirs, $file && next if $file->is_dir;
673
674
675         my $path = $file->full_path;
676         my($ename,$econtent) =
677             get_expect_name_and_contents( $path, $struct );
678
679
680         is( $ename, $path,          "   Expected file found" );
681         ok( -e $path,               "   File '$path' exists" );
682
683         my $fh;
684         open $fh, "$path" or warn "Error opening file '$path': $!\n";
685         binmode $fh;
686
687         ok( $fh,                    "   Opening file" );
688
689         my $content = do{local $/;<$fh>}; chomp $content;
690         like( $content, qr/$econtent/,
691                                     "   Contents OK" );
692
693         $NO_UNLINK or 1 while unlink $path;
694
695         ### alternate extract path tests 
696         ### to abs and rel paths
697         {   for my $outpath (   File::Spec->catdir( @ROOT ),
698                                 File::Spec->rel2abs( 
699                                     File::Spec->catdir( @ROOT )
700                                 )
701             ) {
702
703                 my $outfile = File::Spec->catfile( $outpath, $$ );
704     
705                 ok( $tar->extract_file( $file->full_path, $outfile ),
706                                 "   Extracted file '$path' to $outfile" );
707                 ok( -e $outfile,"   Extracted file '$outfile' exists" );
708     
709                 rm( $outfile ) unless $NO_UNLINK;
710             }            
711         }
712     }
713
714     ### now check if list_files is returning the same info as get_files
715     is_deeply( [$tar->list_files], [ map { $_->full_path } $tar->get_files],
716                                     "   Verified via list_files as well" );
717
718     #do { rmtree $_->full_path if -d $_->full_path && not $NO_UNLINK }
719     #    for @dirs;
720 }
721
722 sub slurp_binfile {
723     my $file    = shift;
724     my $fh      = IO::File->new;
725
726     $fh->open( $file ) or warn( "Error opening '$file': $!" ), return undef;
727
728     binmode $fh;
729     local $/;
730     return <$fh>;
731 }
732
733 sub slurp_gzfile {
734     my $file = shift;
735     my $str;
736     my $buff;
737
738     require IO::Zlib;
739     my $fh = new IO::Zlib;
740     $fh->open( $file, READ_ONLY->(1) )
741         or warn( "Error opening '$file' with IO::Zlib" ), return undef;
742
743     $str .= $buff while $fh->read( $buff, 4096 ) > 0;
744     $fh->close();
745     return $str;
746 }
747
748 sub get_expect_name_and_contents {
749     my $find    = shift;
750     my $struct  = shift or return;
751
752     ### find the proper name + contents for this file from
753     ### the expect structure
754     my ($name, $content) =
755         map {
756             @$_;
757         } grep {
758             $_->[0] eq $find
759         } map {
760             [   ### full path ###
761                 File::Spec::Unix->catfile(
762                     grep { length } @{$_->[0]}, $_->[1]
763                 ),
764                 ### regex
765                 $_->[2],
766             ]
767         } @$struct;
768
769     ### not a qr// yet?
770     unless( ref $content ) {
771         my $x     = quotemeta ($content || '');
772         $content = qr/$x/;
773     }
774
775     unless( $name ) {
776         warn "Could not find '$find' in " . Dumper $struct;
777     }
778
779     return ($name, $content);
780 }
781
782 __END__