File Coverage

blib/lib/Test/Inline/Util.pm
Criterion Covered Total %
statement 39 55 70.9
branch 13 36 36.1
condition 8 18 44.4
subroutine 8 9 88.8
pod 0 5 0.0
total 68 123 55.2


line stmt bran cond sub pod time code
1             # This is an inlined version of the private Phase N module File::DirUtils,
2             # approved for use only in its inline state as Test::Inline::Util.
3             # It will be released to CPAN at some later time, once complete.
4             # We ask that until that time you respect our development process and
5             # do not use this code.
6             package Test::Inline::Util;
7 12     12   70 use strict;
  12         18  
  12         424  
8 12     12   919 use File::Spec::Functions ':ALL';
  12         789  
  12         3256  
9 12     12   60 use vars qw{$VERSION};
  12         22  
  12         520  
10             BEGIN {
11 12     12   7137 $VERSION = '2.213';
12             }
13             sub shorten {
14 24 50   24 0 54 my $class = ref $_[0] ? ref shift : shift;
15 24 100 66     174 my $path = (defined $_[0] and length $_[0])
16             ? canonpath( shift )
17             : return shift;
18 7         37 my @parts = splitdir( $path );
19 7         63 my $i = 0;
20 7         27 while ( defined $parts[++$i] ) {
21 0 0       0 next unless $i;
22 0 0       0 next unless $parts[$i] eq updir();
23 0 0       0 next if $parts[$i - 1] eq updir();
24 0         0 splice @parts, $i - 1, 2;
25 0         0 $i -= 2;
26             }
27 7         37 catdir( @parts );
28             }
29             sub parts {
30 0 0   0 0 0 my $class = ref $_[0] ? ref shift : shift;
31 0         0 my $path = $class->shorten(shift);
32 0 0       0 $path = '' if $path eq curdir();
33 0         0 scalar splitdir($path);
34             }
35             sub inverse {
36 8 50   8 0 28 my $class = ref $_[0] ? ref shift : shift;
37 8         20 my $path = $class->shorten( shift );
38 8 50 33     90 if ( ! defined $path or $path eq '' or $path eq curdir() ) {
      33        
39 8         22 return $path;
40             }
41 0 0       0 return undef if file_name_is_absolute( $path );
42 0         0 my @parts = splitdir( $path );
43 0 0       0 return undef if $parts[0] eq updir();
44 0         0 catdir( (updir()) x scalar @parts );
45             }
46             sub commonise {
47 8 50   8 0 27 my $class = ref $_[0] ? ref shift : shift;
48 8         41 my $first = $class->shorten( shift );
49 8         34 my $second = $class->shorten( shift );
50 8 50       45 return undef unless defined $first;
51 8 50       29 return undef unless defined $second;
52 8         34 my @first = splitdir( $first );
53 8         64 my @second = splitdir( $second );
54 8         41 my @base = ();
55 8   33     43 while ( defined $first[0] and defined $second[0] and $first[0] eq $second[0] ) {
      33        
56 0         0 push @base, $first[0];
57 0         0 shift @first;
58 0         0 shift @second;
59             }
60 8         107 [ catdir(@base), catdir(@first), catdir(@second) ];
61             }
62             sub relative {
63 8 50   8 0 31 my $class = ref $_[0] ? ref shift : shift;
64 8 50       36 my $commonised = $class->commonise( @_ ) or return undef;
65 8         40 my $from = $class->inverse( $commonised->[1] );
66 8 50       21 return undef unless defined $from;
67 8         31 my $to = $commonised->[2];
68 8 100 66     49 if ( $from eq '' and $to eq '' ) {
69 1         3 return '';
70             }
71 7         15 catdir( grep { length $_ } ($from, $to) );
  14         69  
72             }
73             1;