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;
249 my $e = IO::File->new_tmpfile;
252 $pid = open3(\*CHILD_IN, ">&E", ">&E", $cmd{'cmd'} );
254 # push all provided input to executing child
255 map { print CHILD_IN "$_\n" } @{$cmd{'in'}};
258 # if timeout specified, fork and kill executing child at the end of timeout
259 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
260 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
263 die "fork() failed: $!" unless defined $forked;
264 if ( $forked == 0 ) { # child
272 # Cleanup the executing child, and kill the timeouter brother on need
273 $cmd{'return'} = 0 unless defined($cmd{'return'});
275 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
276 $wantret = "got signal $cmd{'expect'}";
278 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
283 $gotret = exit_status($?);
286 # pop all output from executing child
288 while(defined(my $got=<$e>)) {
294 if (!($enable_coverage and $got=~ /^profiling:/)){
304 use sort qw(defaults _quicksort); # force quicksort
305 @got = sort mysort @got;
306 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
307 if(defined($cmd{'out'})){
308 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
312 #Did we timeout ? If yes, handle it. If not, kill the forked process.
314 if($timeout==-1 and $gotret eq "got signal SIGKILL"){
315 $gotret="return code 0";
317 $gotret= "timeout after $time_to_wait sec";
320 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
324 if($gotret ne $wantret) {
326 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
328 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
330 map {$msg .= "|| $_\n"} @got;
333 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
344 # Check the result of execution
347 if (!defined($cmd{'output ignore'})){
348 $diff = build_diff(\@{$cmd{'out'}}, \@got);
350 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
353 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
354 map { print "$_\n" } split(/\n/,$diff);
356 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
364 my $file = $cmd{'arg'};
365 print "[Tesh/INFO] mkfile $file\n";
367 die "[TESH/CRITICAL] no input provided to mkfile\n" unless defined($cmd{'in'}) && scalar @{$cmd{'in'}};
369 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
370 print FILE join("\n", @{$cmd{'in'}});
371 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
376 #my $teshfile=$tesh_file;
377 #$teshfile=~ s{\.[^.]+$}{};
379 unless($tesh_file eq "(stdin)"){
380 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
383 my %cmd; # everything about the next command to run
386 LINE: while (not $finished and not $error) {
390 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
393 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
402 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
404 # deal with line continuations
405 while ($line =~ /^(.*?)\\$/) {
407 die "[TESH/CRITICAL] Continued line at end of file\n"
408 unless defined($next);
411 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
415 # Push delayed commands on empty lines
416 unless ($line =~ m/^(.).(.*)$/) {
417 if (defined($cmd{'cmd'})) {
424 my ($cmd,$arg) = ($1,$2);
427 # handle the commands
428 if ($cmd =~ /^#/) { #comment
429 } elsif ($cmd eq '>'){ #expected result line
430 print "[TESH/debug] push expected result\n" if $opts{'debug'};
433 push @{$cmd{'out'}}, $arg;
436 } elsif ($cmd eq '<') { # provided input
437 print "[TESH/debug] push provided input\n" if $opts{'debug'};
438 push @{$cmd{'in'}}, $arg;
440 } elsif ($cmd eq 'p') { # comment
441 print "[$tesh_name:$line_num] $arg\n";
443 } elsif ($cmd eq '$') { # Command
444 # if we have something buffered, run it now
445 if (defined($cmd{'cmd'})) {
449 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
450 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
453 $cmd{'arg'} =~ s/\s*mkfile //;
457 } elsif ($arg =~ /^\s*cd /) {
458 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
459 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
465 } else { # regular command
467 $cmd{'file'} = $tesh_file;
468 $cmd{'line'} = $line_num;
471 elsif($cmd eq '&'){ # parallel command line
473 if (defined($cmd{'cmd'})) {
477 $cmd{'background'} = 1;
479 $cmd{'file'} = $tesh_file;
480 $cmd{'line'} = $line_num;
482 elsif($line =~ /^!\s*output sort/){ #output sort
483 if (defined($cmd{'cmd'})) {
489 elsif($line =~ /^!\s*output ignore/){ #output ignore
490 if (defined($cmd{'cmd'})) {
494 $cmd{'output ignore'} = 1;
496 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
497 if (defined($cmd{'cmd'})) {
501 $cmd{'expect'} = "$1";
503 elsif($line =~ /^!\s*expect return/){ #expect return
504 if (defined($cmd{'cmd'})) {
508 $line =~ s/^! expect return //g;
510 $cmd{'return'} = $line;
512 elsif($line =~ /^!\s*setenv/){ #setenv
513 if (defined($cmd{'cmd'})) {
517 $line =~ s/^! setenv //g;
521 elsif($line =~ /^!\s*include/){ #output sort
522 if (defined($cmd{'cmd'})) {
526 print color("red"), "[Tesh/CRITICAL] need include";
527 print color("reset"), "\n";
530 elsif($line =~ /^!\s*timeout/){ #timeout
531 if (defined($cmd{'cmd'})) {
535 $line =~ s/^! timeout //;
537 $cmd{'timeout'} = $line;
539 die "[TESH/CRITICAL] parse error: $line\n";
542 kill(SIGKILL, $forked);
550 # Deal with last command
551 if (defined($cmd{'cmd'})) {
558 kill(SIGKILL, $forked);
564 }elsif($tesh_file eq "(stdin)"){
565 print "Test suite from stdin OK\n";
567 print "Test suite `$tesh_name' OK\n";
571 #push @a,"bl1"; push @b,"bl1";
572 #push @a,"bl2"; push @b,"bl2";
573 #push @a,"bl3"; push @b,"bl3";
574 #push @a,"bl4"; push @b,"bl4";
575 #push @a,"bl5"; push @b,"bl5";
576 #push @a,"bl6"; push @b,"bl6";
577 #push @a,"bl7"; push @b,"bl7";
578 ##push @a,"Perl"; push @b,"ruby";
579 #push @a,"END1"; push @b,"END1";
580 #push @a,"END2"; push @b,"END2";
581 #push @a,"END3"; push @b,"END3";
582 #push @a,"END4"; push @b,"END4";
583 #push @a,"END5"; push @b,"END5";
584 #push @a,"END6"; push @b,"END6";
585 #push @a,"END7"; push @b,"END7";
586 #print "Identical:\n". build_diff(\@a,\@b);
589 #push @a,"AZE"; push @b,"EZA";
590 #print "Different:\n".build_diff(\@a,\@b);
592 use lib "@CMAKE_BINARY_DIR@/bin" ;
594 use Diff qw(diff); # postpone a bit to have time to change INC
598 my $diff = Diff->new(@_);
600 $diff->Base( 1 ); # Return line numbers, not indices
601 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
602 return "" if ($chunk_count == 1 && $diff->Same());
604 while( $diff->Next() ) {
605 my @same = $diff->Same();
606 if ($diff->Same() ) {
607 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
608 $res .= ' '.$same[0]."\n" ;
609 $res .= ' '.$same[1]."\n" if (scalar @same>1);
611 $res .= "...\n" if (scalar @same>2);
612 # $res .= $diff->Next(0)."/$chunk_count\n";
613 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
614 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
615 $res .= ' '.$same[scalar @same -1]."\n";
618 next if $diff->Same();
619 map { $res .= "- $_\n" } $diff->Items(1);
620 map { $res .= "+ $_\n" } $diff->Items(2);