blib/lib/MOP4Import/Util/ResolveSymlinks.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 66 CC 6 SCAR 19.3
CC
6
Cov
0%
resolve_symlink
12.0
resolve_symlink_1
12.0
normalize
6.0
CRAP 6.9
1 #!/usr/bin/env perl
2 package MOP4Import::Util::ResolveSymlinks;
3 use strict;
4 use warnings;
5 use constant DEBUG => $ENV{DEBUG_MOP4IMPORT};
6 use Exporter;
7 our @EXPORT_OK = qw(normalize resolve_symlink resolve_symlink_1);
8  
9 use File::Spec ();
10 use File::Basename ();
11  
12 sub normalize {
13 my ($filePath) = @_;
14  
15 my $realFn = -l $filePath ? resolve_symlink(__PACKAGE__, $filePath) : $filePath;
16  
17 File::Spec->rel2abs($filePath);
18 }
19  
20 sub resolve_symlink {
21 my ($pack, $filePath) = @_;
22  
23 print STDERR "# resolve_symlink($filePath)...\n" if DEBUG;
24  
25 (undef, my ($realDir)) = File::Basename::fileparse($filePath);
26  
27 while (defined (my $linkText = readlink $filePath)) {
28 ($filePath, $realDir) = resolve_symlink_1($pack, $linkText, $realDir);
29 print STDERR "# => $filePath (realDir=$realDir)\n" if DEBUG;
30 }
31  
32 return $filePath;
33 }
34  
35 sub resolve_symlink_1 {
36 my ($pack, $linkText, $realDir) = @_;
37  
38 my $filePath = do {
39 if (File::Spec->file_name_is_absolute($linkText)) {
40 $linkText;
41 } else {
42 File::Spec->catfile($realDir, $linkText);
43 }
44 };
45  
46 if (wantarray) {
47 # purify x/../y to y
48 my $realPath = Cwd::realpath($filePath);
49 (undef, $realDir) = File::Basename::fileparse($realPath);
50 ($realPath, $realDir);
51 } else {
52 $filePath;
53 }
54 }
55  
56 unless (caller) {
57 print __PACKAGE__->resolve_symlink(@ARGV), "\n";
58 }
59  
60 1;