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.
8 eval 'exec perl -S $0 ${1+"$@"}'
9 if $running_under_some_shell;
19 B<tesh> [I<options>] I<tesh_file>
27 my $enable_coverage=0;
29 my $diff_tool_tmp_fh=0;
30 my $diff_tool_tmp_filename=0;
38 $SIG{'PIPE'} = 'IGNORE';
42 use Getopt::Long qw(GetOptions);
51 ## Portability bits for windows
54 use constant RUNNING_ON_WINDOWS => ($^O =~ /^(?:mswin|dos|os2)/oi);
55 use POSIX qw(:sys_wait_h WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG
56 :signal_h SIGINT SIGTERM SIGKILL SIGABRT SIGSEGV);
57 # These are not implemented on windows (see bug 6798 and 6470)
59 if (RUNNING_ON_WINDOWS) {
60 *WIFEXITED = sub { not $_[0] & 127 };
61 *WEXITSTATUS = sub { $_[0] >> 8 };
62 *WIFSIGNALED = sub { ($_[0] & 127) && ($_[0] & 127 != 127) };
63 *WTERMSIG = sub { $_[0] & 127 };
69 ## Command line option handling
72 if ($ARGV[0] eq "--internal-killer-process") {
73 # We fork+exec a waiter process in charge of killing the command after timeout
74 # If the command stops earlier, that's fine: the killer sends a signal to an already stopped process, fails, and quits.
75 # Nobody cares about the killer issues.
76 # The only problem could arise if another process is given the same PID than cmd. We bet it won't happen :)
77 my $time_to_wait = $ARGV[1];
88 my ($text, $name, $value) = @_;
90 $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
91 $text =~ s/\$$name(\W|$)/$value$1/g;
94 $text =~ s/\${$name:=([^}]*)}/$1/g;
95 $text =~ s/\${$name}//g;
96 $text =~ s/\$$name(\W|$)/$1/g;
101 # option handling helper subs
105 if (-e $directory && -d $directory) {
107 print "[Tesh/INFO] change directory to $directory\n";
109 } elsif (-e $directory) {
110 print "Cannot change directory to '$directory': it is not a directory\n";
112 print "Chdir to $directory failed: No such file or directory\n";
117 print "Test suite `$tesh_file': NOK (system error)\n";
124 if ($_[0] =~ /^(.*)=(.*)$/) {
126 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
129 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
132 print "[Tesh/INFO] setenv $var=$ctn\n";
133 $environ{$var} = $ctn;
136 # Main option parsing sub
139 # remove the tesh file from the ARGV used
141 $tesh_file = pop @ARGV;
143 # temporary arrays for GetOption
155 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
158 'help|h' => \$opt{'help'},
160 'verbose|v' => \@verbose,
161 'debug|d' => \$opt{"debug"},
163 'difftool=s' => \$diff_tool,
166 'timeout=s' => \$opt{'timeout'},
167 'setenv=s' => \&setenv_cmd,
170 'enable-coverage+' => \$enable_coverage,
173 if($enable_coverage){
174 print "Enable coverage\n";
178 use File::Temp qw/ tempfile /;
179 ($diff_tool_tmp_fh, $diff_tool_tmp_filename) = tempfile();
180 print "New tesh: $diff_tool_tmp_filename\n";
183 unless($tesh_file=~/(.*)\.tesh/){
184 $tesh_file="(stdin)";
185 $tesh_name="(stdin)";
186 print "Test suite from stdin\n";
189 print "Test suite `$tesh_name'\n";
192 $opt{'verbose'} = scalar @verbose;
194 $opt{'cfg'} .= " --cfg=$_";
199 my %opts = get_options(@ARGV);
218 ###########################################################################
222 if (WIFEXITED($status)) {
223 $exitcode=WEXITSTATUS($status)+40;
224 return "returned code ".WEXITSTATUS($status);
225 } elsif (WIFSIGNALED($status)) {
227 if (WTERMSIG($status) == SIGINT){$code="SIGINT"; }
228 elsif (WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
229 elsif (WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
230 elsif (WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
231 elsif (WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
232 $exitcode=WTERMSIG($status)+4;
233 return "got signal $code";
235 return "Unparsable status. Is the process stopped?";
240 if ($opts{'debug'}) {
242 map {print " $_"} @{$cmd{'in'}};
245 map {print " $_"} @{$cmd{'out'}};
247 print "CMD: $cmd{'cmd'}\n";
250 # cleanup the command line
251 if(RUNNING_ON_WINDOWS) {
252 var_subst($cmd{'cmd'}, "EXEEXT", ".exe");
254 var_subst($cmd{'cmd'}, "EXEEXT", "");
257 # substitute environ variables
258 foreach my $key (keys %environ) {
259 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $key, $environ{$key});
261 # substitute remaining variables, if any
262 while ($cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/) {
263 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
265 while ($cmd{'cmd'} =~ /\$(\w+)/) {
266 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
270 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
273 $cmd{'cmd'} =~ s/^\s+//;
274 $cmd{'cmd'} =~ s/\s+$//;
276 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
279 # exec the command line
280 ### $line =~ s/\r//g;
282 $cmd{'got'} = IO::File->new_tmpfile;
283 $cmd{'got'}->autoflush(1);
284 local *E = $cmd{'got'};
285 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E",
286 quotewords('\s+', 0, $cmd{'cmd'}));
288 # push all provided input to executing child
289 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
292 # if timeout specified, fork and kill executing child at the end of timeout
293 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
294 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
297 die "fork() failed: $!" unless defined $forked;
298 if ( $forked == 0 ) { # child
299 exec("$PROGRAM_NAME --internal-killer-process $time_to_wait $cmd{'pid'}");
303 # Cleanup the executing child, and kill the timeouter brother on need
304 $cmd{'return'} = 0 unless defined($cmd{'return'});
305 if($cmd{'background'} != 1){
306 waitpid ($cmd{'pid'}, 0);
307 $cmd{'gotret'} = exit_status($?);
310 # & commands, which will be handled at the end
311 push @bg_cmds, \%cmd;
312 # no timeout for background commands
314 kill('KILL', $forked);
324 my $gotret=$cmd{'gotret'};
328 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
329 $wantret = "got signal $cmd{'expect'}";
331 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
334 local *got = $cmd{'got'};
336 # pop all output from executing child
338 while(defined(my $got=<got>)) {
341 print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
343 if (!($enable_coverage and $got=~ /^profiling:/)){
349 # Save the unsorted observed output to report it on error.
350 map { push @{$cmd{'unsorted got'}}, $_ } @got;
353 substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
356 if ($sort_prefix>0) {
357 @got = sort mysort @got;
361 while (@got and $got[0] eq "") {
365 # Sort the expected output to make it easier to write for humans
366 if(defined($cmd{'out'})){
367 if ($sort_prefix>0) {
368 @{$cmd{'out'}} = sort mysort @{$cmd{'out'}};
370 @{$cmd{'out'}} = sort @{$cmd{'out'}};
372 while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
373 shift @{$cmd{'out'}};
378 # Did we timeout ? If yes, handle it. If not, kill the forked process.
380 if($timeout==-1 and ($gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL")){
381 $gotret="return code 0";
383 $gotret= "timeout after $time_to_wait sec";
386 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
390 if($gotret ne $wantret) {
392 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
394 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
396 map {$msg .= "|| $_\n"} @got;
399 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
410 # Check the result of execution
413 if (defined($cmd{'output display'})){
414 print "[Tesh/INFO] Here is the (ignored) command output:\n";
415 map { print "||$_\n" } @got;
417 elsif (!defined($cmd{'output ignore'})){
418 $diff = build_diff(\@{$cmd{'out'}}, \@got);
420 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
423 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
424 map { print "$_\n" } split(/\n/,$diff);
426 print "WARNING: Both the observed output and expected output were sorted as requested.\n";
427 print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
429 print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
430 # print "----8<--------------- Begin of unprocessed observed output (as it should appear in file):\n";
431 # map {print "> $_\n"} @{$cmd{'unsorted got'}};
432 # print "--------------->8---- End of the unprocessed observed output.\n";
435 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
443 my $file = $cmd{'arg'};
444 print "[Tesh/INFO] mkfile $file\n";
447 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
448 print FILE join("\n", @{$cmd{'in'}});
449 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
454 #my $teshfile=$tesh_file;
455 #$teshfile=~ s{\.[^.]+$}{};
457 unless($tesh_file eq "(stdin)"){
458 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
461 my %cmd; # everything about the next command to run
464 LINE: while (not $finished and not $error) {
468 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
471 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
479 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
481 # deal with line continuations
482 while ($line =~ /^(.*?)\\$/) {
484 die "[TESH/CRITICAL] Continued line at end of file\n"
485 unless defined($next);
488 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
492 # Push delayed commands on empty lines
493 unless ($line =~ m/^(.)(.*)$/) {
494 if (defined($cmd{'cmd'})) {
498 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
502 my ($cmd,$arg) = ($1,$2);
503 print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
507 # handle the commands
508 if ($cmd =~ /^#/) { #comment
509 } elsif ($cmd eq '>'){ #expected result line
510 print "[TESH/debug] push expected result\n" if $opts{'debug'};
511 push @{$cmd{'out'}}, $arg;
513 } elsif ($cmd eq '<') { # provided input
514 print "[TESH/debug] push provided input\n" if $opts{'debug'};
515 push @{$cmd{'in'}}, $arg;
517 } elsif ($cmd eq 'p') { # comment
518 print "[$tesh_name:$line_num] $arg\n";
520 } elsif ($cmd eq '$') { # Command
521 # if we have something buffered, run it now
522 if (defined($cmd{'cmd'})) {
526 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
527 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
530 $cmd{'arg'} =~ s/\s*mkfile //;
534 } elsif ($arg =~ /^\s*cd /) {
535 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
536 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
542 } else { # regular command
544 $cmd{'file'} = $tesh_file;
545 $cmd{'line'} = $line_num;
548 elsif($cmd eq '&'){ # parallel command line
550 if (defined($cmd{'cmd'})) {
554 $cmd{'background'} = 1;
556 $cmd{'file'} = $tesh_file;
557 $cmd{'line'} = $line_num;
559 elsif($line =~ /^!\s*output sort/){ #output sort
560 if (defined($cmd{'cmd'})) {
565 if ($line =~ /^!\s*output sort\s+(\d+)/) {
569 elsif($line =~ /^!\s*output ignore/){ #output ignore
570 if (defined($cmd{'cmd'})) {
574 $cmd{'output ignore'} = 1;
576 elsif($line =~ /^!\s*output display/){ #output display
577 if (defined($cmd{'cmd'})) {
581 $cmd{'output display'} = 1;
583 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
584 if (defined($cmd{'cmd'})) {
589 $cmd{'expect'} = "$1";
591 elsif($line =~ /^!\s*expect return/){ #expect return
592 if (defined($cmd{'cmd'})) {
596 $line =~ s/^! expect return //g;
598 $cmd{'return'} = $line;
600 elsif($line =~ /^!\s*setenv/){ #setenv
601 if (defined($cmd{'cmd'})) {
605 $line =~ s/^! setenv //g;
609 elsif($line =~ /^!\s*include/){ #include
610 if (defined($cmd{'cmd'})) {
614 print color("red"), "[Tesh/CRITICAL] need include";
615 print color("reset"), "\n";
618 elsif($line =~ /^!\s*timeout/){ #timeout
619 if (defined($cmd{'cmd'})) {
623 $line =~ s/^! timeout //;
625 $cmd{'timeout'} = $line;
627 die "[TESH/CRITICAL] parse error: $line\n";
630 kill('KILL', $forked);
638 # Deal with last command
639 if (defined($cmd{'cmd'})) {
646 kill('KILL', $forked);
652 waitpid ($test{'pid'}, 0);
653 $test{'gotret'} = exit_status($?);
660 close $diff_tool_tmp_fh;
661 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
662 unlink $diff_tool_tmp_filename;
667 }elsif($tesh_file eq "(stdin)"){
668 print "Test suite from stdin OK\n";
670 print "Test suite `$tesh_name' OK\n";
674 #push @a,"bl1"; push @b,"bl1";
675 #push @a,"bl2"; push @b,"bl2";
676 #push @a,"bl3"; push @b,"bl3";
677 #push @a,"bl4"; push @b,"bl4";
678 #push @a,"bl5"; push @b,"bl5";
679 #push @a,"bl6"; push @b,"bl6";
680 #push @a,"bl7"; push @b,"bl7";
681 ##push @a,"Perl"; push @b,"ruby";
682 #push @a,"END1"; push @b,"END1";
683 #push @a,"END2"; push @b,"END2";
684 #push @a,"END3"; push @b,"END3";
685 #push @a,"END4"; push @b,"END4";
686 #push @a,"END5"; push @b,"END5";
687 #push @a,"END6"; push @b,"END6";
688 #push @a,"END7"; push @b,"END7";
689 #print "Identical:\n". build_diff(\@a,\@b);
692 #push @a,"AZE"; push @b,"EZA";
693 #print "Different:\n".build_diff(\@a,\@b);
695 use lib "@CMAKE_BINARY_DIR@/bin" ;
697 use Diff qw(diff); # postpone a bit to have time to change INC
701 my $diff = Diff->new(@_);
703 $diff->Base( 1 ); # Return line numbers, not indices
704 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
705 return "" if ($chunk_count == 1 && $diff->Same());
707 while( $diff->Next() ) {
708 my @same = $diff->Same();
709 if ($diff->Same() ) {
710 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
711 $res .= ' '.$same[0]."\n" ;
712 $res .= ' '.$same[1]."\n" if (scalar @same>1);
714 $res .= "...\n" if (scalar @same>2);
715 # $res .= $diff->Next(0)."/$chunk_count\n";
716 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
717 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
718 $res .= ' '.$same[scalar @same -1]."\n";
721 next if $diff->Same();
722 map { $res .= "- $_\n" } $diff->Items(1);
723 map { $res .= "+ $_\n" } $diff->Items(2);