home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / t / lib / filecopy.t < prev    next >
Text File  |  2000-01-04  |  3KB  |  110 lines

  1. #!./perl
  2.  
  3. BEGIN {
  4.     chdir 't' if -d 't';
  5.     unshift @INC, '../lib';
  6. }
  7.  
  8. $| = 1;
  9.  
  10. my @pass = (0,1);
  11. my $tests = 11;
  12. printf "1..%d\n", $tests * scalar(@pass);
  13.  
  14. use File::Copy;
  15.  
  16. for my $pass (@pass) {
  17.  
  18.   require File::Copy;
  19.  
  20.   my $loopconst = $pass*$tests;
  21.  
  22.   # First we create a file
  23.   open(F, ">file-$$") or die;
  24.   binmode F; # for DOSISH platforms, because test 3 copies to stdout
  25.   printf F "ok %d\n", 3 + $loopconst;
  26.   close F;
  27.  
  28.   copy "file-$$", "copy-$$";
  29.  
  30.   open(F, "copy-$$") or die;
  31.   $foo = <F>;
  32.   close(F);
  33.  
  34.   print "not " if -s "file-$$" != -s "copy-$$";
  35.   printf "ok %d\n", 1 + $loopconst;
  36.  
  37.   print "not " unless $foo eq sprintf "ok %d\n", 3+$loopconst;
  38.   printf "ok %d\n", 2+$loopconst;
  39.  
  40.   binmode STDOUT unless $^O eq 'VMS'; # Copy::copy works in binary mode
  41.   copy "copy-$$", \*STDOUT;
  42.   unlink "copy-$$" or die "unlink: $!";
  43.  
  44.   open(F,"file-$$");
  45.   copy(*F, "copy-$$");
  46.   open(R, "copy-$$") or die "open copy-$$: $!"; $foo = <R>; close(R);
  47.   print "not " unless $foo eq sprintf "ok %d\n", 3+$loopconst;
  48.   printf "ok %d\n", 4+$loopconst;
  49.   unlink "copy-$$" or die "unlink: $!";
  50.   open(F,"file-$$");
  51.   copy(\*F, "copy-$$");
  52.   close(F) or die "close: $!";
  53.   open(R, "copy-$$") or die; $foo = <R>; close(R) or die "close: $!";
  54.   print "not " unless $foo eq sprintf "ok %d\n", 3+$loopconst;
  55.   printf "ok %d\n", 5+$loopconst;
  56.   unlink "copy-$$" or die "unlink: $!";
  57.  
  58.   require IO::File;
  59.   $fh = IO::File->new(">copy-$$") or die "Cannot open copy-$$:$!";
  60.   binmode $fh or die;
  61.   copy("file-$$",$fh);
  62.   $fh->close or die "close: $!";
  63.   open(R, "copy-$$") or die; $foo = <R>; close(R);
  64.   print "# foo=`$foo'\nnot " unless $foo eq sprintf "ok %d\n", 3+$loopconst;
  65.   printf "ok %d\n", 6+$loopconst;
  66.   unlink "copy-$$" or die "unlink: $!";
  67.   require FileHandle;
  68.   my $fh = FileHandle->new(">copy-$$") or die "Cannot open copy-$$:$!";
  69.   binmode $fh or die;
  70.   copy("file-$$",$fh);
  71.   $fh->close;
  72.   open(R, "copy-$$") or die; $foo = <R>; close(R);
  73.   print "not " unless $foo eq sprintf "ok %d\n", 3+$loopconst;
  74.   printf "ok %d\n", 7+$loopconst;
  75.   unlink "file-$$" or die "unlink: $!";
  76.  
  77.   print "# moved missing file.\nnot " if move("file-$$", "copy-$$");
  78.   print "# target disappeared.\nnot " if not -e "copy-$$";
  79.   printf "ok %d\n", 8+$loopconst;
  80.  
  81.   move "copy-$$", "file-$$" or print "# move did not succeed.\n";
  82.   print "# not moved: $!\nnot " unless -e "file-$$" and not -e "copy-$$";
  83.   open(R, "file-$$") or die; $foo = <R>; close(R);
  84.   print "# foo=`$foo'\nnot " unless $foo eq sprintf "ok %d\n", 3+$loopconst;
  85.   printf "ok %d\n", 9+$loopconst;
  86.  
  87.   copy "file-$$", "lib";
  88.   open(R, "lib/file-$$") or die; $foo = <R>; close(R);
  89.   print "not " unless $foo eq sprintf "ok %d\n", 3+$loopconst;
  90.   printf "ok %d\n", 10+$loopconst;
  91.   unlink "lib/file-$$" or die "unlink: $!";
  92.  
  93.   move "file-$$", "lib";
  94.   open(R, "lib/file-$$") or die "open lib/file-$$: $!"; $foo = <R>; close(R);
  95.   print "not " unless $foo eq sprintf("ok %d\n", 3+$loopconst)
  96.       and not -e "file-$$";;
  97.   printf "ok %d\n", 11+$loopconst;
  98.   unlink "lib/file-$$" or die "unlink: $!";
  99.  
  100.   # warn sprintf "INC->".$INC{"File/Copy.pm"};
  101.   delete $INC{"File/Copy.pm"};
  102.  
  103. }
  104.  
  105.  
  106. END {
  107.     1 while unlink "file-$$";
  108.     1 while unlink "lib/file-$$";
  109. }
  110.