1Test2::Manual::Tooling:U:sPelrugCionn:t:rTiebsuttTEeexdsittP2(e:3r:)lMaDnoucaulm:e:nTtoaotliionng::Plugin::TestExit(3)
2
3
4
6 Test2::Manual::Tooling::Plugin::TestExit - How to safely add pre-exit
7 behaviors.
8
10 This describes the correct/safe way to add pre-exit behaviors to tests
11 via a custom plugin.
12
13 The naive way to attempt this would be to add an "END { ... }" block.
14 That can work, and may not cause problems.... On the other hand there
15 are a lot of ways that can bite you. Describing all the potential
16 problems of an END block, and how it might conflict with Test2 (Which
17 has its own END block) is beyond the scope of this document.
18
20 package Test2::Plugin::MyPlugin;
21
22 use Test2::API qw{test2_add_callback_exit};
23
24 sub import {
25 my $class = shift;
26
27 test2_add_callback_exit(sub {
28 my ($ctx, $orig_code, $new_exit_code_ref) = @_;
29
30 return if $orig_code == 42;
31
32 $$new_exit_code_ref = 42;
33 });
34 }
35
36 1;
37
39 use Test2::API qw{test2_add_callback_exit};
40 This imports the "(test2_add_callback_exit)" callback.
41
42 test2_add_callback_exit(sub { ... });
43 This adds our callback to be called before exiting.
44
45 my ($ctx, $orig_code, $new_exit_code_ref) = @_
46 The callback gets 3 arguments. First is a context object you may
47 use. The second is the original exit code of the "END" block Test2
48 is using. The third argument is a scalar reference which you may
49 use to get the current exit code, or set a new one.
50
51 return if $orig_code == 42
52 This is a short-cut to do nothing if the original exit code was
53 already 42.
54
55 $$new_exit_code_ref = 42
56 This changes the exit code to 42.
57
59 Test2::Manual - Primary index of the manual.
60
62 The source code repository for Test2-Manual can be found at
63 https://github.com/Test-More/Test2-Suite/.
64
66 Chad Granum <exodist@cpan.org>
67
69 Chad Granum <exodist@cpan.org>
70
72 Copyright 2018 Chad Granum <exodist@cpan.org>.
73
74 This program is free software; you can redistribute it and/or modify it
75 under the same terms as Perl itself.
76
77 See http://dev.perl.org/licenses/
78
79
80
81perl v5.30.1 20T2e0s-t021:-:3M1anual::Tooling::Plugin::TestExit(3)