Akron | eb370a0 | 2022-02-24 13:33:40 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use Test::More; |
| 5 | |
| 6 | use File::Basename 'dirname'; |
| 7 | use File::Spec::Functions qw/catfile catdir/; |
| 8 | use File::Temp qw/tempdir tempfile/; |
| 9 | |
| 10 | use_ok('Archive::Tar'); |
| 11 | |
| 12 | my ($out_tar, $out_tar_fn) = tempfile(); |
| 13 | |
| 14 | use_ok('KorAP::XML::TarBuilder'); |
| 15 | |
| 16 | ok(my $tar = KorAP::XML::TarBuilder->new($out_tar), 'Create new tar'); |
| 17 | |
| 18 | is(ref $tar, 'KorAP::XML::TarBuilder'); |
| 19 | |
| 20 | my $file = catfile(dirname(__FILE__), 'corpus','artificial', 'data.xml'); |
| 21 | ok($tar->archive_as($file, 'example1.xml')); |
| 22 | |
| 23 | $file = catfile(dirname(__FILE__), 'corpus','artificial', 'header.xml'); |
| 24 | ok($tar->archive_as($file, 'example2.xml')); |
| 25 | |
| 26 | ok($tar->finish, 'Finish tar'); |
| 27 | |
| 28 | use_ok('Archive::Tar'); |
| 29 | |
| 30 | my $tar_read = Archive::Tar->new($out_tar_fn); |
| 31 | |
| 32 | ok($tar_read->contains_file('example1.xml'), 'File exists'); |
| 33 | ok($tar_read->contains_file('example2.xml'), 'File exists'); |
| 34 | |
| 35 | my $content = $tar_read->get_content('example1.xml'); |
| 36 | like($content, qr!ART_ABC\.00001!, 'Content is correct'); |
| 37 | |
| 38 | $content = $tar_read->get_content('example2.xml'); |
| 39 | like($content, qr!ART\/ABC\.00001!, 'Content is correct'); |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | # Now test for equivalence to Archive::Tar::Builder |
| 45 | if (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 | } |
| 82 | else { |
| 83 | diag 'Archive::Tar::Builder not installed.'; |
| 84 | }; |
| 85 | |
| 86 | done_testing; |
| 87 | |