Debian Codegraph @

README.md

raw
1 # NAME
2
3 App::Yath - Yet Another Test Harness (Test2-Harness) Command Line Interface
4 (CLI)
5
6 # DESCRIPTION
7
8 This is the primary documentation for `yath`, [App::Yath](https://metacpan.org/pod/App%3A%3AYath), [Test2::Harness](https://metacpan.org/pod/Test2%3A%3AHarness).
9
10 The canonical source of up-to-date command options are the help output when
11 using `$ yath help` and `$ yath help COMMAND`.
12
13 This document is mainly an overview of `yath` usage and common recipes.
14
15 [App::Yath](https://metacpan.org/pod/App%3A%3AYath) is an alternative to [App::Prove](https://metacpan.org/pod/App%3A%3AProve), and [Test2::Harness](https://metacpan.org/pod/Test2%3A%3AHarness) is an alternative to [Test::Harness](https://metacpan.org/pod/Test%3A%3AHarness). It is not designed to
16 replace [Test::Harness](https://metacpan.org/pod/Test%3A%3AHarness)/prove. [Test2::Harness](https://metacpan.org/pod/Test2%3A%3AHarness) is designed to take full
17 advantage of the rich data [Test2](https://metacpan.org/pod/Test2) can provide. [Test2::Harness](https://metacpan.org/pod/Test2%3A%3AHarness) is also able to
18 use non-core modules and provide more functionality than prove can achieve with
19 its restrictions.
20
21 # PLATFORM SUPPORT
22
23 [Test2::Harness](https://metacpan.org/pod/Test2%3A%3AHarness)/[App::Yath](https://metacpan.org/pod/App%3A%3AYath) is is focused on unix-like platforms. Most
24 development happens on linux, but bsd, macos, etc should work fine as well.
25
26 Patches are welcome for any/all platforms, but the primary author (Chad
27 'Exodist' Granum) does not directly develop against non-unix platforms.
28
29 ## WINDOWS
30
31 Currently windows is not supported, and it is known that the package will not
32 install on windows. Patches are be welcome, and it would be great if someone
33 wanted to take on the windows-support role, but it is not a primary goal for
34 the project.
35
36 # OVERVIEW
37
38 To use [Test2::Harness](https://metacpan.org/pod/Test2%3A%3AHarness), you use the `yath` command. Yath will find the tests
39 (or use the ones you specify) and run them. As it runs, it will output
40 diagnostic information such as failures. At the end, yath will print a summary
41 of the test run.
42
43 `yath` can be thought of as a more powerful alternative to `prove`
44 ([Test::Harness](https://metacpan.org/pod/Test%3A%3AHarness))
45
46 # RECIPES
47
48 These are common recipes for using `yath`.
49
50 ## RUN PROJECT TESTS
51
52 $ yath
53
54 Simply running yath with no arguments means "Run all tests for the current
55 project". Yath will look for tests in `./t`, `./t2`, and `./test.pl` and
56 run any which are found.
57
58 Normally this implies the `test` command but will instead imply the `run`
59 command if a persistent test runner is detected.
60
61 ## PRELOAD MODULES
62
63 Yath has the ability to preload modules. Yath normally forks to start new
64 tests, so preloading can reduce the time spent loading modules over and over in
65 each test.
66
67 Note that some tests may depend on certain modules not being loaded. In these
68 cases you can add the `# HARNESS-NO-PRELOAD` directive to the top of the test
69 files that cannot use preload.
70
71 ### SIMPLE PRELOAD
72
73 Any module can be preloaded:
74
75 $ yath -PMoose
76
77 You can preload as many modules as you want:
78
79 $ yath -PList::Util -PScalar::Util
80
81 ### COMPLEX PRELOAD
82
83 If your preload is a subclass of [Test2::Harness::Runner::Preload](https://metacpan.org/pod/Test2%3A%3AHarness%3A%3ARunner%3A%3APreload) then more
84 complex preload behavior is possible. See those docs for more info.
85
86 ## LOGGING
87
88 ### RECORDING A LOG
89
90 You can turn on logging with a flag. The filename of the log will be printed at
91 the end.
92
93 $ yath -L
94 ...
95 Wrote log file: test-logs/2017-09-12~22:44:34~1505281474~25709.jsonl
96
97 The event log can be quite large. It can be compressed with bzip2.
98
99 $ yath -B
100 ...
101 Wrote log file: test-logs/2017-09-12~22:44:34~1505281474~25709.jsonl.bz2
102
103 gzip compression is also supported.
104
105 $ yath -G
106 ...
107 Wrote log file: test-logs/2017-09-12~22:44:34~1505281474~25709.jsonl.gz
108
109 `-B` and `-G` both imply `-L`.
110
111 ### REPLAYING FROM A LOG
112
113 You can replay a test run from a log file:
114
115 $ yath test-logs/2017-09-12~22:44:34~1505281474~25709.jsonl.bz2
116
117 This will be significantly faster than the initial run as no tests are actually
118 being executed. All events are simply read from the log, and processed by the
119 harness.
120
121 You can change display options and limit rendering/processing to specific test
122 jobs from the run:
123
124 $ yath test-logs/2017-09-12~22:44:34~1505281474~25709.jsonl.bz2 -v [TEST UUID(S)]
125
126 Note: This is done using the `$ yath replay ...` command. The `replay`
127 command is implied if the first argument is a log file.
128
129 ## PER-TEST TIMING DATA
130
131 The `-T` option will cause each test file to report how long it took to run.
132
133 $ yath -T
134
135 ( PASSED ) job 1 t/yath_script.t
136 ( TIME ) job 1 Startup: 0.07692s | Events: 0.01170s | Cleanup: 0.00190s | Total: 0.09052s
137
138 ## PERSISTENT RUNNER
139
140 yath supports starting a yath session that waits for tests to run. This is very
141 useful when combined with preload.
142
143 ### STARTING
144
145 This starts the server. Many options available to the 'test' command will work
146 here but not all. See `$ yath help start` for more info.
147
148 $ yath start
149
150 ### RUNNING
151
152 This will run tests using the persistent runner. By default, it will search for
153 tests just like the 'test' command. Many options available to the `test`
154 command will work for this as well. See `$ yath help run` for more details.
155
156 $ yath run
157
158 ### STOPPING
159
160 Stopping a persistent runner is easy.
161
162 $ yath stop
163
164 ### INFORMATIONAL
165
166 The `which` command will tell you which persistent runner will be used. Yath
167 searches for the persistent runner in the current directory, then searches in
168 parent directories until it either hits the root directory, or finds the
169 persistent runner tracking file.
170
171 $ yath which
172
173 The `watch` command will tail the runner's log files.
174
175 $ yath watch
176
177 ### PRELOAD + PERSISTENT RUNNER
178
179 You can use preloads with the `yath start` command. In this case, yath will
180 track all the modules pulled in during preload. If any of them change, the
181 server will reload itself to bring in the changes. Further, modified modules
182 will be blacklisted so that they are not preloaded on subsequent reloads. This
183 behavior is useful if you are actively working on a module that is normally
184 preloaded.
185
186 ## MAKING YOUR PROJECT ALWAYS USE YATH
187
188 $ yath init
189
190 The above command will create `test.pl`. `test.pl` is automatically run by
191 most build utils, in which case only the exit value matters. The generated
192 `test.pl` will run `yath` and execute all tests in the `./t` and/or `./t2`
193 directories. Tests in `./t` will ALSO be run by prove but tests in `./t2`
194 will only be run by yath.
195
196 ## PROJECT-SPECIFIC YATH CONFIG
197
198 You can write a `.yath.rc` file. The file format is very simple. Create a
199 `[COMMAND]` section to start the configuration for a command and then
200 provide any options normally allowed by it. When `yath` is run inside your
201 project, it will use the config specified in the rc file, unless overridden
202 by command line options.
203
204 **Note:** You can also add pre-command options by placing them at the top of
205 your config file _BEFORE_ any `[cmd]` markers.
206
207 Comments start with a semi-colon.
208
209 Example .yath.rc:
210
211 -pFoo ; Load the 'foo' plugin before dealing with commands.
212
213 [test]
214 -B ;Always write a bzip2-compressed log
215
216 [start]
217 -PMoose ;Always preload Moose with a persistent runner
218
219 This file is normally committed into the project's repo.
220
221 ### SPECIAL PATH PSEUDO-FUNCTIONS
222
223 Sometimes you want to specify files relative to the .yath.rc so that the config
224 option works from any subdirectory of the project. Other times you may wish to
225 use a shell expansion. Sometimes you want both!
226
227 - rel(path/to/file)
228
229 -I rel(path/to/extra_lib)
230 -I=rel(path/to/extra_lib)
231
232 This will take the path to `.yath.rc` and prefix it to the path inside
233 `rel(...)`. If for example you have `/project/.yath.rc` then the path would
234 become `/project/path/to/extra_lib`.
235
236 - glob(path/\*/file)
237
238 --default-search glob(subprojects/*/t)
239 --default-search=glob(subprojects/*/t)
240
241 This will add a `--default-search $_` for every item found in the glob. This
242 uses the perl builtin function `glob()` under the hood.
243
244 - relglob(path/\*/file)
245
246 --default-search relglob(subprojects/*/t)
247 --default-search=relglob(subprojects/*/t)
248
249 Same as `glob()` except paths are relative to the `.yath.rc` file.
250
251 ## PROJECT-SPECIFIC YATH CONFIG USER OVERRIDES
252
253 You can add a `.yath.user.rc` file. Format is the same as the regular
254 `.yath.rc` file. This file will be read in addition to the regular config
255 file. Directives in this file will come AFTER the directives in the primary
256 config so it may be used to override config.
257
258 This file should not normally be committed to the project repo.
259
260 ## HARNESS DIRECTIVES INSIDE TESTS
261
262 `yath` will recognise a number of directive comments placed near the top of
263 test files. These directives should be placed after the `#!` line but
264 before any real code.
265
266 Real code is defined as any line that does not start with use, require, BEGIN, package, or #
267
268 - good example 1
269
270 #!/usr/bin/perl
271 # HARNESS-NO-FORK
272
273 ...
274
275 - good example 2
276
277 #!/usr/bin/perl
278 use strict;
279 use warnings;
280
281 # HARNESS-NO-FORK
282
283 ...
284
285 - bad example 1
286
287 #!/usr/bin/perl
288
289 # blah
290
291 # HARNESS-NO-FORK
292
293 ...
294
295 - bad example 2
296
297 #!/usr/bin/perl
298
299 print "hi\n";
300
301 # HARNESS-NO-FORK
302
303 ...
304
305 ### HARNESS-NO-PRELOAD
306
307 #!/usr/bin/perl
308 # HARNESS-NO-PRELOAD
309
310 Use this if your test will fail when modules are preloaded. This will tell yath
311 to start a new perl process to run the script instead of forking with preloaded
312 modules.
313
314 Currently this implies HARNESS-NO-FORK, but that may not always be the case.
315
316 ### HARNESS-NO-FORK
317
318 #!/usr/bin/perl
319 # HARNESS-NO-FORK
320
321 Use this if your test file cannot run in a forked process, but instead must be
322 run directly with a new perl process.
323
324 This implies HARNESS-NO-PRELOAD.
325
326 ### HARNESS-NO-STREAM
327
328 `yath` usually uses the [Test2::Formatter::Stream](https://metacpan.org/pod/Test2%3A%3AFormatter%3A%3AStream) formatter instead of TAP.
329 Some tests depend on using a TAP formatter. This option will make `yath` use
330 [Test2::Formatter::TAP](https://metacpan.org/pod/Test2%3A%3AFormatter%3A%3ATAP) or [Test::Builder::Formatter](https://metacpan.org/pod/Test%3A%3ABuilder%3A%3AFormatter).
331
332 ### HARNESS-NO-IO-EVENTS
333
334 `yath` can be configured to use the [Test2::Plugin::IOEvents](https://metacpan.org/pod/Test2%3A%3APlugin%3A%3AIOEvents) plugin. This
335 plugin replaces STDERR and STDOUT in your test with tied handles that fire off
336 proper [Test2::Event](https://metacpan.org/pod/Test2%3A%3AEvent)'s when they are printed to. Most of the time this is not
337 an issue, but any fancy tests or modules which do anything with STDERR or
338 STDOUT other than print may have really messy errors.
339
340 **Note:** This plugin is disabled by default, so you only need this directive if
341 you enable it globally but need to turn it back off for select tests.
342
343 ### HARNESS-NO-TIMEOUT
344
345 `yath` will usually kill a test if no events occur within a timeout (default
346 60 seconds). You can add this directive to tests that are expected to trip the
347 timeout, but should be allowed to continue.
348
349 NOTE: you usually are doing the wrong thing if you need to set this. See:
350 `HARNESS-TIMEOUT-EVENT`.
351
352 ### HARNESS-TIMEOUT-EVENT 60
353
354 `yath` can be told to alter the default event timeout from 60 seconds to another
355 value. This is the recommended alternative to HARNESS-NO-TIMEOUT
356
357 ### HARNESS-TIMEOUT-POSTEXIT 15
358
359 `yath` can be told to alter the default POSTEXIT timeout from 15 seconds to another value.
360
361 Sometimes a test will fork producing output in the child while the parent is
362 allowed to exit. In these cases we cannot rely on the original process exit to
363 tell us when a test is complete. In cases where we have an exit, and partial
364 output (assertions with no final plan, or a plan that has not been completed)
365 we wait for a timeout period to see if any additional events come into
366
367 ### HARNESS-DURATION-LONG
368
369 This lets you tell `yath` that the test file is long-running. This is
370 primarily used when concurrency is turned on in order to run longer tests
371 earlier, and concurrently with shorter ones. There is also a `yath` option to
372 skip all long tests.
373
374 This duration is set automatically if HARNESS-NO-TIMEOUT is set.
375
376 ### HARNESS-DURATION-MEDIUM
377
378 This lets you tell `yath` that the test is medium.
379
380 This is the default duration.
381
382 ### HARNESS-DURATION-SHORT
383
384 This lets you tell `yath` That the test is short.
385
386 ### HARNESS-CATEGORY-ISOLATION
387
388 This lets you tell `yath` that the test cannot be run concurrently with other
389 tests. Yath will hold off and run these tests one at a time after all other
390 tests.
391
392 ### HARNESS-CATEGORY-IMMISCIBLE
393
394 This lets you tell `yath` that the test cannot be run concurrently with other
395 tests of this class. This is helpful when you have multiple tests which would
396 otherwise have to be run sequentially at the end of the run.
397
398 Yath prioritizes running these tests above HARNESS-CATEGORY-LONG.
399
400 ### HARNESS-CATEGORY-GENERAL
401
402 This is the default category.
403
404 ### HARNESS-CONFLICTS-XXX
405
406 This lets you tell `yath` that no other test of type XXX can be run at the
407 same time as this one. You are able to set multiple conflict types and `yath`
408 will honor them.
409
410 XXX can be replaced with any type of your choosing.
411
412 NOTE: This directive does not alter the category of your test. You are free
413 to mark the test with LONG or MEDIUM in addition to this marker.
414
415 - Example with multiple lines.
416
417 #!/usr/bin/perl
418 # DASH and space are split the same way.
419 # HARNESS-CONFLICTS-DAEMON
420 # HARNESS-CONFLICTS MYSQL
421
422 ...
423
424 - Or on a single line.
425
426 #!/usr/bin/perl
427 # HARNESS-CONFLICTS DAEMON MYSQL
428
429 ...
430
431 ### HARNESS-SHARES-XXX
432
433 This lets you tell `yath` that this test uses XXX, but is happy to share it
434 with other tests that merely use it too. Any number of `HARNESS-SHARES-XXX`
435 tests may run at the same time, but none of them will run at the same time
436 as a `HARNESS-CONFLICTS-XXX` test. If you think of the two directives as a
437 lock on the name XXX, `CONFLICTS` takes it exclusively and `SHARES` takes
438 it shared.
439
440 XXX can be replaced with any type of your choosing, and you may set several
441 of them.
442
443 The usual shape for this is one test that disturbs a resource and many that
444 merely read it: mark the disruptive one `HARNESS-CONFLICTS-XXX` and the
445 rest `HARNESS-SHARES-XXX`, and the readers keep their parallelism instead
446 of being serialized alongside the writer.
447
448 NOTE: An exclusive test has to wait for every shared test holding the name
449 to finish, and `yath` will not hold job slots empty to make room for it. If
450 shared tests keep becoming available the exclusive one may run quite late in
451 the run, possibly last and by itself. This is still better than marking
452 every test `CONFLICTS`, but it is worth knowing before you mark hundreds of
453 tests as sharing one name.
454
455 NOTE: This directive does not alter the category of your test. You are free
456 to mark the test with LONG or MEDIUM in addition to this marker.
457
458 ### HARNESS-JOB-SLOTS 2
459
460 ### HARNESS-JOB-SLOTS 1 10
461
462 Specify a range of job slots needed for the test to run. If set to a single
463 value then the test will only run if it can have the specified number of slots.
464 If given a range the test will require at least the lower number of slots, and
465 use up to the maximum number of slots.
466
467 ### HARNESS-RETRY-n
468
469 This lets you specify a number (minimum n=1) of retries on test failure
470 for a specific test. HARNESS-RETRY-1 means a failing test will be run twice
471 and is equivalent to HARNESS-RETRY.
472
473 ### HARNESS-NO-RETRY
474
475 Use this to avoid this test being retried regardless of your retry settings.
476
477 # MODULE DOCS
478
479 This section documents the [App::Yath](https://metacpan.org/pod/App%3A%3AYath) module itself.
480
481 ## SYNOPSIS
482
483 In practice you should never need to write your own yath script, or construct
484 an [App::Yath](https://metacpan.org/pod/App%3A%3AYath) instance, or even access the main instance when yath is running.
485 However some aspects of doing so are documented here for completeness.
486
487 A minimum yath script looks like this:
488
489 BEGIN {
490 package App::Yath:Script;
491
492 require Time::HiRes;
493 require App::Yath;
494 require Test2::Harness::Settings;
495
496 my $settings = Test2::Harness::Settings->new(
497 harness => {
498 orig_argv => [@ARGV],
499 orig_inc => [grep { !ref($_) } @INC],
500 script => __FILE__,
501 start => Time::HiRes::time(),
502 version => $App::Yath::VERSION,
503 },
504 );
505
506 my $app = App::Yath->new(
507 argv => \@ARGV,
508 config => {},
509 settings => $settings,
510 );
511
512 $app->generate_run_sub('App::Yath::Script::run');
513 }
514
515 exit(App::Yath::Script::run());
516
517 It is important that most logic live in a BEGIN block. This is so that
518 [goto::file](https://metacpan.org/pod/goto%3A%3Afile) can be used post-fork to execute a test script.
519
520 The actual yath script is significantly more complicated with the following behaviors:
521
522 - pre-process essential arguments such as -D and no-scan-plugins
523 - re-exec with a different yath script if in developer mode and a local copy is found
524 - Parse the yath-rc config files
525 - gather and store essential startup information
526
527 ## METHODS
528
529 App::Yath does not provide many methods to use externally.
530
531 - $app->generate\_run\_sub($symbol\_name)
532
533 This tells App::Yath to generate a subroutine at the specified symbol name
534 which can be run and be expected to return an exit value.
535
536 - $lib\_path = $app->app\_path()
537
538 Get the include directory App::Yath was loaded from.
539
540 # SOURCE
541
542 The source code repository for Test2-Harness can be found at
543 `http://github.com/Test-More/Test2-Harness/`.
544
545 # MAINTAINERS
546
547 - Chad Granum <[email protected]>
548
549 # AUTHORS
550
551 - Chad Granum <[email protected]>
552
553 # COPYRIGHT
554
555 Copyright 2020 Chad Granum <[email protected]>.
556
557 This program is free software; you can redistribute it and/or
558 modify it under the same terms as Perl itself.
559
560 See `http://dev.perl.org/licenses/`