blob: 941344032ad3e7aa2a20287d878b9c9e352304ea [file] [log] [blame]
Akroneb370a02022-02-24 13:33:40 +01001#!/usr/bin/env perl
2use strict;
3use warnings;
4use Test::More;
5
6use File::Basename 'dirname';
7use File::Spec::Functions qw/catfile catdir/;
8use File::Temp qw/tempdir tempfile/;
9
10use_ok('Archive::Tar');
11
12my ($out_tar, $out_tar_fn) = tempfile();
13
14use_ok('KorAP::XML::TarBuilder');
15
16ok(my $tar = KorAP::XML::TarBuilder->new($out_tar), 'Create new tar');
17
18is(ref $tar, 'KorAP::XML::TarBuilder');
19
20my $file = catfile(dirname(__FILE__), 'corpus','artificial', 'data.xml');
21ok($tar->archive_as($file, 'example1.xml'));
22
23$file = catfile(dirname(__FILE__), 'corpus','artificial', 'header.xml');
24ok($tar->archive_as($file, 'example2.xml'));
25
26ok($tar->finish, 'Finish tar');
27
28use_ok('Archive::Tar');
29
30my $tar_read = Archive::Tar->new($out_tar_fn);
31
32ok($tar_read->contains_file('example1.xml'), 'File exists');
33ok($tar_read->contains_file('example2.xml'), 'File exists');
34
35my $content = $tar_read->get_content('example1.xml');
36like($content, qr!ART_ABC\.00001!, 'Content is correct');
37
38$content = $tar_read->get_content('example2.xml');
39like($content, qr!ART\/ABC\.00001!, 'Content is correct');
40
41
42
43
44# Now test for equivalence to Archive::Tar::Builder
45if (eval("use Archive::Tar::Builder; 1;")) {
46
47 use_ok('Archive::Tar::Builder');
48
49 # Reset
50 ($out_tar, $out_tar_fn) = tempfile();
51
52 $tar = Archive::Tar::Builder->new(
53 ignore_errors => 1
54 );
55
56 # Set handle
57 $tar->set_handle($out_tar);
58
59 is(ref $tar, 'Archive::Tar::Builder');
60
61 $file = catfile(dirname(__FILE__), 'corpus','artificial', 'data.xml');
62 ok($tar->archive_as($file, 'example1.xml'));
63
64 $file = catfile(dirname(__FILE__), 'corpus','artificial', 'header.xml');
65 ok($tar->archive_as($file, 'example2.xml'));
66
67 ok($tar->finish, 'Finish tar');
68
69 use_ok('Archive::Tar');
70
71 $tar_read = Archive::Tar->new($out_tar_fn);
72
73 ok($tar_read->contains_file('example1.xml'), 'File exists');
74 ok($tar_read->contains_file('example2.xml'), 'File exists');
75
76 $content = $tar_read->get_content('example1.xml');
77 like($content, qr!ART_ABC\.00001!, 'Content is correct');
78
79 $content = $tar_read->get_content('example2.xml');
80 like($content, qr!ART\/ABC\.00001!, 'Content is correct');
81}
82else {
83 diag 'Archive::Tar::Builder not installed.';
84};
85
86done_testing;
87