]> sjero.net Git - wget/blobdiff - tests/WgetTest.pm.in
Fix build when libpsl is not available
[wget] / tests / WgetTest.pm.in
index 5cd6769b8c0686d395c230ac9449fe341c7cca69..092777eaf2aca8dbe1f385159dfa40d9d4e879f4 100644 (file)
@@ -24,6 +24,7 @@ my @unexpected_downloads = ();
         _input        => {},
         _name         => "",
         _output       => {},
+        _server_behavior => {},
     );
 
     sub _default_for
@@ -69,12 +70,18 @@ sub new {
 sub run {
     my $self = shift;
     my $result_message = "Test successful.\n";
+    my $errcode;
 
     printf "Running test $self->{_name}\n";
 
     # Setup
-    $self->_setup();
+    my $new_result = $self->_setup();
     chdir ("$self->{_workdir}/$self->{_name}/input");
+    if (defined $new_result) {
+        $result_message = $new_result;
+        $errcode = 1;
+        goto cleanup;
+    }
 
     # Launch server
     my $pid = $self->_fork_and_launch_server();
@@ -84,10 +91,11 @@ sub run {
     my $cmdline = $self->{_cmdline};
     $cmdline = $self->_substitute_port($cmdline);
     print "Calling $cmdline\n";
-    my $errcode =
+    $errcode =
         ($cmdline =~ m{^/.*})
             ? system ($cmdline)
             : system ("$self->{_workdir}/../src/$cmdline");
+    $errcode >>= 8; # XXX: should handle abnormal error codes.
 
     # Shutdown server
     # if we didn't explicitely kill the server, we would have to call
@@ -98,13 +106,14 @@ sub run {
     # Verify download
     unless ($errcode == $self->{_errcode}) {
         $result_message = "Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
+        goto cleanup;
     }
     my $error_str;
     if ($error_str = $self->_verify_download()) {
         $result_message = $error_str;
     }
 
-    # Cleanup
+  cleanup:
     $self->_cleanup();
 
     print $result_message;
@@ -146,6 +155,7 @@ sub _setup {
     $self->_setup_server();
 
     chdir ($self->{_workdir});
+    return;
 }
 
 
@@ -156,6 +166,58 @@ sub _cleanup {
     File::Path::rmtree ($self->{_name}) unless $ENV{WGET_TEST_NO_CLEANUP};
 }
 
+# not a method
+sub quotechar {
+    my $c = ord( shift );
+    if ($c >= 0x7 && $c <= 0xD) {
+       return '\\' . qw(a b t n v f r)[$c - 0x7];
+    } else {
+        return sprintf('\\x%02x', $c);
+    }
+}
+
+# not a method
+sub _show_diff {
+    my $SNIPPET_SIZE = 10;
+
+    my ($expected, $actual) = @_;
+
+    my $str = '';
+    my $explen = length $expected;
+    my $actlen = length $actual;
+
+    if ($explen != $actlen) {
+        $str .= "Sizes don't match: expected = $explen, actual = $actlen\n";
+    }
+
+    my $min = $explen <= $actlen? $explen : $actlen;
+    my $line = 1;
+    my $col = 1;
+    my $i;
+    for ($i=0; $i != $min; ++$i) {
+        last if substr($expected, $i, 1) ne substr($actual, $i, 1);
+        if (substr($expected, $i, 1) eq '\n') {
+            $line++;
+            $col = 0;
+        } else {
+            $col++;
+        }
+    }
+    my $snip_start = $i - ($SNIPPET_SIZE / 2);
+    if ($snip_start < 0) {
+        $SNIPPET_SIZE += $snip_start; # Take it from the end.
+        $snip_start = 0;
+    }
+    my $exp_snip = substr($expected, $snip_start, $SNIPPET_SIZE);
+    my $act_snip = substr($actual, $snip_start, $SNIPPET_SIZE);
+    $exp_snip =~s/[^[:print:]]/ quotechar($&) /ge;
+    $act_snip =~s/[^[:print:]]/ quotechar($&) /ge;
+    $str .= "Mismatch at line $line, col $col:\n";
+    $str .= "    $exp_snip\n";
+    $str .= "    $act_snip\n";
+
+    return $str;
+}
 
 sub _verify_download {
     my $self = shift;
@@ -173,8 +235,10 @@ sub _verify_download {
         my $content = <FILE>;
         my $expected_content = $filedata->{'content'};
         $expected_content = $self->_substitute_port($expected_content);
-        $content eq $expected_content
-            or return "Test failed: wrong content for file $filename\n";
+        unless ($content eq $expected_content) {
+            return "Test failed: wrong content for file $filename\n"
+                . _show_diff($expected_content, $content);
+        }
 
         if (exists($filedata->{'timestamp'})) {
             my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
@@ -192,7 +256,10 @@ sub _verify_download {
     # make sure no unexpected files were downloaded
     chdir ("$self->{_workdir}/$self->{_name}/output");
 
-    __dir_walk('.', sub { push @unexpected_downloads, $_[0] unless (exists $self->{_output}{$_[0]}) }, sub { shift; return @_ } );
+    __dir_walk('.',
+               sub { push @unexpected_downloads,
+                          $_[0] unless (exists $self->{_output}{$_[0]} || $self->{_existing}{$_[0]}) },
+               sub { shift; return @_ } );
     if (@unexpected_downloads) {
         return "Test failed: unexpected downloaded files [" . join(', ', @unexpected_downloads) . "]\n";
     }