| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# by Erik Osheim |
| 4 |
use warnings FATAL => 'all'; |
| 5 |
use strict; |
| 6 |
use File::Basename qw(dirname basename); |
| 7 |
use List::Util qw(first max); |
| 8 |
use Getopt::Long qw(:config bundling no_ignore_case); |
| 9 |
|
| 10 |
# some nice global variables |
| 11 |
my $quiet = 0; |
| 12 |
my $verbose = $ENV{VERBOSE}; |
| 13 |
my $usecolor = 1; |
| 14 |
|
| 15 |
sub usage { |
| 16 |
my $prog = basename($0); |
| 17 |
print <<USAGE; |
| 18 |
Usage: $prog [options] |
| 19 |
|
| 20 |
Options: |
| 21 |
-h,--help show this message |
| 22 |
-c,--color use ANSI colors (default) |
| 23 |
-C,--no-color don't use ANSI colors |
| 24 |
-q,--quiet only show summary output |
| 25 |
-v,--verbose show all test output |
| 26 |
|
| 27 |
Runs all the unit tests and reports the results. |
| 28 |
USAGE |
| 29 |
exit(@_); |
| 30 |
} |
| 31 |
|
| 32 |
# print strings with (optional) ANSI color |
| 33 |
sub red { $usecolor ? ("\033[01;31m", @_, "\033[0m") : (@_) } |
| 34 |
sub green { $usecolor ? ("\033[01;32m", @_, "\033[0m") : (@_) } |
| 35 |
sub yellow { $usecolor ? ("\033[01;33m", @_, "\033[0m") : (@_) } |
| 36 |
sub blue { $usecolor ? ("\033[01;34m", @_, "\033[0m") : (@_) } |
| 37 |
|
| 38 |
# determine which color to use: green == 100% > yellow >= 90% > red |
| 39 |
sub getcolor { |
| 40 |
my ($pass, $total) = @_; |
| 41 |
my $perc = $total ? ($pass * 100) / $total : 100; |
| 42 |
return $pass == $total ? \&green : $perc >= 90 ? \&yellow : \&red; |
| 43 |
} |
| 44 |
|
| 45 |
sub main { |
| 46 |
GetOptions( |
| 47 |
'help|h' => sub { usage(0) }, |
| 48 |
'color|c' => sub { $usecolor = 1 }, |
| 49 |
'no-color|C' => sub { $usecolor = 0 }, |
| 50 |
'verbose|v' => sub { $verbose = 1; $quiet = 0 }, |
| 51 |
'quiet|q' => sub { $quiet = 1; $verbose = 0 }, |
| 52 |
) || usage(1); |
| 53 |
|
| 54 |
my $dir = dirname($0); |
| 55 |
my @paths = `find $dir -mindepth 2 -maxdepth 2 -type f -perm -u+x`; |
| 56 |
my $pass = 0; |
| 57 |
my $total = 0; |
| 58 |
my $maxpath = (max map { length($_) } @paths) - 3; |
| 59 |
my $len = $maxpath + 1 + 7; |
| 60 |
my $exitcode = 0; |
| 61 |
|
| 62 |
print "Running ", scalar(@paths), " suites:\n" unless $quiet; |
| 63 |
foreach my $path (sort @paths) { |
| 64 |
chomp $path; |
| 65 |
|
| 66 |
# actually run the test program here, getting the lines of output |
| 67 |
my @lines = $verbose ? `$path -v` : `$path`; |
| 68 |
|
| 69 |
if ($? != 0) { |
| 70 |
print red("$path: Suite died"), "\n"; |
| 71 |
$exitcode = $?; |
| 72 |
|
| 73 |
# We don't know how many tests the suite had, |
| 74 |
# but at least one failed |
| 75 |
$total += 1; |
| 76 |
next; |
| 77 |
} |
| 78 |
unless ($lines[-1] =~ m#^([^:]+) finished: (\d+)/(\d+) passed$#) { |
| 79 |
print red("$path: Malformed output"), "\n"; |
| 80 |
$exitcode = 1; |
| 81 |
next; |
| 82 |
} |
| 83 |
|
| 84 |
# tally the results |
| 85 |
$pass += $2; |
| 86 |
$total += $3; |
| 87 |
|
| 88 |
next if $quiet; |
| 89 |
|
| 90 |
# print a one-line summary of what happened |
| 91 |
my $ns = "$2/$3"; |
| 92 |
my $pad = $len - length($1) - length($ns); |
| 93 |
my $color = getcolor($2, $3); |
| 94 |
if ($verbose) { |
| 95 |
print ' ', $_ for @lines[0..$#lines - 1]; |
| 96 |
print ' ', $1, ' finished: ', &$color($ns), " passed\n"; |
| 97 |
} else { |
| 98 |
print ' ', $1, ' ' x $pad, &$color($ns), " passed\n"; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
# print a summary of all the test results |
| 103 |
my $color = getcolor($pass, $total); |
| 104 |
my $ns = join('', &$color("$pass/$total")); |
| 105 |
my $ps = join('', &$color(sprintf("%.1f%%", ($total ? ($pass * 100) / $total : 100)))); |
| 106 |
|
| 107 |
printf("Total: %s passed (%s)\n", $ns, $ps); |
| 108 |
|
| 109 |
if ($exitcode != 0 && $pass != $total) { |
| 110 |
$exitcode = 2; |
| 111 |
} |
| 112 |
exit($exitcode); |
| 113 |
} |
| 114 |
|
| 115 |
main(); |