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>
25 my $enable_coverage=0;
27 my $diff_tool_tmp_fh=0;
28 my $diff_tool_tmp_filename=0;
36 $SIG{'PIPE'} = 'IGNORE';
40 use Getopt::Long qw(GetOptions);
49 ## Portability bits for windows
52 use constant RUNNING_ON_WINDOWS => ($OSNAME =~ /^(?:mswin|dos|os2)/oi);
53 use POSIX qw(:sys_wait_h WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG
54 :signal_h SIGINT SIGTERM SIGKILL SIGABRT SIGSEGV);
55 # These are not implemented on windows
57 if (RUNNING_ON_WINDOWS) {
58 *WIFEXITED = sub { not $_[0] & 127 };
59 *WEXITSTATUS = sub { $_[0] >> 8 };
60 *WIFSIGNALED = sub { ($_[0] & 127) && ($_[0] & 127 != 127) };
61 *WTERMSIG = sub { $_[0] & 127 };
67 ## Command line option handling
70 if ($ARGV[0] eq "--internal-killer-process") {
71 # We fork+exec a waiter process in charge of killing the command after timeout
72 # If the command stops earlier, that's fine: the killer sends a signal to an already stopped process, fails, and quits.
73 # Nobody cares about the killer issues.
74 # The only problem could arise if another process is given the same PID than cmd. We bet it won't happen :)
75 my $time_to_wait = $ARGV[1];
86 my ($text, $name, $value) = @_;
88 $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
89 $text =~ s/\$$name(\W|$)/$value$1/g;
92 $text =~ s/\${$name:=([^}]*)}/$1/g;
93 $text =~ s/\${$name}//g;
94 $text =~ s/\$$name(\W|$)/$1/g;
99 # option handling helper subs
103 if (-e $directory && -d $directory) {
105 print "[Tesh/INFO] change directory to $directory\n";
107 } elsif (-e $directory) {
108 print "Cannot change directory to '$directory': it is not a directory\n";
110 print "Chdir to $directory failed: No such file or directory\n";
115 print "Test suite `$tesh_file': NOK (system error)\n";
122 if ($_[0] =~ /^(.*)=(.*)$/) {
124 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
127 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
130 print "[Tesh/INFO] setenv $var=$ctn\n";
131 $environ{$var} = $ctn;
134 # Main option parsing sub
137 # remove the tesh file from the ARGV used
139 $tesh_file = pop @ARGV;
141 # temporary arrays for GetOption
151 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
154 'help|h' => \$opt{'help'},
156 'debug|d' => \$opt{"debug"},
158 'difftool=s' => \$diff_tool,
161 'timeout=s' => \$opt{'timeout'},
162 'setenv=s' => \&setenv_cmd,
165 'enable-coverage+' => \$enable_coverage,
168 if($enable_coverage){
169 print "Enable coverage\n";
173 use File::Temp qw/ tempfile /;
174 ($diff_tool_tmp_fh, $diff_tool_tmp_filename) = tempfile();
175 print "New tesh: $diff_tool_tmp_filename\n";
178 unless($tesh_file=~/(.*)\.tesh/){
179 $tesh_file="(stdin)";
180 $tesh_name="(stdin)";
181 print "Test suite from stdin\n";
184 print "Test suite `$tesh_name'\n";
188 $opt{'cfg'} .= " --cfg=$_";
193 my %opts = get_options(@ARGV);
203 ###########################################################################
207 if (WIFEXITED($status)) {
208 $exitcode=WEXITSTATUS($status)+40;
209 return "returned code ".WEXITSTATUS($status);
210 } elsif (WIFSIGNALED($status)) {
212 if (WTERMSIG($status) == SIGINT){$code="SIGINT"; }
213 elsif (WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
214 elsif (WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
215 elsif (WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
216 elsif (WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
217 $exitcode=WTERMSIG($status)+4;
218 return "got signal $code";
220 return "Unparsable status. Is the process stopped?";
225 if ($opts{'debug'}) {
227 map {print " $_"} @{$cmd{'in'}};
230 map {print " $_"} @{$cmd{'out'}};
232 print "CMD: $cmd{'cmd'}\n";
235 # cleanup the command line
236 if(RUNNING_ON_WINDOWS) {
237 var_subst($cmd{'cmd'}, "EXEEXT", ".exe");
239 var_subst($cmd{'cmd'}, "EXEEXT", "");
242 # substitute environ variables
243 foreach my $key (keys %environ) {
244 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $key, $environ{$key});
246 # substitute remaining variables, if any
247 while ($cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/) {
248 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
250 while ($cmd{'cmd'} =~ /\$(\w+)/) {
251 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
255 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
258 $cmd{'cmd'} =~ s/^\s+//;
259 $cmd{'cmd'} =~ s/\s+$//;
261 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
264 # exec the command line
265 ### $line =~ s/\r//g;
267 $cmd{'got'} = IO::File->new_tmpfile;
268 $cmd{'got'}->autoflush(1);
269 local *E = $cmd{'got'};
270 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E",
271 quotewords('\s+', 0, $cmd{'cmd'}));
273 # push all provided input to executing child
274 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
277 # if timeout specified, fork and kill executing child at the end of timeout
278 if (not $cmd{'background'} and (defined($cmd{'timeout'}) or defined($opts{'timeout'}))){
279 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
282 die "fork() failed: $!" unless defined $forked;
283 if ( $forked == 0 ) { # child
284 exec("$PROGRAM_NAME --internal-killer-process $time_to_wait $cmd{'pid'}");
288 # Cleanup the executing child, and kill the timeouter brother on need
289 $cmd{'return'} = 0 unless defined($cmd{'return'});
290 if ($cmd{'background'} != 1) {
291 waitpid ($cmd{'pid'}, 0);
292 $cmd{'gotret'} = exit_status($?);
295 # & commands, which will be handled at the end
296 push @bg_cmds, \%cmd;
303 my $gotret=$cmd{'gotret'};
307 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
308 $wantret = "got signal $cmd{'expect'}";
310 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
313 local *got = $cmd{'got'};
315 # pop all output from executing child
317 while(defined(my $got=<got>)) {
320 print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
322 if (!($enable_coverage and $got=~ /^profiling:/)){
328 # Save the unsorted observed output to report it on error.
329 map { push @{$cmd{'unsorted got'}}, $_ } @got;
332 substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
335 if ($sort_prefix>0) {
336 @got = sort mysort @got;
340 while (@got and $got[0] eq "") {
344 # Sort the expected output to make it easier to write for humans
345 if(defined($cmd{'out'})){
346 if ($sort_prefix>0) {
347 @{$cmd{'out'}} = sort mysort @{$cmd{'out'}};
349 @{$cmd{'out'}} = sort @{$cmd{'out'}};
351 while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
352 shift @{$cmd{'out'}};
357 # Did we timeout ? If yes, handle it. If not, kill the forked process.
359 if($timeout==-1 and ($gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL")){
360 $gotret="return code 0";
362 $gotret= "timeout after $time_to_wait sec";
365 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
369 if($gotret ne $wantret) {
371 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
373 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
375 map {$msg .= "|| $_\n"} @got;
378 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
389 # Check the result of execution
392 if (defined($cmd{'output display'})){
393 print "[Tesh/INFO] Here is the (ignored) command output:\n";
394 map { print "||$_\n" } @got;
396 elsif (!defined($cmd{'output ignore'})){
397 $diff = build_diff(\@{$cmd{'out'}}, \@got);
399 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
402 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
403 map { print "$_\n" } split(/\n/,$diff);
405 print "WARNING: Both the observed output and expected output were sorted as requested.\n";
406 print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
408 print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
409 # print "----8<--------------- Begin of unprocessed observed output (as it should appear in file):\n";
410 # map {print "> $_\n"} @{$cmd{'unsorted got'}};
411 # print "--------------->8---- End of the unprocessed observed output.\n";
414 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
422 my $file = $cmd{'arg'};
423 print "[Tesh/INFO] mkfile $file\n";
426 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
427 print FILE join("\n", @{$cmd{'in'}});
428 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
433 #my $teshfile=$tesh_file;
434 #$teshfile=~ s{\.[^.]+$}{};
436 unless($tesh_file eq "(stdin)"){
437 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
440 my %cmd; # everything about the next command to run
443 LINE: while (not $finished and not $error) {
447 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
450 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
458 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
460 # deal with line continuations
461 while ($line =~ /^(.*?)\\$/) {
463 die "[TESH/CRITICAL] Continued line at end of file\n"
464 unless defined($next);
467 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
471 # Push delayed commands on empty lines
472 unless ($line =~ m/^(.)(.*)$/) {
473 if (defined($cmd{'cmd'})) {
477 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
481 my ($cmd,$arg) = ($1,$2);
482 print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
486 # handle the commands
487 if ($cmd =~ /^#/) { #comment
488 } elsif ($cmd eq '>'){ #expected result line
489 print "[TESH/debug] push expected result\n" if $opts{'debug'};
490 push @{$cmd{'out'}}, $arg;
492 } elsif ($cmd eq '<') { # provided input
493 print "[TESH/debug] push provided input\n" if $opts{'debug'};
494 push @{$cmd{'in'}}, $arg;
496 } elsif ($cmd eq 'p') { # comment
497 print "[$tesh_name:$line_num] $arg\n";
499 } elsif ($cmd eq '$') { # Command
500 # if we have something buffered, run it now
501 if (defined($cmd{'cmd'})) {
505 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
506 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
509 $cmd{'arg'} =~ s/\s*mkfile //;
513 } elsif ($arg =~ /^\s*cd /) {
514 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
515 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
521 } else { # regular command
523 $cmd{'file'} = $tesh_file;
524 $cmd{'line'} = $line_num;
527 elsif($cmd eq '&'){ # parallel command line
529 if (defined($cmd{'cmd'})) {
533 $cmd{'background'} = 1;
535 $cmd{'file'} = $tesh_file;
536 $cmd{'line'} = $line_num;
538 elsif($line =~ /^!\s*output sort/){ #output sort
539 if (defined($cmd{'cmd'})) {
544 if ($line =~ /^!\s*output sort\s+(\d+)/) {
548 elsif($line =~ /^!\s*output ignore/){ #output ignore
549 if (defined($cmd{'cmd'})) {
553 $cmd{'output ignore'} = 1;
555 elsif($line =~ /^!\s*output display/){ #output display
556 if (defined($cmd{'cmd'})) {
560 $cmd{'output display'} = 1;
562 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
563 if (defined($cmd{'cmd'})) {
568 $cmd{'expect'} = "$1";
570 elsif($line =~ /^!\s*expect return/){ #expect return
571 if (defined($cmd{'cmd'})) {
575 $line =~ s/^! expect return //g;
577 $cmd{'return'} = $line;
579 elsif($line =~ /^!\s*setenv/){ #setenv
580 if (defined($cmd{'cmd'})) {
584 $line =~ s/^! setenv //g;
588 elsif($line =~ /^!\s*include/){ #include
589 if (defined($cmd{'cmd'})) {
593 print color("red"), "[Tesh/CRITICAL] need include";
594 print color("reset"), "\n";
597 elsif($line =~ /^!\s*timeout/){ #timeout
598 if (defined($cmd{'cmd'})) {
602 $line =~ s/^! timeout //;
604 $cmd{'timeout'} = $line;
606 die "[TESH/CRITICAL] parse error: $line\n";
609 kill('KILL', $forked);
617 # Deal with last command
618 if (defined($cmd{'cmd'})) {
625 kill('KILL', $forked);
631 waitpid ($test{'pid'}, 0);
632 $test{'gotret'} = exit_status($?);
639 close $diff_tool_tmp_fh;
640 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
641 unlink $diff_tool_tmp_filename;
646 }elsif($tesh_file eq "(stdin)"){
647 print "Test suite from stdin OK\n";
649 print "Test suite `$tesh_name' OK\n";
653 #push @a,"bl1"; push @b,"bl1";
654 #push @a,"bl2"; push @b,"bl2";
655 #push @a,"bl3"; push @b,"bl3";
656 #push @a,"bl4"; push @b,"bl4";
657 #push @a,"bl5"; push @b,"bl5";
658 #push @a,"bl6"; push @b,"bl6";
659 #push @a,"bl7"; push @b,"bl7";
660 ##push @a,"Perl"; push @b,"ruby";
661 #push @a,"END1"; push @b,"END1";
662 #push @a,"END2"; push @b,"END2";
663 #push @a,"END3"; push @b,"END3";
664 #push @a,"END4"; push @b,"END4";
665 #push @a,"END5"; push @b,"END5";
666 #push @a,"END6"; push @b,"END6";
667 #push @a,"END7"; push @b,"END7";
668 #print "Identical:\n". build_diff(\@a,\@b);
671 #push @a,"AZE"; push @b,"EZA";
672 #print "Different:\n".build_diff(\@a,\@b);
674 use lib "@CMAKE_BINARY_DIR@/bin" ;
676 use Diff qw(diff); # postpone a bit to have time to change INC
680 my $diff = Diff->new(@_);
682 $diff->Base( 1 ); # Return line numbers, not indices
683 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
684 return "" if ($chunk_count == 1 && $diff->Same());
686 while( $diff->Next() ) {
687 my @same = $diff->Same();
688 if ($diff->Same() ) {
689 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
690 $res .= ' '.$same[0]."\n" ;
691 $res .= ' '.$same[1]."\n" if (scalar @same>1);
693 $res .= "...\n" if (scalar @same>2);
694 # $res .= $diff->Next(0)."/$chunk_count\n";
695 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
696 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
697 $res .= ' '.$same[scalar @same -1]."\n";
700 next if $diff->Same();
701 map { $res .= "- $_\n" } $diff->Items(1);
702 map { $res .= "+ $_\n" } $diff->Items(2);