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;
31 use Getopt::Long qw(GetOptions);
41 $ENV{"PRINTF_EXPONENT_DIGITS"} = "2";
53 # make sure we received a tesh file
54 scalar @ARGV > 0 || die "Usage:\n tesh [*options*] *tesh_file*\n";
56 #Add current directory to path
57 $ENV{PATH} = "$ENV{PATH}:.";
60 ## Command line option handling
63 # option handling helper subs
67 if (-e $directory && -d $directory) {
69 print "[Tesh/INFO] change directory to $directory\n";
71 } elsif (-e $directory) {
72 print "Cannot change directory to '$directory': it is not a directory\n";
74 print "Chdir to $directory failed: No such file or directory\n";
79 print "Test suite `$tesh_file': NOK (system error)\n";
86 if ($_[0] =~ /^(.*)=(.*)$/) {
88 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
91 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
95 print "[Tesh/INFO] setenv $var=$ctn\n";
100 if($var =~ /srcdir/){
105 print "[Tesh/INFO] setenv $var=$ctn\n";
110 # Main option parsing sub
113 # remove the tesh file from the ARGV used
115 $tesh_file = pop @ARGV;
117 # temporary arrays for GetOption
129 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
132 'help|h' => \$opt{'help'},
134 'verbose|v' => \@verbose,
135 'debug|d' => \$opt{"debug"},
138 'timeout=s' => \$opt{'timeout'},
139 'setenv=s' => \&setenv_cmd,
142 'enable-coverage+' => \$enable_coverage,
145 if($enable_coverage){
146 print "Enable coverage\n";
149 unless($tesh_file=~/(.*)\.tesh/){
150 $tesh_file="(stdin)";
151 $tesh_name="(stdin)";
152 print "Test suite from stdin\n";
155 print "Test suite `$tesh_name'\n";
158 $opt{'verbose'} = scalar @verbose;
160 $opt{'cfg'} .= " --cfg=$_";
165 my %opts = get_options(@ARGV);
189 if (POSIX::WIFEXITED($status)) {
190 return "returned code ".POSIX::WEXITSTATUS($status);
191 } elsif (POSIX::WIFSIGNALED($status)) {
193 if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
194 elsif (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
195 elsif (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
196 elsif (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
197 elsif (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
198 $exitcode=POSIX::WTERMSIG($status)+4;
199 return "got signal $code";
201 return "Unparsable status. Is the process stopped?";
204 #if ($@) { # no POSIX available?
205 # warn "POSIX not usable to parse the return value of forked child: $@\n";
207 # return "returned code -1 $@ ";
213 if ($opts{'debug'}) {
215 map {print " $_"} @{$cmd{'in'}};
218 map {print " $_"} @{$cmd{'out'}};
220 print "CMD: $cmd{'cmd'}\n";
223 # cleanup the command line
225 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
226 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
227 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
230 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
232 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
233 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/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|^\./||g;
239 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
240 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
241 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
243 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
246 # exec the command line
247 ### $line =~ s/\r//g;
248 $pid = open3(\*CHILD_IN, \*OUT, \*OUT, $cmd{'cmd'} );
250 # push all provided input to executing child
251 map { print CHILD_IN "$_\n" } @{$cmd{'in'}};
254 # if timeout specified, fork and kill executing child at the end of timeout
255 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
256 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
259 die "fork() failed: $!" unless defined $forked;
260 if ( $forked == 0 ) { # child
268 # pop all output from executing child
270 while(defined(my $got=<OUT>)) {
276 if (!($enable_coverage and $got=~ /^profiling:/)){
287 use sort qw(defaults _quicksort); # force quicksort
288 @got = sort mysort @got;
289 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
290 if(defined($cmd{'out'})){
291 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
295 # Cleanup the executing child, and kill the timeouter brother on need
296 $cmd{'return'} = 0 unless defined($cmd{'return'});
298 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
299 $wantret = "got signal $cmd{'expect'}";
301 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
306 $gotret = exit_status($?);
307 #Did we timeout ? If yes, handle it. If not, kill the forked process.
309 if($timeout==-1 and $gotret eq "got signal SIGKILL"){
310 $gotret="return code 0";
312 $gotret= "timeout after $time_to_wait sec";
315 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
319 if($gotret ne $wantret) {
321 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
323 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
325 map {$msg .= "|| $_\n"} @got;
328 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
339 # Check the result of execution
342 if (!defined($cmd{'output ignore'})){
343 $diff = build_diff(\@{$cmd{'out'}}, \@got);
345 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
348 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
349 map { print "$_\n" } split(/\n/,$diff);
351 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
359 my $file = $cmd{'arg'};
360 print "[Tesh/INFO] mkfile $file\n";
362 die "[TESH/CRITICAL] no input provided to mkfile\n" unless defined($cmd{'in'}) && scalar @{$cmd{'in'}};
364 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
365 print FILE join("\n", @{$cmd{'in'}});
366 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
371 #my $teshfile=$tesh_file;
372 #$teshfile=~ s{\.[^.]+$}{};
374 unless($tesh_file eq "(stdin)"){
375 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
378 my %cmd; # everything about the next command to run
381 LINE: while (not $finished and not $error) {
385 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
388 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
397 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
399 # deal with line continuations
400 while ($line =~ /^(.*?)\\$/) {
402 die "[TESH/CRITICAL] Continued line at end of file\n"
403 unless defined($next);
406 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
410 # Push delayed commands on empty lines
411 unless ($line =~ m/^(.).(.*)$/) {
412 if (defined($cmd{'cmd'})) {
419 my ($cmd,$arg) = ($1,$2);
422 # handle the commands
423 if ($cmd =~ /^#/) { #comment
424 } elsif ($cmd eq '>'){ #expected result line
425 print "[TESH/debug] push expected result\n" if $opts{'debug'};
428 push @{$cmd{'out'}}, $arg;
431 } elsif ($cmd eq '<') { # provided input
432 print "[TESH/debug] push provided input\n" if $opts{'debug'};
433 push @{$cmd{'in'}}, $arg;
435 } elsif ($cmd eq 'p') { # comment
436 print "[$tesh_name:$line_num] $arg\n";
438 } elsif ($cmd eq '$') { # Command
439 # if we have something buffered, run it now
440 if (defined($cmd{'cmd'})) {
444 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
445 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
448 $cmd{'arg'} =~ s/\s*mkfile //;
452 } elsif ($arg =~ /^\s*cd /) {
453 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
454 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
460 } else { # regular command
462 $cmd{'file'} = $tesh_file;
463 $cmd{'line'} = $line_num;
466 elsif($cmd eq '&'){ # parallel command line
468 if (defined($cmd{'cmd'})) {
472 $cmd{'background'} = 1;
474 $cmd{'file'} = $tesh_file;
475 $cmd{'line'} = $line_num;
477 elsif($line =~ /^!\s*output sort/){ #output sort
478 if (defined($cmd{'cmd'})) {
484 elsif($line =~ /^!\s*output ignore/){ #output ignore
485 if (defined($cmd{'cmd'})) {
489 $cmd{'output ignore'} = 1;
491 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
492 if (defined($cmd{'cmd'})) {
496 $cmd{'expect'} = "$1";
498 elsif($line =~ /^!\s*expect return/){ #expect return
499 if (defined($cmd{'cmd'})) {
503 $line =~ s/^! expect return //g;
505 $cmd{'return'} = $line;
507 elsif($line =~ /^!\s*setenv/){ #setenv
508 if (defined($cmd{'cmd'})) {
512 $line =~ s/^! setenv //g;
516 elsif($line =~ /^!\s*include/){ #output sort
517 if (defined($cmd{'cmd'})) {
521 print color("red"), "[Tesh/CRITICAL] need include";
522 print color("reset"), "\n";
525 elsif($line =~ /^!\s*timeout/){ #timeout
526 if (defined($cmd{'cmd'})) {
530 $line =~ s/^! timeout //;
532 $cmd{'timeout'} = $line;
534 die "[TESH/CRITICAL] parse error: $line\n";
537 kill(SIGKILL, $forked);
545 # Deal with last command
546 if (defined($cmd{'cmd'})) {
553 kill(SIGKILL, $forked);
559 }elsif($tesh_file eq "(stdin)"){
560 print "Test suite from stdin OK\n";
562 print "Test suite `$tesh_name' OK\n";
566 #push @a,"bl1"; push @b,"bl1";
567 #push @a,"bl2"; push @b,"bl2";
568 #push @a,"bl3"; push @b,"bl3";
569 #push @a,"bl4"; push @b,"bl4";
570 #push @a,"bl5"; push @b,"bl5";
571 #push @a,"bl6"; push @b,"bl6";
572 #push @a,"bl7"; push @b,"bl7";
573 ##push @a,"Perl"; push @b,"ruby";
574 #push @a,"END1"; push @b,"END1";
575 #push @a,"END2"; push @b,"END2";
576 #push @a,"END3"; push @b,"END3";
577 #push @a,"END4"; push @b,"END4";
578 #push @a,"END5"; push @b,"END5";
579 #push @a,"END6"; push @b,"END6";
580 #push @a,"END7"; push @b,"END7";
581 #print "Identical:\n". build_diff(\@a,\@b);
584 #push @a,"AZE"; push @b,"EZA";
585 #print "Different:\n".build_diff(\@a,\@b);
587 use lib "@CMAKE_BINARY_DIR@/bin" ;
589 use Diff qw(diff); # postpone a bit to have time to change INC
593 my $diff = Diff->new(@_);
595 $diff->Base( 1 ); # Return line numbers, not indices
596 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
597 return "" if ($chunk_count == 1 && $diff->Same());
599 while( $diff->Next() ) {
600 my @same = $diff->Same();
601 if ($diff->Same() ) {
602 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
603 $res .= ' '.$same[0]."\n" ;
604 $res .= ' '.$same[1]."\n" if (scalar @same>1);
606 $res .= "...\n" if (scalar @same>2);
607 # $res .= $diff->Next(0)."/$chunk_count\n";
608 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
609 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
610 $res .= ' '.$same[scalar @same -1]."\n";
613 next if $diff->Same();
614 map { $res .= "- $_\n" } $diff->Items(1);
615 map { $res .= "+ $_\n" } $diff->Items(2);