Просмотр исходного кода

Add non-regression tests for Dpkg::Compression::CompressedFile

Raphaël Hertzog лет назад: 16
Родитель
Сommit
c5fea0a576
2 измененных файлов с 100 добавлено и 0 удалено
  1. 1 0
      scripts/Makefile.am
  2. 99 0
      scripts/t/850_Dpkg_Compression.t

+ 1 - 0
scripts/Makefile.am

@@ -147,6 +147,7 @@ test_cases = \
 	t/700_Dpkg_Control.t \
 	t/750_Dpkg_Substvars.t \
 	t/800_Dpkg_IPC.t \
+	t/850_Dpkg_Compression.t \
 	t/900_update_alternatives.t
 
 check_DATA = \

+ 99 - 0
scripts/t/850_Dpkg_Compression.t

@@ -0,0 +1,99 @@
+# -*- mode: cperl -*-
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+use Test::More tests => 9;
+
+use strict;
+use warnings;
+
+use_ok('Dpkg::Compression::CompressedFile');
+
+my $tmpdir = "t.tmp/850_Dpkg_Compression";
+mkdir $tmpdir;
+my @lines = ("One\n", "Two\n", "Three\n");
+my $fh;
+
+sub test_write {
+    my ($filename, $check_result) = @_;
+
+    $fh = Dpkg::Compression::CompressedFile->new();
+    open $fh, ">", $filename or die "open failed";
+    print $fh $lines[0];
+    syswrite($fh, $lines[1]);
+    printf $fh "%s", $lines[2];
+    close $fh or die "close failed";
+
+    &$check_result($filename, "std functions");
+
+    unlink $filename or die "cannot unlink $filename";
+
+    $fh = Dpkg::Compression::CompressedFile->new();
+    $fh->open($filename, "w");
+    $fh->print($lines[0]);
+    $fh->write($lines[1], length($lines[1]));
+    $fh->printf("%s", $lines[2]);
+    $fh->close() or die "close failed";
+
+    &$check_result($filename, "IO::Handle methods");
+}
+
+sub check_uncompressed {
+    my ($filename, $method) = @_;
+    open(READ, "<", $filename) or die "cannot read $filename";
+    my @read = <READ>;
+    close READ or die "cannot close";
+    is_deeply(\@lines, \@read, "$filename correctly written ($method)");
+}
+
+sub check_compressed {
+    my ($filename, $method) = @_;
+    open(READ, "-|", "zcat $tmpdir/myfile.gz") or die "cannot fork zcat";
+    my @read = <READ>;
+    close READ or die "cannot close";
+    is_deeply(\@lines, \@read, "$filename correctly written ($method)");
+}
+
+sub test_read {
+    my ($filename) = @_;
+
+    $fh = Dpkg::Compression::CompressedFile->new();
+    open($fh, "<", $filename) or die "open failed";
+    my @read = <$fh>;
+    close $fh or die "close failed";
+
+    is_deeply(\@lines, \@read, "$filename correctly read (std functions)");
+
+    @read = ();
+    $fh = Dpkg::Compression::CompressedFile->new();
+    $fh->open($filename, "r") or die "open failed";
+    @read = $fh->getlines();
+    $fh->close() or die "close failed";
+
+    is_deeply(\@lines, \@read, "$filename correctly read (IO::Handle methods)");
+}
+
+# Test write on uncompressed file
+test_write("$tmpdir/myfile", \&check_uncompressed);
+
+# Test write on compressed file
+test_write("$tmpdir/myfile.gz", \&check_compressed);
+
+# Test read on uncompressed file
+test_read("$tmpdir/myfile");
+
+# Test read on compressed file
+test_read("$tmpdir/myfile.gz");
+
+