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