Debian Codegraph @

t/yath_script.t

raw
1 #!/usr/bin/perl
2 # HARNESS-NO-FORK
3
4 use strict;
5 use warnings;
6
7 my %ORIG_INC = (%INC);
8
9 @ARGV = ();
10
11 my $in_section;
12 my %sections;
13
14 {
15 local $.;
16 my ($script) = grep { -f $_ } 'lib/App/Yath/Script/V1.pm', '../lib/App/Yath/Script/V1.pm';
17 die "Could not find App::Yath::Script::V1 module" unless $script;
18 open(my $fh, '<', $script) or die "Could not open V1 module: $!";
19
20 my ($tdir) = grep { -d $_ } 't/yath_script', 'yath_script';
21 die "Could not find the t/yath_script directory" unless $tdir;
22 chdir("$tdir/nested") or die "Could not change directory: $!";
23
24 while (my $line = <$fh>) {
25 chomp($line);
26 if ($line =~ m/^\s*# ==START TESTABLE CODE (\S+)==\s*$/) {
27 $in_section = $1;
28 push @{$sections{lc($in_section)}} => ("use strict;", "use warnings FATAL => 'all';", "#line " . ($. + 1) . ' "' . $script . '"');
29 next;
30 }
31 if ($line =~ m/^\s*# ==END TESTABLE CODE\s?(\S+)==\s*$/) {
32 die "In section '$in_section' but found end of section '$1'" unless $1 eq $in_section;
33 $sections{lc($in_section)} = join "\n" => @{delete $sections{lc($in_section)}};
34 $in_section = undef;
35 }
36
37 next unless $in_section;
38 push @{$sections{lc($in_section)}} => $line;
39 }
40 }
41
42 my @RESULTS;
43
44 sub is($$;$) { push @RESULTS => ['is', [@_], [caller()]] }
45 sub like($$;$) { push @RESULTS => ['like', [@_], [caller()]] }
46 sub ok($;$) { push @RESULTS => ['ok', [@_], [caller()]] }
47
48 our %T;
49 sub T() { \%T }
50
51 test_parse_config_files();
52 sub test_parse_config_files {
53 my ($config_args, $to_clean);
54 my $code = delete $sections{parse_config_files};
55
56 local @ARGV = ('START', 'END');
57 my %CONFIG;
58
59 eval <<" EOT" or die $@;
60 my \$config_file = './../.yath.rc';
61 my \$user_config_file = './.yath.user.rc';
62 $code
63 \$config_args = \\\@CONFIG_ARGS;
64 \$to_clean = \\\@TO_CLEAN;
65 1;
66 EOT
67
68 is(
69 $config_args,
70 [
71 '-Dpre_lib',
72 '-D=./../pre/xxx/lib',
73 '-D=./../pre/yyy/lib',
74 '-D=SPLIT',
75 '--no-scan-plugins',
76 '-Dpre_user_lib',
77 '-D=./pre/xxx/user/lib',
78 '-D=./pre/yyy/user/lib',
79 ],
80 "Got pre-args from all config files"
81 );
82
83 is(
84 [@ARGV],
85 [
86 '-Dpre_lib',
87 '-D=./../pre/xxx/lib',
88 '-D=./../pre/yyy/lib',
89 '-D=SPLIT',
90 '--no-scan-plugins',
91 '-Dpre_user_lib',
92 '-D=./pre/xxx/user/lib',
93 '-D=./pre/yyy/user/lib',
94 'START',
95 'END',
96 ],
97 "Prepended args to \@ARGV"
98 );
99
100 is(
101 {%CONFIG},
102 {
103 '~' => [
104 '-pXXX',
105 '-p' => 'YYY',
106 '-pUSER_XXX',
107 '-p' => 'USER_YYY',
108 ],
109 test => [
110 '-Itest_lib',
111 '-I=./../test/xxx/lib',
112 '-I' => './../test/yyy/lib',
113 (map {('--default-search' => $_)} glob('./../../*.t')),
114 '-xxxx',
115 'foo',
116 'bar',
117 'baz' => 'bat',
118 '-Itest_user_lib',
119 '-I=./test/xxx/user/lib',
120 '-I' => './test/yyy/user/lib',
121 '-user_xxxx',
122 'user_foo',
123 'user_bar',
124 'user_baz' => 'user_bat',
125 ],
126 run => [
127 '-Irun_lib',
128 '-I=./../run/xxx/lib',
129 '-I' => './../run/yyy/lib',
130 '-xxxx',
131 'foo',
132 'bar',
133 '-Irun_user_lib',
134 '-I=./run/xxx/user/lib',
135 '-I' => './run/yyy/user/lib',
136 '-user_xxxx',
137 'user_foo',
138 'user_bar',
139 ],
140 },
141 "Parsed all command args properly"
142 );
143
144
145 is(
146 $to_clean,
147 [
148 ['test', T, '-I', '=', './../test/xxx/lib'],
149 ['test', T, '-I', ' ', './../test/yyy/lib'],
150
151 (map { ['test', T, '--default-search', ' ', $_] } glob('./../../*.t')),
152
153 ['run', T, '-I', '=', './../run/xxx/lib'],
154 ['run', T, '-I', ' ', './../run/yyy/lib'],
155
156 ['test', T, '-I', '=', './test/xxx/user/lib'],
157 ['test', T, '-I', ' ', './test/yyy/user/lib'],
158
159 ['run', T, '-I', '=', './run/xxx/user/lib'],
160 ['run', T, '-I', ' ', './run/yyy/user/lib'],
161 ],
162 "Will come back and clean these later"
163 );
164 }
165
166 test_pre_parse_d_args();
167 sub test_pre_parse_d_args {
168 my $code = delete $sections{pre_parse_d_args};
169
170 local @INC = ('START', 'END');
171 local @ARGV = ('START', '-D=aaa', '-Dbbb', '--no-scan-plugins', '--no-dev-lib', '-D', '--dev-lib', '-Dxxx', '-Dxxx', '--dev-lib=foo', '-Dbbb', '::', '-Doops', 'END');
172 my @DEVLIBS;
173 my $NO_PLUGINS;
174
175 my ($libs, $done);
176 eval $code . <<' EOT' or die $@;
177 $libs = \@libs;
178 $done = \%done;
179 1;
180 EOT
181
182 is(
183 [@ARGV],
184 ['START', '::', '-Doops', 'END'],
185 "Modified \@ARGV"
186 );
187
188 is(
189 $libs,
190 ['lib', 'blib/lib', 'blib/arch', 'xxx', 'foo', 'bbb'],
191 "Got expected libs"
192 );
193
194 is(
195 [@DEVLIBS],
196 ['lib', 'blib/lib', 'blib/arch', 'xxx', 'foo', 'bbb'],
197 "Got expected devlibs"
198 );
199
200 is(
201 [@INC],
202 ['lib', 'blib/lib', 'blib/arch', 'xxx', 'foo', 'bbb', 'START', 'END'],
203 "prepended libs to \@INC"
204 );
205
206 is(
207 $NO_PLUGINS,
208 1,
209 "Set no plugins"
210 );
211
212 is(
213 $done,
214 {
215 'lib' => 2,
216 'blib/lib' => 2,
217 'blib/arch' => 2,
218 'xxx' => 2,
219 'foo' => 1,
220 'bbb' => 1,
221 },
222 "Saw each arg as many times as we expected (including the reset mid-way wiping previously seen out)"
223 );
224
225 local @INC = ('START', 'END');
226 local @ARGV = ('START', '-Dbbb', '--', '-Doops', 'END');
227 @DEVLIBS = ();
228
229 eval $code . <<' EOT' or die $@;
230 $libs = \@libs;
231 $done = \%done;
232 1;
233 EOT
234
235 is(
236 [@ARGV],
237 ['START', '--', '-Doops', 'END'],
238 "Modified \@ARGV"
239 );
240
241 is(
242 $libs,
243 ['bbb'],
244 "Got expected libs"
245 );
246
247 is(
248 [@INC],
249 ['bbb', 'START', 'END'],
250 "prepended libs to \@INC"
251 );
252
253 is(
254 $done,
255 {
256 'bbb' => 1,
257 },
258 "Saw each arg as many times as we expected"
259 );
260 }
261
262 is({%INC}, {%ORIG_INC}, "Did not load anything.");
263 require Test2::V0;
264
265 # such a dirty hack!
266 # Turn %T into an instance of the T check.
267 my $t = Test2::V0::T();
268 %T = %$t;
269 bless(\%T, ref($t));
270
271 for my $res (@RESULTS) {
272 my ($func, $args, $caller) = @$res;
273
274 my $sub = Test2::V0->can($func) or die "No such test function: $func at $caller->[1] line $caller->[2]\n";
275
276 $sub->(@$args) or warn "Actual assertion at $caller->[1] line $caller->[2]\n";
277 }
278
279 test_cleanup_paths();
280 sub test_cleanup_paths {
281 my $code = delete $sections{cleanup_paths};
282
283 require Cwd;
284 require File::Spec;
285
286 my @libs = ('../../lib', './', '../');
287 my @DEVLIBS = @libs;
288 local @INC = (@libs, 'START', 'END');
289 my %CONFIG = (
290 test => [
291 '-I' => '../../lib',
292 '-I=../../lib',
293 ],
294 );
295
296 my @TO_CLEAN = (
297 ['test', 1, '-I', ' ', '../../lib'],
298 ['test', 2, '-I', '=', '../../lib'],
299 );
300
301 eval $code . "\n1;" or die $@;
302
303 Test2::V0::is(
304 \@INC,
305 [(map { Cwd::realpath($_) } @libs), 'START', 'END'],
306 "Cleaned up \@INC"
307 );
308
309 Test2::V0::is(
310 \@DEVLIBS,
311 [(map { Cwd::realpath($_) } @libs)],
312 "Cleaned up \@DEVLIBS"
313 );
314
315 Test2::V0::is(
316 \%CONFIG,
317 {
318 test => [
319 '-I' => Cwd::realpath('../../lib'),
320 '-I=' . Cwd::realpath('../../lib'),
321 ],
322 },
323 "Cleaned up \%CONFIG"
324 );
325 }
326
327 test_create_app();
328 sub test_create_app {
329 my $code = delete $sections{create_app};
330
331 my $args;
332 require App::Yath;
333 my $control = Test2::V0::mock(
334 'App::Yath' => (
335 override => {
336 generate_run_sub => sub { $args = [@_] },
337 },
338 )
339 );
340
341 my (%ORIG_SIG, @ORIG_ARGV, @ORIG_INC, @DEVLIBS, @ARGV, %CONFIG, $NO_PLUGINS, $ORIG_TMP, $ORIG_TMP_PERMS, $config_file, $user_config_file);
342 my $script = "foobar";
343 $NO_PLUGINS = 2;
344 eval $code or die $@;
345
346 my ($app, $symbol) = @$args;
347 Test2::V0::isa_ok($app, 'App::Yath');
348 Test2::V0::is($symbol, 'App::Yath::Script::V1::_run', "Got correct symbol");
349
350 Test2::V0::ref_is($app->_argv, \@ARGV, "Used ARGV");
351 Test2::V0::ref_is($app->config, \%CONFIG, "Used CONFIG");
352
353 my $settings = $app->settings;
354 is(
355 $settings,
356 {
357 yath => {
358 orig_sig => Test2::V0::exact_ref(\%ORIG_SIG),
359 orig_argv => Test2::V0::exact_ref(\@ORIG_ARGV),
360 orig_inc => Test2::V0::exact_ref(\@ORIG_INC),
361 dev_libs => Test2::V0::exact_ref(\@DEVLIBS),
362 script => $script,
363 no_scan_plugins => 2,
364 start => Test2::V0::T(),
365 },
366 },
367 "Got settings"
368 );
369 }
370
371 die "The following sections were not tested: " . join(', ', keys %sections)
372 if keys %sections;
373
374 Test2::V0::done_testing();