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, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 50 | '--cache' => $cache, |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 51 | '--output' => $output, |
| 52 | ); |
| 53 | |
Akron | 2812ba2 | 2016-10-28 21:55:59 +0200 | [diff] [blame] | 54 | my $sep = qr!\.\.\.[\n\r]+?\.\.\.!; |
| 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 | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 61 | qr!TEST/BSP/1 $sep extracted!s, |
| 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 | 2812ba2 | 2016-10-28 21:55:59 +0200 | [diff] [blame] | 94 | qr!TEST/BSP/4 $sep extracted.!s, |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 95 | $call |
| 96 | ); |
| 97 | |
| 98 | # Test with sigle |
| 99 | stdout_unlike( |
| 100 | sub { |
| 101 | system($call); |
| 102 | }, |
Akron | 2812ba2 | 2016-10-28 21:55:59 +0200 | [diff] [blame] | 103 | qr!TEST/BSP/5 $sep extracted.!s, |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 104 | $call |
| 105 | ); |
| 106 | |
| 107 | ok(!-d catdir($output2, 'TEST', 'BSP', '1'), 'Directory created'); |
| 108 | ok(!-d catdir($output2, 'TEST', 'BSP', '2'), 'Directory created'); |
| 109 | ok(!-d catdir($output2, 'TEST', 'BSP', '3'), 'Directory created'); |
| 110 | ok(-d catdir($output2, 'TEST', 'BSP', '4'), 'Directory created'); |
| 111 | ok(!-d catdir($output2, 'TEST', 'BSP', '5'), 'Directory created'); |
| 112 | |
Akron | 2080758 | 2016-10-26 17:11:34 +0200 | [diff] [blame] | 113 | |
| 114 | # Test with document sigle |
| 115 | my $input_rei = catdir($f, '..', 'corpus', 'archive_rei.zip'); |
| 116 | ok(-f $input_rei, 'Input archive found'); |
| 117 | |
| 118 | $call = join( |
| 119 | ' ', |
| 120 | 'perl', $script, |
| 121 | 'extract', |
| 122 | '--input' => $input_rei, |
| 123 | '--output' => $output2, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 124 | '--cache' => $cache, |
Akron | 2080758 | 2016-10-26 17:11:34 +0200 | [diff] [blame] | 125 | '-sg' => 'REI/BNG' |
| 126 | ); |
| 127 | |
| 128 | # Test with sigle |
| 129 | stdout_like( |
| 130 | sub { |
| 131 | system($call); |
| 132 | }, |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 133 | qr!Extract .+? REI/BNG!s, |
Akron | 2080758 | 2016-10-26 17:11:34 +0200 | [diff] [blame] | 134 | $call |
| 135 | ); |
| 136 | |
| 137 | # Test with sigle |
| 138 | stdout_unlike( |
| 139 | sub { |
| 140 | system($call); |
| 141 | }, |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 142 | qr!Extract .+? REI/RBR!s, |
Akron | 2080758 | 2016-10-26 17:11:34 +0200 | [diff] [blame] | 143 | $call |
| 144 | ); |
| 145 | |
| 146 | ok(-d catdir($output2, 'REI', 'BNG', '00071'), 'Directory created'); |
| 147 | ok(-d catdir($output2, 'REI', 'BNG', '00128'), 'Directory created'); |
| 148 | ok(!-d catdir($output2, 'REI', 'RBR', '00610'), 'Directory not created'); |
| 149 | |
Akron | 2fd402b | 2016-10-27 21:26:48 +0200 | [diff] [blame] | 150 | |
| 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 | 9ec8887 | 2017-04-12 16:29:06 +0200 | [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 | 2812ba2 | 2016-10-28 21:55:59 +0200 | [diff] [blame] | 179 | qr!REI/RBR $sep 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 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
Akron | 651cb8d | 2016-08-16 21:44:49 +0200 | [diff] [blame] | 193 | # Check multiple archives |
| 194 | $output = tempdir(CLEANUP => 1); |
| 195 | ok(-d $output, 'Output directory exists'); |
| 196 | |
| 197 | $call = join( |
| 198 | ' ', |
| 199 | 'perl', $script, |
| 200 | 'extract', |
| 201 | '-i' => catfile($f, '..', 'corpus', 'archives', 'wpd15-single.zip'), |
| 202 | '-i' => catfile($f, '..', 'corpus', 'archives', 'wpd15-single.tree_tagger.zip'), |
| 203 | '-i' => catfile($f, '..', 'corpus', 'archives', 'wpd15-single.opennlp.zip'), |
| 204 | '--output' => $output |
| 205 | ); |
| 206 | |
| 207 | # Test with sigle |
| 208 | stdout_like( |
| 209 | sub { |
| 210 | system($call); |
| 211 | }, |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 212 | qr!WPD15/A00/00081 $sep 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 | }, |
| 246 | qr!TEST/BSP "Example"\/1 $sep extracted!s, |
Akron | 9ec8887 | 2017-04-12 16:29:06 +0200 | [diff] [blame] | 247 | # qr!Extract .+? TEST/BSP "Example"\/1!s, |
Akron | 60a8caa | 2017-02-17 21:51:27 +0100 | [diff] [blame] | 248 | $call |
| 249 | ); |
| 250 | |
Akron | 03b24db | 2016-08-16 20:54:32 +0200 | [diff] [blame] | 251 | done_testing; |
| 252 | __END__ |