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;
33 use Getopt::Long qw(GetOptions);
44 $ENV{"PRINTF_EXPONENT_DIGITS"} = "2";
47 #Add current directory to path
48 $ENV{PATH} = "$ENV{PATH}:.";
51 ## Command line option handling
54 # option handling helper subs
58 if (-e $directory && -d $directory) {
60 print "[Tesh/INFO] change directory to $directory\n";
62 } elsif (-e $directory) {
63 print "Cannot change directory to '$directory': it is not a directory\n";
65 print "Chdir to $directory failed: No such file or directory\n";
70 print "Test suite `$tesh_file': NOK (system error)\n";
77 if ($_[0] =~ /^(.*)=(.*)$/) {
79 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
82 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
86 print "[Tesh/INFO] setenv $var=$ctn\n";
96 print "[Tesh/INFO] setenv $var=$ctn\n";
101 # Main option parsing sub
104 # remove the tesh file from the ARGV used
106 $tesh_file = pop @ARGV;
108 # temporary arrays for GetOption
120 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
123 'help|h' => \$opt{'help'},
125 'verbose|v' => \@verbose,
126 'debug|d' => \$opt{"debug"},
129 'timeout=s' => \$opt{'timeout'},
130 'setenv=s' => \&setenv_cmd,
133 'enable-coverage+' => \$enable_coverage,
136 if($enable_coverage){
137 print "Enable coverage\n";
140 unless($tesh_file=~/(.*)\.tesh/){
141 $tesh_file="(stdin)";
142 $tesh_name="(stdin)";
143 print "Test suite from stdin\n";
146 print "Test suite `$tesh_name'\n";
149 $opt{'verbose'} = scalar @verbose;
151 $opt{'cfg'} .= " --cfg=$_";
156 my %opts = get_options(@ARGV);
180 if (POSIX::WIFEXITED($status)) {
181 $exitcode=POSIX::WEXITSTATUS($status)+40;
182 return "returned code ".POSIX::WEXITSTATUS($status);
183 } elsif (POSIX::WIFSIGNALED($status)) {
185 if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
186 elsif (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
187 elsif (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
188 elsif (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
189 elsif (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
190 $exitcode=POSIX::WTERMSIG($status)+4;
191 return "got signal $code";
193 return "Unparsable status. Is the process stopped?";
196 #if ($@) { # no POSIX available?
197 # warn "POSIX not usable to parse the return value of forked child: $@\n";
199 # return "returned code -1 $@ ";
205 if ($opts{'debug'}) {
207 map {print " $_"} @{$cmd{'in'}};
210 map {print " $_"} @{$cmd{'out'}};
212 print "CMD: $cmd{'cmd'}\n";
215 # cleanup the command line
217 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
218 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
219 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
222 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
224 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
225 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
226 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
227 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
228 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
229 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
230 # $cmd{'cmd'} =~ s|^\./||g;
231 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
232 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
233 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
235 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
238 # exec the command line
239 ### $line =~ s/\r//g;
241 $cmd{'got'} = IO::File->new_tmpfile;
242 $cmd{'got'}->autoflush(1);
243 local *E = $cmd{'got'};
244 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E", $cmd{'cmd'} );
246 # push all provided input to executing child
247 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
250 # if timeout specified, fork and kill executing child at the end of timeout
251 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
252 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
255 die "fork() failed: $!" unless defined $forked;
256 if ( $forked == 0 ) { # child
258 kill(SIGKILL, $cmd{'pid'});
264 # Cleanup the executing child, and kill the timeouter brother on need
265 $cmd{'return'} = 0 unless defined($cmd{'return'});
266 if($cmd{'background'} != 1){
267 waitpid ($cmd{'pid'}, 0);
268 $cmd{'gotret'} = exit_status($?);
271 # & commands, which will be handled at the end
272 push @bg_cmds, \%cmd;
273 # no timeout for background commands
275 kill(SIGKILL, $forked);
285 my $gotret=$cmd{'gotret'};
289 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
290 $wantret = "got signal $cmd{'expect'}";
292 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
295 local *got = $cmd{'got'};
297 # pop all output from executing child
299 while(defined(my $got=<got>)) {
302 if (!($enable_coverage and $got=~ /^profiling:/)){
309 substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
312 @got = sort mysort @got;
313 while (@got and $got[0] eq "") {
317 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
318 if(defined($cmd{'out'})){
319 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
320 while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
321 shift @{$cmd{'out'}};
326 #Did we timeout ? If yes, handle it. If not, kill the forked process.
328 if($timeout==-1 and $gotret eq "got signal SIGKILL"){
329 $gotret="return code 0";
331 $gotret= "timeout after $time_to_wait sec";
334 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
338 if($gotret ne $wantret) {
340 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
342 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
344 map {$msg .= "|| $_\n"} @got;
347 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
358 # Check the result of execution
361 if (!defined($cmd{'output ignore'})){
362 $diff = build_diff(\@{$cmd{'out'}}, \@got);
364 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
367 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
368 map { print "$_\n" } split(/\n/,$diff);
370 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
378 my $file = $cmd{'arg'};
379 print "[Tesh/INFO] mkfile $file\n";
382 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
383 print FILE join("\n", @{$cmd{'in'}});
384 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
389 #my $teshfile=$tesh_file;
390 #$teshfile=~ s{\.[^.]+$}{};
392 unless($tesh_file eq "(stdin)"){
393 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
396 my %cmd; # everything about the next command to run
399 LINE: while (not $finished and not $error) {
403 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
406 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
415 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
417 # deal with line continuations
418 while ($line =~ /^(.*?)\\$/) {
420 die "[TESH/CRITICAL] Continued line at end of file\n"
421 unless defined($next);
424 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
428 # Push delayed commands on empty lines
429 unless ($line =~ m/^(.)(.*)$/) {
430 if (defined($cmd{'cmd'})) {
437 my ($cmd,$arg) = ($1,$2);
441 # handle the commands
442 if ($cmd =~ /^#/) { #comment
443 } elsif ($cmd eq '>'){ #expected result line
444 print "[TESH/debug] push expected result\n" if $opts{'debug'};
445 push @{$cmd{'out'}}, $arg;
447 } elsif ($cmd eq '<') { # provided input
448 print "[TESH/debug] push provided input\n" if $opts{'debug'};
449 push @{$cmd{'in'}}, $arg;
451 } elsif ($cmd eq 'p') { # comment
452 print "[$tesh_name:$line_num] $arg\n";
454 } elsif ($cmd eq '$') { # Command
455 # if we have something buffered, run it now
456 if (defined($cmd{'cmd'})) {
460 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
461 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
464 $cmd{'arg'} =~ s/\s*mkfile //;
468 } elsif ($arg =~ /^\s*cd /) {
469 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
470 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
476 } else { # regular command
478 $cmd{'file'} = $tesh_file;
479 $cmd{'line'} = $line_num;
482 elsif($cmd eq '&'){ # parallel command line
484 if (defined($cmd{'cmd'})) {
488 $cmd{'background'} = 1;
490 $cmd{'file'} = $tesh_file;
491 $cmd{'line'} = $line_num;
493 elsif($line =~ /^!\s*output sort/){ #output sort
494 if (defined($cmd{'cmd'})) {
499 if ($line =~ /^!\s*output sort\s+(\d+)/) {
503 elsif($line =~ /^!\s*output ignore/){ #output ignore
504 if (defined($cmd{'cmd'})) {
508 $cmd{'output ignore'} = 1;
510 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
511 if (defined($cmd{'cmd'})) {
516 $cmd{'expect'} = "$1";
518 elsif($line =~ /^!\s*expect return/){ #expect return
519 if (defined($cmd{'cmd'})) {
523 $line =~ s/^! expect return //g;
525 $cmd{'return'} = $line;
527 elsif($line =~ /^!\s*setenv/){ #setenv
528 if (defined($cmd{'cmd'})) {
532 $line =~ s/^! setenv //g;
536 elsif($line =~ /^!\s*include/){ #include
537 if (defined($cmd{'cmd'})) {
541 print color("red"), "[Tesh/CRITICAL] need include";
542 print color("reset"), "\n";
545 elsif($line =~ /^!\s*timeout/){ #timeout
546 if (defined($cmd{'cmd'})) {
550 $line =~ s/^! timeout //;
552 $cmd{'timeout'} = $line;
554 die "[TESH/CRITICAL] parse error: $line\n";
557 kill(SIGKILL, $forked);
565 # Deal with last command
566 if (defined($cmd{'cmd'})) {
573 kill(SIGKILL, $forked);
579 waitpid ($test{'pid'}, 0);
580 $test{'gotret'} = exit_status($?);
589 }elsif($tesh_file eq "(stdin)"){
590 print "Test suite from stdin OK\n";
592 print "Test suite `$tesh_name' OK\n";
596 #push @a,"bl1"; push @b,"bl1";
597 #push @a,"bl2"; push @b,"bl2";
598 #push @a,"bl3"; push @b,"bl3";
599 #push @a,"bl4"; push @b,"bl4";
600 #push @a,"bl5"; push @b,"bl5";
601 #push @a,"bl6"; push @b,"bl6";
602 #push @a,"bl7"; push @b,"bl7";
603 ##push @a,"Perl"; push @b,"ruby";
604 #push @a,"END1"; push @b,"END1";
605 #push @a,"END2"; push @b,"END2";
606 #push @a,"END3"; push @b,"END3";
607 #push @a,"END4"; push @b,"END4";
608 #push @a,"END5"; push @b,"END5";
609 #push @a,"END6"; push @b,"END6";
610 #push @a,"END7"; push @b,"END7";
611 #print "Identical:\n". build_diff(\@a,\@b);
614 #push @a,"AZE"; push @b,"EZA";
615 #print "Different:\n".build_diff(\@a,\@b);
617 use lib "@CMAKE_BINARY_DIR@/bin" ;
619 use Diff qw(diff); # postpone a bit to have time to change INC
623 my $diff = Diff->new(@_);
625 $diff->Base( 1 ); # Return line numbers, not indices
626 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
627 return "" if ($chunk_count == 1 && $diff->Same());
629 while( $diff->Next() ) {
630 my @same = $diff->Same();
631 if ($diff->Same() ) {
632 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
633 $res .= ' '.$same[0]."\n" ;
634 $res .= ' '.$same[1]."\n" if (scalar @same>1);
636 $res .= "...\n" if (scalar @same>2);
637 # $res .= $diff->Next(0)."/$chunk_count\n";
638 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
639 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
640 $res .= ' '.$same[scalar @same -1]."\n";
643 next if $diff->Same();
644 map { $res .= "- $_\n" } $diff->Items(1);
645 map { $res .= "+ $_\n" } $diff->Items(2);