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);
42 $ENV{"PRINTF_EXPONENT_DIGITS"} = "2";
54 # make sure we received a tesh file
55 scalar @ARGV > 0 || die "Usage:\n tesh [*options*] *tesh_file*\n";
57 #Add current directory to path
58 $ENV{PATH} = "$ENV{PATH}:.";
61 ## Command line option handling
64 # option handling helper subs
68 if (-e $directory && -d $directory) {
70 print "[Tesh/INFO] change directory to $directory\n";
72 } elsif (-e $directory) {
73 print "Cannot change directory to '$directory': it is not a directory\n";
75 print "Chdir to $directory failed: No such file or directory\n";
80 print "Test suite `$tesh_file': NOK (system error)\n";
87 if ($_[0] =~ /^(.*)=(.*)$/) {
89 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
92 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
96 print "[Tesh/INFO] setenv $var=$ctn\n";
101 if($var =~ /srcdir/){
106 print "[Tesh/INFO] setenv $var=$ctn\n";
111 # Main option parsing sub
114 # remove the tesh file from the ARGV used
116 $tesh_file = pop @ARGV;
118 # temporary arrays for GetOption
130 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
133 'help|h' => \$opt{'help'},
135 'verbose|v' => \@verbose,
136 'debug|d' => \$opt{"debug"},
139 'timeout=s' => \$opt{'timeout'},
140 'setenv=s' => \&setenv_cmd,
143 'enable-coverage+' => \$enable_coverage,
146 if($enable_coverage){
147 print "Enable coverage\n";
150 unless($tesh_file=~/(.*)\.tesh/){
151 $tesh_file="(stdin)";
152 $tesh_name="(stdin)";
153 print "Test suite from stdin\n";
156 print "Test suite `$tesh_name'\n";
159 $opt{'verbose'} = scalar @verbose;
161 $opt{'cfg'} .= " --cfg=$_";
166 my %opts = get_options(@ARGV);
190 if (POSIX::WIFEXITED($status)) {
191 $exitcode=POSIX::WEXITSTATUS($status)+40;
192 return "returned code ".POSIX::WEXITSTATUS($status);
193 } elsif (POSIX::WIFSIGNALED($status)) {
195 if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
196 elsif (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
197 elsif (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
198 elsif (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
199 elsif (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
200 $exitcode=POSIX::WTERMSIG($status)+4;
201 return "got signal $code";
203 return "Unparsable status. Is the process stopped?";
206 #if ($@) { # no POSIX available?
207 # warn "POSIX not usable to parse the return value of forked child: $@\n";
209 # return "returned code -1 $@ ";
215 if ($opts{'debug'}) {
217 map {print " $_"} @{$cmd{'in'}};
220 map {print " $_"} @{$cmd{'out'}};
222 print "CMD: $cmd{'cmd'}\n";
225 # cleanup the command line
227 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
228 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
229 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
232 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
234 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
235 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
236 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
237 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
238 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
239 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
240 # $cmd{'cmd'} =~ s|^\./||g;
241 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
242 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
243 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
245 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
248 # exec the command line
249 ### $line =~ s/\r//g;
251 $cmd{'got'} = IO::File->new_tmpfile;
252 $cmd{'got'}->autoflush(1);
253 local *E = $cmd{'got'};
254 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E", $cmd{'cmd'} );
256 # push all provided input to executing child
257 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
260 # if timeout specified, fork and kill executing child at the end of timeout
261 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
262 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
265 die "fork() failed: $!" unless defined $forked;
266 if ( $forked == 0 ) { # child
268 kill(SIGKILL, $cmd{'pid'});
274 # Cleanup the executing child, and kill the timeouter brother on need
275 $cmd{'return'} = 0 unless defined($cmd{'return'});
276 if($cmd{'background'} != 1){
277 waitpid ($cmd{'pid'}, 0);
278 $cmd{'gotret'} = exit_status($?);
281 # & commands, which will be handled at the end
282 push @bg_cmds, \%cmd;
283 # no timeout for background commands
285 kill(SIGKILL, $forked);
295 my $gotret=$cmd{'gotret'};
299 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
300 $wantret = "got signal $cmd{'expect'}";
302 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
305 local *got = $cmd{'got'};
307 # pop all output from executing child
309 while(defined(my $got=<got>)) {
315 if (!($enable_coverage and $got=~ /^profiling:/)){
325 use sort qw(defaults _quicksort); # force quicksort
326 @got = sort mysort @got;
327 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
328 if(defined($cmd{'out'})){
329 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
333 #Did we timeout ? If yes, handle it. If not, kill the forked process.
335 if($timeout==-1 and $gotret eq "got signal SIGKILL"){
336 $gotret="return code 0";
338 $gotret= "timeout after $time_to_wait sec";
341 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
345 if($gotret ne $wantret) {
347 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
349 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
351 map {$msg .= "|| $_\n"} @got;
354 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
365 # Check the result of execution
368 if (!defined($cmd{'output ignore'})){
369 $diff = build_diff(\@{$cmd{'out'}}, \@got);
371 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
374 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
375 map { print "$_\n" } split(/\n/,$diff);
377 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
385 my $file = $cmd{'arg'};
386 print "[Tesh/INFO] mkfile $file\n";
389 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
390 print FILE join("\n", @{$cmd{'in'}});
391 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
396 #my $teshfile=$tesh_file;
397 #$teshfile=~ s{\.[^.]+$}{};
399 unless($tesh_file eq "(stdin)"){
400 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
403 my %cmd; # everything about the next command to run
406 LINE: while (not $finished and not $error) {
410 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
413 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
422 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
424 # deal with line continuations
425 while ($line =~ /^(.*?)\\$/) {
427 die "[TESH/CRITICAL] Continued line at end of file\n"
428 unless defined($next);
431 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
435 # Push delayed commands on empty lines
436 unless ($line =~ m/^(.).(.*)$/) {
437 if (defined($cmd{'cmd'})) {
444 my ($cmd,$arg) = ($1,$2);
447 # handle the commands
448 if ($cmd =~ /^#/) { #comment
449 } elsif ($cmd eq '>'){ #expected result line
450 print "[TESH/debug] push expected result\n" if $opts{'debug'};
453 push @{$cmd{'out'}}, $arg;
456 } elsif ($cmd eq '<') { # provided input
457 print "[TESH/debug] push provided input\n" if $opts{'debug'};
458 push @{$cmd{'in'}}, $arg;
460 } elsif ($cmd eq 'p') { # comment
461 print "[$tesh_name:$line_num] $arg\n";
463 } elsif ($cmd eq '$') { # Command
464 # if we have something buffered, run it now
465 if (defined($cmd{'cmd'})) {
469 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
470 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
473 $cmd{'arg'} =~ s/\s*mkfile //;
477 } elsif ($arg =~ /^\s*cd /) {
478 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
479 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
485 } else { # regular command
487 $cmd{'file'} = $tesh_file;
488 $cmd{'line'} = $line_num;
491 elsif($cmd eq '&'){ # parallel command line
493 if (defined($cmd{'cmd'})) {
497 $cmd{'background'} = 1;
499 $cmd{'file'} = $tesh_file;
500 $cmd{'line'} = $line_num;
502 elsif($line =~ /^!\s*output sort/){ #output sort
503 if (defined($cmd{'cmd'})) {
509 elsif($line =~ /^!\s*output ignore/){ #output ignore
510 if (defined($cmd{'cmd'})) {
514 $cmd{'output ignore'} = 1;
516 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
517 if (defined($cmd{'cmd'})) {
522 $cmd{'expect'} = "$1";
524 elsif($line =~ /^!\s*expect return/){ #expect return
525 if (defined($cmd{'cmd'})) {
529 $line =~ s/^! expect return //g;
531 $cmd{'return'} = $line;
533 elsif($line =~ /^!\s*setenv/){ #setenv
534 if (defined($cmd{'cmd'})) {
538 $line =~ s/^! setenv //g;
542 elsif($line =~ /^!\s*include/){ #output sort
543 if (defined($cmd{'cmd'})) {
547 print color("red"), "[Tesh/CRITICAL] need include";
548 print color("reset"), "\n";
551 elsif($line =~ /^!\s*timeout/){ #timeout
552 if (defined($cmd{'cmd'})) {
556 $line =~ s/^! timeout //;
558 $cmd{'timeout'} = $line;
560 die "[TESH/CRITICAL] parse error: $line\n";
563 kill(SIGKILL, $forked);
571 # Deal with last command
572 if (defined($cmd{'cmd'})) {
579 kill(SIGKILL, $forked);
585 waitpid ($test{'pid'}, 0);
586 $test{'gotret'} = exit_status($?);
595 }elsif($tesh_file eq "(stdin)"){
596 print "Test suite from stdin OK\n";
598 print "Test suite `$tesh_name' OK\n";
602 #push @a,"bl1"; push @b,"bl1";
603 #push @a,"bl2"; push @b,"bl2";
604 #push @a,"bl3"; push @b,"bl3";
605 #push @a,"bl4"; push @b,"bl4";
606 #push @a,"bl5"; push @b,"bl5";
607 #push @a,"bl6"; push @b,"bl6";
608 #push @a,"bl7"; push @b,"bl7";
609 ##push @a,"Perl"; push @b,"ruby";
610 #push @a,"END1"; push @b,"END1";
611 #push @a,"END2"; push @b,"END2";
612 #push @a,"END3"; push @b,"END3";
613 #push @a,"END4"; push @b,"END4";
614 #push @a,"END5"; push @b,"END5";
615 #push @a,"END6"; push @b,"END6";
616 #push @a,"END7"; push @b,"END7";
617 #print "Identical:\n". build_diff(\@a,\@b);
620 #push @a,"AZE"; push @b,"EZA";
621 #print "Different:\n".build_diff(\@a,\@b);
623 use lib "@CMAKE_BINARY_DIR@/bin" ;
625 use Diff qw(diff); # postpone a bit to have time to change INC
629 my $diff = Diff->new(@_);
631 $diff->Base( 1 ); # Return line numbers, not indices
632 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
633 return "" if ($chunk_count == 1 && $diff->Same());
635 while( $diff->Next() ) {
636 my @same = $diff->Same();
637 if ($diff->Same() ) {
638 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
639 $res .= ' '.$same[0]."\n" ;
640 $res .= ' '.$same[1]."\n" if (scalar @same>1);
642 $res .= "...\n" if (scalar @same>2);
643 # $res .= $diff->Next(0)."/$chunk_count\n";
644 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
645 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
646 $res .= ' '.$same[scalar @same -1]."\n";
649 next if $diff->Same();
650 map { $res .= "- $_\n" } $diff->Items(1);
651 map { $res .= "+ $_\n" } $diff->Items(2);