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";
113 print "Test suite `$tesh_file': NOK (system error)\n";
120 if ($_[0] =~ /^(.*)=(.*)$/) {
122 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
125 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
128 print "[Tesh/INFO] setenv $var=$ctn\n";
129 $environ{$var} = $ctn;
132 # Main option parsing sub
135 # remove the tesh file from the ARGV used
137 $tesh_file = pop @ARGV;
139 # temporary arrays for GetOption
149 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
152 'help|h' => \$opt{'help'},
154 'debug|d' => \$opt{"debug"},
156 'difftool=s' => \$diff_tool,
159 'timeout=s' => \$opt{'timeout'},
160 'setenv=s' => \&setenv_cmd,
163 'enable-coverage+' => \$enable_coverage,
166 if($enable_coverage){
167 print "Enable coverage\n";
171 use File::Temp qw/ tempfile /;
172 ($diff_tool_tmp_fh, $diff_tool_tmp_filename) = tempfile();
173 print "New tesh: $diff_tool_tmp_filename\n";
176 if ($tesh_file =~ m/(.*)\.tesh/) {
178 print "Test suite `$tesh_name'\n";
180 $tesh_file="(stdin)";
181 $tesh_name="(stdin)";
182 print "Test suite from stdin\n";
186 $opt{'cfg'} .= " --cfg=$_";
191 my %opts = get_options(@ARGV);
201 ###########################################################################
205 if (WIFEXITED($status)) {
206 $exitcode=WEXITSTATUS($status)+40;
207 return "returned code ".WEXITSTATUS($status);
208 } elsif (WIFSIGNALED($status)) {
210 if (WTERMSIG($status) == SIGINT){$code="SIGINT"; }
211 elsif (WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
212 elsif (WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
213 elsif (WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
214 elsif (WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
215 $exitcode=WTERMSIG($status)+4;
216 return "got signal $code";
218 return "Unparsable status. Is the process stopped?";
223 if ($opts{'debug'}) {
225 map {print " $_"} @{$cmd{'in'}};
228 map {print " $_"} @{$cmd{'out'}};
230 print "CMD: $cmd{'cmd'}\n";
233 # cleanup the command line
234 if(RUNNING_ON_WINDOWS) {
235 var_subst($cmd{'cmd'}, "EXEEXT", ".exe");
237 var_subst($cmd{'cmd'}, "EXEEXT", "");
240 # substitute environ variables
241 foreach my $key (keys %environ) {
242 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $key, $environ{$key});
244 # substitute remaining variables, if any
245 while ($cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/) {
246 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
248 while ($cmd{'cmd'} =~ /\$(\w+)/) {
249 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
253 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
256 $cmd{'cmd'} =~ s/^\s+//;
257 $cmd{'cmd'} =~ s/\s+$//;
259 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
262 # exec the command line
263 ### $line =~ s/\r//g;
265 $cmd{'got'} = IO::File->new_tmpfile;
266 $cmd{'got'}->autoflush(1);
267 local *E = $cmd{'got'};
268 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E",
269 quotewords('\s+', 0, $cmd{'cmd'}));
271 # push all provided input to executing child
272 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
275 # if timeout specified, fork and kill executing child at the end of timeout
276 if (not $cmd{'background'} and (defined($cmd{'timeout'}) or defined($opts{'timeout'}))){
277 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
280 die "fork() failed: $!" unless defined $forked;
281 if ( $forked == 0 ) { # child
282 exec("$PROGRAM_NAME --internal-killer-process $time_to_wait $cmd{'pid'}");
286 # Cleanup the executing child, and kill the timeouter brother on need
287 $cmd{'return'} = 0 unless defined($cmd{'return'});
288 if ($cmd{'background'} != 1) {
289 waitpid ($cmd{'pid'}, 0);
290 $cmd{'gotret'} = exit_status($?);
293 # & commands, which will be handled at the end
294 push @bg_cmds, \%cmd;
301 my $gotret=$cmd{'gotret'};
305 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
306 $wantret = "got signal $cmd{'expect'}";
308 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
311 local *got = $cmd{'got'};
313 # pop all output from executing child
315 while(defined(my $got=<got>)) {
318 print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
320 if (!($enable_coverage and $got=~ /^profiling:/)){
326 # Save the unsorted observed output to report it on error.
327 map { push @{$cmd{'unsorted got'}}, $_ } @got;
330 substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
333 if ($sort_prefix>0) {
334 @got = sort mysort @got;
338 while (@got and $got[0] eq "") {
342 # Sort the expected output to make it easier to write for humans
343 if(defined($cmd{'out'})){
344 if ($sort_prefix>0) {
345 @{$cmd{'out'}} = sort mysort @{$cmd{'out'}};
347 @{$cmd{'out'}} = sort @{$cmd{'out'}};
349 while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
350 shift @{$cmd{'out'}};
355 # Did we timeout ? If yes, handle it. If not, kill the forked process.
357 if($timeout==-1 and ($gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL")){
358 $gotret="return code 0";
360 $gotret= "timeout after $time_to_wait sec";
363 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
367 if($gotret ne $wantret) {
369 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
371 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
373 map {$msg .= "|| $_\n"} @got;
376 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
387 # Check the result of execution
390 if (defined($cmd{'output display'})) {
391 print "[Tesh/INFO] Here is the (ignored) command output:\n";
392 map { print "||$_\n" } @got;
393 } elsif (defined($cmd{'output ignore'})) {
394 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
396 $diff = build_diff(\@{$cmd{'out'}}, \@got);
399 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
400 map { print "$_\n" } split(/\n/,$diff);
402 print "WARNING: Both the observed output and expected output were sorted as requested.\n";
403 print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
405 print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
406 # print "----8<--------------- Begin of unprocessed observed output (as it should appear in file):\n";
407 # map {print "> $_\n"} @{$cmd{'unsorted got'}};
408 # print "--------------->8---- End of the unprocessed observed output.\n";
411 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
418 my $file = $cmd{'arg'};
419 print "[Tesh/INFO] mkfile $file\n";
422 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
423 print FILE join("\n", @{$cmd{'in'}});
424 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
429 my $infh; # The file descriptor from which we should read the teshfile
430 if($tesh_file eq "(stdin)"){
433 open $infh, $tesh_file
434 or die "[Tesh/CRITICAL] Unable to open $tesh_file: $!\n";
437 my %cmd; # everything about the next command to run
439 LINE: while (defined(my $line=<$infh>) and not $error) {
444 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
446 # deal with line continuations
447 while ($line =~ /^(.*?)\\$/) {
449 die "[TESH/CRITICAL] Continued line at end of file\n"
450 unless defined($next);
453 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
457 # Push delayed commands on empty lines
458 unless ($line =~ m/^(.)(.*)$/) {
459 if (defined($cmd{'cmd'})) {
463 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
467 my ($cmd,$arg) = ($1,$2);
468 print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
472 # handle the commands
473 if ($cmd =~ /^#/) { #comment
474 } elsif ($cmd eq '>'){ #expected result line
475 print "[TESH/debug] push expected result\n" if $opts{'debug'};
476 push @{$cmd{'out'}}, $arg;
478 } elsif ($cmd eq '<') { # provided input
479 print "[TESH/debug] push provided input\n" if $opts{'debug'};
480 push @{$cmd{'in'}}, $arg;
482 } elsif ($cmd eq 'p') { # comment
483 print "[$tesh_name:$line_num] $arg\n";
485 } elsif ($cmd eq '$') { # Command
486 # if we have something buffered, run it now
487 if (defined($cmd{'cmd'})) {
491 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
492 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
495 $cmd{'arg'} =~ s/\s*mkfile //;
499 } elsif ($arg =~ /^\s*cd /) {
500 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
501 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
507 } else { # regular command
509 $cmd{'file'} = $tesh_file;
510 $cmd{'line'} = $line_num;
513 elsif($cmd eq '&'){ # parallel command line
515 if (defined($cmd{'cmd'})) {
519 $cmd{'background'} = 1;
521 $cmd{'file'} = $tesh_file;
522 $cmd{'line'} = $line_num;
524 elsif($line =~ /^!\s*output sort/){ #output sort
525 if (defined($cmd{'cmd'})) {
530 if ($line =~ /^!\s*output sort\s+(\d+)/) {
534 elsif($line =~ /^!\s*output ignore/){ #output ignore
535 if (defined($cmd{'cmd'})) {
539 $cmd{'output ignore'} = 1;
541 elsif($line =~ /^!\s*output display/){ #output display
542 if (defined($cmd{'cmd'})) {
546 $cmd{'output display'} = 1;
548 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
549 if (defined($cmd{'cmd'})) {
554 $cmd{'expect'} = "$1";
556 elsif($line =~ /^!\s*expect return/){ #expect return
557 if (defined($cmd{'cmd'})) {
561 $line =~ s/^! expect return //g;
563 $cmd{'return'} = $line;
565 elsif($line =~ /^!\s*setenv/){ #setenv
566 if (defined($cmd{'cmd'})) {
570 $line =~ s/^! setenv //g;
574 elsif($line =~ /^!\s*include/){ #include
575 if (defined($cmd{'cmd'})) {
579 print color("red"), "[Tesh/CRITICAL] need include";
580 print color("reset"), "\n";
583 elsif($line =~ /^!\s*timeout/){ #timeout
584 if (defined($cmd{'cmd'})) {
588 $line =~ s/^! timeout //;
590 $cmd{'timeout'} = $line;
592 die "[TESH/CRITICAL] parse error: $line\n";
595 kill('KILL', $forked);
600 # We're done reading the input file
601 close $infh unless ($tesh_file eq "(stdin)");
603 # Deal with last command
604 if (defined($cmd{'cmd'})) {
611 kill('KILL', $forked);
617 waitpid ($test{'pid'}, 0);
618 $test{'gotret'} = exit_status($?);
625 close $diff_tool_tmp_fh;
626 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
627 unlink $diff_tool_tmp_filename;
632 } elsif($tesh_file eq "(stdin)") {
633 print "Test suite from stdin OK\n";
635 print "Test suite `$tesh_name' OK\n";
639 #push @a,"bl1"; push @b,"bl1";
640 #push @a,"bl2"; push @b,"bl2";
641 #push @a,"bl3"; push @b,"bl3";
642 #push @a,"bl4"; push @b,"bl4";
643 #push @a,"bl5"; push @b,"bl5";
644 #push @a,"bl6"; push @b,"bl6";
645 #push @a,"bl7"; push @b,"bl7";
646 ##push @a,"Perl"; push @b,"ruby";
647 #push @a,"END1"; push @b,"END1";
648 #push @a,"END2"; push @b,"END2";
649 #push @a,"END3"; push @b,"END3";
650 #push @a,"END4"; push @b,"END4";
651 #push @a,"END5"; push @b,"END5";
652 #push @a,"END6"; push @b,"END6";
653 #push @a,"END7"; push @b,"END7";
654 #print "Identical:\n". build_diff(\@a,\@b);
657 #push @a,"AZE"; push @b,"EZA";
658 #print "Different:\n".build_diff(\@a,\@b);
660 use lib "@CMAKE_BINARY_DIR@/bin" ;
662 use Diff qw(diff); # postpone a bit to have time to change INC
666 my $diff = Diff->new(@_);
668 $diff->Base( 1 ); # Return line numbers, not indices
669 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
670 return "" if ($chunk_count == 1 && $diff->Same());
672 while( $diff->Next() ) {
673 my @same = $diff->Same();
674 if ($diff->Same() ) {
675 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
676 $res .= ' '.$same[0]."\n" ;
677 $res .= ' '.$same[1]."\n" if (scalar @same>1);
679 $res .= "...\n" if (scalar @same>2);
680 # $res .= $diff->Next(0)."/$chunk_count\n";
681 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
682 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
683 $res .= ' '.$same[scalar @same -1]."\n";
686 next if $diff->Same();
687 map { $res .= "- $_\n" } $diff->Items(1);
688 map { $res .= "+ $_\n" } $diff->Items(2);