2 eval 'exec perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
13 B<tesh> [I<options>] I<tesh_file>
22 my $enable_coverage=0;
32 use Getopt::Long qw(GetOptions);
43 $ENV{"PRINTF_EXPONENT_DIGITS"} = "2";
55 # make sure we received a tesh file
56 scalar @ARGV > 0 || die "Usage:\n tesh [*options*] *tesh_file*\n";
58 #Add current directory to path
59 $ENV{PATH} = "$ENV{PATH}:.";
62 ## Command line option handling
65 # option handling helper subs
69 if (-e $directory && -d $directory) {
71 print "[Tesh/INFO] change directory to $directory\n";
73 } elsif (-e $directory) {
74 print "Cannot change directory to '$directory': it is not a directory\n";
76 print "Chdir to $directory failed: No such file or directory\n";
81 print "Test suite `$tesh_file': NOK (system error)\n";
88 if ($_[0] =~ /^(.*)=(.*)$/) {
90 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
93 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
97 print "[Tesh/INFO] setenv $var=$ctn\n";
102 if($var =~ /srcdir/){
107 print "[Tesh/INFO] setenv $var=$ctn\n";
112 # Main option parsing sub
115 # remove the tesh file from the ARGV used
117 $tesh_file = pop @ARGV;
119 # temporary arrays for GetOption
131 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
134 'help|h' => \$opt{'help'},
136 'verbose|v' => \@verbose,
137 'debug|d' => \$opt{"debug"},
140 'timeout=s' => \$opt{'timeout'},
141 'setenv=s' => \&setenv_cmd,
144 'enable-coverage+' => \$enable_coverage,
147 if($enable_coverage){
148 print "Enable coverage\n";
151 unless($tesh_file=~/(.*)\.tesh/){
152 $tesh_file="(stdin)";
153 $tesh_name="(stdin)";
154 print "Test suite from stdin\n";
157 print "Test suite `$tesh_name'\n";
160 $opt{'verbose'} = scalar @verbose;
162 $opt{'cfg'} .= " --cfg=$_";
167 my %opts = get_options(@ARGV);
191 if (POSIX::WIFEXITED($status)) {
192 $exitcode=POSIX::WEXITSTATUS($status)+40;
193 return "returned code ".POSIX::WEXITSTATUS($status);
194 } elsif (POSIX::WIFSIGNALED($status)) {
196 if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
197 elsif (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
198 elsif (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
199 elsif (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
200 elsif (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
201 $exitcode=POSIX::WTERMSIG($status)+4;
202 return "got signal $code";
204 return "Unparsable status. Is the process stopped?";
207 #if ($@) { # no POSIX available?
208 # warn "POSIX not usable to parse the return value of forked child: $@\n";
210 # return "returned code -1 $@ ";
216 if ($opts{'debug'}) {
218 map {print " $_"} @{$cmd{'in'}};
221 map {print " $_"} @{$cmd{'out'}};
223 print "CMD: $cmd{'cmd'}\n";
226 # cleanup the command line
228 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
229 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
230 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
233 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
235 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
236 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
237 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
238 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
239 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
240 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
241 # $cmd{'cmd'} =~ s|^\./||g;
242 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
243 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
244 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
246 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
249 # exec the command line
250 ### $line =~ s/\r//g;
252 $cmd{'got'} = IO::File->new_tmpfile;
253 $cmd{'got'}->autoflush(1);
254 local *E = $cmd{'got'};
255 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E", $cmd{'cmd'} );
257 # push all provided input to executing child
258 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
261 # if timeout specified, fork and kill executing child at the end of timeout
262 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
263 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
266 die "fork() failed: $!" unless defined $forked;
267 if ( $forked == 0 ) { # child
269 kill(SIGKILL, $cmd{'pid'});
275 # Cleanup the executing child, and kill the timeouter brother on need
276 $cmd{'return'} = 0 unless defined($cmd{'return'});
277 if($cmd{'background'} != 1){
278 waitpid ($cmd{'pid'}, 0);
279 $cmd{'gotret'} = exit_status($?);
282 # & commands, which will be handled at the end
283 push @bg_cmds, \%cmd;
284 # no timeout for background commands
286 kill(SIGKILL, $forked);
296 my $gotret=$cmd{'gotret'};
300 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
301 $wantret = "got signal $cmd{'expect'}";
303 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
306 local *got = $cmd{'got'};
308 # pop all output from executing child
310 while(defined(my $got=<got>)) {
316 if (!($enable_coverage and $got=~ /^profiling:/)){
326 use sort qw(defaults _quicksort); # force quicksort
327 @got = sort mysort @got;
328 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
329 if(defined($cmd{'out'})){
330 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
334 #Did we timeout ? If yes, handle it. If not, kill the forked process.
336 if($timeout==-1 and $gotret eq "got signal SIGKILL"){
337 $gotret="return code 0";
339 $gotret= "timeout after $time_to_wait sec";
342 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
346 if($gotret ne $wantret) {
348 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
350 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
352 map {$msg .= "|| $_\n"} @got;
355 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
366 # Check the result of execution
369 if (!defined($cmd{'output ignore'})){
370 $diff = build_diff(\@{$cmd{'out'}}, \@got);
372 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
375 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
376 map { print "$_\n" } split(/\n/,$diff);
378 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
386 my $file = $cmd{'arg'};
387 print "[Tesh/INFO] mkfile $file\n";
390 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
391 print FILE join("\n", @{$cmd{'in'}});
392 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
397 #my $teshfile=$tesh_file;
398 #$teshfile=~ s{\.[^.]+$}{};
400 unless($tesh_file eq "(stdin)"){
401 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
404 my %cmd; # everything about the next command to run
407 LINE: while (not $finished and not $error) {
411 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
414 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
423 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
425 # deal with line continuations
426 while ($line =~ /^(.*?)\\$/) {
428 die "[TESH/CRITICAL] Continued line at end of file\n"
429 unless defined($next);
432 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
436 # Push delayed commands on empty lines
437 unless ($line =~ m/^(.).(.*)$/) {
438 if (defined($cmd{'cmd'})) {
445 my ($cmd,$arg) = ($1,$2);
448 # handle the commands
449 if ($cmd =~ /^#/) { #comment
450 } elsif ($cmd eq '>'){ #expected result line
451 print "[TESH/debug] push expected result\n" if $opts{'debug'};
454 push @{$cmd{'out'}}, $arg;
457 } elsif ($cmd eq '<') { # provided input
458 print "[TESH/debug] push provided input\n" if $opts{'debug'};
459 push @{$cmd{'in'}}, $arg;
461 } elsif ($cmd eq 'p') { # comment
462 print "[$tesh_name:$line_num] $arg\n";
464 } elsif ($cmd eq '$') { # Command
465 # if we have something buffered, run it now
466 if (defined($cmd{'cmd'})) {
470 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
471 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
474 $cmd{'arg'} =~ s/\s*mkfile //;
478 } elsif ($arg =~ /^\s*cd /) {
479 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
480 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
486 } else { # regular command
488 $cmd{'file'} = $tesh_file;
489 $cmd{'line'} = $line_num;
492 elsif($cmd eq '&'){ # parallel command line
494 if (defined($cmd{'cmd'})) {
498 $cmd{'background'} = 1;
500 $cmd{'file'} = $tesh_file;
501 $cmd{'line'} = $line_num;
503 elsif($line =~ /^!\s*output sort/){ #output sort
504 if (defined($cmd{'cmd'})) {
510 elsif($line =~ /^!\s*output ignore/){ #output ignore
511 if (defined($cmd{'cmd'})) {
515 $cmd{'output ignore'} = 1;
517 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
518 if (defined($cmd{'cmd'})) {
523 $cmd{'expect'} = "$1";
525 elsif($line =~ /^!\s*expect return/){ #expect return
526 if (defined($cmd{'cmd'})) {
530 $line =~ s/^! expect return //g;
532 $cmd{'return'} = $line;
534 elsif($line =~ /^!\s*setenv/){ #setenv
535 if (defined($cmd{'cmd'})) {
539 $line =~ s/^! setenv //g;
543 elsif($line =~ /^!\s*include/){ #output sort
544 if (defined($cmd{'cmd'})) {
548 print color("red"), "[Tesh/CRITICAL] need include";
549 print color("reset"), "\n";
552 elsif($line =~ /^!\s*timeout/){ #timeout
553 if (defined($cmd{'cmd'})) {
557 $line =~ s/^! timeout //;
559 $cmd{'timeout'} = $line;
561 die "[TESH/CRITICAL] parse error: $line\n";
564 kill(SIGKILL, $forked);
572 # Deal with last command
573 if (defined($cmd{'cmd'})) {
580 kill(SIGKILL, $forked);
586 waitpid ($test{'pid'}, 0);
587 $test{'gotret'} = exit_status($?);
596 }elsif($tesh_file eq "(stdin)"){
597 print "Test suite from stdin OK\n";
599 print "Test suite `$tesh_name' OK\n";
603 #push @a,"bl1"; push @b,"bl1";
604 #push @a,"bl2"; push @b,"bl2";
605 #push @a,"bl3"; push @b,"bl3";
606 #push @a,"bl4"; push @b,"bl4";
607 #push @a,"bl5"; push @b,"bl5";
608 #push @a,"bl6"; push @b,"bl6";
609 #push @a,"bl7"; push @b,"bl7";
610 ##push @a,"Perl"; push @b,"ruby";
611 #push @a,"END1"; push @b,"END1";
612 #push @a,"END2"; push @b,"END2";
613 #push @a,"END3"; push @b,"END3";
614 #push @a,"END4"; push @b,"END4";
615 #push @a,"END5"; push @b,"END5";
616 #push @a,"END6"; push @b,"END6";
617 #push @a,"END7"; push @b,"END7";
618 #print "Identical:\n". build_diff(\@a,\@b);
621 #push @a,"AZE"; push @b,"EZA";
622 #print "Different:\n".build_diff(\@a,\@b);
624 use lib "@CMAKE_BINARY_DIR@/bin" ;
626 use Diff qw(diff); # postpone a bit to have time to change INC
630 my $diff = Diff->new(@_);
632 $diff->Base( 1 ); # Return line numbers, not indices
633 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
634 return "" if ($chunk_count == 1 && $diff->Same());
636 while( $diff->Next() ) {
637 my @same = $diff->Same();
638 if ($diff->Same() ) {
639 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
640 $res .= ' '.$same[0]."\n" ;
641 $res .= ' '.$same[1]."\n" if (scalar @same>1);
643 $res .= "...\n" if (scalar @same>2);
644 # $res .= $diff->Next(0)."/$chunk_count\n";
645 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
646 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
647 $res .= ' '.$same[scalar @same -1]."\n";
650 next if $diff->Same();
651 map { $res .= "- $_\n" } $diff->Items(1);
652 map { $res .= "+ $_\n" } $diff->Items(2);