3 # Copyright (c) 2012-2014. The SimGrid Team.
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
9 eval 'exec perl -S $0 ${1+"$@"}'
10 if $running_under_some_shell;
20 B<tesh> [I<options>] I<tesh_file>
29 my $enable_coverage=0;
37 $SIG{'PIPE'} = 'IGNORE';
41 use Getopt::Long qw(GetOptions);
53 $ENV{"PRINTF_EXPONENT_DIGITS"} = "2";
57 ## Command line option handling
61 my ($text, $name, $value) = @_;
63 $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
64 $text =~ s/\$$name(\W|$)/$value$1/g;
67 $text =~ s/\${$name:=([^}]*)}/$1/g;
68 $text =~ s/\${$name}//g;
69 $text =~ s/\$$name(\W|$)/$1/g;
74 # option handling helper subs
78 if (-e $directory && -d $directory) {
80 print "[Tesh/INFO] change directory to $directory\n";
82 } elsif (-e $directory) {
83 print "Cannot change directory to '$directory': it is not a directory\n";
85 print "Chdir to $directory failed: No such file or directory\n";
90 print "Test suite `$tesh_file': NOK (system error)\n";
97 if ($_[0] =~ /^(.*)=(.*)$/) {
99 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
102 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
105 print "[Tesh/INFO] setenv $var=$ctn\n";
106 $environ{$var} = $ctn;
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 $exitcode=POSIX::WEXITSTATUS($status)+40;
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 var_subst($cmd{'cmd'}, "EXEEXT", ".exe");
227 var_subst($cmd{'cmd'}, "EXEEXT", "");
230 # substitute environ variables
231 foreach my $key (keys %environ) {
232 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $key, $environ{$key});
234 # substitute remaining variables, if any
235 while ($cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/) {
236 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
238 while ($cmd{'cmd'} =~ /\$(\w+)/) {
239 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
243 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
246 $cmd{'cmd'} =~ s/^\s+//;
247 $cmd{'cmd'} =~ s/\s+$//;
249 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
252 # exec the command line
253 ### $line =~ s/\r//g;
255 $cmd{'got'} = IO::File->new_tmpfile;
256 $cmd{'got'}->autoflush(1);
257 local *E = $cmd{'got'};
258 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E",
259 quotewords('\s+', 0, $cmd{'cmd'}));
261 # push all provided input to executing child
262 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
265 # if timeout specified, fork and kill executing child at the end of timeout
266 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
267 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
270 die "fork() failed: $!" unless defined $forked;
271 if ( $forked == 0 ) { # child
273 kill(SIGKILL, $cmd{'pid'});
279 # Cleanup the executing child, and kill the timeouter brother on need
280 $cmd{'return'} = 0 unless defined($cmd{'return'});
281 if($cmd{'background'} != 1){
282 waitpid ($cmd{'pid'}, 0);
283 $cmd{'gotret'} = exit_status($?);
286 # & commands, which will be handled at the end
287 push @bg_cmds, \%cmd;
288 # no timeout for background commands
290 kill(SIGKILL, $forked);
300 my $gotret=$cmd{'gotret'};
304 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
305 $wantret = "got signal $cmd{'expect'}";
307 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
310 local *got = $cmd{'got'};
312 # pop all output from executing child
314 while(defined(my $got=<got>)) {
317 if (!($enable_coverage and $got=~ /^profiling:/)){
324 substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
327 @got = sort mysort @got;
328 while (@got and $got[0] eq "") {
332 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
333 if(defined($cmd{'out'})){
334 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
335 while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
336 shift @{$cmd{'out'}};
341 #Did we timeout ? If yes, handle it. If not, kill the forked process.
343 if($timeout==-1 and $gotret eq "got signal SIGKILL"){
344 $gotret="return code 0";
346 $gotret= "timeout after $time_to_wait sec";
349 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
353 if($gotret ne $wantret) {
355 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
357 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
359 map {$msg .= "|| $_\n"} @got;
362 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
373 # Check the result of execution
376 if (defined($cmd{'output display'})){
377 print "[Tesh/INFO] Here is the (ignored) command output:\n";
378 map { print "||$_\n" } @got;
380 elsif (!defined($cmd{'output ignore'})){
381 $diff = build_diff(\@{$cmd{'out'}}, \@got);
383 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
386 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
387 map { print "$_\n" } split(/\n/,$diff);
389 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
397 my $file = $cmd{'arg'};
398 print "[Tesh/INFO] mkfile $file\n";
401 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
402 print FILE join("\n", @{$cmd{'in'}});
403 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
408 #my $teshfile=$tesh_file;
409 #$teshfile=~ s{\.[^.]+$}{};
411 unless($tesh_file eq "(stdin)"){
412 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
415 my %cmd; # everything about the next command to run
418 LINE: while (not $finished and not $error) {
422 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
425 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
434 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
436 # deal with line continuations
437 while ($line =~ /^(.*?)\\$/) {
439 die "[TESH/CRITICAL] Continued line at end of file\n"
440 unless defined($next);
443 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
447 # Push delayed commands on empty lines
448 unless ($line =~ m/^(.)(.*)$/) {
449 if (defined($cmd{'cmd'})) {
456 my ($cmd,$arg) = ($1,$2);
460 # handle the commands
461 if ($cmd =~ /^#/) { #comment
462 } elsif ($cmd eq '>'){ #expected result line
463 print "[TESH/debug] push expected result\n" if $opts{'debug'};
464 push @{$cmd{'out'}}, $arg;
466 } elsif ($cmd eq '<') { # provided input
467 print "[TESH/debug] push provided input\n" if $opts{'debug'};
468 push @{$cmd{'in'}}, $arg;
470 } elsif ($cmd eq 'p') { # comment
471 print "[$tesh_name:$line_num] $arg\n";
473 } elsif ($cmd eq '$') { # Command
474 # if we have something buffered, run it now
475 if (defined($cmd{'cmd'})) {
479 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
480 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
483 $cmd{'arg'} =~ s/\s*mkfile //;
487 } elsif ($arg =~ /^\s*cd /) {
488 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
489 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
495 } else { # regular command
497 $cmd{'file'} = $tesh_file;
498 $cmd{'line'} = $line_num;
501 elsif($cmd eq '&'){ # parallel command line
503 if (defined($cmd{'cmd'})) {
507 $cmd{'background'} = 1;
509 $cmd{'file'} = $tesh_file;
510 $cmd{'line'} = $line_num;
512 elsif($line =~ /^!\s*output sort/){ #output sort
513 if (defined($cmd{'cmd'})) {
518 if ($line =~ /^!\s*output sort\s+(\d+)/) {
522 elsif($line =~ /^!\s*output ignore/){ #output ignore
523 if (defined($cmd{'cmd'})) {
527 $cmd{'output ignore'} = 1;
529 elsif($line =~ /^!\s*output display/){ #output display
530 if (defined($cmd{'cmd'})) {
534 $cmd{'output display'} = 1;
536 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
537 if (defined($cmd{'cmd'})) {
542 $cmd{'expect'} = "$1";
544 elsif($line =~ /^!\s*expect return/){ #expect return
545 if (defined($cmd{'cmd'})) {
549 $line =~ s/^! expect return //g;
551 $cmd{'return'} = $line;
553 elsif($line =~ /^!\s*setenv/){ #setenv
554 if (defined($cmd{'cmd'})) {
558 $line =~ s/^! setenv //g;
562 elsif($line =~ /^!\s*include/){ #include
563 if (defined($cmd{'cmd'})) {
567 print color("red"), "[Tesh/CRITICAL] need include";
568 print color("reset"), "\n";
571 elsif($line =~ /^!\s*timeout/){ #timeout
572 if (defined($cmd{'cmd'})) {
576 $line =~ s/^! timeout //;
578 $cmd{'timeout'} = $line;
580 die "[TESH/CRITICAL] parse error: $line\n";
583 kill(SIGKILL, $forked);
591 # Deal with last command
592 if (defined($cmd{'cmd'})) {
599 kill(SIGKILL, $forked);
605 waitpid ($test{'pid'}, 0);
606 $test{'gotret'} = exit_status($?);
615 }elsif($tesh_file eq "(stdin)"){
616 print "Test suite from stdin OK\n";
618 print "Test suite `$tesh_name' OK\n";
622 #push @a,"bl1"; push @b,"bl1";
623 #push @a,"bl2"; push @b,"bl2";
624 #push @a,"bl3"; push @b,"bl3";
625 #push @a,"bl4"; push @b,"bl4";
626 #push @a,"bl5"; push @b,"bl5";
627 #push @a,"bl6"; push @b,"bl6";
628 #push @a,"bl7"; push @b,"bl7";
629 ##push @a,"Perl"; push @b,"ruby";
630 #push @a,"END1"; push @b,"END1";
631 #push @a,"END2"; push @b,"END2";
632 #push @a,"END3"; push @b,"END3";
633 #push @a,"END4"; push @b,"END4";
634 #push @a,"END5"; push @b,"END5";
635 #push @a,"END6"; push @b,"END6";
636 #push @a,"END7"; push @b,"END7";
637 #print "Identical:\n". build_diff(\@a,\@b);
640 #push @a,"AZE"; push @b,"EZA";
641 #print "Different:\n".build_diff(\@a,\@b);
643 use lib "@CMAKE_BINARY_DIR@/bin" ;
645 use Diff qw(diff); # postpone a bit to have time to change INC
649 my $diff = Diff->new(@_);
651 $diff->Base( 1 ); # Return line numbers, not indices
652 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
653 return "" if ($chunk_count == 1 && $diff->Same());
655 while( $diff->Next() ) {
656 my @same = $diff->Same();
657 if ($diff->Same() ) {
658 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
659 $res .= ' '.$same[0]."\n" ;
660 $res .= ' '.$same[1]."\n" if (scalar @same>1);
662 $res .= "...\n" if (scalar @same>2);
663 # $res .= $diff->Next(0)."/$chunk_count\n";
664 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
665 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
666 $res .= ' '.$same[scalar @same -1]."\n";
669 next if $diff->Same();
670 map { $res .= "- $_\n" } $diff->Items(1);
671 map { $res .= "+ $_\n" } $diff->Items(2);