Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 1 | #/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use File::Basename 'dirname'; |
| 5 | use File::Spec::Functions qw/catdir catfile/; |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 6 | use File::Temp qw/:POSIX tempdir/; |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 7 | use Mojo::JSON qw/decode_json/; |
| 8 | use IO::Uncompress::Gunzip; |
| 9 | use Test::More; |
| 10 | use Test::Output; |
| 11 | use Data::Dumper; |
Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 12 | use KorAP::XML::Archive; |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 13 | use utf8; |
| 14 | |
Akron | fab17d3 | 2020-07-31 14:38:29 +0200 | [diff] [blame^] | 15 | if ($ENV{SKIP_SCRIPT}) { |
| 16 | plan skip_all => 'Skip script tests'; |
| 17 | }; |
| 18 | |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 19 | my $f = dirname(__FILE__); |
| 20 | my $script = catfile($f, '..', '..', 'script', 'korapxml2krill'); |
| 21 | |
| 22 | my $call = join( |
| 23 | ' ', |
| 24 | 'perl', $script, |
| 25 | 'extract' |
| 26 | ); |
| 27 | |
Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 28 | unless (KorAP::XML::Archive::test_unzip) { |
| 29 | plan skip_all => 'unzip not found'; |
| 30 | }; |
| 31 | |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 32 | # Test without parameters |
| 33 | stdout_like( |
| 34 | sub { |
| 35 | system($call); |
| 36 | }, |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 37 | qr!extract.+?\$ korapxml2krill!s, |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 38 | $call |
| 39 | ); |
| 40 | |
| 41 | my $input = catfile($f, '..', 'corpus', 'archive.zip'); |
| 42 | ok(-f $input, 'Input archive found'); |
| 43 | |
| 44 | my $output = tempdir(CLEANUP => 1); |
| 45 | ok(-d $output, 'Output directory exists'); |
| 46 | |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 47 | my $cache = tmpnam(); |
| 48 | |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 49 | $call = join( |
| 50 | ' ', |
| 51 | 'perl', $script, |
| 52 | 'extract', |
| 53 | '--input' => $input, |
| 54 | '--output' => $output, |
Akron | aaea246 | 2017-06-19 16:56:54 +0200 | [diff] [blame] | 55 | '--cache' => $cache |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 56 | ); |
| 57 | |
Akron | 955b75b | 2019-02-21 14:28:41 +0100 | [diff] [blame] | 58 | # my $sep = qr!\.\.\.[\n\r]+?\.\.\.!; |
Akron | 2812ba2 | 2016-10-28 21:55:59 +0200 | [diff] [blame] | 59 | |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 60 | # Test without compression |
| 61 | stdout_like( |
| 62 | sub { |
| 63 | system($call); |
| 64 | }, |
Akron | 955b75b | 2019-02-21 14:28:41 +0100 | [diff] [blame] | 65 | qr!TEST/BSP/1 .* extracted!s, |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 66 | # qr!TEST/BSP/1 $sep extracted.!s, |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 67 | $call |
| 68 | ); |
| 69 | |
| 70 | ok(-d catdir($output, 'TEST', 'BSP', '1'), 'Directory created'); |
| 71 | ok(-d catdir($output, 'TEST', 'BSP', '1', 'base'), 'Directory created'); |
| 72 | ok(-d catdir($output, 'TEST', 'BSP', '1', 'sgbr'), 'Directory created'); |
| 73 | ok(-d catdir($output, 'TEST', 'BSP', '1', 'struct'), 'Directory created'); |
| 74 | ok(-f catfile($output, 'TEST', 'BSP', '1', 'data.xml'), 'File created'); |
| 75 | ok(-f catfile($output, 'TEST', 'BSP', '1', 'header.xml'), 'File created'); |
| 76 | ok(-d catdir($output, 'TEST', 'BSP', '2'), 'Directory created'); |
| 77 | ok(-d catdir($output, 'TEST', 'BSP', '3'), 'Directory created'); |
| 78 | |
| 79 | # Check sigles |
| 80 | my $output2 = tempdir(CLEANUP => 1); |
| 81 | ok(-d $output2, 'Output directory exists'); |
| 82 | |
| 83 | $call = join( |
| 84 | ' ', |
| 85 | 'perl', $script, |
| 86 | 'extract', |
| 87 | '--input' => $input, |
| 88 | '--output' => $output2, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 89 | '--cache' => $cache, |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 90 | '-sg' => 'TEST/BSP/4' |
| 91 | ); |
| 92 | |
| 93 | # Test with sigle |
| 94 | stdout_like( |
| 95 | sub { |
| 96 | system($call); |
| 97 | }, |
Akron | 955b75b | 2019-02-21 14:28:41 +0100 | [diff] [blame] | 98 | # qr!TEST/BSP/4 $sep extracted.!s, |
| 99 | qr!TEST/BSP/4 .* extracted.!s, |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 100 | $call |
| 101 | ); |
| 102 | |
| 103 | # Test with sigle |
| 104 | stdout_unlike( |
| 105 | sub { |
| 106 | system($call); |
| 107 | }, |
Akron | 955b75b | 2019-02-21 14:28:41 +0100 | [diff] [blame] | 108 | # qr!TEST/BSP/5 $sep extracted.!s, |
| 109 | qr!TEST/BSP/5 .* extracted.!s, |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 110 | $call |
| 111 | ); |
| 112 | |
| 113 | ok(!-d catdir($output2, 'TEST', 'BSP', '1'), 'Directory created'); |
| 114 | ok(!-d catdir($output2, 'TEST', 'BSP', '2'), 'Directory created'); |
| 115 | ok(!-d catdir($output2, 'TEST', 'BSP', '3'), 'Directory created'); |
| 116 | ok(-d catdir($output2, 'TEST', 'BSP', '4'), 'Directory created'); |
| 117 | ok(!-d catdir($output2, 'TEST', 'BSP', '5'), 'Directory created'); |
| 118 | |
Akron | 2080758 | 2016-10-26 17:11:34 +0200 | [diff] [blame] | 119 | # Test with document sigle |
| 120 | my $input_rei = catdir($f, '..', 'corpus', 'archive_rei.zip'); |
| 121 | ok(-f $input_rei, 'Input archive found'); |
| 122 | |
| 123 | $call = join( |
| 124 | ' ', |
| 125 | 'perl', $script, |
| 126 | 'extract', |
| 127 | '--input' => $input_rei, |
| 128 | '--output' => $output2, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 129 | '--cache' => $cache, |
Akron | 2080758 | 2016-10-26 17:11:34 +0200 | [diff] [blame] | 130 | '-sg' => 'REI/BNG' |
| 131 | ); |
| 132 | |
| 133 | # Test with sigle |
| 134 | stdout_like( |
| 135 | sub { |
| 136 | system($call); |
| 137 | }, |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 138 | qr!Extract .+? REI/BNG!s, |
Akron | 2080758 | 2016-10-26 17:11:34 +0200 | [diff] [blame] | 139 | $call |
| 140 | ); |
| 141 | |
| 142 | # Test with sigle |
| 143 | stdout_unlike( |
| 144 | sub { |
| 145 | system($call); |
| 146 | }, |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 147 | qr!Extract .+? REI/RBR!s, |
Akron | 2080758 | 2016-10-26 17:11:34 +0200 | [diff] [blame] | 148 | $call |
| 149 | ); |
| 150 | |
| 151 | ok(-d catdir($output2, 'REI', 'BNG', '00071'), 'Directory created'); |
| 152 | ok(-d catdir($output2, 'REI', 'BNG', '00128'), 'Directory created'); |
| 153 | ok(!-d catdir($output2, 'REI', 'RBR', '00610'), 'Directory not created'); |
| 154 | |
Akron | 2fd402b | 2016-10-27 21:26:48 +0200 | [diff] [blame] | 155 | # Test with document sigle |
| 156 | $output2 = undef; |
| 157 | $output2 = tempdir(CLEANUP => 1); |
| 158 | |
| 159 | $call = join( |
| 160 | ' ', |
| 161 | 'perl', $script, |
| 162 | 'extract', |
| 163 | '--input' => $input_rei, |
| 164 | '--output' => $output2, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 165 | '--cache' => $cache, |
Akron | 2fd402b | 2016-10-27 21:26:48 +0200 | [diff] [blame] | 166 | '-sg' => 'REI/BN*' |
| 167 | ); |
| 168 | |
| 169 | # Test with sigle |
| 170 | stdout_like( |
| 171 | sub { |
| 172 | system($call); |
| 173 | }, |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 174 | qr!Extract .+? REI/BN!s, |
Akron | 2fd402b | 2016-10-27 21:26:48 +0200 | [diff] [blame] | 175 | $call |
| 176 | ); |
| 177 | |
| 178 | # Test with sigle |
| 179 | stdout_unlike( |
| 180 | sub { |
| 181 | system($call); |
| 182 | }, |
Akron | 955b75b | 2019-02-21 14:28:41 +0100 | [diff] [blame] | 183 | qr!REI/RBR .* extracted!s, |
Akron | 2fd402b | 2016-10-27 21:26:48 +0200 | [diff] [blame] | 184 | $call |
| 185 | ); |
| 186 | |
| 187 | ok(-d catdir($output2, 'REI', 'BNG', '00071'), 'Directory created'); |
| 188 | ok(-d catdir($output2, 'REI', 'BNG', '00128'), 'Directory created'); |
| 189 | ok(!-d catdir($output2, 'REI', 'RBR', '00610'), 'Directory not created'); |
| 190 | |
| 191 | |
Akron | 651cb8d | 2016-08-16 21:44:49 +0200 | [diff] [blame] | 192 | # Check multiple archives |
| 193 | $output = tempdir(CLEANUP => 1); |
| 194 | ok(-d $output, 'Output directory exists'); |
| 195 | |
| 196 | $call = join( |
| 197 | ' ', |
| 198 | 'perl', $script, |
| 199 | 'extract', |
| 200 | '-i' => catfile($f, '..', 'corpus', 'archives', 'wpd15-single.zip'), |
| 201 | '-i' => catfile($f, '..', 'corpus', 'archives', 'wpd15-single.tree_tagger.zip'), |
| 202 | '-i' => catfile($f, '..', 'corpus', 'archives', 'wpd15-single.opennlp.zip'), |
Akron | aaea246 | 2017-06-19 16:56:54 +0200 | [diff] [blame] | 203 | '--output' => $output, |
| 204 | '--cache' => $cache |
Akron | 651cb8d | 2016-08-16 21:44:49 +0200 | [diff] [blame] | 205 | ); |
| 206 | |
| 207 | # Test with sigle |
| 208 | stdout_like( |
| 209 | sub { |
| 210 | system($call); |
| 211 | }, |
Akron | 955b75b | 2019-02-21 14:28:41 +0100 | [diff] [blame] | 212 | qr!WPD15/A00/00081 .* extracted!s, |
Akron | 651cb8d | 2016-08-16 21:44:49 +0200 | [diff] [blame] | 213 | $call |
| 214 | ); |
| 215 | |
| 216 | ok(-d catdir($output, 'WPD15', 'A00', '00081'), 'Directory created'); |
| 217 | ok(-f catfile($output, 'WPD15', 'A00', 'header.xml'), 'Header file created'); |
| 218 | ok(-d catdir($output, 'WPD15', 'A00', '00081', 'base'), 'Directory created'); |
| 219 | |
| 220 | ok(-f catfile($output, 'WPD15', 'A00', '00081', 'tree_tagger', 'morpho.xml'), 'New archive'); |
| 221 | ok(-f catfile($output, 'WPD15', 'A00', '00081', 'opennlp', 'morpho.xml'), 'New archive'); |
| 222 | |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 223 | |
Akron | 60a8caa | 2017-02-17 21:51:27 +0100 | [diff] [blame] | 224 | # With quotes: |
| 225 | # Test with document sigle |
| 226 | my $input_quotes = catfile($f, '..', 'corpus', 'archive_quotes.zip'); |
| 227 | ok(-f $input, 'Input archive found'); |
| 228 | $output2 = undef; |
| 229 | $output2 = tempdir(CLEANUP => 1); |
| 230 | |
| 231 | $call = join( |
| 232 | ' ', |
| 233 | 'perl', $script, |
| 234 | 'extract', |
| 235 | '--input' => $input_quotes, |
| 236 | '--output' => $output2, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 237 | '--cache' => $cache, |
Akron | 60a8caa | 2017-02-17 21:51:27 +0100 | [diff] [blame] | 238 | '-sg' => '"TEST/BSP \"Example\"/1"' |
| 239 | ); |
| 240 | |
| 241 | # Test with sigle |
| 242 | stdout_like( |
| 243 | sub { |
| 244 | system($call); |
| 245 | }, |
Akron | 955b75b | 2019-02-21 14:28:41 +0100 | [diff] [blame] | 246 | qr!TEST/BSP "Example"\/1 .* extracted!s, |
| 247 | # qr!TEST/BSP "Example"\/1 $sep extracted!s, |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 248 | # qr!Extract .+? TEST/BSP "Example"\/1!s, |
Akron | 60a8caa | 2017-02-17 21:51:27 +0100 | [diff] [blame] | 249 | $call |
| 250 | ); |
| 251 | |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 252 | done_testing; |
| 253 | __END__ |