root/trunk/t/14-archive.t

Revision 5086, 3.6 kB (checked in by takayama, 7 days ago)

* Fixed for work.

  • Property svn:mime-type set to text/plain
Line 
1# $Id: 14-archive.t 2562 2008-06-12 05:12:23Z bchoate $
2
3use lib 't/lib', 'extlib', 'lib', '../lib', '../extlib';
4use Test::More tests => 38;
5use Cwd;
6use MT;
7use MT::Test;
8use strict;
9
10my $mt = MT->new;
11use MT::Util::Archive;
12my $tmp = MT->config->TempDir;
13my %files = (
14    'zip' => File::Spec->catfile($tmp, 'test1.zip'),
15    'tgz' => File::Spec->catfile($tmp, 'test1.tar.gz'),
16);
17
18my $str = <<TXT;
19When I look into your eyes, I can see the love restrained.
20But darling when I hold you, don't you know I feel the same.
21Nothing lasts forever, and we both know hearts can change.
22And it's hard to hold a candle, in the cold november rain.
23TXT
24
25my $arc = MT::Util::Archive->new('txt', $files{'zip'});
26is($arc, undef, 'Type not registered');
27
28for 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
86open my $file4, '<', $files{'tgz'};
87bless $file4, 'IO::File';
88require IO::Uncompress::Gunzip;
89my $z = new IO::Uncompress::Gunzip $file4;
90my $data = do { local $/; <$z> };
91close $z;
92close $file4;
93open my $fileX, '>', $files{'tgz'} . '.tar';
94print $fileX $data;
95close $fileX;
96
97
98# Run the tests
99my $ext = MT::Util::Archive->new('tgz', $files{'tgz'} . '.tar');
100ok($ext, 'Archive file read');
101
102my @files = $ext->files;
103is(@files, 2, 'Number of files is 2');
104is($files[0], 'mt-config.cgi-original', 'The name of the file 0 is correct');
105is($files[1], 'november.txt', 'The name of the file 1 is correct');
106
107ok($ext->extract($tmp), 'Extracted successfully');
108$ext->close;
109
110my $file5 = File::Spec->catfile($tmp, $files[0]);
111my $file6 = File::Spec->catfile($tmp, $files[1]);
112
113open my $f5, '<', $file5;
114my $content5 = do { local $/; <$f5> };
115close $f5;
116open my $f6, '<', File::Spec->catfile(cwd(), 'mt-config.cgi-original');
117my $content6 = do { local $/; <$f6> };
118close $f6;
119is($content5, $content6, 'Contents are the same');
120
121open my $f7, '<', $file6;
122my $content7 = do { local $/; <$f7> };
123close $f7;
124is($content7, $str, 'Contents are the same');
125
126unlink $file5;
127unlink $file6;
128unlink $files{'tgz'};
129unlink $files{'tgz'} . '.tar';
Note: See TracBrowser for help on using the browser.