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);
50 ## Portability bits for windows
53 use constant RUNNING_ON_WINDOWS => ($^O =~ /^(?:mswin|dos|os2)/oi);
54 use POSIX qw(:sys_wait_h WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG
55 :signal_h SIGINT SIGTERM SIGKILL SIGABRT SIGSEGV);
56 # These are not implemented on windows (see bug 6798 and 6470)
58 if (RUNNING_ON_WINDOWS) {
59 *WIFEXITED = sub { not $_[0] & 127 };
60 *WEXITSTATUS = sub { $_[0] >> 8 };
61 *WIFSIGNALED = sub { ($_[0] & 127) && ($_[0] & 127 != 127) };
62 *WTERMSIG = sub { $_[0] & 127 };
68 ## Command line option handling
72 my ($text, $name, $value) = @_;
74 $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
75 $text =~ s/\$$name(\W|$)/$value$1/g;
78 $text =~ s/\${$name:=([^}]*)}/$1/g;
79 $text =~ s/\${$name}//g;
80 $text =~ s/\$$name(\W|$)/$1/g;
85 # option handling helper subs
89 if (-e $directory && -d $directory) {
91 print "[Tesh/INFO] change directory to $directory\n";
93 } elsif (-e $directory) {
94 print "Cannot change directory to '$directory': it is not a directory\n";
96 print "Chdir to $directory failed: No such file or directory\n";
101 print "Test suite `$tesh_file': NOK (system error)\n";
108 if ($_[0] =~ /^(.*)=(.*)$/) {
110 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
113 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
116 print "[Tesh/INFO] setenv $var=$ctn\n";
117 $environ{$var} = $ctn;
120 # Main option parsing sub
123 # remove the tesh file from the ARGV used
125 $tesh_file = pop @ARGV;
127 # temporary arrays for GetOption
139 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
142 'help|h' => \$opt{'help'},
144 'verbose|v' => \@verbose,
145 'debug|d' => \$opt{"debug"},
147 'difftool=s' => \$diff_tool,
150 'timeout=s' => \$opt{'timeout'},
151 'setenv=s' => \&setenv_cmd,
154 'enable-coverage+' => \$enable_coverage,
157 if($enable_coverage){
158 print "Enable coverage\n";
162 use File::Temp qw/ tempfile /;
163 ($diff_tool_tmp_fh, $diff_tool_tmp_filename) = tempfile();
164 print "New tesh: $diff_tool_tmp_filename\n";
167 unless($tesh_file=~/(.*)\.tesh/){
168 $tesh_file="(stdin)";
169 $tesh_name="(stdin)";
170 print "Test suite from stdin\n";
173 print "Test suite `$tesh_name'\n";
176 $opt{'verbose'} = scalar @verbose;
178 $opt{'cfg'} .= " --cfg=$_";
183 my %opts = get_options(@ARGV);
202 ###########################################################################
206 if (WIFEXITED($status)) {
207 $exitcode=WEXITSTATUS($status)+40;
208 return "returned code ".WEXITSTATUS($status);
209 } elsif (WIFSIGNALED($status)) {
211 if (WTERMSIG($status) == SIGINT){$code="SIGINT"; }
212 elsif (WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
213 elsif (WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
214 elsif (WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
215 elsif (WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
216 $exitcode=WTERMSIG($status)+4;
217 return "got signal $code";
219 return "Unparsable status. Is the process stopped?";
224 if ($opts{'debug'}) {
226 map {print " $_"} @{$cmd{'in'}};
229 map {print " $_"} @{$cmd{'out'}};
231 print "CMD: $cmd{'cmd'}\n";
234 # cleanup the command line
235 if(RUNNING_ON_WINDOWS) {
236 var_subst($cmd{'cmd'}, "EXEEXT", ".exe");
238 var_subst($cmd{'cmd'}, "EXEEXT", "");
241 # substitute environ variables
242 foreach my $key (keys %environ) {
243 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $key, $environ{$key});
245 # substitute remaining variables, if any
246 while ($cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/) {
247 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
249 while ($cmd{'cmd'} =~ /\$(\w+)/) {
250 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
254 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
257 $cmd{'cmd'} =~ s/^\s+//;
258 $cmd{'cmd'} =~ s/\s+$//;
260 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
263 # exec the command line
264 ### $line =~ s/\r//g;
266 $cmd{'got'} = IO::File->new_tmpfile;
267 $cmd{'got'}->autoflush(1);
268 local *E = $cmd{'got'};
269 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E",
270 quotewords('\s+', 0, $cmd{'cmd'}));
272 # push all provided input to executing child
273 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
276 # if timeout specified, fork and kill executing child at the end of timeout
277 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
278 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
281 die "fork() failed: $!" unless defined $forked;
282 if ( $forked == 0 ) { # child
284 # if (RUNNING_ON_WINDOWS) {
285 # system("TASKKILL /F /T /PID $cmd{'pid'}");
290 kill('TERM', $cmd{'pid'});
292 kill('KILL', $cmd{'pid'});
299 # Cleanup the executing child, and kill the timeouter brother on need
300 $cmd{'return'} = 0 unless defined($cmd{'return'});
301 if($cmd{'background'} != 1){
302 waitpid ($cmd{'pid'}, 0);
303 $cmd{'gotret'} = exit_status($?);
306 # & commands, which will be handled at the end
307 push @bg_cmds, \%cmd;
308 # no timeout for background commands
310 kill(SIGKILL, $forked);
320 my $gotret=$cmd{'gotret'};
324 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
325 $wantret = "got signal $cmd{'expect'}";
327 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
330 local *got = $cmd{'got'};
332 # pop all output from executing child
334 while(defined(my $got=<got>)) {
337 print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
339 if (!($enable_coverage and $got=~ /^profiling:/)){
345 # Save the unsorted observed output to report it on error.
346 map { push @{$cmd{'unsorted got'}}, $_ } @got;
349 substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
352 if ($sort_prefix>0) {
353 @got = sort mysort @got;
357 while (@got and $got[0] eq "") {
361 # Sort the expected output to make it easier to write for humans
362 if(defined($cmd{'out'})){
363 if ($sort_prefix>0) {
364 @{$cmd{'out'}} = sort mysort @{$cmd{'out'}};
366 @{$cmd{'out'}} = sort @{$cmd{'out'}};
368 while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
369 shift @{$cmd{'out'}};
374 # Did we timeout ? If yes, handle it. If not, kill the forked process.
376 if($timeout==-1 and ($gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL")){
377 $gotret="return code 0";
379 $gotret= "timeout after $time_to_wait sec";
382 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
386 if($gotret ne $wantret) {
388 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
390 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
392 map {$msg .= "|| $_\n"} @got;
395 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
406 # Check the result of execution
409 if (defined($cmd{'output display'})){
410 print "[Tesh/INFO] Here is the (ignored) command output:\n";
411 map { print "||$_\n" } @got;
413 elsif (!defined($cmd{'output ignore'})){
414 $diff = build_diff(\@{$cmd{'out'}}, \@got);
416 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
419 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
420 map { print "$_\n" } split(/\n/,$diff);
422 print "WARNING: Both the observed output and expected output were sorted as requested.\n";
423 print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
425 print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
426 # print "----8<--------------- Begin of unprocessed observed output (as it should appear in file):\n";
427 # map {print "> $_\n"} @{$cmd{'unsorted got'}};
428 # print "--------------->8---- End of the unprocessed observed output.\n";
431 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
439 my $file = $cmd{'arg'};
440 print "[Tesh/INFO] mkfile $file\n";
443 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
444 print FILE join("\n", @{$cmd{'in'}});
445 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
450 #my $teshfile=$tesh_file;
451 #$teshfile=~ s{\.[^.]+$}{};
453 unless($tesh_file eq "(stdin)"){
454 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
457 my %cmd; # everything about the next command to run
460 LINE: while (not $finished and not $error) {
464 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
467 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
475 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
477 # deal with line continuations
478 while ($line =~ /^(.*?)\\$/) {
480 die "[TESH/CRITICAL] Continued line at end of file\n"
481 unless defined($next);
484 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
488 # Push delayed commands on empty lines
489 unless ($line =~ m/^(.)(.*)$/) {
490 if (defined($cmd{'cmd'})) {
494 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
498 my ($cmd,$arg) = ($1,$2);
499 print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
503 # handle the commands
504 if ($cmd =~ /^#/) { #comment
505 } elsif ($cmd eq '>'){ #expected result line
506 print "[TESH/debug] push expected result\n" if $opts{'debug'};
507 push @{$cmd{'out'}}, $arg;
509 } elsif ($cmd eq '<') { # provided input
510 print "[TESH/debug] push provided input\n" if $opts{'debug'};
511 push @{$cmd{'in'}}, $arg;
513 } elsif ($cmd eq 'p') { # comment
514 print "[$tesh_name:$line_num] $arg\n";
516 } elsif ($cmd eq '$') { # Command
517 # if we have something buffered, run it now
518 if (defined($cmd{'cmd'})) {
522 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
523 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
526 $cmd{'arg'} =~ s/\s*mkfile //;
530 } elsif ($arg =~ /^\s*cd /) {
531 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
532 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
538 } else { # regular command
540 $cmd{'file'} = $tesh_file;
541 $cmd{'line'} = $line_num;
544 elsif($cmd eq '&'){ # parallel command line
546 if (defined($cmd{'cmd'})) {
550 $cmd{'background'} = 1;
552 $cmd{'file'} = $tesh_file;
553 $cmd{'line'} = $line_num;
555 elsif($line =~ /^!\s*output sort/){ #output sort
556 if (defined($cmd{'cmd'})) {
561 if ($line =~ /^!\s*output sort\s+(\d+)/) {
565 elsif($line =~ /^!\s*output ignore/){ #output ignore
566 if (defined($cmd{'cmd'})) {
570 $cmd{'output ignore'} = 1;
572 elsif($line =~ /^!\s*output display/){ #output display
573 if (defined($cmd{'cmd'})) {
577 $cmd{'output display'} = 1;
579 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
580 if (defined($cmd{'cmd'})) {
585 $cmd{'expect'} = "$1";
587 elsif($line =~ /^!\s*expect return/){ #expect return
588 if (defined($cmd{'cmd'})) {
592 $line =~ s/^! expect return //g;
594 $cmd{'return'} = $line;
596 elsif($line =~ /^!\s*setenv/){ #setenv
597 if (defined($cmd{'cmd'})) {
601 $line =~ s/^! setenv //g;
605 elsif($line =~ /^!\s*include/){ #include
606 if (defined($cmd{'cmd'})) {
610 print color("red"), "[Tesh/CRITICAL] need include";
611 print color("reset"), "\n";
614 elsif($line =~ /^!\s*timeout/){ #timeout
615 if (defined($cmd{'cmd'})) {
619 $line =~ s/^! timeout //;
621 $cmd{'timeout'} = $line;
623 die "[TESH/CRITICAL] parse error: $line\n";
626 kill(SIGKILL, $forked);
634 # Deal with last command
635 if (defined($cmd{'cmd'})) {
642 kill(SIGKILL, $forked);
648 waitpid ($test{'pid'}, 0);
649 $test{'gotret'} = exit_status($?);
656 close $diff_tool_tmp_fh;
657 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
658 unlink $diff_tool_tmp_filename;
663 }elsif($tesh_file eq "(stdin)"){
664 print "Test suite from stdin OK\n";
666 print "Test suite `$tesh_name' OK\n";
670 #push @a,"bl1"; push @b,"bl1";
671 #push @a,"bl2"; push @b,"bl2";
672 #push @a,"bl3"; push @b,"bl3";
673 #push @a,"bl4"; push @b,"bl4";
674 #push @a,"bl5"; push @b,"bl5";
675 #push @a,"bl6"; push @b,"bl6";
676 #push @a,"bl7"; push @b,"bl7";
677 ##push @a,"Perl"; push @b,"ruby";
678 #push @a,"END1"; push @b,"END1";
679 #push @a,"END2"; push @b,"END2";
680 #push @a,"END3"; push @b,"END3";
681 #push @a,"END4"; push @b,"END4";
682 #push @a,"END5"; push @b,"END5";
683 #push @a,"END6"; push @b,"END6";
684 #push @a,"END7"; push @b,"END7";
685 #print "Identical:\n". build_diff(\@a,\@b);
688 #push @a,"AZE"; push @b,"EZA";
689 #print "Different:\n".build_diff(\@a,\@b);
691 use lib "@CMAKE_BINARY_DIR@/bin" ;
693 use Diff qw(diff); # postpone a bit to have time to change INC
697 my $diff = Diff->new(@_);
699 $diff->Base( 1 ); # Return line numbers, not indices
700 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
701 return "" if ($chunk_count == 1 && $diff->Same());
703 while( $diff->Next() ) {
704 my @same = $diff->Same();
705 if ($diff->Same() ) {
706 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
707 $res .= ' '.$same[0]."\n" ;
708 $res .= ' '.$same[1]."\n" if (scalar @same>1);
710 $res .= "...\n" if (scalar @same>2);
711 # $res .= $diff->Next(0)."/$chunk_count\n";
712 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
713 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
714 $res .= ' '.$same[scalar @same -1]."\n";
717 next if $diff->Same();
718 map { $res .= "- $_\n" } $diff->Items(1);
719 map { $res .= "+ $_\n" } $diff->Items(2);