This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate mainline.
[perl5.git] / t / lib / filecomp.t
CommitLineData
a080fe3d
NIS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8BEGIN {
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
17print "1..12\n";
18
19use File::Compare qw(compare compare_text);
20
21print "ok 1\n";
22
23# named files, same, existing but different, cause an error
24print "not " unless compare("README","README") == 0;
25print "ok 2\n";
26
27print "not " unless compare("TEST","README") == 1;
28print "ok 3\n";
29
30print "not " unless compare("README","HLAGHLAG") == -1;
31 # a file which doesn't exist
32print "ok 4\n";
33
34# compare_text, the same file, different but existing files
35# cause error, test sub form.
36print "not " unless compare_text("README","README") == 0;
37print "ok 5\n";
38
39print "not " unless compare_text("TEST","README") == 1;
40print "ok 6\n";
41
42print "not " unless compare_text("TEST","HLAGHLAG") == -1;
43print "ok 7\n";
44
45print "not " unless
46 compare_text("README","README",sub {$_[0] ne $_[1]}) == 0;
47print "ok 8\n";
48
49# filehandle and same file
50{
51 my $fh;
52 open ($fh, "<README") or print "not ";
53 print "not " unless compare($fh,"README") == 0;
54 print "ok 9\n";
55 close $fh;
56}
57
58# filehandle and different (but existing) file.
59{
60 my $fh;
61 open ($fh, "<README") or print "not ";
62 print "not " unless compare_text($fh,"TEST") == 1;
63 print "ok 10\n";
64 close $fh;
65}
66
67# Different file with contents of known file,
68# will use File::Temp to do this, skip rest of
69# tests if this doesn't seem to work
70
71my @donetests;
72eval {
73 require File::Spec; import File::Spec;
74 require File::Path; import File::Path;
75 require File::Temp; import File::Temp qw/ :mktemp unlink0 /;
76
77 my $template = File::Spec->catfile(File::Spec->tmpdir, 'fcmpXXXX');
78 my($tfh,$filename) = mkstemp($template);
79 {
80 local $/; #slurp
81 my $fh;
82 open($fh,'README');
83 my $data = <$fh>;
84 print $tfh $data;
85 close($fh);
86 }
87 seek($tfh,0,0);
88 $donetests[0] = compare($tfh,'README');
89 $donetests[1] = compare("$filename",'README');
90 unlink0($tfh,$filename);
91};
92print "# problems when testing with a tempory file\n" if $@;
93
94if (@donetests == 2) {
95 print "not " unless $donetests[0] == 0;
96 print "ok 11\n";
97 print "not " unless $donetests[1] == 0;
98 print "ok 12\n";
99}
100else {
101 print "ok 11# Skip\nok 12 # Skip Likely due to File::Temp\n";
102}
103