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