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 $infh; # The file descriptor from which we should read the teshfile
434 if($tesh_file eq "(stdin)"){
437 open $infh, $tesh_file
438 or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
441 my %cmd; # everything about the next command to run
443 LINE: while (defined(my $line=<$infh>) and not $error) {
448 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
450 # deal with line continuations
451 while ($line =~ /^(.*?)\\$/) {
453 die "[TESH/CRITICAL] Continued line at end of file\n"
454 unless defined($next);
457 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
461 # Push delayed commands on empty lines
462 unless ($line =~ m/^(.)(.*)$/) {
463 if (defined($cmd{'cmd'})) {
467 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
471 my ($cmd,$arg) = ($1,$2);
472 print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
476 # handle the commands
477 if ($cmd =~ /^#/) { #comment
478 } elsif ($cmd eq '>'){ #expected result line
479 print "[TESH/debug] push expected result\n" if $opts{'debug'};
480 push @{$cmd{'out'}}, $arg;
482 } elsif ($cmd eq '<') { # provided input
483 print "[TESH/debug] push provided input\n" if $opts{'debug'};
484 push @{$cmd{'in'}}, $arg;
486 } elsif ($cmd eq 'p') { # comment
487 print "[$tesh_name:$line_num] $arg\n";
489 } elsif ($cmd eq '$') { # Command
490 # if we have something buffered, run it now
491 if (defined($cmd{'cmd'})) {
495 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
496 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
499 $cmd{'arg'} =~ s/\s*mkfile //;
503 } elsif ($arg =~ /^\s*cd /) {
504 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
505 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
511 } else { # regular command
513 $cmd{'file'} = $tesh_file;
514 $cmd{'line'} = $line_num;
517 elsif($cmd eq '&'){ # parallel command line
519 if (defined($cmd{'cmd'})) {
523 $cmd{'background'} = 1;
525 $cmd{'file'} = $tesh_file;
526 $cmd{'line'} = $line_num;
528 elsif($line =~ /^!\s*output sort/){ #output sort
529 if (defined($cmd{'cmd'})) {
534 if ($line =~ /^!\s*output sort\s+(\d+)/) {
538 elsif($line =~ /^!\s*output ignore/){ #output ignore
539 if (defined($cmd{'cmd'})) {
543 $cmd{'output ignore'} = 1;
545 elsif($line =~ /^!\s*output display/){ #output display
546 if (defined($cmd{'cmd'})) {
550 $cmd{'output display'} = 1;
552 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
553 if (defined($cmd{'cmd'})) {
558 $cmd{'expect'} = "$1";
560 elsif($line =~ /^!\s*expect return/){ #expect return
561 if (defined($cmd{'cmd'})) {
565 $line =~ s/^! expect return //g;
567 $cmd{'return'} = $line;
569 elsif($line =~ /^!\s*setenv/){ #setenv
570 if (defined($cmd{'cmd'})) {
574 $line =~ s/^! setenv //g;
578 elsif($line =~ /^!\s*include/){ #include
579 if (defined($cmd{'cmd'})) {
583 print color("red"), "[Tesh/CRITICAL] need include";
584 print color("reset"), "\n";
587 elsif($line =~ /^!\s*timeout/){ #timeout
588 if (defined($cmd{'cmd'})) {
592 $line =~ s/^! timeout //;
594 $cmd{'timeout'} = $line;
596 die "[TESH/CRITICAL] parse error: $line\n";
599 kill('KILL', $forked);
604 # We're done reading the input file
605 close $infh unless ($tesh_file eq "(stdin)");
607 # Deal with last command
608 if (defined($cmd{'cmd'})) {
615 kill('KILL', $forked);
621 waitpid ($test{'pid'}, 0);
622 $test{'gotret'} = exit_status($?);
629 close $diff_tool_tmp_fh;
630 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
631 unlink $diff_tool_tmp_filename;
636 }elsif($tesh_file eq "(stdin)"){
637 print "Test suite from stdin OK\n";
639 print "Test suite `$tesh_name' OK\n";
643 #push @a,"bl1"; push @b,"bl1";
644 #push @a,"bl2"; push @b,"bl2";
645 #push @a,"bl3"; push @b,"bl3";
646 #push @a,"bl4"; push @b,"bl4";
647 #push @a,"bl5"; push @b,"bl5";
648 #push @a,"bl6"; push @b,"bl6";
649 #push @a,"bl7"; push @b,"bl7";
650 ##push @a,"Perl"; push @b,"ruby";
651 #push @a,"END1"; push @b,"END1";
652 #push @a,"END2"; push @b,"END2";
653 #push @a,"END3"; push @b,"END3";
654 #push @a,"END4"; push @b,"END4";
655 #push @a,"END5"; push @b,"END5";
656 #push @a,"END6"; push @b,"END6";
657 #push @a,"END7"; push @b,"END7";
658 #print "Identical:\n". build_diff(\@a,\@b);
661 #push @a,"AZE"; push @b,"EZA";
662 #print "Different:\n".build_diff(\@a,\@b);
664 use lib "@CMAKE_BINARY_DIR@/bin" ;
666 use Diff qw(diff); # postpone a bit to have time to change INC
670 my $diff = Diff->new(@_);
672 $diff->Base( 1 ); # Return line numbers, not indices
673 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
674 return "" if ($chunk_count == 1 && $diff->Same());
676 while( $diff->Next() ) {
677 my @same = $diff->Same();
678 if ($diff->Same() ) {
679 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
680 $res .= ' '.$same[0]."\n" ;
681 $res .= ' '.$same[1]."\n" if (scalar @same>1);
683 $res .= "...\n" if (scalar @same>2);
684 # $res .= $diff->Next(0)."/$chunk_count\n";
685 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
686 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
687 $res .= ' '.$same[scalar @same -1]."\n";
690 next if $diff->Same();
691 map { $res .= "- $_\n" } $diff->Items(1);
692 map { $res .= "+ $_\n" } $diff->Items(2);