| 1 | # $Id: 14-archive.t 2562 2008-06-12 05:12:23Z bchoate $ |
|---|
| 2 | |
|---|
| 3 | use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib'; |
|---|
| 4 | use Test::More tests => 38; |
|---|
| 5 | use Cwd; |
|---|
| 6 | use MT; |
|---|
| 7 | use MT::Test; |
|---|
| 8 | use strict; |
|---|
| 9 | |
|---|
| 10 | my $mt = MT->new; |
|---|
| 11 | use MT::Util::Archive; |
|---|
| 12 | my $tmp = MT->config->TempDir; |
|---|
| 13 | my %files = ( |
|---|
| 14 | 'zip' => File::Spec->catfile($tmp, 'test1.zip'), |
|---|
| 15 | 'tgz' => File::Spec->catfile($tmp, 'test1.tar.gz'), |
|---|
| 16 | ); |
|---|
| 17 | |
|---|
| 18 | my $str = <<TXT; |
|---|
| 19 | When I look into your eyes, I can see the love restrained. |
|---|
| 20 | But darling when I hold you, don't you know I feel the same. |
|---|
| 21 | Nothing lasts forever, and we both know hearts can change. |
|---|
| 22 | And it's hard to hold a candle, in the cold november rain. |
|---|
| 23 | TXT |
|---|
| 24 | |
|---|
| 25 | my $arc = MT::Util::Archive->new('txt', $files{'zip'}); |
|---|
| 26 | is($arc, undef, 'Type not registered'); |
|---|
| 27 | |
|---|
| 28 | for my $type (qw( zip tgz )) { |
|---|
| 29 | my $file = $files{$type}; |
|---|
| 30 | |
|---|
| 31 | my $arc = MT::Util::Archive->new($type, $file); |
|---|
| 32 | ok($arc, "Empty $type archive created"); |
|---|
| 33 | |
|---|
| 34 | is($arc->type, $type, 'Type is ' . $type); |
|---|
| 35 | ok($arc->is($type), 'Type is ' . $type); |
|---|
| 36 | ok(!$arc->is('txt'), 'Type is not txt'); |
|---|
| 37 | |
|---|
| 38 | my $path = cwd(); |
|---|
| 39 | |
|---|
| 40 | ok($arc->add_file($path, 'mt-config.cgi-original'), 'Add file'); |
|---|
| 41 | ok($arc->add_string($str, 'november.txt'), 'Added string'); |
|---|
| 42 | |
|---|
| 43 | ok($arc->close, 'Archive created'); |
|---|
| 44 | |
|---|
| 45 | my $ext = MT::Util::Archive->new($type, $file); |
|---|
| 46 | ok($ext, 'Archive file read'); |
|---|
| 47 | $ext->close; |
|---|
| 48 | |
|---|
| 49 | open my $fh, '<', $file; |
|---|
| 50 | $ext = MT::Util::Archive->new($type, $fh); |
|---|
| 51 | ok($ext, 'Archive file read'); |
|---|
| 52 | |
|---|
| 53 | my @files = $ext->files; |
|---|
| 54 | is(@files, 2, 'Number of files is 2'); |
|---|
| 55 | is($files[0], 'mt-config.cgi-original', 'The name of the file 0 is correct'); |
|---|
| 56 | is($files[1], 'november.txt', 'The name of the file 1 is correct'); |
|---|
| 57 | |
|---|
| 58 | ok($ext->extract($tmp), 'Extracted successfully'); |
|---|
| 59 | close $fh; |
|---|
| 60 | |
|---|
| 61 | my $file1 = File::Spec->catfile($tmp, $files[0]); |
|---|
| 62 | my $file2 = File::Spec->catfile($tmp, $files[1]); |
|---|
| 63 | |
|---|
| 64 | open my $f1, '<', $file1; |
|---|
| 65 | my $content1 = do { local $/; <$f1> }; |
|---|
| 66 | close $f1; |
|---|
| 67 | open my $f2, '<', File::Spec->catfile(cwd(), 'mt-config.cgi-original'); |
|---|
| 68 | my $content2 = do { local $/; <$f2> }; |
|---|
| 69 | close $f2; |
|---|
| 70 | is($content1, $content2, 'Contents are the same'); |
|---|
| 71 | |
|---|
| 72 | open my $f3, '<', $file2; |
|---|
| 73 | my $content3 = do { local $/; <$f3> }; |
|---|
| 74 | close $f3; |
|---|
| 75 | is($content3, $str, 'Contents are the same'); |
|---|
| 76 | |
|---|
| 77 | unlink $file1; |
|---|
| 78 | unlink $file2; |
|---|
| 79 | unlink $file if $type ne 'tgz'; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | ## Tar (not tgz) test... |
|---|
| 83 | |
|---|
| 84 | # Uncompress gunzip and create tar file |
|---|
| 85 | |
|---|
| 86 | open my $file4, '<', $files{'tgz'}; |
|---|
| 87 | bless $file4, 'IO::File'; |
|---|
| 88 | require IO::Uncompress::Gunzip; |
|---|
| 89 | my $z = new IO::Uncompress::Gunzip $file4; |
|---|
| 90 | my $data = do { local $/; <$z> }; |
|---|
| 91 | close $z; |
|---|
| 92 | close $file4; |
|---|
| 93 | open my $fileX, '>', $files{'tgz'} . '.tar'; |
|---|
| 94 | print $fileX $data; |
|---|
| 95 | close $fileX; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | # Run the tests |
|---|
| 99 | my $ext = MT::Util::Archive->new('tgz', $files{'tgz'} . '.tar'); |
|---|
| 100 | ok($ext, 'Archive file read'); |
|---|
| 101 | |
|---|
| 102 | my @files = $ext->files; |
|---|
| 103 | is(@files, 2, 'Number of files is 2'); |
|---|
| 104 | is($files[0], 'mt-config.cgi-original', 'The name of the file 0 is correct'); |
|---|
| 105 | is($files[1], 'november.txt', 'The name of the file 1 is correct'); |
|---|
| 106 | |
|---|
| 107 | ok($ext->extract($tmp), 'Extracted successfully'); |
|---|
| 108 | $ext->close; |
|---|
| 109 | |
|---|
| 110 | my $file5 = File::Spec->catfile($tmp, $files[0]); |
|---|
| 111 | my $file6 = File::Spec->catfile($tmp, $files[1]); |
|---|
| 112 | |
|---|
| 113 | open my $f5, '<', $file5; |
|---|
| 114 | my $content5 = do { local $/; <$f5> }; |
|---|
| 115 | close $f5; |
|---|
| 116 | open my $f6, '<', File::Spec->catfile(cwd(), 'mt-config.cgi-original'); |
|---|
| 117 | my $content6 = do { local $/; <$f6> }; |
|---|
| 118 | close $f6; |
|---|
| 119 | is($content5, $content6, 'Contents are the same'); |
|---|
| 120 | |
|---|
| 121 | open my $f7, '<', $file6; |
|---|
| 122 | my $content7 = do { local $/; <$f7> }; |
|---|
| 123 | close $f7; |
|---|
| 124 | is($content7, $str, 'Contents are the same'); |
|---|
| 125 | |
|---|
| 126 | unlink $file5; |
|---|
| 127 | unlink $file6; |
|---|
| 128 | unlink $files{'tgz'}; |
|---|
| 129 | unlink $files{'tgz'} . '.tar'; |
|---|