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