1use strict;
2use Test::More tests => 15;
3use Cwd;
4use File::Spec;
5use File::Temp qw/tempdir/;
6
7BEGIN {
8  use_ok('Archive::Rar');
9}
10
11# temp dir
12my $tmpdir = tempdir( CLEANUP => 1 );
13
14my $olddir = cwd();
15END { chdir($olddir) }
16$SIG{__DIE__} = sub { chdir($olddir); die @_;};
17chdir($tmpdir);
18
19my $datafile = 'add_test.rar';
20my $textfile = 'mytext.txt';
21open my $fh, '>', $textfile or die $!;
22print $fh <<HERE;
23I am a nice little text file!
24HERE
25close $fh;
26
27ok(not(-f $datafile), "output rar file does not exist yet");
28
29my $rar = Archive::Rar->new(-verbose=>1);
30isa_ok($rar, 'Archive::Rar');
31
32is($rar->Add(-files => [$textfile], -archive => $datafile), 0, "Add() command succeeds for file '$textfile' and archive '$datafile'");
33ok(-f $datafile, 'Add() creates a rar file');
34
35
36TODO: {
37  local $TODO = "Seems to currently fail with the legacy Archive::Rar.";
38
39  my $archivename = $rar->{"-archive"};
40  # Here, the archive name is already curropted.
41  # So it seems to be that Add() puts a broken archive name in the object
42  is($rar->List(), 0, "List() succeeded");
43  is($rar->{"-archive"}, $archivename, "List() does not change archive name.");
44  ok(-f $archivename, 'archive exists');
45
46  my $list = $rar->{list};
47  my $okay = ref($list) eq 'ARRAY' && @$list==1 && ref($list->[0]) eq 'HASH';
48  ok($okay , "List structure is defined and array ref and length 1");
49
50  if ($okay) {
51    is($list->[0]{name}, $textfile, "File name is '$textfile'");
52    is($list->[0]{size}, -s $textfile, "File size is correct");
53  }
54  else { fail; fail; } # I know...
55}
56
57is($rar->List(-archive => $datafile), 0, "List(-archive) succeeded");
58
59my $list = $rar->{list};
60my $okay = ref($list) eq 'ARRAY' && @$list==1 && ref($list->[0]) eq 'HASH';
61ok($okay , "List structure is defined and array ref and length 1");
62
63if ($okay) {
64  is($list->[0]{name}, $textfile, "File name is '$textfile'");
65  is($list->[0]{size}, -s $textfile, "File size is correct");
66}
67else { fail; fail; } # I know...
68
69
701;
71