Re: [PATCH] Tests for File::Compare
Alexander Gough [Thu, 31 May 2001 13:26:34 +0000 (14:26 +0100)]
Message-Id: <E155RX8-0004X2-00@wing1.herald.ox.ac.uk>

p4raw-id: //depot/perl@10354

MANIFEST
t/lib/1_compile.t
t/lib/filecomp.t [new file with mode: 0644]

index 8da41ab..2af84c0 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -1524,6 +1524,7 @@ t/lib/fatal.t           See if Fatal works
 t/lib/fcntl.t           See if Fcntl works
 t/lib/fields.t          See if base/fields works
 t/lib/filecache.t      See if FileCache works
+t/lib/filecomp.t       See if File::Compare works
 t/lib/filecopy.t       See if File::Copy works
 t/lib/filefind.t       See if File::Find works
 t/lib/filefunc.t       See if File::Spec::Functions works
index eb2d70b..e46e14b 100644 (file)
@@ -147,6 +147,7 @@ Fatal
 Fcntl
 File::Basename
 File::CheckTree
+File::Compare
 File::Copy
 File::DosGlob
 File::Find
diff --git a/t/lib/filecomp.t b/t/lib/filecomp.t
new file mode 100644 (file)
index 0000000..b841d87
--- /dev/null
@@ -0,0 +1,103 @@
+#!./perl
+
+BEGIN {
+  chdir 't' if -d 't';
+  @INC = '../lib';
+}
+
+BEGIN {
+  our @TEST = stat "TEST";
+  our @README = stat "README";
+  unless (@TEST && @README) {
+    print "1..0 # Skip: no file TEST or README\n";
+    exit 0;
+  }
+}
+
+print "1..12\n";
+
+use File::Compare qw(compare compare_text);
+
+print "ok 1\n";
+
+# named files, same, existing but different, cause an error
+print "not " unless compare("README","README") == 0;
+print "ok 2\n";
+
+print "not " unless compare("TEST","README") == 1;
+print "ok 3\n";
+
+print "not " unless compare("README","HLAGHLAG") == -1;
+                               # a file which doesn't exist
+print "ok 4\n";
+
+# compare_text, the same file, different but existing files
+# cause error, test sub form.
+print "not " unless compare_text("README","README") == 0;
+print "ok 5\n";
+
+print "not " unless compare_text("TEST","README") == 1;
+print "ok 6\n";
+
+print "not " unless compare_text("TEST","HLAGHLAG") == -1;
+print "ok 7\n";
+
+print "not " unless
+  compare_text("README","README",sub {$_[0] ne $_[1]}) == 0;
+print "ok 8\n";
+
+# filehandle and same file
+{
+  my $fh;
+  open ($fh, "<README") or print "not ";
+  print "not " unless compare($fh,"README") == 0;
+  print "ok 9\n";
+  close $fh;
+}
+
+# filehandle and different (but existing) file.
+{
+  my $fh;
+  open ($fh, "<README") or print "not ";
+  print "not " unless compare_text($fh,"TEST") == 1;
+  print "ok 10\n";
+  close $fh;
+}
+
+# Different file with contents of known file,
+# will use File::Temp to do this, skip rest of
+# tests if this doesn't seem to work
+
+my @donetests;
+eval {
+  require File::Spec; import File::Spec;
+  require File::Path; import File::Path;
+  require File::Temp; import File::Temp qw/ :mktemp unlink0 /;
+
+  my $template = File::Spec->catfile(File::Spec->tmpdir, 'fcmpXXXX');
+  my($tfh,$filename) = mkstemp($template);
+  {
+    local $/; #slurp
+    my $fh;
+    open($fh,'README');
+    my $data = <$fh>;
+    print $tfh $data;
+    close($fh);
+  }
+  seek($tfh,0,0);
+  $donetests[0] = compare($tfh,'README');
+  $donetests[1] = compare("$filename",'README');
+  unlink0($tfh,$filename);
+};
+print "# problems when testing with a tempory file\n" if $@;
+
+if (@donetests == 2) {
+  print "not " unless $donetests[0] == 0;
+  print "ok 11\n";
+  print "not " unless $donetests[1] == 0;
+  print "ok 12\n";
+}
+else {
+  print "ok 11# Skip\nok 12 # Skip Likely due to File::Temp\n";
+}
+