16fb12b70Safresh1package Testing; 26fb12b70Safresh1use 5.006_001; 36fb12b70Safresh1use strict; 46fb12b70Safresh1use warnings; 5256a93a4Safresh1use Exporter 'import'; 66fb12b70Safresh1our @EXPORT_OK = qw( 76fb12b70Safresh1 create_file_ok 86fb12b70Safresh1 mkdir_ok 96fb12b70Safresh1 symlink_ok 106fb12b70Safresh1 dir_path 116fb12b70Safresh1 file_path 12*f2a19305Safresh1 _cleanup_start 136fb12b70Safresh1); 146fb12b70Safresh1 156fb12b70Safresh1# Wrappers around Test::More::ok() for creation of files, directories and 166fb12b70Safresh1# symlinks used in testing of File-Find 176fb12b70Safresh1 186fb12b70Safresh1*ok = \&Test::More::ok; 196fb12b70Safresh1 206fb12b70Safresh1sub create_file_ok($;$) { 216fb12b70Safresh1 my $file = $_[0]; 226fb12b70Safresh1 my $msg = $_[2] || "able to create file: $file"; 236fb12b70Safresh1 ok( open(my $T,'>',$file), $msg ) 246fb12b70Safresh1 or die("Unable to create file: $file"); 256fb12b70Safresh1} 266fb12b70Safresh1 276fb12b70Safresh1sub mkdir_ok($$;$) { 286fb12b70Safresh1 my ($dir, $mask) = @_[0..1]; 296fb12b70Safresh1 my $msg = $_[2] || "able to mkdir: $dir"; 306fb12b70Safresh1 ok( mkdir($dir, $mask), $msg ) 31256a93a4Safresh1 or die("Unable to mkdir $!: $dir"); 326fb12b70Safresh1} 336fb12b70Safresh1 346fb12b70Safresh1sub symlink_ok($$;$) { 356fb12b70Safresh1 my ($oldfile, $newfile) = @_[0..1]; 366fb12b70Safresh1 my $msg = $_[2] || "able to symlink from $oldfile to $newfile"; 376fb12b70Safresh1 ok( symlink( $oldfile, $newfile ), $msg) 386fb12b70Safresh1 or die("Unable to symlink from $oldfile to $newfile"); 396fb12b70Safresh1} 406fb12b70Safresh1 416fb12b70Safresh1# Use dir_path() to specify a directory path that is expected for 426fb12b70Safresh1# $File::Find::dir (%Expect_Dir). Also use it in file operations like 436fb12b70Safresh1# chdir, rmdir etc. 446fb12b70Safresh1# 456fb12b70Safresh1# dir_path() concatenates directory names to form a *relative* 466fb12b70Safresh1# directory path, independent from the platform it is run on, although 476fb12b70Safresh1# there are limitations. Do not try to create an absolute path, 486fb12b70Safresh1# because that may fail on operating systems that have the concept of 496fb12b70Safresh1# volume names (e.g. Mac OS). As a special case, you can pass it a "." 506fb12b70Safresh1# as first argument, to create a directory path like "./fa/dir". If there is 516fb12b70Safresh1# no second argument, this function will return "./" 526fb12b70Safresh1 536fb12b70Safresh1sub dir_path { 546fb12b70Safresh1 my $first_arg = shift @_; 556fb12b70Safresh1 566fb12b70Safresh1 if ($first_arg eq '.') { 576fb12b70Safresh1 return './' unless @_; 586fb12b70Safresh1 my $path = File::Spec->catdir(@_); 596fb12b70Safresh1 # add leading "./" 606fb12b70Safresh1 $path = "./$path"; 616fb12b70Safresh1 return $path; 626fb12b70Safresh1 } 636fb12b70Safresh1 else { # $first_arg ne '.' 646fb12b70Safresh1 return $first_arg unless @_; # return plain filename 656fb12b70Safresh1 my $fname = File::Spec->catdir($first_arg, @_); # relative path 666fb12b70Safresh1 $fname = VMS::Filespec::unixpath($fname) if $^O eq 'VMS'; 676fb12b70Safresh1 return $fname; 686fb12b70Safresh1 } 696fb12b70Safresh1} 706fb12b70Safresh1 716fb12b70Safresh1# Use file_path() to specify a file path that is expected for $_ 726fb12b70Safresh1# (%Expect_File). Also suitable for file operations like unlink etc. 736fb12b70Safresh1# 746fb12b70Safresh1# file_path() concatenates directory names (if any) and a filename to 756fb12b70Safresh1# form a *relative* file path (the last argument is assumed to be a 766fb12b70Safresh1# file). It is independent from the platform it is run on, although 776fb12b70Safresh1# there are limitations. As a special case, you can pass it a "." as 786fb12b70Safresh1# first argument, to create a file path like "./fa/file" on operating 796fb12b70Safresh1# systems. If there is no second argument, this function will return the 806fb12b70Safresh1# string "./" 816fb12b70Safresh1 826fb12b70Safresh1sub file_path { 836fb12b70Safresh1 my $first_arg = shift @_; 846fb12b70Safresh1 856fb12b70Safresh1 if ($first_arg eq '.') { 866fb12b70Safresh1 return './' unless @_; 876fb12b70Safresh1 my $path = File::Spec->catfile(@_); 886fb12b70Safresh1 # add leading "./" 896fb12b70Safresh1 $path = "./$path"; 906fb12b70Safresh1 return $path; 916fb12b70Safresh1 } 926fb12b70Safresh1 else { # $first_arg ne '.' 936fb12b70Safresh1 return $first_arg unless @_; # return plain filename 946fb12b70Safresh1 my $fname = File::Spec->catfile($first_arg, @_); # relative path 956fb12b70Safresh1 $fname = VMS::Filespec::unixify($fname) if $^O eq 'VMS'; 966fb12b70Safresh1 return $fname; 976fb12b70Safresh1 } 986fb12b70Safresh1} 996fb12b70Safresh1 100*f2a19305Safresh1sub _something_wrong { 101*f2a19305Safresh1 my ($message) = @_; 102*f2a19305Safresh1 warn "in cleanup: $message\n" . 103*f2a19305Safresh1 "Something seems to be very wrong. Possibly the directory\n" . 104*f2a19305Safresh1 "we are testing in has been removed or wiped while we ran?\n"; 105*f2a19305Safresh1 return 0; 106*f2a19305Safresh1} 107*f2a19305Safresh1 108*f2a19305Safresh1sub _cleanup_start { 109*f2a19305Safresh1 my ($test_root_dir, $test_temp_dir)= @_; 110*f2a19305Safresh1 111*f2a19305Safresh1 # doing the following two chdirs (and their validation) in two 112*f2a19305Safresh1 # distinct steps avoids the need to know about directory separators, 113*f2a19305Safresh1 # or other FS specifics, which is helpful as the test files that use 114*f2a19305Safresh1 # this function overrides the File::Spec heirarchy, so we can't ask it 115*f2a19305Safresh1 # to help us here. 116*f2a19305Safresh1 117*f2a19305Safresh1 # chdir into the $test_root_dir to start the cleanup. But first validate. 118*f2a19305Safresh1 if (!$test_root_dir) { 119*f2a19305Safresh1 return _something_wrong("No test_root_dir?"); 120*f2a19305Safresh1 } 121*f2a19305Safresh1 if (!-d $test_root_dir) { 122*f2a19305Safresh1 return _something_wrong("test_root_dir '$test_root_dir' seems to have disappeared!"); 123*f2a19305Safresh1 } 124*f2a19305Safresh1 chdir($test_root_dir) 125*f2a19305Safresh1 or return _something_wrong("Failed to chdir to '$test_root_dir': $!"); 126*f2a19305Safresh1 127*f2a19305Safresh1 # chdir into the $test_temp_dir to start the cleanup. But first validate. 128*f2a19305Safresh1 if (!$test_temp_dir) { 129*f2a19305Safresh1 return _something_wrong("No test_temp_dir?"); 130*f2a19305Safresh1 } 131*f2a19305Safresh1 if (!-d $test_temp_dir) { 132*f2a19305Safresh1 return _something_wrong("test_temp_dir '$test_temp_dir' seems to have disappeared!"); 133*f2a19305Safresh1 } 134*f2a19305Safresh1 chdir($test_temp_dir) 135*f2a19305Safresh1 or return _wrong("Failed to chdir to '$test_temp_dir': $!"); 136*f2a19305Safresh1 137*f2a19305Safresh1 return 1; 138*f2a19305Safresh1} 139*f2a19305Safresh1 1406fb12b70Safresh11; 141