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;
40 use Getopt::Long qw(GetOptions);
51 $ENV{"PRINTF_EXPONENT_DIGITS"} = "2";
54 #Add current directory to path
55 $ENV{PATH} = "$ENV{PATH}:.";
58 ## Command line option handling
61 # option handling helper subs
65 if (-e $directory && -d $directory) {
67 print "[Tesh/INFO] change directory to $directory\n";
69 } elsif (-e $directory) {
70 print "Cannot change directory to '$directory': it is not a directory\n";
72 print "Chdir to $directory failed: No such file or directory\n";
77 print "Test suite `$tesh_file': NOK (system error)\n";
84 if ($_[0] =~ /^(.*)=(.*)$/) {
86 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
89 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
93 print "[Tesh/INFO] setenv $var=$ctn\n";
103 print "[Tesh/INFO] setenv $var=$ctn\n";
108 # Main option parsing sub
111 # remove the tesh file from the ARGV used
113 $tesh_file = pop @ARGV;
115 # temporary arrays for GetOption
127 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
130 'help|h' => \$opt{'help'},
132 'verbose|v' => \@verbose,
133 'debug|d' => \$opt{"debug"},
136 'timeout=s' => \$opt{'timeout'},
137 'setenv=s' => \&setenv_cmd,
140 'enable-coverage+' => \$enable_coverage,
143 if($enable_coverage){
144 print "Enable coverage\n";
147 unless($tesh_file=~/(.*)\.tesh/){
148 $tesh_file="(stdin)";
149 $tesh_name="(stdin)";
150 print "Test suite from stdin\n";
153 print "Test suite `$tesh_name'\n";
156 $opt{'verbose'} = scalar @verbose;
158 $opt{'cfg'} .= " --cfg=$_";
163 my %opts = get_options(@ARGV);
187 if (POSIX::WIFEXITED($status)) {
188 $exitcode=POSIX::WEXITSTATUS($status)+40;
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
246 ### $line =~ s/\r//g;
248 $cmd{'got'} = IO::File->new_tmpfile;
249 $cmd{'got'}->autoflush(1);
250 local *E = $cmd{'got'};
251 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E", $cmd{'cmd'} );
253 # push all provided input to executing child
254 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
257 # if timeout specified, fork and kill executing child at the end of timeout
258 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
259 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
262 die "fork() failed: $!" unless defined $forked;
263 if ( $forked == 0 ) { # child
265 kill(SIGKILL, $cmd{'pid'});
271 # Cleanup the executing child, and kill the timeouter brother on need
272 $cmd{'return'} = 0 unless defined($cmd{'return'});
273 if($cmd{'background'} != 1){
274 waitpid ($cmd{'pid'}, 0);
275 $cmd{'gotret'} = exit_status($?);
278 # & commands, which will be handled at the end
279 push @bg_cmds, \%cmd;
280 # no timeout for background commands
282 kill(SIGKILL, $forked);
292 my $gotret=$cmd{'gotret'};
296 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
297 $wantret = "got signal $cmd{'expect'}";
299 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
302 local *got = $cmd{'got'};
304 # pop all output from executing child
306 while(defined(my $got=<got>)) {
309 if (!($enable_coverage and $got=~ /^profiling:/)){
316 substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
319 @got = sort mysort @got;
320 while (@got and $got[0] eq "") {
324 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
325 if(defined($cmd{'out'})){
326 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
327 while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
328 shift @{$cmd{'out'}};
333 #Did we timeout ? If yes, handle it. If not, kill the forked process.
335 if($timeout==-1 and $gotret eq "got signal SIGKILL"){
336 $gotret="return code 0";
338 $gotret= "timeout after $time_to_wait sec";
341 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
345 if($gotret ne $wantret) {
347 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
349 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
351 map {$msg .= "|| $_\n"} @got;
354 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
365 # Check the result of execution
368 if (!defined($cmd{'output ignore'})){
369 $diff = build_diff(\@{$cmd{'out'}}, \@got);
371 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
374 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
375 map { print "$_\n" } split(/\n/,$diff);
377 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
385 my $file = $cmd{'arg'};
386 print "[Tesh/INFO] mkfile $file\n";
389 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
390 print FILE join("\n", @{$cmd{'in'}});
391 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
396 #my $teshfile=$tesh_file;
397 #$teshfile=~ s{\.[^.]+$}{};
399 unless($tesh_file eq "(stdin)"){
400 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
403 my %cmd; # everything about the next command to run
406 LINE: while (not $finished and not $error) {
410 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
413 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
422 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
424 # deal with line continuations
425 while ($line =~ /^(.*?)\\$/) {
427 die "[TESH/CRITICAL] Continued line at end of file\n"
428 unless defined($next);
431 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
435 # Push delayed commands on empty lines
436 unless ($line =~ m/^(.)(.*)$/) {
437 if (defined($cmd{'cmd'})) {
444 my ($cmd,$arg) = ($1,$2);
448 # handle the commands
449 if ($cmd =~ /^#/) { #comment
450 } elsif ($cmd eq '>'){ #expected result line
451 print "[TESH/debug] push expected result\n" if $opts{'debug'};
452 push @{$cmd{'out'}}, $arg;
454 } elsif ($cmd eq '<') { # provided input
455 print "[TESH/debug] push provided input\n" if $opts{'debug'};
456 push @{$cmd{'in'}}, $arg;
458 } elsif ($cmd eq 'p') { # comment
459 print "[$tesh_name:$line_num] $arg\n";
461 } elsif ($cmd eq '$') { # Command
462 # if we have something buffered, run it now
463 if (defined($cmd{'cmd'})) {
467 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
468 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
471 $cmd{'arg'} =~ s/\s*mkfile //;
475 } elsif ($arg =~ /^\s*cd /) {
476 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
477 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
483 } else { # regular command
485 $cmd{'file'} = $tesh_file;
486 $cmd{'line'} = $line_num;
489 elsif($cmd eq '&'){ # parallel command line
491 if (defined($cmd{'cmd'})) {
495 $cmd{'background'} = 1;
497 $cmd{'file'} = $tesh_file;
498 $cmd{'line'} = $line_num;
500 elsif($line =~ /^!\s*output sort/){ #output sort
501 if (defined($cmd{'cmd'})) {
506 if ($line =~ /^!\s*output sort\s+(\d+)/) {
510 elsif($line =~ /^!\s*output ignore/){ #output ignore
511 if (defined($cmd{'cmd'})) {
515 $cmd{'output ignore'} = 1;
517 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
518 if (defined($cmd{'cmd'})) {
523 $cmd{'expect'} = "$1";
525 elsif($line =~ /^!\s*expect return/){ #expect return
526 if (defined($cmd{'cmd'})) {
530 $line =~ s/^! expect return //g;
532 $cmd{'return'} = $line;
534 elsif($line =~ /^!\s*setenv/){ #setenv
535 if (defined($cmd{'cmd'})) {
539 $line =~ s/^! setenv //g;
543 elsif($line =~ /^!\s*include/){ #include
544 if (defined($cmd{'cmd'})) {
548 print color("red"), "[Tesh/CRITICAL] need include";
549 print color("reset"), "\n";
552 elsif($line =~ /^!\s*timeout/){ #timeout
553 if (defined($cmd{'cmd'})) {
557 $line =~ s/^! timeout //;
559 $cmd{'timeout'} = $line;
561 die "[TESH/CRITICAL] parse error: $line\n";
564 kill(SIGKILL, $forked);
572 # Deal with last command
573 if (defined($cmd{'cmd'})) {
580 kill(SIGKILL, $forked);
586 waitpid ($test{'pid'}, 0);
587 $test{'gotret'} = exit_status($?);
596 }elsif($tesh_file eq "(stdin)"){
597 print "Test suite from stdin OK\n";
599 print "Test suite `$tesh_name' OK\n";
603 #push @a,"bl1"; push @b,"bl1";
604 #push @a,"bl2"; push @b,"bl2";
605 #push @a,"bl3"; push @b,"bl3";
606 #push @a,"bl4"; push @b,"bl4";
607 #push @a,"bl5"; push @b,"bl5";
608 #push @a,"bl6"; push @b,"bl6";
609 #push @a,"bl7"; push @b,"bl7";
610 ##push @a,"Perl"; push @b,"ruby";
611 #push @a,"END1"; push @b,"END1";
612 #push @a,"END2"; push @b,"END2";
613 #push @a,"END3"; push @b,"END3";
614 #push @a,"END4"; push @b,"END4";
615 #push @a,"END5"; push @b,"END5";
616 #push @a,"END6"; push @b,"END6";
617 #push @a,"END7"; push @b,"END7";
618 #print "Identical:\n". build_diff(\@a,\@b);
621 #push @a,"AZE"; push @b,"EZA";
622 #print "Different:\n".build_diff(\@a,\@b);
624 use lib "@CMAKE_BINARY_DIR@/bin" ;
626 use Diff qw(diff); # postpone a bit to have time to change INC
630 my $diff = Diff->new(@_);
632 $diff->Base( 1 ); # Return line numbers, not indices
633 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
634 return "" if ($chunk_count == 1 && $diff->Same());
636 while( $diff->Next() ) {
637 my @same = $diff->Same();
638 if ($diff->Same() ) {
639 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
640 $res .= ' '.$same[0]."\n" ;
641 $res .= ' '.$same[1]."\n" if (scalar @same>1);
643 $res .= "...\n" if (scalar @same>2);
644 # $res .= $diff->Next(0)."/$chunk_count\n";
645 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
646 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
647 $res .= ' '.$same[scalar @same -1]."\n";
650 next if $diff->Same();
651 map { $res .= "- $_\n" } $diff->Items(1);
652 map { $res .= "+ $_\n" } $diff->Items(2);