1=pod 2 3=head1 NAME 4 5Test2::Transition - Transition notes when upgrading to Test2 6 7=head1 DESCRIPTION 8 9This is where gotchas and breakages related to the Test2 upgrade are 10documented. The upgrade causes Test::Builder to defer to Test2 under the hood. 11This transition is mostly transparent, but there are a few cases that can trip 12you up. 13 14=head1 THINGS THAT BREAK 15 16This is the list of scenarios that break with the new internals. 17 18=head2 Test::Builder1.5/2 conditionals 19 20=head3 The Problem 21 22a few years back there were two attempts to upgrade/replace Test::Builder. 23Confusingly these were called Test::Builder2 and Test::Builder1.5, in that 24order. Many people put conditionals in their code to check the Test::Builder 25version number and adapt their code accordingly. 26 27The Test::Builder2/1.5 projects both died out. Now the conditional code people 28added has become a mine field. A vast majority of modules broken by Test2 fall 29into this category. 30 31=head3 The Fix 32 33The fix is to remove all Test::Builder1.5/2 related code. Either use the 34legacy Test::Builder API, or use Test2 directly. 35 36=head2 Replacing the Test::Builder singleton 37 38=head3 The Problem 39 40Some test modules would replace the Test::Builder singleton instance with their 41own instance or subclass. This was usually done to intercept or modify results 42as they happened. 43 44The Test::Builder singleton is now a simple compatibility wrapper around 45Test2. The Test::Builder singleton is no longer the central place for 46results. Many results bypass the Test::Builder singleton completely, which 47breaks and behavior intended when replacing the singleton. 48 49=head3 The Fix 50 51If you simply want to intercept all results instead of letting them go to TAP, 52you should look at the L<Test2::API> docs and read about pushing a new hub onto 53the hub stack. Replacing the hub temporarily is now the correct way to 54intercept results. 55 56If your goal is purely monitoring of events use the C<< Test2::Hub->listen() >> 57method exported by Test::More to watch events as they are fired. If you wish to 58modify results before they go to TAP look at the C<< Test2::Hub->filter() >> 59method. 60 61=head2 Directly Accessing Hash Elements 62 63=head3 The Problem 64 65Some modules look directly at hash keys on the Test::Builder singleton. The 66problem here is that the Test::Builder singleton no longer holds anything 67important. 68 69=head3 The Fix 70 71The fix is to use the API specified in L<Test2::API> to look at or modify state 72as needed. 73 74=head2 Subtest indentation 75 76=head3 The Problem 77 78An early change, in fact the change that made Test2 an idea, was a change to 79the indentation of the subtest note. IT was decided it would be more readable 80to outdent the subtest note instead of having it inline with the subtest: 81 82 # subtest foo 83 ok 1 - blah 84 1..1 85 ok 1 - subtest foo 86 87The old style indented the note: 88 89 # subtest foo 90 ok 1 - blah 91 1..1 92 ok 1 - subtest foo 93 94This breaks tests that do string comparison of TAP output. 95 96=head3 The Fix 97 98 my $indent = $INC{'Test2/API.pm'} ? '' : ' '; 99 100 is( 101 $subtest_output, 102 "${indent}# subtest foo", 103 "Got subtest note" 104 ); 105 106Check if C<$INC{'Test2/API.pm'}> is set, if it is then no indentation should be 107expected. If it is not set than the old Test::Builder is in use, indentation 108should be expected. 109 110=head1 DISTRIBUTIONS THAT BREAK OR NEED TO BE UPGRADED 111 112This is a list of cpan modules that have been known to have been broken by the 113upgrade at one point. 114 115=head2 WORKS BUT TESTS WILL FAIL 116 117These modules still function correctly, but their test suites will not pass. If 118you already have these modules installed then you can continue to use them. If 119you are trying to install them after upgrading Test::Builder you will need to 120force installation, or bypass the broken tests. 121 122=over 4 123 124=item Test::DBIx::Class::Schema 125 126This module has a test that appears to work around a Test::Builder bug. The bug 127appears to have been fixed by Test2, which means the workaround causes a 128failure. This can be easily updated, but nobody has done so yet. 129 130Known broken in versions: 1.0.9 and older 131 132=item Test::Kit 133 134This actually works fine, but will not install because L<Test::Aggregate> is in 135the dependency chain. 136 137See the L<Test::Aggregate> info below for additional information. 138 139=item Device::Chip 140 141Tests break due to subtest indentation. 142 143Known broken in version 0.07. Apparently works fine in 0.06 though. Patch has 144been submitted to fix the issue. 145 146=back 147 148=head2 UPGRADE SUGGESTED 149 150These are modules that did not break, but had broken test suites that have 151since been fixed. 152 153=over 4 154 155=item Test::Exception 156 157Old versions work fine, but have a minor test name behavior that breaks with 158Test2. Old versions will no longer install because of this. The latest version 159on CPAN will install just fine. Upgrading is not required, but is recommended. 160 161Fixed in version: 0.43 162 163=item Data::Peek 164 165Some tests depended on C<$!> and C<$?> being modified in subtle ways. A patch 166was applied to correct things that changed. 167 168The module itself works fine, there is no need to upgrade. 169 170Fixed in version: 0.45 171 172=item circular::require 173 174Some tests were fragile and required base.pm to be loaded at a late stage. 175Test2 was loading base.pm too early. The tests were updated to fix this. 176 177The module itself never broke, you do not need to upgrade. 178 179Fixed in version: 0.12 180 181=item Test::Module::Used 182 183A test worked around a now-fixed planning bug. There is no need to upgrade if 184you have an old version installed. New versions install fine if you want them. 185 186Fixed in version: 0.2.5 187 188=item Test::Moose::More 189 190Some tests were fragile, but have been fixed. The actual breakage was from the 191subtest comment indentation change. 192 193No need to upgrade, old versions work fine. Only new versions will install. 194 195Fixed in version: 0.025 196 197=item Test::FITesque 198 199This was broken by a bugfix to how planning is done. The test was updated after 200the bugfix. 201 202Fixed in version: 0.04 203 204=item autouse 205 206A test broke because it depended on Scalar::Util not being loaded. Test2 loads 207Scalar::Util. The test was updated to load Test2 after checking Scalar::Util's 208load status. 209 210There is no need to upgrade if you already have it installed. 211 212Fixed in version: 1.11 213 214=back 215 216=head2 NEED TO UPGRADE 217 218=over 4 219 220=item Test::SharedFork 221 222Old versions need to directly access Test::Builder singleton hash elements. The 223latest version on CPAN will still do this on old Test::Builder, but will defer 224to L<Test2::IPC> on Test2. 225 226Fixed in version: 0.35 227 228=item Test::Builder::Clutch 229 230This works by doing overriding methods on the singleton, and directly accessing 231hash values on the singleton. A new version has been released that uses the 232Test2 API to accomplish the same result in a saner way. 233 234Fixed in version: 0.07 235 236=item Test::Dist::VersionSync 237 238This had Test::Builder2 conditionals. This was fixed by removing the 239conditionals. 240 241Fixed in version: 1.1.4 242 243=item Test::Modern 244 245This relied on C<< Test::Builder->_try() >> which was a private method, 246documented as something nobody should use. This was fixed by using a different 247tool. 248 249Fixed in version: 0.012 250 251=item Test::UseAllModules 252 253Version 0.14 relied on C<< Test::Builder->history >> which was available in 254Test::Builder 1.5. Versions 0.12 and 0.13 relied on other Test::Builder 255internals. 256 257Fixed in version: 0.15 258 259=item Test::More::Prefix 260 261Worked by applying a role that wrapped C<< Test::Builder->_print_comment >>. 262Fixed by adding an event filter that modifies the message instead when running 263under Test2. 264 265Fixed in version: 0.007 266 267=back 268 269=head2 STILL BROKEN 270 271=over 4 272 273=item Test::Aggregate 274 275This distribution directly accesses the hash keys in the L<Test::Builder> 276singleton. It also approaches the problem from the wrong angle, please consider 277using L<Test2::Harness> or L<App::ForkProve> which both solve the same problem 278at the harness level. 279 280Still broken as of version: 0.373 281 282=item Test::Wrapper 283 284This module directly uses hash keys in the L<Test::Builder> singleton. This 285module is also obsolete thanks to the benefits of L<Test2>. Use C<intercept()> 286from L<Test2::API> to achieve a similar result. 287 288Still broken as of version: 0.3.0 289 290=item Test::ParallelSubtest 291 292This module overrides C<Test::Builder::subtest()> and 293C<Test::Builder::done_testing()>. It also directly accesses hash elements of 294the singleton. It has not yet been fixed. 295 296Alternatives: L<Test2::AsyncSubtest> and L<Test2::Workflow> (not stable). 297 298Still broken as of version: 0.05 299 300=item Test::Pretty 301 302See https://github.com/tokuhirom/Test-Pretty/issues/25 303 304The author admits the module is crazy, and he is awaiting a stable release of 305something new (Test2) to completely rewrite it in a sane way. 306 307Still broken as of version: 0.32 308 309=item Net::BitTorrent 310 311The tests for this module directly access L<Test::Builder> hash keys. Most, if 312not all of these hash keys have public API methods that could be used instead 313to avoid the problem. 314 315Still broken in version: 0.052 316 317=item Test::Group 318 319It monkeypatches Test::Builder, and calls it "black magic" in the code. 320 321Still broken as of version: 0.20 322 323=item Test::Flatten 324 325This modifies the Test::Builder internals in many ways. A better was to 326accomplish the goal of this module is to write your own subtest function. 327 328Still broken as of version: 0.11 329 330=item Log::Dispatch::Config::TestLog 331 332Modifies Test::Builder internals. 333 334Still broken as of version: 0.02 335 336=item Test::Able 337 338Modifies Test::Builder internals. 339 340Still broken as of version: 0.11 341 342=back 343 344=head1 MAKE ASSERTIONS -> SEND EVENTS 345 346=head2 LEGACY 347 348 use Test::Builder; 349 350 # A majority of tools out there do this: 351 # my $TB = Test::Builder->new; 352 # This works, but has always been wrong, forcing Test::Builder to implement 353 # subtests as a horrific hack. It also causes problems for tools that try 354 # to replace the singleton (also discouraged). 355 356 sub my_ok($;$) { 357 my ($bool, $name) = @_; 358 my $TB = Test::Builder->new; 359 $TB->ok($bool, $name); 360 } 361 362 sub my_diag($) { 363 my ($msg) = @_; 364 my $TB = Test::Builder->new; 365 $TB->diag($msg); 366 } 367 368=head2 TEST2 369 370 use Test2::API qw/context/; 371 372 sub my_ok($;$) { 373 my ($bool, $name) = @_; 374 my $ctx = context(); 375 $ctx->ok($bool, $name); 376 $ctx->release; 377 } 378 379 sub my_diag($) { 380 my ($msg) = @_; 381 my $ctx = context(); 382 $ctx->diag($msg); 383 $ctx->release; 384 } 385 386The context object has API compatible implementations of the following methods: 387 388=over 4 389 390=item ok($bool, $name) 391 392=item diag(@messages) 393 394=item note(@messages) 395 396=item subtest($name, $code) 397 398=back 399 400If you are looking for helpers with C<is>, C<like>, and others, see 401L<Test2::Suite>. 402 403=head1 WRAP EXISTING TOOLS 404 405=head2 LEGACY 406 407 use Test::More; 408 409 sub exclusive_ok { 410 my ($bool1, $bool2, $name) = @_; 411 412 # Ensure errors are reported 1 level higher 413 local $Test::Builder::Level = $Test::Builder::Level + 1; 414 415 $ok = $bool1 || $bool2; 416 $ok &&= !($bool1 && $bool2); 417 ok($ok, $name); 418 419 return $bool; 420 } 421 422Every single tool in the chain from this, to C<ok>, to anything C<ok> calls 423needs to increment the C<$Level> variable. When an error occurs Test::Builder 424will do a trace to the stack frame determined by C<$Level>, and report that 425file+line as the one where the error occurred. If you or any other tool you use 426forgets to set C<$Level> then errors will be reported to the wrong place. 427 428=head2 TEST2 429 430 use Test::More; 431 432 sub exclusive_ok { 433 my ($bool1, $bool2, $name) = @_; 434 435 # Grab and store the context, even if you do not need to use it 436 # directly. 437 my $ctx = context(); 438 439 $ok = $bool1 || $bool2; 440 $ok &&= !($bool1 && $bool2); 441 ok($ok, $name); 442 443 $ctx->release; 444 return $bool; 445 } 446 447Instead of using C<$Level> to perform a backtrace, Test2 uses a context 448object. In this sample you create a context object and store it. This locks the 449context (errors report 1 level up from here) for all wrapped tools to find. You 450do not need to use the context object, but you do need to store it in a 451variable. Once the sub ends the C<$ctx> variable is destroyed which lets future 452tools find their own. 453 454=head1 USING UTF8 455 456=head2 LEGACY 457 458 # Set the mode BEFORE anything loads Test::Builder 459 use open ':std', ':encoding(utf8)'; 460 use Test::More; 461 462Or 463 464 # Modify the filehandles 465 my $builder = Test::More->builder; 466 binmode $builder->output, ":encoding(utf8)"; 467 binmode $builder->failure_output, ":encoding(utf8)"; 468 binmode $builder->todo_output, ":encoding(utf8)"; 469 470=head2 TEST2 471 472 use Test2::API qw/test2_stack/; 473 474 test2_stack->top->format->encoding('utf8'); 475 476Though a much better way is to use the L<Test2::Plugin::UTF8> plugin, which is 477part of L<Test2::Suite>. 478 479=head1 AUTHORS, CONTRIBUTORS AND REVIEWERS 480 481The following people have all contributed to this document in some way, even if 482only for review. 483 484=over 4 485 486=item Chad Granum (EXODIST) E<lt>exodist@cpan.orgE<gt> 487 488=back 489 490=head1 SOURCE 491 492The source code repository for Test2 can be found at 493F<http://github.com/Test-More/test-more/>. 494 495=head1 MAINTAINER 496 497=over 4 498 499=item Chad Granum E<lt>exodist@cpan.orgE<gt> 500 501=back 502 503=head1 COPYRIGHT 504 505Copyright 2019 Chad Granum E<lt>exodist@cpan.orgE<gt>. 506 507This program is free software; you can redistribute it and/or 508modify it under the same terms as Perl itself. 509 510See F<http://www.perl.com/perl/misc/Artistic.html> 511 512=cut 513