root/branches/boomer/t/14-archive.t @ 1100

Revision 1100, 3.7 kB (checked in by hachi, 2 years ago)

Merging release-20 to boomer branch: svn merge -r62323:63659 http://svn.sixapart.com/repos/eng/movabletype/branches/release-20 .

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