File Coverage

blib/lib/Test/Run/Plugin/TrimDisplayedFilenames.pm
Criterion Covered Total %
statement 59 63 93.6
branch 9 16 56.2
condition 1 2 50.0
subroutine 17 17 100.0
pod n/a
total 86 98 87.7


line stmt bran cond sub pod time code
1             package Test::Run::Plugin::TrimDisplayedFilenames;
2              
3 4     4   1640899 use warnings;
  4         8  
  4         203  
4 4     4   15 use strict;
  4         7  
  4         74  
5              
6 4     4   52 use 5.014;
  4         12  
7              
8 4     4   1296 use Moose;
  4         1143162  
  4         507  
9              
10 4     4   22807 use MRO::Compat;
  4         8  
  4         116  
11 4     4   19 use File::Spec ();
  4         9  
  4         95  
12 4     4   19 use File::Basename qw/ basename dirname /;
  4         6  
  4         335  
13 4     4   2583 use List::SomeUtils ();
  4         25228  
  4         2813  
14              
15             extends('Test::Run::Base');
16              
17             =head1 NAME
18              
19             Test::Run::Plugin::TrimDisplayedFilenames - trim the first components
20             of the displayed filename to deal with excessively long ones.
21              
22             =head1 VERSION
23              
24             Version 0.0127
25              
26             =cut
27              
28             our $VERSION = '0.0127';
29              
30             has 'trim_displayed_filenames_query' => ( is => "rw", isa => "Str" );
31              
32             sub _process_filename_dirs
33             {
34 10     10   29 my ( $self, $fn, $callback ) = @_;
35              
36 10         656 my $basename = basename($fn);
37 10         332 my $dirpath = dirname($fn);
38              
39 10         104 my ( $volume, $directories, $filename ) =
40             File::Spec->splitpath( $dirpath, 1 );
41              
42             # The actual manipulation.
43 10         91 my $dirs = $callback->( [ File::Spec->splitdir($directories) ] );
44              
45 10         127 my $final_dir =
46             File::Spec->catpath( $volume, File::Spec->catdir(@$dirs), $filename );
47              
48 10 50       31 if ( $final_dir eq "" )
49             {
50 10         989 return $basename;
51             }
52             else
53             {
54 0         0 return File::Spec->catfile( $final_dir, $basename );
55             }
56             }
57              
58             sub _get_search_from_callback
59             {
60 8     8   19 my ( $self, $options ) = @_;
61              
62 8 50       77 return +( $options->{search_from} eq "start" )
63             ? \&List::SomeUtils::firstidx
64             : \&List::SomeUtils::lasttidx;
65             }
66              
67             sub _get_array_portion
68             {
69 8     8   23 my ( $self, $options, $dirs, $idx ) = @_;
70              
71 8         24 my @copy = @$dirs;
72              
73             return [
74 8 50       43 +( $options->{keep_from} eq "start" )
75             ? splice( @copy, 0, $idx )
76             : splice( @copy, $idx + 1 )
77             ];
78             }
79              
80             sub _trim_filename_dir_components
81             {
82 8     8   24 my ( $self, $filename, $component_callback, $options ) = @_;
83              
84 8   50     25 $options ||= { 'search_from' => "start", 'keep_from' => "end" };
85              
86             return $self->_process_filename_dirs(
87             $filename,
88             sub {
89 8     8   16 my $dirs = shift;
90              
91 8         57 my $idx =
92             $self->_get_search_from_callback($options)
93             ->( $component_callback, @$dirs );
94              
95 8 50       27 if ( !defined($idx) )
96             {
97 0         0 return $dirs;
98             }
99              
100 8         32 return $self->_get_array_portion( $options, $dirs, $idx );
101             },
102 8         104 );
103             }
104              
105             sub _process_output_leader_fn
106             {
107 10     10   23 my ( $self, $fn ) = @_;
108              
109 10         583 my $query = $self->trim_displayed_filenames_query();
110              
111 10 50       31 if ( !defined($query) )
112             {
113 0         0 return $fn;
114             }
115              
116 10 50       75 if ( $query =~ m{\A(fromre|keep):(.*)}ms )
117             {
118 10         35 my ( $cmd, $arg ) = ( $1, $2 );
119              
120 10 100       47 if ( $cmd eq "fromre" )
121             {
122 8         169 my $re = qr{$arg};
123              
124             return $self->_trim_filename_dir_components(
125             $fn,
126 24     24   154 sub { $_ =~ m{$re} },
127 8         118 +{ search_from => "start", keep_from => "end" }
128             );
129             }
130             else # $cmd eq "keep"
131             {
132             # We need to decrement 1 because there's also the filename.
133 2         8 my $num_keep = int($arg);
134             return $self->_process_filename_dirs(
135             $fn,
136             sub {
137 2     2   6 my @dirs = @{ shift() };
  2         7  
138 2 50       10 return +( $num_keep <= 1 )
139             ? []
140             : [ splice( @dirs, -( $num_keep - 1 ) ) ];
141             },
142 2         17 );
143             }
144             }
145             else
146             {
147             # TODO - Replace with an exception object.
148 0         0 die "Unrecognized trim_displayed_filename_query.";
149             }
150             }
151              
152             sub _calc_test_file_data_display_path
153             {
154 10     10   8624470 my ( $self, $idx, $test_file ) = @_;
155              
156 10         48 return $self->_process_output_leader_fn($test_file);
157             }
158              
159             =head1 SYNOPSIS
160              
161             package MyTestRun;
162              
163             use Moose;
164              
165             extends('Test::Run::Plugin::TrimDisplayedFilenames');
166              
167             =head1 FUNCTIONS
168              
169             =cut
170              
171             =head1 AUTHOR
172              
173             Shlomi Fish, C<< <shlomif at cpan.org> >>
174              
175             =head1 BUGS
176              
177             Please report any bugs or feature requests to
178             C<bug-test-run-plugin-alternateinterpreters at rt.cpan.org>, or through the web interface at
179             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test::Run::Plugin::TrimDisplayedFilenames>.
180             I will be notified, and then you'll automatically be notified of progress on
181             your bug as I make changes.
182              
183             =head1 SUPPORT
184              
185             You can find documentation for this module with the perldoc command.
186              
187             perldoc Test::Run::Plugin::TrimDisplayedFilenames
188              
189             You can also look for information at:
190              
191             =over 4
192              
193             =item * CPAN Ratings
194              
195             L<http://cpanratings.perl.org/d/Test::Run::Plugin::TrimDisplayedFilenames>
196              
197             =item * RT: CPAN's request tracker
198              
199             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test::Run::Plugin::TrimDisplayedFilenames>
200              
201             =item * MetaCPAN
202              
203             L<http://metacpan.org/release/Test-Run-Plugin-TrimDisplayedFilenames>
204              
205             =back
206              
207             =head1 ACKNOWLEDGEMENTS
208              
209             Curtis "Ovid" Poe ( L<http://search.cpan.org/~ovid/> ) who gave the idea
210             of testing several tests from several interpreters in one go here:
211              
212             L<http://use.perl.org/~Ovid/journal/32092>
213              
214             =head1 SEE ALSO
215              
216             L<Test::Run>, L<Test::Run::CmdLine>, L<TAP::Parser>,
217             L<Test::Run::CmdLine::Plugin::TrimDisplayedFilenames>
218              
219             =head1 COPYRIGHT & LICENSE
220              
221             Copyright 2007 Shlomi Fish, all rights reserved.
222              
223             This program is released under the following license: MIT X11.
224              
225             =cut
226              
227             1; # End of Test::Run::Plugin::TrimDisplayedFilenames