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