1 BEGIN { chdir 't' if -d 't' }
8 use File::Temp qw( tempfile );
12 # Identify tarballs available for testing
13 # Some contain only files
14 # Others contain both files and directories
16 my @file_only_archives = (
17 [qw( src short bar.tar )],
19 push @file_only_archives, [qw( src short foo.tgz )]
20 if Archive::Tar->has_zlib_support;
21 push @file_only_archives, [qw( src short foo.tbz )]
22 if Archive::Tar->has_bzip2_support;
24 @file_only_archives = map File::Spec->catfile(@$_), @file_only_archives;
27 my @file_and_directory_archives = (
28 [qw( src long bar.tar )],
29 [qw( src linktest linktest_with_dir.tar )],
31 push @file_and_directory_archives, [qw( src long foo.tgz )]
32 if Archive::Tar->has_zlib_support;
33 push @file_and_directory_archives, [qw( src long foo.tbz )]
34 if Archive::Tar->has_bzip2_support;
36 @file_and_directory_archives = map File::Spec->catfile(@$_), @file_and_directory_archives;
38 my @archives = (@file_only_archives, @file_and_directory_archives);
39 plan tests => scalar @archives;
42 for my $archive_name (@file_only_archives) {
44 # create a new tarball with the same content as the old one
45 my $old = Archive::Tar->new($archive_name);
46 my $new = Archive::Tar->new();
47 $new->add_files( $old->get_files );
49 # save differently if compressed
50 my $ext = ( split /\./, $archive_name )[-1];
52 $ext =~ /t?gz$/ ? (COMPRESS_GZIP)
53 : $ext =~ /(tbz|bz2?)$/ ? (COMPRESS_BZIP)
56 my ( $fh, $filename ) = tempfile( UNLINK => 1 );
57 $new->write( $filename, @compress );
59 # read the archive again from disk
60 $new = Archive::Tar->new($filename);
62 # compare list of files
66 "$archive_name roundtrip on file names"
71 # t/09_roundtrip.t was added with all 7 then existent tests marked TODO even
72 # though 3 of them were passing. So what was really TODO was to figure out
73 # why the other 4 were not passing.
75 # It turns out that the tests are expecting behavior which, though on the face
76 # of it plausible and desirable, is not Archive::Tar::write()'s current
77 # behavior. write() -- which is used in the unit tests in this file -- relies
78 # on Archive::Tar::File::_prefix_and_file(). Since at least 2006 this helper
79 # method has had the effect of removing a trailing slash from archive entries
80 # which are in fact directories. So we have to adjust our expectations for
81 # what we'll get when round-tripping on an archive which contains one or more
82 # entries for directories.
84 for my $archive_name (@file_and_directory_archives) {
86 if ($archive_name =~ m/\.tar$/) {
87 @contents = qx{tar tvf $archive_name};
89 elsif ($archive_name =~ m/\.tgz$/) {
90 @contents = qx{tar tzvf $archive_name};
92 elsif ($archive_name =~ m/\.tbz$/) {
93 @contents = qx{tar tjvf $archive_name};
97 for my $entry (@contents) {
98 my $perms = (split(/\s+/ => $entry))[0];
99 my @chars = split('' => $perms);
100 push @directory_or_not,
101 ($chars[0] eq 'd' ? 1 : 0);
104 # create a new tarball with the same content as the old one
105 my $old = Archive::Tar->new($archive_name);
106 my $new = Archive::Tar->new();
107 $new->add_files( $old->get_files );
109 # save differently if compressed
110 my $ext = ( split /\./, $archive_name )[-1];
112 $ext =~ /t?gz$/ ? (COMPRESS_GZIP)
113 : $ext =~ /(tbz|bz2?)$/ ? (COMPRESS_BZIP)
116 my ( $fh, $filename ) = tempfile( UNLINK => 1 );
117 $new->write( $filename, @compress );
119 # read the archive again from disk
120 $new = Archive::Tar->new($filename);
122 # Adjust our expectations of
123 my @oldfiles = $old->list_files;
124 for (my $i = 0; $i <= $#oldfiles; $i++) {
125 chop $oldfiles[$i] if $directory_or_not[$i];
128 # compare list of files
130 [ $new->list_files ],
132 "$archive_name roundtrip on file names"