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";
46 #Add current directory to path
47 $ENV{PATH} = "$ENV{PATH}:.";
50 ## Command line option handling
53 # option handling helper subs
57 if (-e $directory && -d $directory) {
59 print "[Tesh/INFO] change directory to $directory\n";
61 } elsif (-e $directory) {
62 print "Cannot change directory to '$directory': it is not a directory\n";
64 print "Chdir to $directory failed: No such file or directory\n";
69 print "Test suite `$tesh_file': NOK (system error)\n";
76 if ($_[0] =~ /^(.*)=(.*)$/) {
78 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
81 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
85 print "[Tesh/INFO] setenv $var=$ctn\n";
95 print "[Tesh/INFO] setenv $var=$ctn\n";
100 # Main option parsing sub
103 # remove the tesh file from the ARGV used
105 $tesh_file = pop @ARGV;
107 # temporary arrays for GetOption
119 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
122 'help|h' => \$opt{'help'},
124 'verbose|v' => \@verbose,
125 'debug|d' => \$opt{"debug"},
128 'timeout=s' => \$opt{'timeout'},
129 'setenv=s' => \&setenv_cmd,
132 'enable-coverage+' => \$enable_coverage,
135 if($enable_coverage){
136 print "Enable coverage\n";
139 unless($tesh_file=~/(.*)\.tesh/){
140 $tesh_file="(stdin)";
141 $tesh_name="(stdin)";
142 print "Test suite from stdin\n";
145 print "Test suite `$tesh_name'\n";
148 $opt{'verbose'} = scalar @verbose;
150 $opt{'cfg'} .= " --cfg=$_";
155 my %opts = get_options(@ARGV);
179 if (POSIX::WIFEXITED($status)) {
180 $exitcode=POSIX::WEXITSTATUS($status)+40;
181 return "returned code ".POSIX::WEXITSTATUS($status);
182 } elsif (POSIX::WIFSIGNALED($status)) {
184 if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
185 elsif (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
186 elsif (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
187 elsif (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
188 elsif (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
189 $exitcode=POSIX::WTERMSIG($status)+4;
190 return "got signal $code";
192 return "Unparsable status. Is the process stopped?";
195 #if ($@) { # no POSIX available?
196 # warn "POSIX not usable to parse the return value of forked child: $@\n";
198 # return "returned code -1 $@ ";
204 if ($opts{'debug'}) {
206 map {print " $_"} @{$cmd{'in'}};
209 map {print " $_"} @{$cmd{'out'}};
211 print "CMD: $cmd{'cmd'}\n";
214 # cleanup the command line
216 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
217 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
218 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
221 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
223 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
224 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
225 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
226 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
227 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
228 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
229 # $cmd{'cmd'} =~ s|^\./||g;
230 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
231 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
232 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
234 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
237 # exec the command line
238 ### $line =~ s/\r//g;
240 $cmd{'got'} = IO::File->new_tmpfile;
241 $cmd{'got'}->autoflush(1);
242 local *E = $cmd{'got'};
243 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E", $cmd{'cmd'} );
245 # push all provided input to executing child
246 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
249 # if timeout specified, fork and kill executing child at the end of timeout
250 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
251 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
254 die "fork() failed: $!" unless defined $forked;
255 if ( $forked == 0 ) { # child
257 kill(SIGKILL, $cmd{'pid'});
263 # Cleanup the executing child, and kill the timeouter brother on need
264 $cmd{'return'} = 0 unless defined($cmd{'return'});
265 if($cmd{'background'} != 1){
266 waitpid ($cmd{'pid'}, 0);
267 $cmd{'gotret'} = exit_status($?);
270 # & commands, which will be handled at the end
271 push @bg_cmds, \%cmd;
272 # no timeout for background commands
274 kill(SIGKILL, $forked);
284 my $gotret=$cmd{'gotret'};
288 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
289 $wantret = "got signal $cmd{'expect'}";
291 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
294 local *got = $cmd{'got'};
296 # pop all output from executing child
298 while(defined(my $got=<got>)) {
301 if (!($enable_coverage and $got=~ /^profiling:/)){
310 use sort qw(defaults _quicksort); # force quicksort
311 @got = sort mysort @got;
312 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
313 if(defined($cmd{'out'})){
314 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
318 #Did we timeout ? If yes, handle it. If not, kill the forked process.
320 if($timeout==-1 and $gotret eq "got signal SIGKILL"){
321 $gotret="return code 0";
323 $gotret= "timeout after $time_to_wait sec";
326 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
330 if($gotret ne $wantret) {
332 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
334 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
336 map {$msg .= "|| $_\n"} @got;
339 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
350 # Check the result of execution
353 if (!defined($cmd{'output ignore'})){
354 $diff = build_diff(\@{$cmd{'out'}}, \@got);
356 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
359 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
360 map { print "$_\n" } split(/\n/,$diff);
362 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
370 my $file = $cmd{'arg'};
371 print "[Tesh/INFO] mkfile $file\n";
374 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
375 print FILE join("\n", @{$cmd{'in'}});
376 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
381 #my $teshfile=$tesh_file;
382 #$teshfile=~ s{\.[^.]+$}{};
384 unless($tesh_file eq "(stdin)"){
385 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
388 my %cmd; # everything about the next command to run
391 LINE: while (not $finished and not $error) {
395 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
398 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
407 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
409 # deal with line continuations
410 while ($line =~ /^(.*?)\\$/) {
412 die "[TESH/CRITICAL] Continued line at end of file\n"
413 unless defined($next);
416 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
420 # Push delayed commands on empty lines
421 unless ($line =~ m/^(.)(.*)$/) {
422 if (defined($cmd{'cmd'})) {
429 my ($cmd,$arg) = ($1,$2);
433 # handle the commands
434 if ($cmd =~ /^#/) { #comment
435 } elsif ($cmd eq '>'){ #expected result line
436 print "[TESH/debug] push expected result\n" if $opts{'debug'};
437 push @{$cmd{'out'}}, $arg;
439 } elsif ($cmd eq '<') { # provided input
440 print "[TESH/debug] push provided input\n" if $opts{'debug'};
441 push @{$cmd{'in'}}, $arg;
443 } elsif ($cmd eq 'p') { # comment
444 print "[$tesh_name:$line_num] $arg\n";
446 } elsif ($cmd eq '$') { # Command
447 # if we have something buffered, run it now
448 if (defined($cmd{'cmd'})) {
452 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
453 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
456 $cmd{'arg'} =~ s/\s*mkfile //;
460 } elsif ($arg =~ /^\s*cd /) {
461 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
462 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
468 } else { # regular command
470 $cmd{'file'} = $tesh_file;
471 $cmd{'line'} = $line_num;
474 elsif($cmd eq '&'){ # parallel command line
476 if (defined($cmd{'cmd'})) {
480 $cmd{'background'} = 1;
482 $cmd{'file'} = $tesh_file;
483 $cmd{'line'} = $line_num;
485 elsif($line =~ /^!\s*output sort/){ #output sort
486 if (defined($cmd{'cmd'})) {
492 elsif($line =~ /^!\s*output ignore/){ #output ignore
493 if (defined($cmd{'cmd'})) {
497 $cmd{'output ignore'} = 1;
499 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
500 if (defined($cmd{'cmd'})) {
505 $cmd{'expect'} = "$1";
507 elsif($line =~ /^!\s*expect return/){ #expect return
508 if (defined($cmd{'cmd'})) {
512 $line =~ s/^! expect return //g;
514 $cmd{'return'} = $line;
516 elsif($line =~ /^!\s*setenv/){ #setenv
517 if (defined($cmd{'cmd'})) {
521 $line =~ s/^! setenv //g;
525 elsif($line =~ /^!\s*include/){ #output sort
526 if (defined($cmd{'cmd'})) {
530 print color("red"), "[Tesh/CRITICAL] need include";
531 print color("reset"), "\n";
534 elsif($line =~ /^!\s*timeout/){ #timeout
535 if (defined($cmd{'cmd'})) {
539 $line =~ s/^! timeout //;
541 $cmd{'timeout'} = $line;
543 die "[TESH/CRITICAL] parse error: $line\n";
546 kill(SIGKILL, $forked);
554 # Deal with last command
555 if (defined($cmd{'cmd'})) {
562 kill(SIGKILL, $forked);
568 waitpid ($test{'pid'}, 0);
569 $test{'gotret'} = exit_status($?);
578 }elsif($tesh_file eq "(stdin)"){
579 print "Test suite from stdin OK\n";
581 print "Test suite `$tesh_name' OK\n";
585 #push @a,"bl1"; push @b,"bl1";
586 #push @a,"bl2"; push @b,"bl2";
587 #push @a,"bl3"; push @b,"bl3";
588 #push @a,"bl4"; push @b,"bl4";
589 #push @a,"bl5"; push @b,"bl5";
590 #push @a,"bl6"; push @b,"bl6";
591 #push @a,"bl7"; push @b,"bl7";
592 ##push @a,"Perl"; push @b,"ruby";
593 #push @a,"END1"; push @b,"END1";
594 #push @a,"END2"; push @b,"END2";
595 #push @a,"END3"; push @b,"END3";
596 #push @a,"END4"; push @b,"END4";
597 #push @a,"END5"; push @b,"END5";
598 #push @a,"END6"; push @b,"END6";
599 #push @a,"END7"; push @b,"END7";
600 #print "Identical:\n". build_diff(\@a,\@b);
603 #push @a,"AZE"; push @b,"EZA";
604 #print "Different:\n".build_diff(\@a,\@b);
606 use lib "@CMAKE_BINARY_DIR@/bin" ;
608 use Diff qw(diff); # postpone a bit to have time to change INC
612 my $diff = Diff->new(@_);
614 $diff->Base( 1 ); # Return line numbers, not indices
615 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
616 return "" if ($chunk_count == 1 && $diff->Same());
618 while( $diff->Next() ) {
619 my @same = $diff->Same();
620 if ($diff->Same() ) {
621 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
622 $res .= ' '.$same[0]."\n" ;
623 $res .= ' '.$same[1]."\n" if (scalar @same>1);
625 $res .= "...\n" if (scalar @same>2);
626 # $res .= $diff->Next(0)."/$chunk_count\n";
627 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
628 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
629 $res .= ' '.$same[scalar @same -1]."\n";
632 next if $diff->Same();
633 map { $res .= "- $_\n" } $diff->Items(1);
634 map { $res .= "+ $_\n" } $diff->Items(2);