Re: [perl #37716] Re: File::Compare broken for filenames with whitespace
[p5sagit/p5-mst-13.2.git] / lib / File / Compare.t
1 #!./perl
2
3 BEGIN {
4   chdir 't' if -d 't';
5   @INC = '../lib';
6 }
7
8 BEGIN {
9   our @TEST = stat "TEST";
10   our @README = stat "README";
11   unless (@TEST && @README) {
12     print "1..0 # Skip: no file TEST or README\n";
13     exit 0;
14   }
15 }
16
17 print "1..13\n";
18
19 use File::Compare qw(compare compare_text);
20
21 print "ok 1\n";
22
23 # named files, same, existing but different, cause an error
24 print "not " unless compare("README","README") == 0;
25 print "ok 2\n";
26
27 print "not " unless compare("TEST","README") == 1;
28 print "ok 3\n";
29
30 print "not " unless compare("README","HLAGHLAG") == -1;
31                                # a file which doesn't exist
32 print "ok 4\n";
33
34 # compare_text, the same file, different but existing files
35 # cause error, test sub form.
36 print "not " unless compare_text("README","README") == 0;
37 print "ok 5\n";
38
39 print "not " unless compare_text("TEST","README") == 1;
40 print "ok 6\n";
41
42 print "not " unless compare_text("TEST","HLAGHLAG") == -1;
43 print "ok 7\n";
44
45 print "not " unless
46   compare_text("README","README",sub {$_[0] ne $_[1]}) == 0;
47 print "ok 8\n";
48
49 # filehandle and same file
50 {
51   my $fh;
52   open ($fh, "<README") or print "not ";
53   binmode($fh);
54   print "not " unless compare($fh,"README") == 0;
55   print "ok 9\n";
56   close $fh;
57 }
58
59 # filehandle and different (but existing) file.
60 {
61   my $fh;
62   open ($fh, "<README") or print "not ";
63   binmode($fh);
64   print "not " unless compare_text($fh,"TEST") == 1;
65   print "ok 10\n";
66   close $fh;
67 }
68
69 # Different file with contents of known file,
70 # will use File::Temp to do this, skip rest of
71 # tests if this doesn't seem to work
72
73 my @donetests;
74 eval {
75   require File::Spec; import File::Spec;
76   require File::Path; import File::Path;
77   require File::Temp; import File::Temp qw/ :mktemp unlink0 /;
78
79   my $template = File::Spec->catfile(File::Spec->tmpdir, 'fcmpXXXX');
80   my($tfh,$filename) = mkstemp($template);
81   open my $tfhCR, ">", "$filename\cM"
82       or die "Could no open '$filename^M' for writing: $!";
83   {
84     local $/; #slurp
85     my $fh;
86     open($fh,'README');
87     binmode($fh);
88     my $data = <$fh>;
89     print $tfh $data;
90     close($fh);
91     print $tfhCR $data;
92     close($tfhCR);
93   }
94   seek($tfh,0,0);
95   $donetests[0] = compare($tfh, 'README');
96   $donetests[1] = compare($filename, 'README');
97   unlink0($tfh,$filename);
98   $donetests[2] = compare('README', "$filename\cM");
99   unlink "$filename\cM";
100 };
101 print "# problem '$@' when testing with a temporary file\n" if $@;
102
103 if (@donetests == 3) {
104   print "not " unless $donetests[0] == 0;
105   print "ok 11 # fh/file [$donetests[0]]\n";
106   if ($^O eq 'VMS') {
107     # The open attempt on FROM in File::Compare::compare should fail
108     # on this OS since files are not shared by default.
109     print "not " unless $donetests[1] == -1;
110     print "ok 12 # file/file [$donetests[1]]\n";
111   }
112   else {
113     print "not " unless $donetests[1] == 0;
114     print "ok 12 # file/file [$donetests[1]]\n";
115   }
116   print "not " unless $donetests[2] == 0;
117   print "ok 13 # file/fileCR [$donetests[2]]\n";
118 }
119 else {
120   print "ok 11# Skip\nok 12 # Skip\nok 13 # Skip Likely due to File::Temp\n";
121 }
122