1#!/usr/bin/perl -w 2 3use strict; 4use Test::More; 5 6use Cwd qw(cwd getcwd abs_path); 7use File::Spec(); 8use File::Temp qw(tempdir); 9use File::Path qw(mkpath); 10 11my $startdir = cwd(); 12my @files = ( 'anyfile', './anyfile', '../first_sub_dir/anyfile', '../second_sub_dir/second_file' ); 13 14for my $file (@files) { 15 test_rel2abs($file); 16} 17 18sub test_rel2abs { 19 my $first_file = shift; 20 my $tdir = tempdir( CLEANUP => 1 ); 21 chdir $tdir or die "Unable to change to $tdir: $!"; 22 23 my @subdirs = ( 24 'first_sub_dir', 25 File::Spec->catdir('first_sub_dir', 'sub_sub_dir'), 26 'second_sub_dir' 27 ); 28 mkpath(\@subdirs, 0, 0711) 29 or die "Unable to mkpath: $!"; 30 31 open my $OUT2, '>', 32 File::Spec->catfile('second_sub_dir', 'second_file') 33 or die "Unable to open 'second_file' for writing: $!"; 34 print $OUT2 "Attempting to resolve RT #121360\n"; 35 close $OUT2 or die "Unable to close 'second_file' after writing: $!"; 36 37 chdir 'first_sub_dir' 38 or die "Unable to change to 'first_sub_dir': $!"; 39 open my $OUT1, '>', $first_file 40 or die "Unable to open $first_file for writing: $!"; 41 print $OUT1 "Attempting to resolve RT #121360\n"; 42 close $OUT1 or die "Unable to close $first_file after writing: $!"; 43 44 my $rel_path = $first_file; 45 my $rel_base = File::Spec->catdir(File::Spec->curdir(), 'sub_sub_dir'); 46 my $abs_path = File::Spec->rel2abs($rel_path); 47 my $abs_base = File::Spec->rel2abs($rel_base); 48 ok(-f $rel_path, "'$rel_path' is readable by effective uid/gid"); 49 ok(-f $abs_path, "'$abs_path' is readable by effective uid/gid"); 50 is_deeply( 51 [ (stat $rel_path)[0..5] ], 52 [ (stat $abs_path)[0..5] ], 53 "rel_path and abs_path stat same" 54 ); 55 ok(-d $rel_base, "'$rel_base' is a directory"); 56 ok(-d $abs_base, "'$abs_base' is a directory"); 57 is_deeply( 58 [ (stat $rel_base)[0..5] ], 59 [ (stat $abs_base)[0..5] ], 60 "rel_base and abs_base stat same" 61 ); 62 my $rr_link = File::Spec->abs2rel($rel_path, $rel_base); 63 my $ra_link = File::Spec->abs2rel($rel_path, $abs_base); 64 my $ar_link = File::Spec->abs2rel($abs_path, $rel_base); 65 my $aa_link = File::Spec->abs2rel($abs_path, $abs_base); 66 is($rr_link, $ra_link, 67 "rel_path-rel_base '$rr_link' = rel_path-abs_base '$ra_link'"); 68 is($ar_link, $aa_link, 69 "abs_path-rel_base '$ar_link' = abs_path-abs_base '$aa_link'"); 70 is($rr_link, $aa_link, 71 "rel_path-rel_base '$rr_link' = abs_path-abs_base '$aa_link'"); 72 73 chdir $startdir or die "Unable to change back to $startdir: $!"; 74} 75 76done_testing(); 77