File Coverage

blib/lib/Test/OnlySome/RerunFailed.pm
Criterion Covered Total %
statement 52 55 94.5
branch 12 20 60.0
condition 2 5 40.0
subroutine 11 11 100.0
pod n/a
total 77 91 84.6


line stmt bran cond sub pod time code
1             #!perl
2             package Test::OnlySome::RerunFailed;
3 10     10   1138418 use 5.012;
  10         55  
4 10     10   46 use strict;
  10         17  
  10         202  
5 10     10   41 use warnings;
  10         20  
  10         264  
6              
7 10     10   67 use Carp qw(croak);
  10         18  
  10         394  
8 10     10   3135 use Import::Into;
  10         17625  
  10         291  
9 10     10   3952 use Best [ [qw(YAML::XS YAML)], qw(LoadFile) ];
  10         15850  
  10         52  
10 10     10   46819 use Data::Dumper; # for verbose
  10         50961  
  10         612  
11              
12 10     10   3062 use Test::OnlySome::PathCapsule;
  10         23  
  10         353  
13              
14             our $VERSION = '0.001003';
15              
16 10     10   52 use constant DEFAULT_FILENAME => '.onlysome.yml';
  10         15  
  10         3559  
17              
18             # Docs {{{2
19              
20             =head1 NAME
21              
22             Test::OnlySome::RerunFailed - Load Test::OnlySome, and skip tests based on a file on disk
23              
24             =head1 INSTALLATION
25              
26             See L, with which this module is distributed.
27              
28             =head1 SYNOPSIS
29              
30             use Test::OnlySome::RerunFailed;
31              
32             This will load L and configure it to skip any test marked
33             as a clean pass by the last run of L.
34              
35             =head1 OPTIONS
36              
37             The C line can list the following options:
38              
39             =over
40              
41             =item C<< filename => 'some filename' >>
42              
43             Specify the file from which to read test results. The default is
44             C<.onlysome.yml>.
45              
46             =item C<< verbose => 1 >>
47              
48             If specified, print debugging information.
49              
50             =back
51              
52             =cut
53              
54             # }}}2
55              
56             sub import {
57 10     10   83 my $self = shift;
58 10         33 my %opts = @_; # options from the `use` statement
59 10         18 my @skips; # test numbers to skip
60 10         34 my ($target, $caller_fn) = caller;
61              
62             # Process options
63 10   50     42 $opts{filename} //= DEFAULT_FILENAME;
64              
65             print STDERR "# Called from $target in $caller_fn; YML in $opts{filename}\n"
66 10 50       42 if $opts{verbose};
67              
68             # Read the YAML file
69 10         49 my $fn = _localpath(1, $opts{filename}, 1);
70 10         197 my $hrCfg;
71 10         72 eval { $hrCfg = LoadFile($fn); };
  10         138  
72              
73 10 50       3093 if($opts{verbose}) {
74 0         0 my $msg = "Configuration:\n" . Dumper($hrCfg);
75 0         0 $msg =~ s/^/# /gm;
76 0         0 print STDERR $msg;
77             }
78              
79 10 100       75 if($hrCfg) {
80 8 50       102 if($hrCfg->{$caller_fn}->{actual_passed}) {
81             #my %skipped = map { $_ => 1 }
82             # @{ $hrCfg->{$caller_fn}->{skipped} // \() };
83             #@skips = grep { !$skipped{$_} } @{ $hrCfg->{$caller_fn}->{actual_passed} };
84 8         25 @skips = @{ $hrCfg->{$caller_fn}->{actual_passed} };
  8         62  
85             print STDERR "# Skipping ", join(", ", @skips), "\n"
86             if $opts{verbose}
87 8 50       48 }
88             }
89              
90             # Load Test::OnlySome with the appropriate skips
91             'Test::OnlySome'->import::into($target,
92             @skips ? 'skip' : (), @skips,
93 10 100       335 $opts{verbose} ? (verbose => 1) : ()
    50          
94             );
95             }
96              
97             sub _localpath { # Return the path to a file in the same directory as the caller {{{2
98 10 50   10   27 my $calleridx = shift or croak 'Need a caller index';
99 10 50       27 my $newfn = shift or croak 'Need a filename';
100 10         13 my $moveup = shift;
101              
102 10         96 my ($package, $filename) = caller($calleridx);
103              
104 10 50 33     72 $filename = 'dummy' unless $filename && $filename ne '-e';
105             # Dummy filename assumed to be in cwd, if we're running from -e
106             # or are otherwise without a caller.
107              
108 10         122 my $path = Test::OnlySome::PathCapsule->new($filename);
109             # Assume the code up to this point hasn't changed cwd
110              
111 10         240 $path->up while $moveup--;
112 10         92 $path->file($newfn);
113              
114 10         77 return $path->abs;
115             } #}}}2
116              
117             1;
118             # vi: set fdm=marker fdl=1 fo-=ro: