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>
28 my $enable_coverage=0;
30 my $diff_tool_tmp_fh=0;
31 my $diff_tool_tmp_filename=0;
39 $SIG{'PIPE'} = 'IGNORE';
43 use Getopt::Long qw(GetOptions);
50 if($^O eq "linux" || $^O eq "MacOS" || $^O eq "darwin"){
52 } elsif ($^O eq "MSWin32") {
54 $ENV{"PRINTF_EXPONENT_DIGITS"} = "2";
56 die "Tesh: Unknown operating system: $^O\n";
58 use if $^O eq 'MSWin32', 'Win32::Job';
61 ## Command line option handling
65 my ($text, $name, $value) = @_;
67 $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
68 $text =~ s/\$$name(\W|$)/$value$1/g;
71 $text =~ s/\${$name:=([^}]*)}/$1/g;
72 $text =~ s/\${$name}//g;
73 $text =~ s/\$$name(\W|$)/$1/g;
78 # option handling helper subs
82 if (-e $directory && -d $directory) {
84 print "[Tesh/INFO] change directory to $directory\n";
86 } elsif (-e $directory) {
87 print "Cannot change directory to '$directory': it is not a directory\n";
89 print "Chdir to $directory failed: No such file or directory\n";
94 print "Test suite `$tesh_file': NOK (system error)\n";
101 if ($_[0] =~ /^(.*)=(.*)$/) {
103 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
106 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
109 print "[Tesh/INFO] setenv $var=$ctn\n";
110 $environ{$var} = $ctn;
113 # Main option parsing sub
116 # remove the tesh file from the ARGV used
118 $tesh_file = pop @ARGV;
120 # temporary arrays for GetOption
132 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
135 'help|h' => \$opt{'help'},
137 'verbose|v' => \@verbose,
138 'debug|d' => \$opt{"debug"},
140 'difftool=s' => \$diff_tool,
143 'timeout=s' => \$opt{'timeout'},
144 'setenv=s' => \&setenv_cmd,
147 'enable-coverage+' => \$enable_coverage,
150 if($enable_coverage){
151 print "Enable coverage\n";
155 use File::Temp qw/ tempfile /;
156 ($diff_tool_tmp_fh, $diff_tool_tmp_filename) = tempfile();
157 print "New tesh: $diff_tool_tmp_filename\n";
160 unless($tesh_file=~/(.*)\.tesh/){
161 $tesh_file="(stdin)";
162 $tesh_name="(stdin)";
163 print "Test suite from stdin\n";
166 print "Test suite `$tesh_name'\n";
169 $opt{'verbose'} = scalar @verbose;
171 $opt{'cfg'} .= " --cfg=$_";
176 my %opts = get_options(@ARGV);
200 if (POSIX::WIFEXITED($status)) {
201 $exitcode=POSIX::WEXITSTATUS($status)+40;
202 return "returned code ".POSIX::WEXITSTATUS($status);
203 } elsif (POSIX::WIFSIGNALED($status)) {
205 if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
206 elsif (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
207 elsif (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
208 elsif (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
209 elsif (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
210 $exitcode=POSIX::WTERMSIG($status)+4;
211 return "got signal $code";
213 return "Unparsable status. Is the process stopped?";
216 #if ($@) { # no POSIX available?
217 # warn "POSIX not usable to parse the return value of forked child: $@\n";
219 # return "returned code -1 $@ ";
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
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 (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
286 kill(SIGTERM, $cmd{'pid'});
288 kill(SIGKILL, $cmd{'pid'});
289 } elsif ($OS eq "WIN") {
290 system("TASKKILL /F /T /PID $cmd{'pid'}");
300 # Cleanup the executing child, and kill the timeouter brother on need
301 $cmd{'return'} = 0 unless defined($cmd{'return'});
302 if($cmd{'background'} != 1){
303 waitpid ($cmd{'pid'}, 0);
304 $cmd{'gotret'} = exit_status($?);
307 # & commands, which will be handled at the end
308 push @bg_cmds, \%cmd;
309 # no timeout for background commands
311 kill(SIGKILL, $forked);
321 my $gotret=$cmd{'gotret'};
325 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
326 $wantret = "got signal $cmd{'expect'}";
328 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
331 local *got = $cmd{'got'};
333 # pop all output from executing child
335 while(defined(my $got=<got>)) {
338 print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
340 if (!($enable_coverage and $got=~ /^profiling:/)){
346 # Save the unsorted observed output to report it on error.
347 map { push @{$cmd{'unsorted got'}}, $_ } @got;
350 substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
353 if ($sort_prefix>0) {
354 @got = sort mysort @got;
358 while (@got and $got[0] eq "") {
362 # Sort the expected output to make it easier to write for humans
363 if(defined($cmd{'out'})){
364 if ($sort_prefix>0) {
365 @{$cmd{'out'}} = sort mysort @{$cmd{'out'}};
367 @{$cmd{'out'}} = sort @{$cmd{'out'}};
369 while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
370 shift @{$cmd{'out'}};
375 # Did we timeout ? If yes, handle it. If not, kill the forked process.
377 if($timeout==-1 and ($gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL")){
378 $gotret="return code 0";
380 $gotret= "timeout after $time_to_wait sec";
383 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
387 if($gotret ne $wantret) {
389 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
391 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
393 map {$msg .= "|| $_\n"} @got;
396 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
407 # Check the result of execution
410 if (defined($cmd{'output display'})){
411 print "[Tesh/INFO] Here is the (ignored) command output:\n";
412 map { print "||$_\n" } @got;
414 elsif (!defined($cmd{'output ignore'})){
415 $diff = build_diff(\@{$cmd{'out'}}, \@got);
417 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
420 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
421 map { print "$_\n" } split(/\n/,$diff);
423 print "WARNING: Both the observed output and expected output were sorted as requested.\n";
424 print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
426 print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
427 # print "----8<--------------- Begin of unprocessed observed output (as it should appear in file):\n";
428 # map {print "> $_\n"} @{$cmd{'unsorted got'}};
429 # print "--------------->8---- End of the unprocessed observed output.\n";
432 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
440 my $file = $cmd{'arg'};
441 print "[Tesh/INFO] mkfile $file\n";
444 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
445 print FILE join("\n", @{$cmd{'in'}});
446 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
451 #my $teshfile=$tesh_file;
452 #$teshfile=~ s{\.[^.]+$}{};
454 unless($tesh_file eq "(stdin)"){
455 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
458 my %cmd; # everything about the next command to run
461 LINE: while (not $finished and not $error) {
465 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
468 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
476 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
478 # deal with line continuations
479 while ($line =~ /^(.*?)\\$/) {
481 die "[TESH/CRITICAL] Continued line at end of file\n"
482 unless defined($next);
485 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
489 # Push delayed commands on empty lines
490 unless ($line =~ m/^(.)(.*)$/) {
491 if (defined($cmd{'cmd'})) {
495 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
499 my ($cmd,$arg) = ($1,$2);
500 print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
504 # handle the commands
505 if ($cmd =~ /^#/) { #comment
506 } elsif ($cmd eq '>'){ #expected result line
507 print "[TESH/debug] push expected result\n" if $opts{'debug'};
508 push @{$cmd{'out'}}, $arg;
510 } elsif ($cmd eq '<') { # provided input
511 print "[TESH/debug] push provided input\n" if $opts{'debug'};
512 push @{$cmd{'in'}}, $arg;
514 } elsif ($cmd eq 'p') { # comment
515 print "[$tesh_name:$line_num] $arg\n";
517 } elsif ($cmd eq '$') { # Command
518 # if we have something buffered, run it now
519 if (defined($cmd{'cmd'})) {
523 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
524 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
527 $cmd{'arg'} =~ s/\s*mkfile //;
531 } elsif ($arg =~ /^\s*cd /) {
532 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
533 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
539 } else { # regular command
541 $cmd{'file'} = $tesh_file;
542 $cmd{'line'} = $line_num;
545 elsif($cmd eq '&'){ # parallel command line
547 if (defined($cmd{'cmd'})) {
551 $cmd{'background'} = 1;
553 $cmd{'file'} = $tesh_file;
554 $cmd{'line'} = $line_num;
556 elsif($line =~ /^!\s*output sort/){ #output sort
557 if (defined($cmd{'cmd'})) {
562 if ($line =~ /^!\s*output sort\s+(\d+)/) {
566 elsif($line =~ /^!\s*output ignore/){ #output ignore
567 if (defined($cmd{'cmd'})) {
571 $cmd{'output ignore'} = 1;
573 elsif($line =~ /^!\s*output display/){ #output display
574 if (defined($cmd{'cmd'})) {
578 $cmd{'output display'} = 1;
580 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
581 if (defined($cmd{'cmd'})) {
586 $cmd{'expect'} = "$1";
588 elsif($line =~ /^!\s*expect return/){ #expect return
589 if (defined($cmd{'cmd'})) {
593 $line =~ s/^! expect return //g;
595 $cmd{'return'} = $line;
597 elsif($line =~ /^!\s*setenv/){ #setenv
598 if (defined($cmd{'cmd'})) {
602 $line =~ s/^! setenv //g;
606 elsif($line =~ /^!\s*include/){ #include
607 if (defined($cmd{'cmd'})) {
611 print color("red"), "[Tesh/CRITICAL] need include";
612 print color("reset"), "\n";
615 elsif($line =~ /^!\s*timeout/){ #timeout
616 if (defined($cmd{'cmd'})) {
620 $line =~ s/^! timeout //;
622 $cmd{'timeout'} = $line;
624 die "[TESH/CRITICAL] parse error: $line\n";
627 kill(SIGKILL, $forked);
635 # Deal with last command
636 if (defined($cmd{'cmd'})) {
643 kill(SIGKILL, $forked);
649 waitpid ($test{'pid'}, 0);
650 $test{'gotret'} = exit_status($?);
657 close $diff_tool_tmp_fh;
658 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
659 unlink $diff_tool_tmp_filename;
664 }elsif($tesh_file eq "(stdin)"){
665 print "Test suite from stdin OK\n";
667 print "Test suite `$tesh_name' OK\n";
671 #push @a,"bl1"; push @b,"bl1";
672 #push @a,"bl2"; push @b,"bl2";
673 #push @a,"bl3"; push @b,"bl3";
674 #push @a,"bl4"; push @b,"bl4";
675 #push @a,"bl5"; push @b,"bl5";
676 #push @a,"bl6"; push @b,"bl6";
677 #push @a,"bl7"; push @b,"bl7";
678 ##push @a,"Perl"; push @b,"ruby";
679 #push @a,"END1"; push @b,"END1";
680 #push @a,"END2"; push @b,"END2";
681 #push @a,"END3"; push @b,"END3";
682 #push @a,"END4"; push @b,"END4";
683 #push @a,"END5"; push @b,"END5";
684 #push @a,"END6"; push @b,"END6";
685 #push @a,"END7"; push @b,"END7";
686 #print "Identical:\n". build_diff(\@a,\@b);
689 #push @a,"AZE"; push @b,"EZA";
690 #print "Different:\n".build_diff(\@a,\@b);
692 use lib "@CMAKE_BINARY_DIR@/bin" ;
694 use Diff qw(diff); # postpone a bit to have time to change INC
698 my $diff = Diff->new(@_);
700 $diff->Base( 1 ); # Return line numbers, not indices
701 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
702 return "" if ($chunk_count == 1 && $diff->Same());
704 while( $diff->Next() ) {
705 my @same = $diff->Same();
706 if ($diff->Same() ) {
707 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
708 $res .= ' '.$same[0]."\n" ;
709 $res .= ' '.$same[1]."\n" if (scalar @same>1);
711 $res .= "...\n" if (scalar @same>2);
712 # $res .= $diff->Next(0)."/$chunk_count\n";
713 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
714 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
715 $res .= ' '.$same[scalar @same -1]."\n";
718 next if $diff->Same();
719 map { $res .= "- $_\n" } $diff->Items(1);
720 map { $res .= "+ $_\n" } $diff->Items(2);