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);
52 # make sure we received a tesh file
53 scalar @ARGV > 0 || die "Usage:\n tesh [*options*] *tesh_file*\n";
55 #Add current directory to path
56 $ENV{PATH} = "$ENV{PATH}:.";
59 ## Command line option handling
62 # option handling helper subs
66 if (-e $directory && -d $directory) {
68 print "[Tesh/INFO] change directory to $directory\n";
70 } elsif (-e $directory) {
71 print "Cannot change directory to '$directory': it is not a directory\n";
73 print "Chdir to $directory failed: No such file or directory\n";
78 print "Test suite `$tesh_file': NOK (system error)\n";
85 if ($_[0] =~ /^(.*)=(.*)$/) {
87 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
90 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
94 print "[Tesh/INFO] setenv $var=$ctn\n";
104 print "[Tesh/INFO] setenv $var=$ctn\n";
109 # Main option parsing sub
112 # remove the tesh file from the ARGV used
114 $tesh_file = pop @ARGV;
116 # temporary arrays for GetOption
128 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
131 'help|h' => \$opt{'help'},
133 'verbose|v' => \@verbose,
134 'debug|d' => \$opt{"debug"},
137 'timeout=s' => \$opt{'timeout'},
138 'setenv=s' => \&setenv_cmd,
141 'enable-coverage+' => \$enable_coverage,
144 if($enable_coverage){
145 print "Enable coverage\n";
148 unless($tesh_file=~/(.*)\.tesh/){
149 $tesh_file="(stdin)";
150 $tesh_name="(stdin)";
151 print "Test suite from stdin\n";
154 print "Test suite `$tesh_name'\n";
157 $opt{'verbose'} = scalar @verbose;
159 $opt{'cfg'} .= " --cfg=$_";
164 my %opts = get_options(@ARGV);
188 if (POSIX::WIFEXITED($status)) {
189 return "returned code ".POSIX::WEXITSTATUS($status);
190 } elsif (POSIX::WIFSIGNALED($status)) {
192 if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
193 elsif (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
194 elsif (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
195 elsif (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
196 elsif (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
197 $exitcode=POSIX::WTERMSIG($status)+4;
198 return "got signal $code";
200 return "Unparsable status. Is the process stopped?";
203 #if ($@) { # no POSIX available?
204 # warn "POSIX not usable to parse the return value of forked child: $@\n";
206 # return "returned code -1 $@ ";
212 if ($opts{'debug'}) {
214 map {print " $_"} @{$cmd{'in'}};
217 map {print " $_"} @{$cmd{'out'}};
219 print "CMD: $cmd{'cmd'}\n";
222 # cleanup the command line
224 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
225 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
226 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
229 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
231 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
232 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
233 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
234 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
235 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
236 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
237 # $cmd{'cmd'} =~ s|^\./||g;
238 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
239 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
240 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
242 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
245 # exec the command line
247 $pid = open3(\*CHILD_IN, \*OUT, \*OUT, $cmd{'cmd'} );
249 # push all provided input to executing child
250 map { print CHILD_IN "$_\n" } @{$cmd{'in'}};
253 # if timeout specified, fork and kill executing child at the end of timeout
254 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
255 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
258 die "fork() failed: $!" unless defined $forked;
259 if ( $forked == 0 ) { # child
267 # pop all output from executing child
269 while(defined(my $got=<OUT>)) {
275 if (!($enable_coverage and $got=~ /^profiling:/)){
286 use sort qw(defaults _quicksort); # force quicksort
287 @got = sort mysort @got;
288 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
289 if(defined($cmd{'out'})){
290 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
294 # Cleanup the executing child, and kill the timeouter brother on need
295 $cmd{'return'} = 0 unless defined($cmd{'return'});
297 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
298 $wantret = "got signal $cmd{'expect'}";
300 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
305 $gotret = exit_status($?);
306 #Did we timeout ? If yes, handle it. If not, kill the forked process.
308 if($timeout==-1 and $gotret eq "got signal SIGKILL"){
309 $gotret="return code 0";
311 $gotret= "timeout after $time_to_wait sec";
314 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
318 if($gotret ne $wantret) {
320 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
322 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
324 map {$msg .= "|| $_\n"} @got;
327 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
338 # Check the result of execution
341 if (!defined($cmd{'output ignore'})){
342 $diff = build_diff(\@{$cmd{'out'}}, \@got);
344 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
347 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
348 map { print "$_\n" } split(/\n/,$diff);
350 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
358 my $file = $cmd{'arg'};
359 print "[Tesh/INFO] mkfile $file\n";
361 die "[TESH/CRITICAL] no input provided to mkfile\n" unless defined($cmd{'in'}) && scalar @{$cmd{'in'}};
363 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
364 print FILE join("\n", @{$cmd{'in'}});
365 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
370 #my $teshfile=$tesh_file;
371 #$teshfile=~ s{\.[^.]+$}{};
373 unless($tesh_file eq "(stdin)"){
374 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
377 my %cmd; # everything about the next command to run
380 LINE: while (not $finished and not $error) {
384 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
387 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
395 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
397 # deal with line continuations
398 while ($line =~ /^(.*?)\\$/) {
400 die "[TESH/CRITICAL] Continued line at end of file\n"
401 unless defined($next);
404 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
408 # Push delayed commands on empty lines
409 unless ($line =~ m/^(.).(.*)$/) {
410 if (defined($cmd{'cmd'})) {
417 my ($cmd,$arg) = ($1,$2);
420 # handle the commands
421 if ($cmd =~ /^#/) { #comment
422 } elsif ($cmd eq '>'){ #expected result line
423 print "[TESH/debug] push expected result\n" if $opts{'debug'};
426 push @{$cmd{'out'}}, $arg;
429 } elsif ($cmd eq '<') { # provided input
430 print "[TESH/debug] push provided input\n" if $opts{'debug'};
431 push @{$cmd{'in'}}, $arg;
433 } elsif ($cmd eq 'p') { # comment
434 print "[$tesh_name:$line_num] $arg\n";
436 } elsif ($cmd eq '$') { # Command
437 # if we have something buffered, run it now
438 if (defined($cmd{'cmd'})) {
442 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
443 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
446 $cmd{'arg'} =~ s/\s*mkfile //;
450 } elsif ($arg =~ /^\s*cd /) {
451 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
452 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
458 } else { # regular command
460 $cmd{'file'} = $tesh_file;
461 $cmd{'line'} = $line_num;
464 elsif($cmd eq '&'){ # parallel command line
466 if (defined($cmd{'cmd'})) {
470 $cmd{'background'} = 1;
472 $cmd{'file'} = $tesh_file;
473 $cmd{'line'} = $line_num;
475 elsif($line =~ /^!\s*output sort/){ #output sort
476 if (defined($cmd{'cmd'})) {
482 elsif($line =~ /^!\s*output ignore/){ #output ignore
483 if (defined($cmd{'cmd'})) {
487 $cmd{'output ignore'} = 1;
489 elsif($line =~ /^!\s*expect signal (\w*)$/) {#expect signal SIGABRT
490 if (defined($cmd{'cmd'})) {
494 $cmd{'expect'} = "$1";
496 elsif($line =~ /^!\s*expect return/){ #expect return
497 if (defined($cmd{'cmd'})) {
501 $line =~ s/^! expect return //g;
503 $cmd{'return'} = $line;
505 elsif($line =~ /^!\s*setenv/){ #setenv
506 if (defined($cmd{'cmd'})) {
510 $line =~ s/^! setenv //g;
514 elsif($line =~ /^!\s*include/){ #output sort
515 if (defined($cmd{'cmd'})) {
519 print color("red"), "[Tesh/CRITICAL] need include";
520 print color("reset"), "\n";
523 elsif($line =~ /^!\s*timeout/){ #timeout
524 if (defined($cmd{'cmd'})) {
528 $line =~ s/^! timeout //;
530 $cmd{'timeout'} = $line;
532 die "[TESH/CRITICAL] parse error: $line\n";
543 # Deal with last command
544 if (defined($cmd{'cmd'})) {
557 }elsif($tesh_file eq "(stdin)"){
558 print "Test suite from stdin OK\n";
560 print "Test suite `$tesh_name' OK\n";
564 #push @a,"bl1"; push @b,"bl1";
565 #push @a,"bl2"; push @b,"bl2";
566 #push @a,"bl3"; push @b,"bl3";
567 #push @a,"bl4"; push @b,"bl4";
568 #push @a,"bl5"; push @b,"bl5";
569 #push @a,"bl6"; push @b,"bl6";
570 #push @a,"bl7"; push @b,"bl7";
571 ##push @a,"Perl"; push @b,"ruby";
572 #push @a,"END1"; push @b,"END1";
573 #push @a,"END2"; push @b,"END2";
574 #push @a,"END3"; push @b,"END3";
575 #push @a,"END4"; push @b,"END4";
576 #push @a,"END5"; push @b,"END5";
577 #push @a,"END6"; push @b,"END6";
578 #push @a,"END7"; push @b,"END7";
579 #print "Identical:\n". build_diff(\@a,\@b);
582 #push @a,"AZE"; push @b,"EZA";
583 #print "Different:\n".build_diff(\@a,\@b);
585 use lib "@CMAKE_BINARY_DIR@/bin" ;
587 use Diff qw(diff); # postpone a bit to have time to change INC
591 my $diff = Diff->new(@_);
593 $diff->Base( 1 ); # Return line numbers, not indices
594 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
595 return "" if ($chunk_count == 1 && $diff->Same());
597 while( $diff->Next() ) {
598 my @same = $diff->Same();
599 if ($diff->Same() ) {
600 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
601 $res .= ' '.$same[0]."\n" ;
602 $res .= ' '.$same[1]."\n" if (scalar @same>1);
604 $res .= "...\n" if (scalar @same>2);
605 # $res .= $diff->Next(0)."/$chunk_count\n";
606 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
607 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
608 $res .= ' '.$same[scalar @same -1]."\n";
611 next if $diff->Same();
612 map { $res .= "- $_\n" } $diff->Items(1);
613 map { $res .= "+ $_\n" } $diff->Items(2);