| 1 |
#!/usr/bin/env perl |
| 2 |
# ============================================================================= |
| 3 |
# |
| 4 |
# This file is part of BibTool. |
| 5 |
# It is distributed under the GNU General Public License. |
| 6 |
# See the file COPYING for details. |
| 7 |
# |
| 8 |
# (c) 2011-2020 Gerd Neugebauer |
| 9 |
# |
| 10 |
# Net: [email protected] |
| 11 |
# |
| 12 |
# This program is free software; you can redistribute it and/or modify |
| 13 |
# it under the terms of the GNU General Public License as published by |
| 14 |
# the Free Software Foundation; either version 2, or (at your option) |
| 15 |
# any later version. |
| 16 |
# |
| 17 |
# This program is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
# GNU General Public License for more details. |
| 21 |
# |
| 22 |
# You should have received a copy of the GNU General Public License |
| 23 |
# along with this program; if not, write to the Free Software |
| 24 |
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 |
# |
| 26 |
#*============================================================================= |
| 27 |
|
| 28 |
=head1 NAME |
| 29 |
BUnit.pm - Driver for BibTool test cases |
| 30 |
|
| 31 |
=head1 SYNOPSIS |
| 32 |
|
| 33 |
perl -MBUnit -e "exit all()" |
| 34 |
|
| 35 |
=head1 DESCRIPTION |
| 36 |
|
| 37 |
This is the test driver for unit tests of BibTool. |
| 38 |
|
| 39 |
=head1 Attributes |
| 40 |
|
| 41 |
=head1 Methods |
| 42 |
|
| 43 |
=head2 all() |
| 44 |
|
| 45 |
This method collects all files with the extension .t and executes them as test |
| 46 |
cases. |
| 47 |
|
| 48 |
=head2 suites() |
| 49 |
|
| 50 |
This method takes a glob pattern as argument and executes all matching files |
| 51 |
as test cases. |
| 52 |
|
| 53 |
=head2 run() |
| 54 |
|
| 55 |
This method defines a single test case. |
| 56 |
|
| 57 |
=head1 AUTHOR |
| 58 |
|
| 59 |
Gerd Neugebauer |
| 60 |
|
| 61 |
=head1 BUGS |
| 62 |
|
| 63 |
none |
| 64 |
|
| 65 |
=cut |
| 66 |
|
| 67 |
#------------------------------------------------------------------------------ |
| 68 |
package BUnit; |
| 69 |
|
| 70 |
use strict; |
| 71 |
use warnings; |
| 72 |
use Exporter; |
| 73 |
use base qw(Exporter); |
| 74 |
our @EXPORT = qw(all summary run quiet); |
| 75 |
our @EXPORT_OK = qw(); |
| 76 |
|
| 77 |
use Cwd; |
| 78 |
use FileHandle; |
| 79 |
|
| 80 |
use Time::HiRes qw/gettimeofday/; |
| 81 |
|
| 82 |
use constant TEST_RSC =>'_test.rsc'; |
| 83 |
use constant TEST_BIB =>'_test.bib'; |
| 84 |
|
| 85 |
#------------------------------------------------------------------------------ |
| 86 |
# Variable: $verbose |
| 87 |
# Description: |
| 88 |
# |
| 89 |
my $verbose = 1; |
| 90 |
|
| 91 |
#------------------------------------------------------------------------------ |
| 92 |
# Variable: $VERSION |
| 93 |
# Description: |
| 94 |
# |
| 95 |
our $VERSION = '1.17'; |
| 96 |
|
| 97 |
#------------------------------------------------------------------------------ |
| 98 |
# Variable: $BIBTOOL |
| 99 |
# Description: |
| 100 |
# |
| 101 |
our $BIBTOOL = $ENV{'BIBTOOL_PRG'} || '../bibtool'; |
| 102 |
|
| 103 |
#------------------------------------------------------------------------------ |
| 104 |
# Variable: $name_prefix |
| 105 |
# Description: |
| 106 |
# |
| 107 |
our $name_prefix = ''; |
| 108 |
|
| 109 |
#------------------------------------------------------------------------------ |
| 110 |
# Variable: %summary |
| 111 |
# Description: |
| 112 |
# |
| 113 |
our %summary = (); |
| 114 |
|
| 115 |
#------------------------------------------------------------------------------ |
| 116 |
# Variable: %id |
| 117 |
# Description: |
| 118 |
# |
| 119 |
our %names = (); |
| 120 |
|
| 121 |
our ($success, $ignored, $failure); |
| 122 |
|
| 123 |
#------------------------------------------------------------------------------ |
| 124 |
# Function: quiet |
| 125 |
# |
| 126 |
sub quiet |
| 127 |
{ |
| 128 |
$verbose = 0; |
| 129 |
} |
| 130 |
|
| 131 |
#------------------------------------------------------------------------------ |
| 132 |
# Function: out |
| 133 |
# |
| 134 |
sub out (@) { |
| 135 |
print @_ if $verbose; |
| 136 |
} |
| 137 |
|
| 138 |
#------------------------------------------------------------------------------ |
| 139 |
# Function: run |
| 140 |
# |
| 141 |
sub run { |
| 142 |
my %a = @_; |
| 143 |
my $name = $name_prefix . $a{name}; |
| 144 |
my $prepare = $a{prepare}; |
| 145 |
|
| 146 |
if (defined $names{$name}) { |
| 147 |
print STDERR "*** Warning: Multiple test cases named $name\n"; |
| 148 |
} else { |
| 149 |
$names{$name} = 1; |
| 150 |
} |
| 151 |
|
| 152 |
&{$prepare}($name) if defined $prepare; |
| 153 |
|
| 154 |
local $_ = ''; |
| 155 |
$_ = '.' x (40-length($name)) if length($name) < 40; |
| 156 |
out " ",$name,$_; |
| 157 |
|
| 158 |
if ($a{ignore}) { |
| 159 |
$ignored++; |
| 160 |
out "ignored $a{ignore}\n"; |
| 161 |
return 1; |
| 162 |
} |
| 163 |
|
| 164 |
my $out = "$name.out"; |
| 165 |
my $err = "$name.err"; |
| 166 |
my $null = "/dev/null"; |
| 167 |
|
| 168 |
my $args = ($a{args} || ''); |
| 169 |
my $rsc = store_file(TEST_RSC, $a{resource}, "-r ".TEST_RSC); |
| 170 |
my $bib = store_file(TEST_BIB, $a{bib}, TEST_BIB) if $a{bib}; |
| 171 |
$bib = '' if not $bib; |
| 172 |
|
| 173 |
$ENV{'BIBTOOL'} = $a{BIBTOOL} if $a{BIBTOOL}; |
| 174 |
|
| 175 |
if ($a{stdin}) { |
| 176 |
print "\n$BIBTOOL $args $rsc <$bib 1>$out 2>$err\n" if $a{debug}; |
| 177 |
`$BIBTOOL $args $rsc <$bib 1>$out 2>$err`; |
| 178 |
} else { |
| 179 |
print "\n$BIBTOOL $args $rsc $bib <$null 1>$out 2>$err\n" if $a{debug}; |
| 180 |
`$BIBTOOL $args $rsc $bib <$null 1>$out 2>$err`; |
| 181 |
} |
| 182 |
|
| 183 |
if ( run_check($name, $a{check}) + |
| 184 |
check($a{expected_out}, $out, 'out', $a{fct_out}) + |
| 185 |
check($a{expected_err}, $err, 'err', $a{fct_err}) ) { |
| 186 |
$failure++; |
| 187 |
out "\n\t\t\t\t fail\n" |
| 188 |
} else { |
| 189 |
$success++; |
| 190 |
out "ok\n" |
| 191 |
} |
| 192 |
unlink(TEST_RSC) if -e TEST_RSC; |
| 193 |
unlink(TEST_BIB) if -e TEST_BIB; |
| 194 |
my $post = $a{post}; |
| 195 |
&{$post}($name) if defined $post; |
| 196 |
|
| 197 |
return 1; |
| 198 |
} |
| 199 |
|
| 200 |
#------------------------------------------------------------------------------ |
| 201 |
# Function: store_file |
| 202 |
# |
| 203 |
sub store_file { |
| 204 |
my ($file, $content, $ret) = @_; |
| 205 |
return '' if not defined $content; |
| 206 |
|
| 207 |
local $_ = new FileHandle($file,'w') || die "$file: $!\n"; |
| 208 |
print $_ $content; |
| 209 |
$_->close(); |
| 210 |
return $ret; |
| 211 |
} |
| 212 |
|
| 213 |
#------------------------------------------------------------------------------ |
| 214 |
# Function: run_check |
| 215 |
# |
| 216 |
sub run_check { |
| 217 |
my ($name, $check) = @_; |
| 218 |
return 0 if not defined $check; |
| 219 |
return &{$check}($name); |
| 220 |
} |
| 221 |
|
| 222 |
#------------------------------------------------------------------------------ |
| 223 |
# Function: check |
| 224 |
# |
| 225 |
sub check { |
| 226 |
my ($expected, $file, $type, $fct) = @_; |
| 227 |
|
| 228 |
if (defined $expected) { |
| 229 |
my $fd = new FileHandle($file,'r') || die("$file: $!\n"); |
| 230 |
local $/ = undef; |
| 231 |
local $_ = <$fd>; |
| 232 |
$fd->close(); |
| 233 |
|
| 234 |
$_ = &$fct($_) if defined $fct; |
| 235 |
|
| 236 |
if ($_ ne $expected) { |
| 237 |
my $fd = new FileHandle("$file-expected",'w') || die("$file-expected: $!\n"); |
| 238 |
print $fd $expected; |
| 239 |
$fd->close(); |
| 240 |
print STDERR "\n***\tSee actual $type in $file; " if $verbose; |
| 241 |
return 1; |
| 242 |
} |
| 243 |
} |
| 244 |
unlink $file; |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
#------------------------------------------------------------------------------ |
| 249 |
# Function: all |
| 250 |
# |
| 251 |
sub all { |
| 252 |
suites (glob '*.t */*.t') |
| 253 |
} |
| 254 |
|
| 255 |
#------------------------------------------------------------------------------ |
| 256 |
# Function: suites |
| 257 |
# |
| 258 |
sub suites { |
| 259 |
local $_; |
| 260 |
my @a = sort(@_); |
| 261 |
my $len = 6; |
| 262 |
my $suite; |
| 263 |
|
| 264 |
$ENV{HOME} = getcwd; |
| 265 |
|
| 266 |
die "*** '$BIBTOOL' executable not found\n" if not -x $BIBTOOL; |
| 267 |
die "*** Current dircetory is not writable\n" if not -w '.'; |
| 268 |
die "*** .bibtoolrsc exists in current directory. Aborted\n" |
| 269 |
if -e ".bibtoolrsc"; |
| 270 |
die "*** .bibtoolrsc exists in this directory. Aborted \n" |
| 271 |
if -e "$ENV{HOME}/.bibtoolrsc"; |
| 272 |
|
| 273 |
foreach $_ (@a) { |
| 274 |
my $l = length($_); |
| 275 |
$len = $l if $l > $len; |
| 276 |
} |
| 277 |
|
| 278 |
my $time = gettimeofday; |
| 279 |
|
| 280 |
foreach $_ (@a) { |
| 281 |
$success = 0; |
| 282 |
$ignored = 0; |
| 283 |
$failure = 0; |
| 284 |
$suite = $_; |
| 285 |
$suite =~ s/\.t$//; |
| 286 |
|
| 287 |
my $ret = do "./$_"; |
| 288 |
unless($ret) { |
| 289 |
if ($@) { warn "couldn't parse $_: $@\n" |
| 290 |
} elsif(not defined $ret) { warn "couldn't do $_: $!\n" |
| 291 |
} else { warn "couldn't run $_\n" |
| 292 |
} |
| 293 |
$failure++; |
| 294 |
} |
| 295 |
$summary{$_} = [$success, $ignored, $failure]; |
| 296 |
} |
| 297 |
|
| 298 |
$success = 0; |
| 299 |
$ignored = 0; |
| 300 |
$failure = 0; |
| 301 |
printf("\n%-${len}s success ignored failure\n%s\n", '', |
| 302 |
('-' x ($len + 25))) if $verbose; |
| 303 |
|
| 304 |
foreach $suite (@a) { |
| 305 |
$_ = $suite; |
| 306 |
s/\.t$//; |
| 307 |
printf("%-${len}s%7d %7d %7d\n", $_, |
| 308 |
$summary{$suite}[0], $summary{$suite}[1], $summary{$suite}[2]) |
| 309 |
if $verbose; |
| 310 |
$success += $summary{$suite}[0]; |
| 311 |
$ignored += $summary{$suite}[1]; |
| 312 |
$failure += $summary{$suite}[2];; |
| 313 |
} |
| 314 |
|
| 315 |
$time = gettimeofday - $time; |
| 316 |
|
| 317 |
my $no = $success + $failure; |
| 318 |
if ($no == 0) { $_ = 100 } |
| 319 |
else { $_ = 100. * $success/$no; } |
| 320 |
|
| 321 |
printf("%s\n%-${len}s%7d %7d %7d\nSuccess rate: %3.2f%%\n", |
| 322 |
('_' x ($len + 25)), |
| 323 |
'TOTAL', |
| 324 |
$success, |
| 325 |
$ignored, |
| 326 |
$failure, |
| 327 |
$_) if $verbose; |
| 328 |
printf("Run time: %1.2fs\tAverage: %1.0fms\n\n", |
| 329 |
$time, $time*1000/$no) if $verbose; |
| 330 |
|
| 331 |
return $failure; |
| 332 |
} |
| 333 |
|
| 334 |
#------------------------------------------------------------------------------ |
| 335 |
# Function: get_library_path |
| 336 |
# |
| 337 |
sub get_library_path { |
| 338 |
my $err = '_lib.err'; |
| 339 |
`$BIBTOOL -h 2>$err`; |
| 340 |
|
| 341 |
my $library_path; |
| 342 |
local $_; |
| 343 |
my $fd = new FileHandle($err) || die "$err: $!\n"; |
| 344 |
while(<$fd>) { |
| 345 |
$library_path = $1 if m/^Library path: (.*)/; |
| 346 |
} |
| 347 |
$fd->close(); |
| 348 |
unlink($err); |
| 349 |
return $library_path; |
| 350 |
} |
| 351 |
|
| 352 |
#------------------------------------------------------------------------------ |
| 353 |
# Function: get_configuration_options |
| 354 |
# |
| 355 |
sub get_configuration_options { |
| 356 |
my $err = '_lib.err'; |
| 357 |
`$BIBTOOL -h 2>$err`; |
| 358 |
|
| 359 |
my $options; |
| 360 |
local $_; |
| 361 |
my $fd = new FileHandle($err) || die "$err: $!\n"; |
| 362 |
while(<$fd>) { |
| 363 |
$options = $1 if m/^Special configuration options: (.*)/; |
| 364 |
} |
| 365 |
$fd->close(); |
| 366 |
unlink($err); |
| 367 |
return $options; |
| 368 |
} |
| 369 |
|
| 370 |
#------------------------------------------------------------------------------ |
| 371 |
1; |
| 372 |
# |