]> arthur.barton.de Git - bup.git/commitdiff
wvtest: move top-level code to a run() function
authorRob Browning <rlb@defaultvalue.org>
Sat, 2 May 2015 16:31:37 +0000 (11:31 -0500)
committerRob Browning <rlb@defaultvalue.org>
Sat, 23 May 2015 16:08:36 +0000 (11:08 -0500)
Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
Reviewed-by: Gabriel Filion <gabster@lelutin.ca>
wvtest

diff --git a/wvtest b/wvtest
index 2359bce73016ced9d890d841e6df533d4af75082..109b223df0bbe28ad71e200a65e872c2247bd1d6 100755 (executable)
--- a/wvtest
+++ b/wvtest
@@ -10,31 +10,22 @@ use strict;
 use warnings;
 use Time::HiRes qw(time);
 
-# always flush
-$| = 1;
+my $pid;
+my $istty = -t STDOUT;
+my @log = ();
 
-if (@ARGV < 2 || $ARGV[0] ne 'run') {
-    print STDERR "Usage: run $0 <command line...>\n";
-    exit 127;
+sub usage
+{
+    my ($fh) = @_;
+    print $fh "Usage: $0 run <command line...>\n";
 }
 
-shift @ARGV;
-
-print STDERR "Testing \"all\" in @ARGV:\n";
-
-my $pid = open(my $fh, "-|");
-if (!$pid) {
-    # child
-    setpgrp();
-    open STDERR, '>&STDOUT' or die("Can't dup stdout: $!\n");
-    exec(@ARGV);
-    exit 126; # just in case
+sub usage_death()
+{
+    usage(\*STDERR);
+    exit 127;
 }
 
-my $istty = -t STDOUT;
-my @log = ();
-my ($gpasses, $gfails) = (0,0);
-
 sub bigkill($)
 {
     my $pid = shift;
@@ -59,14 +50,6 @@ sub bigkill($)
     exit(125);
 }
 
-# parent
-local $SIG{INT} = sub { bigkill($pid); };
-local $SIG{TERM} = sub { bigkill($pid); };
-local $SIG{ALRM} = sub {
-    print STDERR "Alarm timed out!  No test results for too long.\n";
-    bigkill($pid);
-};
-
 sub colourize($)
 {
     my $result = shift;
@@ -101,7 +84,6 @@ sub resultline($$)
     return sprintf("! %-65s %s", $name, colourize($result));
 }
 
-my $allstart = time();
 my ($start, $stop);
 
 sub endsect()
@@ -112,79 +94,119 @@ sub endsect()
     }
 }
 
-while (<$fh>)
+sub run()
 {
-    chomp;
-    s/\r//g;
+    usage_death() if (@ARGV < 1);
 
-    if (/^\s*Testing "(.*)" in (.*):\s*$/)
-    {
-        alarm(120);
-       my ($sect, $file) = ($1, $2);
+    # always flush
+    $| = 1;
 
-       endsect();
+    print STDERR "Testing \"all\" in @ARGV:\n";
 
-       printf("! %s  %s: ", $file, $sect);
-       @log = ();
-       $start = $stop;
+    $pid = open(my $fh, "-|");
+    if (!$pid) {
+        # child
+        setpgrp();
+        open STDERR, '>&STDOUT' or die("Can't dup stdout: $!\n");
+        exec(@ARGV);
+        exit 126; # just in case
     }
-    elsif (/^!\s*(.*?)\s+(\S+)\s*$/)
+
+    # parent
+    my $allstart = time();
+    local $SIG{INT} = sub { bigkill($pid); };
+    local $SIG{TERM} = sub { bigkill($pid); };
+    local $SIG{ALRM} = sub {
+        print STDERR "Alarm timed out!  No test results for too long.\n";
+        bigkill($pid);
+    };
+
+    my ($gpasses, $gfails) = (0,0);
+    while (<$fh>)
     {
-        alarm(120);
-
-       my ($name, $result) = ($1, $2);
-       my $pass = ($result eq "ok");
-
-       if (!$start) {
-           printf("\n! Startup: ");
-           $start = time();
-       }
-
-       push @log, resultline($name, $result);
-
-       if (!$pass) {
-           $gfails++;
-           if (@log) {
-               print "\n" . join("\n", @log) . "\n";
-               @log = ();
-           }
-       } else {
-           $gpasses++;
-           print ".";
-       }
+        chomp;
+        s/\r//g;
+
+        if (/^\s*Testing "(.*)" in (.*):\s*$/)
+        {
+            alarm(120);
+            my ($sect, $file) = ($1, $2);
+
+            endsect();
+
+            printf("! %s  %s: ", $file, $sect);
+            @log = ();
+            $start = $stop;
+        }
+        elsif (/^!\s*(.*?)\s+(\S+)\s*$/)
+        {
+            alarm(120);
+
+            my ($name, $result) = ($1, $2);
+            my $pass = ($result eq "ok");
+
+            if (!$start) {
+                printf("\n! Startup: ");
+                $start = time();
+            }
+
+            push @log, resultline($name, $result);
+
+            if (!$pass) {
+                $gfails++;
+                if (@log) {
+                    print "\n" . join("\n", @log) . "\n";
+                    @log = ();
+                }
+            } else {
+                $gpasses++;
+                print ".";
+            }
+        }
+        else
+        {
+            push @log, $_;
+        }
     }
-    else
-    {
-       push @log, $_;
+
+    endsect();
+
+    my $newpid = waitpid($pid, 0);
+    if ($newpid != $pid) {
+        die("waitpid returned '$newpid', expected '$pid'\n");
     }
-}
 
-endsect();
+    my $code = $?;
+    my $ret = ($code >> 8);
 
-my $newpid = waitpid($pid, 0);
-if ($newpid != $pid) {
-    die("waitpid returned '$newpid', expected '$pid'\n");
-}
+    # return death-from-signal exits as >128.  This is what bash does if you ran
+    # the program directly.
+    if ($code && !$ret) { $ret = $code | 128; }
 
-my $code = $?;
-my $ret = ($code >> 8);
+    if ($ret && @log) {
+        print "\n" . join("\n", @log) . "\n";
+    }
 
-# return death-from-signal exits as >128.  This is what bash does if you ran
-# the program directly.
-if ($code && !$ret) { $ret = $code | 128; }
+    if ($code != 0) {
+        print resultline("Program returned non-zero exit code ($ret)", "FAILED");
+    }
 
-if ($ret && @log) {
-    print "\n" . join("\n", @log) . "\n";
+    my $gtotal = $gpasses+$gfails;
+    printf("\nWvTest: %d test%s, %d failure%s, total time %s.\n",
+           $gtotal, $gtotal==1 ? "" : "s",
+           $gfails, $gfails==1 ? "" : "s",
+           mstime(time() - $allstart, 2000, 5000));
+    print STDERR "\nWvTest result code: $ret\n";
+    exit( $ret ? $ret : ($gfails ? 125 : 0) );
 }
 
-if ($code != 0) {
-    print resultline("Program returned non-zero exit code ($ret)", "FAILED");
-}
+usage_death() unless (@ARGV > 1);
+my $action = $ARGV[0];
+shift @ARGV;
 
-my $gtotal = $gpasses+$gfails;
-printf("\nWvTest: %d test%s, %d failure%s, total time %s.\n",
-    $gtotal, $gtotal==1 ? "" : "s",
-    $gfails, $gfails==1 ? "" : "s",
-    mstime(time() - $allstart, 2000, 5000));
-print STDERR "\nWvTest result code: $ret\n";
-exit( $ret ? $ret : ($gfails ? 125 : 0) );
+if ($action eq 'run') {
+  run();
+}
+else {
+  usage_death();
+}