Akron | 7d4cdd8 | 2016-08-17 21:39:45 +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/; |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 7 | use Mojo::File; |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 8 | use Mojo::Util qw/quote/; |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 9 | use Mojo::JSON qw/decode_json/; |
| 10 | use IO::Uncompress::Gunzip; |
| 11 | use Test::More; |
Akron | 63d03ee | 2019-02-13 18:49:38 +0100 | [diff] [blame] | 12 | use Test::Output qw/:stdout :stderr :combined :functions/; |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 13 | use Data::Dumper; |
Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 14 | use KorAP::XML::Archive; |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 15 | use utf8; |
| 16 | |
Akron | fab17d3 | 2020-07-31 14:38:29 +0200 | [diff] [blame] | 17 | if ($ENV{SKIP_SCRIPT}) { |
| 18 | plan skip_all => 'Skip script tests'; |
| 19 | }; |
| 20 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 21 | my $f = dirname(__FILE__); |
| 22 | my $script = catfile($f, '..', '..', 'script', 'korapxml2krill'); |
| 23 | |
| 24 | my $call = join( |
| 25 | ' ', |
| 26 | 'perl', $script, |
| 27 | 'archive' |
| 28 | ); |
| 29 | |
Nils Diewald | b3e9ccd | 2016-10-24 15:16:52 +0200 | [diff] [blame] | 30 | unless (KorAP::XML::Archive::test_unzip) { |
| 31 | plan skip_all => 'unzip not found'; |
| 32 | }; |
| 33 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 34 | # Test without parameters |
| 35 | stdout_like( |
| 36 | sub { |
| 37 | system($call); |
| 38 | }, |
Akron | a76d835 | 2016-10-27 16:27:32 +0200 | [diff] [blame] | 39 | qr!archive.+?\$ korapxml2krill!s, |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 40 | $call |
| 41 | ); |
| 42 | |
| 43 | my $input = catfile($f, '..', 'corpus', 'archive.zip'); |
| 44 | ok(-f $input, 'Input archive found'); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 45 | my $output = File::Temp->newdir(CLEANUP => 0); |
| 46 | $output->unlink_on_destroy(0); |
| 47 | |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 48 | my $cache = tmpnam(); |
| 49 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 50 | ok(-d $output, 'Output directory exists'); |
| 51 | |
| 52 | $call = join( |
| 53 | ' ', |
| 54 | 'perl', $script, |
| 55 | 'archive', |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 56 | '--input' => '' . $input, |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 57 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 58 | '--cache' => $cache, |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 59 | '-t' => 'Base#tokens_aggr', |
| 60 | '-m' => 'Sgbr' |
| 61 | ); |
| 62 | |
| 63 | # Test without compression |
| 64 | my $json; |
| 65 | { |
| 66 | local $SIG{__WARN__} = sub {}; |
| 67 | my $out = stdout_from(sub { system($call); }); |
| 68 | |
| 69 | like($out, qr!TEST-BSP-1\.json!s, $call); |
| 70 | |
| 71 | $out =~ m!Processed (.+?\.json)!; |
| 72 | $json = $1; |
| 73 | }; |
| 74 | |
| 75 | ok(-f $json, 'Json file exists'); |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 76 | ok((my $file = Mojo::File->new($json)->slurp), 'Slurp data'); |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 77 | ok(($json = decode_json $file), 'decode json'); |
| 78 | |
| 79 | is($json->{data}->{tokenSource}, 'base#tokens_aggr', 'Title'); |
| 80 | is($json->{data}->{foundries}, 'base base/paragraphs base/sentences dereko dereko/structure sgbr sgbr/lemma sgbr/morpho', 'Foundries'); |
| 81 | is($json->{sgbrKodex}, 'M', 'Kodex meta data'); |
| 82 | |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 83 | |
| 84 | # Use directory |
| 85 | $input = catdir($f, '..', 'annotation', 'corpus'); |
| 86 | |
| 87 | $call = join( |
| 88 | ' ', |
| 89 | 'perl', $script, |
| 90 | 'archive', |
| 91 | '--input' => $input, |
| 92 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 93 | '--cache' => $cache, |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 94 | '-t' => 'Tree_Tagger#Tokens', |
| 95 | '-j' => 4 # 4 jobs! |
| 96 | ); |
| 97 | |
| 98 | my ($json_1, $json_2); |
| 99 | |
| 100 | { |
| 101 | local $SIG{__WARN__} = sub {}; |
| 102 | |
| 103 | # That's not really stable on slow machines! |
| 104 | my $out = stdout_from(sub { system($call); }); |
| 105 | |
Marc Kupietz | 7fe9cd9 | 2024-03-18 11:50:22 +0100 | [diff] [blame] | 106 | ok($out =~ m!\[\$(\d+?):1\/3\]!s, $call . ' pid 1'); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 107 | my $pid1 = $1; |
Marc Kupietz | 7fe9cd9 | 2024-03-18 11:50:22 +0100 | [diff] [blame] | 108 | ok($out =~ m!\[\$(\d+?):2\/3\]!s, $call . ' pid 2'); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 109 | my $pid2 = $1; |
Marc Kupietz | 7fe9cd9 | 2024-03-18 11:50:22 +0100 | [diff] [blame] | 110 | ok($out =~ m!\[\$(\d+?):3\/3\]!s, $call . ' pid 3'); |
| 111 | my $pid3 = $1; |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 112 | |
| 113 | isnt($pid1, $pid2, 'No PID match'); |
Marc Kupietz | 7fe9cd9 | 2024-03-18 11:50:22 +0100 | [diff] [blame] | 114 | isnt($pid2, $pid3, 'No PID match'); |
| 115 | isnt($pid1, $pid3, 'No PID match'); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 116 | |
| 117 | ok($out =~ m!Processed .+?\/corpus-doc-0001\.json!s, $call); |
| 118 | ok($out =~ m!Processed .+?\/corpus-doc-0002\.json!s, $call); |
Marc Kupietz | 7fe9cd9 | 2024-03-18 11:50:22 +0100 | [diff] [blame] | 119 | ok($out =~ m!Processed .+?\/corpus-doc-0003\.json!s, $call); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 120 | |
| 121 | ok(-d $output, 'Temporary directory still exists'); |
| 122 | my $json_1 = catfile($output, 'corpus-doc-0001.json'); |
| 123 | ok(-f $json_1, 'Json file exists 1'); |
| 124 | my $json_2 = catfile($output, 'corpus-doc-0002.json'); |
| 125 | ok(-f $json_2, 'Json file exists 2'); |
| 126 | |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 127 | ok(($file = Mojo::File->new($json_1)->slurp), 'Slurp data'); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 128 | ok(($json_1 = decode_json $file), 'decode json'); |
| 129 | |
| 130 | is($json_1->{data}->{tokenSource}, 'tree_tagger#tokens', 'TokenSource'); |
Marc Kupietz | b8c5382 | 2024-03-16 18:54:08 +0100 | [diff] [blame] | 131 | is($json_1->{data}->{foundries}, 'base base/paragraphs base/sentences connexor connexor/morpho connexor/phrase connexor/sentences connexor/syntax corenlp corenlp/constituency corenlp/morpho corenlp/sentences dereko dereko/structure glemm glemm/morpho mate mate/dependency mate/morpho opennlp opennlp/morpho opennlp/sentences spacy spacy/morpho treetagger treetagger/morpho treetagger/sentences xip xip/constituency xip/morpho xip/sentences', 'Foundries'); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 132 | is($json_1->{textSigle}, 'Corpus/Doc/0001', 'Sigle'); |
| 133 | |
| 134 | ok(-f $json_2, 'Json file exists'); |
Akron | 3ec0a1c | 2017-01-18 14:41:55 +0100 | [diff] [blame] | 135 | ok(($file = Mojo::File->new($json_2)->slurp), 'Slurp data'); |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 136 | ok(($json_2 = decode_json $file), 'decode json'); |
| 137 | |
| 138 | is($json_2->{data}->{tokenSource}, 'tree_tagger#tokens', 'TokenSource'); |
| 139 | is($json_2->{data}->{foundries}, 'base base/paragraphs base/sentences dereko dereko/structure malt malt/dependency treetagger treetagger/morpho treetagger/sentences', 'Foundries'); |
| 140 | is($json_2->{textSigle}, 'Corpus/Doc/0002', 'Sigle'); |
| 141 | }; |
| 142 | |
| 143 | ok(-d $output, 'Ouput directory exists'); |
Akron | 89df4fa | 2016-11-04 14:35:37 +0100 | [diff] [blame] | 144 | |
| 145 | |
Akron | 63d03ee | 2019-02-13 18:49:38 +0100 | [diff] [blame] | 146 | my $temp_extract = tmpnam(); |
| 147 | |
| 148 | # Ignore -te when archive is a directory |
| 149 | $call = join( |
| 150 | ' ', |
| 151 | 'perl', $script, |
| 152 | 'archive', |
| 153 | '--input' => $input, |
| 154 | '--output' => $output, |
| 155 | '--cache' => $cache, |
| 156 | '-t' => 'Tree_Tagger#Tokens', |
| 157 | '-j' => 4, # 4 jobs! |
| 158 | '-te' => $temp_extract |
| 159 | ); |
| 160 | |
| 161 | { |
| 162 | local $SIG{__WARN__} = sub {}; |
| 163 | |
| 164 | my $out = combined_from(sub { system($call); }); |
| 165 | |
| 166 | ok($out =~ m!Processed .+?\/corpus-doc-0001\.json!s, $call); |
| 167 | ok($out =~ m!Processed .+?\/corpus-doc-0002\.json!s, $call); |
| 168 | }; |
| 169 | |
| 170 | |
Akron | f624084 | 2017-02-17 23:45:26 +0100 | [diff] [blame] | 171 | my $input_quotes = catfile($f, '..', 'corpus', 'archive_quotes.zip'); |
| 172 | $call = join( |
| 173 | ' ', |
| 174 | 'perl', $script, |
| 175 | 'archive', |
| 176 | '--input' => $input_quotes, |
| 177 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 178 | '--cache' => $cache, |
Akron | f624084 | 2017-02-17 23:45:26 +0100 | [diff] [blame] | 179 | '-t' => 'Base#tokens_aggr' |
| 180 | ); |
| 181 | |
| 182 | # Test without parameters |
| 183 | stdout_like( |
| 184 | sub { |
| 185 | system($call); |
| 186 | }, |
| 187 | qr!Done\.!is, |
| 188 | $call |
| 189 | ); |
| 190 | |
Akron | 89df4fa | 2016-11-04 14:35:37 +0100 | [diff] [blame] | 191 | |
Akron | 3ec4897 | 2016-08-17 23:24:52 +0200 | [diff] [blame] | 192 | unlink($output); |
| 193 | |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 194 | $input_quotes = "'".catfile($f, '..', 'corpus', 'archives', 'wpd15*.zip') . "'"; |
| 195 | |
| 196 | $call = join( |
| 197 | ' ', |
| 198 | 'perl', $script, |
| 199 | 'archive', |
| 200 | '--input' => $input_quotes, |
| 201 | '--output' => $output, |
Akron | d5bb434 | 2017-06-19 11:50:49 +0200 | [diff] [blame] | 202 | '--cache' => $cache, |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 203 | '-t' => 'Base#tokens_aggr' |
| 204 | ); |
| 205 | |
| 206 | # Test without parameters |
| 207 | stdout_like( |
| 208 | sub { |
| 209 | system($call); |
| 210 | }, |
Akron | 63f20d4 | 2017-04-10 23:40:29 +0200 | [diff] [blame] | 211 | qr!Input is .+?wpd15-single\.zip,.+?wpd15-single\.malt\.zip,.+?wpd15-single\.corenlp\.zip,.+?wpd15-single\.opennlp\.zip,.+?wpd15-single\.mdparser\.zip,.+?wpd15-single\.tree_tagger\.zip!is, |
Akron | 821db3d | 2017-04-06 21:19:31 +0200 | [diff] [blame] | 212 | $call |
| 213 | ); |
| 214 | |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 215 | |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 216 | # Test with sigles |
| 217 | $input = catfile($f, '..', 'corpus', 'archive.zip'); |
| 218 | ok(-f $input, 'Input archive found'); |
| 219 | |
| 220 | unlink($output); |
| 221 | |
| 222 | $call = join( |
| 223 | ' ', |
| 224 | 'perl', $script, |
| 225 | 'archive', |
| 226 | '--input' => '' . $input, |
| 227 | '--output' => $output, |
| 228 | '--sigle' => 'TEST/BSP/2', |
| 229 | '--sigle' => 'TEST/BSP/5', |
| 230 | '-t' => 'Base#tokens_aggr', |
| 231 | '-m' => 'Sgbr' |
| 232 | ); |
| 233 | |
| 234 | { |
| 235 | local $SIG{__WARN__} = sub {}; |
| 236 | my $out = stdout_from(sub { system($call); }); |
| 237 | |
| 238 | like($out, qr!TEST-BSP-1\.json!s, $call); |
| 239 | |
| 240 | $out =~ m!Processed (.+?\.json)!; |
| 241 | $json = $1; |
| 242 | }; |
| 243 | |
| 244 | ok(-f $json, 'Json file exists'); |
| 245 | |
Akron | a351837 | 2024-01-22 23:29:00 +0100 | [diff] [blame] | 246 | # Test quiet |
| 247 | |
| 248 | # my $input = catfile($f, '..', 'corpus', 'archive.zip'); |
| 249 | # ok(-f $input, 'Input archive found'); |
| 250 | |
| 251 | $call = join( |
| 252 | ' ', |
| 253 | 'perl', $script, |
| 254 | 'archive', |
| 255 | '--input' => '' . $input, |
| 256 | '--quiet', |
| 257 | '--output' => $output, |
| 258 | '--sigle' => 'TEST/BSP/2', |
| 259 | '--sigle' => 'TEST/BSP/5', |
| 260 | '-t' => 'Base#tokens_aggr', |
| 261 | '-m' => 'Sgbr' |
| 262 | ); |
| 263 | |
| 264 | { |
| 265 | local $SIG{__WARN__} = sub {}; |
| 266 | my $out = stdout_from(sub { system($call); }); |
| 267 | |
| 268 | is($out, "\n", $call); |
| 269 | }; |
Akron | 31a08cb | 2019-02-20 20:43:26 +0100 | [diff] [blame] | 270 | |
Akron | 7d4cdd8 | 2016-08-17 21:39:45 +0200 | [diff] [blame] | 271 | done_testing; |
| 272 | __END__ |