1# Before `make install' is performed this script should be runnable with 2# `make test'. After `make install' it should work as `perl 1.t' 3 4######################### 5 6# change 'tests => 1' to 'tests => last_test_to_print'; 7 8use Test::More tests => 11; 9 10BEGIN { use_ok('File::Rename') }; 11 12######################### 13 14# Insert your test code below, the Test::More module is use()ed here so read 15# its man page ( perldoc Test::More ) for help writing this test script. 16 17unshift @INC, 't' if -d 't'; 18require 'testlib.pl'; 19 20sub test_rename { goto &test_rename_files; } 21 22my $dir = do { require File::Temp; File::Temp::tempdir() }; 23my($test_foo, $test_bar, $copy_foo, $copy_bar, $new1, $new2, $old2, $old3) = 24 map { File::Spec->catfile($dir, $_) } 25 qw(test.foo test.bar copy.foo copy.bar 1.new 2.new 2.old 3.old); 26 27my $subdir = File::Spec->catdir($dir, 'food'); 28File::Path::mkpath($subdir) or die; 29my $sub_test = File::Spec->catfile($subdir,'test.txt'); 30 31for my $file ($test_foo, $copy_foo, $copy_bar, $new1, $old2, $sub_test) { 32 create_file($file) or die; 33} 34 35our $found; 36our $print; 37our $warn; 38local $SIG{__WARN__} = sub { $warn .= $_[0] }; 39 40my $s = sub { s/foo/bar/ }; 41my $h = options( q(-d) ); 42 43test_rename($s, $test_foo, $h); 44ok( (-e $test_bar and !-e $test_foo and $found), "rename foo->bar"); 45diag_rename(); 46 47test_rename($s, $new1, $h); 48ok( (-e $new1 and $found), "rename: filename not changed"); 49diag_rename(); 50 51test_rename($s, $copy_foo, $h, "$copy_foo not renamed"); 52ok( (-e $copy_foo and $found), "rename: file exists"); 53diag_rename(); 54 55test_rename($s, $copy_foo, options( qw(-filename -f) ) ); 56ok( (!-e $copy_foo and $found), "rename: over_write"); 57diag_rename(); 58 59create_file($copy_foo); 60test_rename($s, $copy_foo, options( qw(-nopath -f -v) ), 61 undef, "$copy_foo renamed as $copy_bar"); 62ok( (!-e $copy_foo and $found), "rename: over_write+verbose"); 63diag_rename(); 64 65test_rename($s, $sub_test, $h); 66ok( (-e $sub_test and $found), "rename: silently not renamed"); 67diag_rename(); 68 69my $inc = sub { s/(\d+)/ $1 + 1 /e unless /\.old\z/ }; 70 71test_rename($inc, $new1, options( qw(-n -nofullpath) ), 72 undef, "rename($new1, $new2)"); 73ok( (-e $new1 and !-e $new2 and $found), "rename: no_action"); 74diag_rename(); 75 76test_rename($inc, $new1, options( qw(--verbose --filename) ), 77 undef, "$new1 renamed as $new2"); 78ok( (-e $new2 and !-e $new1 and $found), "rename 1->2"); 79diag_rename(); 80 81test_rename($inc, $old2, options( qw(-d -v) ) ); 82ok( (-e $old2 and !-e $old3 and $found), 83 "rename: filename not changed (1->2)"); 84diag_rename(); 85 86END { chdir File::Spec->rootdir; 87 File::Path::rmtree($dir); 88 ok( !-d $dir, "test dir removed"); 89} 90 91