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   807739 use 5.012;
  10         140  
4 10     10   58 use strict;
  10         19  
  10         203  
5 10     10   46 use warnings;
  10         17  
  10         345  
6              
7 10     10   60 use Carp qw(croak);
  10         19  
  10         485  
8 10     10   3960 use Import::Into;
  10         21704  
  10         360  
9 10     10   5019 use Best [ [qw(YAML::XS YAML)], qw(LoadFile) ];
  10         40845  
  10         68  
10 10     10   42504 use Data::Dumper; # for verbose
  10         68706  
  10         649  
11              
12 10     10   3482 use Test::OnlySome::PathCapsule;
  10         28  
  10         468  
13              
14             our $VERSION = '0.001003';
15              
16 10     10   69 use constant DEFAULT_FILENAME => '.onlysome.yml';
  10         20  
  10         4255  
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   85 my $self = shift;
58 10         39 my %opts = @_; # options from the `use` statement
59 10         20 my @skips; # test numbers to skip
60 10         37 my ($target, $caller_fn) = caller;
61              
62             # Process options
63 10   50     52 $opts{filename} //= DEFAULT_FILENAME;
64              
65             print STDERR "# Called from $target in $caller_fn; YML in $opts{filename}\n"
66 10 50       61 if $opts{verbose};
67              
68             # Read the YAML file
69 10         52 my $fn = _localpath(1, $opts{filename}, 1);
70 10         61 my $hrCfg;
71 10         71 eval { $hrCfg = LoadFile($fn); };
  10         179  
72              
73 10 50       3005 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       38 if($hrCfg) {
80 8 50       40 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         22 @skips = @{ $hrCfg->{$caller_fn}->{actual_passed} };
  8         60  
85             print STDERR "# Skipping ", join(", ", @skips), "\n"
86             if $opts{verbose}
87 8 50       61 }
88             }
89              
90             # Load Test::OnlySome with the appropriate skips
91             'Test::OnlySome'->import::into($target,
92             @skips ? 'skip' : (), @skips,
93 10 100       272 $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   34 my $calleridx = shift or croak 'Need a caller index';
99 10 50       31 my $newfn = shift or croak 'Need a filename';
100 10         17 my $moveup = shift;
101              
102 10         72 my ($package, $filename) = caller($calleridx);
103              
104 10 50 33     84 $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         39 my $path = Test::OnlySome::PathCapsule->new($filename);
109             # Assume the code up to this point hasn't changed cwd
110              
111 10         338 $path->up while $moveup--;
112 10         75 $path->file($newfn);
113              
114 10         47 return $path->abs;
115             } #}}}2
116              
117             1;
118             # vi: set fdm=marker fdl=1 fo-=ro: