File Coverage

blib/lib/Test2/Plugin/SRand.pm
Criterion Covered Total %
statement 36 36 100.0
branch 15 16 93.7
condition 7 15 46.6
subroutine 9 9 100.0
pod 0 2 0.0
total 67 78 85.9


line stmt bran cond sub pod time code
1             package Test2::Plugin::SRand;
2 155     155   993 use strict;
  155         310  
  155         5991  
3 155     155   787 use warnings;
  155         3296  
  155         10437  
4              
5             our $VERSION = '0.000153';
6              
7 155     155   853 use Carp qw/carp/;
  155         393  
  155         12965  
8              
9 155         66578 use Test2::API qw{
10             context
11             test2_add_callback_post_load
12             test2_add_callback_exit
13             test2_stack
14 155     155   95457 };
  155         10383822  
15              
16             my $ADDED_HOOK = 0;
17             my $SEED;
18             my $FROM;
19              
20 8     8 0 54 sub seed { $SEED }
21 5     5 0 21 sub from { $FROM }
22              
23             sub import {
24 161     161   7679 my $class = shift;
25              
26 161 100       1407 carp "SRand loaded multiple times, re-seeding rand"
27             if defined $SEED;
28              
29 161 100 66     1565 if (@_ == 1) {
    100          
    100          
30 2         6 ($SEED) = @_;
31 2         5 $FROM = 'import arg';
32             }
33             elsif (@_ == 2 and $_[0] eq 'seed') {
34 1         3 $SEED = $_[1];
35 1         2 $FROM = 'import arg';
36             }
37             elsif(exists $ENV{T2_RAND_SEED}) {
38 1         4 $SEED = $ENV{T2_RAND_SEED};
39 1         2 $FROM = 'environment variable';
40             }
41             else {
42 157         7987 my @ltime = localtime;
43             # Yes, this would be an awful seed if you actually wanted randomness.
44             # The idea here is that we want "random" behavior to be predictable
45             # within a given day. This allows you to reproduce failures that may or
46             # may not happen due to randomness.
47 157         1778 $SEED = sprintf('%04d%02d%02d', 1900 + $ltime[5], 1 + $ltime[4], $ltime[3]);
48 157         655 $FROM = 'local date';
49             }
50              
51 161 100       658 $SEED = 0 unless $SEED;
52 161         604 srand($SEED);
53              
54 161 100 66     2207 if ($ENV{HARNESS_IS_VERBOSE} || !$ENV{HARNESS_ACTIVE}) {
    100          
55             # If the harness is verbose then just display the message for all to
56             # see. It is nice info and they already asked for noisy output.
57              
58             test2_add_callback_post_load(sub {
59 4     4   84 test2_stack()->top; # Ensure we have at least 1 hub.
60 4         50 my ($hub) = test2_stack()->all;
61 4         53 $hub->send(
62             Test2::Event::Note->new(
63             trace => Test2::Util::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__, 'SRAND']),
64             message => "Seeded srand with seed '$SEED' from $FROM.",
65             )
66             );
67 4         38 });
68             }
69             elsif (!$ADDED_HOOK++) {
70             # The seed can be important for debugging, so if anything is wrong we
71             # should output the seed message as a diagnostics message. This must be
72             # done at the very end, even later than a hub hook.
73             test2_add_callback_exit(
74             sub {
75 102     102   1560 my ($ctx, $real, $new) = @_;
76              
77 102 50 33     1638 $ctx->diag("Seeded srand with seed '$SEED' from $FROM.")
      33        
      33        
78             if $real
79             || ($new && $$new)
80             || !$ctx->hub->is_passing;
81             }
82 155         1976 );
83             }
84             }
85              
86             1;
87              
88             __END__