File Coverage

blib/lib/Test/Inline/Util.pm
Criterion Covered Total %
statement 35 51 68.6
branch 13 36 36.1
condition 8 18 44.4
subroutine 6 7 85.7
pod 0 5 0.0
total 62 117 52.9


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   88 use strict;
  12         26  
  12         398  
8 12     12   544 use File::Spec::Functions ':ALL';
  12         2917  
  12         10186  
9             our $VERSION = '2.214';
10             sub shorten {
11 24 50   24 0 50 my $class = ref $_[0] ? ref shift : shift;
12 24 100 66     132 my $path = (defined $_[0] and length $_[0])
13             ? canonpath( shift )
14             : return shift;
15 7         72 my @parts = splitdir( $path );
16 7         54 my $i = 0;
17 7         24 while ( defined $parts[++$i] ) {
18 0 0       0 next unless $i;
19 0 0       0 next unless $parts[$i] eq updir();
20 0 0       0 next if $parts[$i - 1] eq updir();
21 0         0 splice @parts, $i - 1, 2;
22 0         0 $i -= 2;
23             }
24 7         38 catdir( @parts );
25             }
26             sub parts {
27 0 0   0 0 0 my $class = ref $_[0] ? ref shift : shift;
28 0         0 my $path = $class->shorten(shift);
29 0 0       0 $path = '' if $path eq curdir();
30 0         0 scalar splitdir($path);
31             }
32             sub inverse {
33 8 50   8 0 33 my $class = ref $_[0] ? ref shift : shift;
34 8         54 my $path = $class->shorten( shift );
35 8 50 33     48 if ( ! defined $path or $path eq '' or $path eq curdir() ) {
      33        
36 8         21 return $path;
37             }
38 0 0       0 return undef if file_name_is_absolute( $path );
39 0         0 my @parts = splitdir( $path );
40 0 0       0 return undef if $parts[0] eq updir();
41 0         0 catdir( (updir()) x scalar @parts );
42             }
43             sub commonise {
44 8 50   8 0 33 my $class = ref $_[0] ? ref shift : shift;
45 8         32 my $first = $class->shorten( shift );
46 8         78 my $second = $class->shorten( shift );
47 8 50       35 return undef unless defined $first;
48 8 50       20 return undef unless defined $second;
49 8         34 my @first = splitdir( $first );
50 8         45 my @second = splitdir( $second );
51 8         37 my @base = ();
52 8   33     39 while ( defined $first[0] and defined $second[0] and $first[0] eq $second[0] ) {
      33        
53 0         0 push @base, $first[0];
54 0         0 shift @first;
55 0         0 shift @second;
56             }
57 8         109 [ catdir(@base), catdir(@first), catdir(@second) ];
58             }
59             sub relative {
60 8 50   8 0 74 my $class = ref $_[0] ? ref shift : shift;
61 8 50       59 my $commonised = $class->commonise( @_ ) or return undef;
62 8         38 my $from = $class->inverse( $commonised->[1] );
63 8 50       30 return undef unless defined $from;
64 8         17 my $to = $commonised->[2];
65 8 100 66     37 if ( $from eq '' and $to eq '' ) {
66 1         6 return '';
67             }
68 7         15 catdir( grep { length $_ } ($from, $to) );
  14         53  
69             }
70             1;