use strict; use warnings; use Cwd; use File::Spec; use Test::More 'no_plan'; use File::pushd qw( pushd ); # Meditation benefits from short directory names, so save our initial # directory then switch to the tempdir and create a dummy directory my $initial_dir = cwd(); chdir File::Spec->tmpdir(); mkdir "t" unless -e "t"; # Record the directory before pushd() is called my $original_dir = cwd(); # Establish a limited scope for pushd() { # Change to target directory until $new_dir goes out of scope # Returns absolute path to new directory my $new_dir = pushd( 't' ); is( cwd(), $new_dir, "changed to new directory" ); } is( cwd(), $original_dir, "changed back to original directory" ); # Get back to the starting point chdir $initial_dir; #### ok 1 - changed to new directory ok 2 - changed back to original directory 1..2 #### not ok 1 - changed to new directory # Failed test (cwd_post1.pl at line 26) # got: 'C:/Temp/t' # expected: 'C:\Temp\t' ok 2 - changed back to original directory 1..2 # Looks like you failed 1 test of 2. #### - is( cwd(), $new_dir, "changed to new directory" ); + is( File::Spec->canonpath( cwd() ), $new_dir, "changed to new directory" ); #### - my $new_dir = pushd( 't' ); + my $new_dir = pushd( File::Spec->rootdir ); #### not ok 1 - changed to new directory # Failed test (cwd_post3.pl at line 26) # got: 'C:\' # expected: '\' ok 2 - changed back to original directory 1..2 # Looks like you failed 1 test of 2. #### - is( File::Spec->canonpath( cwd() ), $new_dir, "changed to new directory" ); + is( File::Spec->abs2rel( cwd(), $new_dir), File::Spec->curdir, + "changed to new directory" + ); #### not ok 1 - changed to new directory # Failed test (cwd_post4.pl at line 26) # got: '' # expected: '.' ok 2 - changed back to original directory #### $ perl -MFile::Spec -e 'print File::Spec->catdir(".","t")' t $ perl -MFile::Spec -e 'print File::Spec->catdir("","t")' /t #### not ok 1 - changed to new directory # Failed test (cwd_post4.pl at line 26) # got: 'C:\' # expected: '.' ok 2 - changed back to original directory 1..2 # Looks like you failed 1 test of 2. #### use File::Spec; +use File::Spec::Functions qw( canonpath splitpath ); #### - is( File::Spec->abs2rel( cwd(), $new_dir), q{.}, + is( canonpath( [ splitpath( cwd(), 1 ) ]->[1] ), + $new_dir, "changed to new directory" ); #### - my $new_dir = pushd( File::Spec->rootdir ); + my $new_dir = pushd( "t" ); #### - is( canonpath( [ splitpath( cwd(), 1 ) ]->[1] ), - $new_dir, + is( canonpath( [ splitpath( cwd(), 1 ) ]->[1] ), + canonpath( [ splitpath( $new_dir, 1 ) ]->[1] ), "changed to new directory" ); #### -use Cwd; +use Cwd qw( cwd abs_path ); use File::Spec; -use File::Spec::Functions qw( canonpath splitpath ); #### - is( canonpath( [ splitpath( cwd(), 1 ) ]->[1] ), - canonpath( [ splitpath( $new_dir, 1 ) ]->[1] ), - "changed to new directory" - ); + is( cwd(), abs_path( $new_dir ), "changed to new directory" ); #### - my $new_dir = pushd( "t" ); + my $new_dir = pushd( File::Spec->rootdir );