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