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);
55 $ENV{"PRINTF_EXPONENT_DIGITS"} = "2";
59 ## Command line option handling
63 my ($text, $name, $value) = @_;
65 $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
66 $text =~ s/\$$name(\W|$)/$value$1/g;
69 $text =~ s/\${$name:=([^}]*)}/$1/g;
70 $text =~ s/\${$name}//g;
71 $text =~ s/\$$name(\W|$)/$1/g;
76 # option handling helper subs
80 if (-e $directory && -d $directory) {
82 print "[Tesh/INFO] change directory to $directory\n";
84 } elsif (-e $directory) {
85 print "Cannot change directory to '$directory': it is not a directory\n";
87 print "Chdir to $directory failed: No such file or directory\n";
92 print "Test suite `$tesh_file': NOK (system error)\n";
99 if ($_[0] =~ /^(.*)=(.*)$/) {
101 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
104 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
107 print "[Tesh/INFO] setenv $var=$ctn\n";
108 $environ{$var} = $ctn;
111 # Main option parsing sub
114 # remove the tesh file from the ARGV used
116 $tesh_file = pop @ARGV;
118 # temporary arrays for GetOption
130 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
133 'help|h' => \$opt{'help'},
135 'verbose|v' => \@verbose,
136 'debug|d' => \$opt{"debug"},
138 'difftool=s' => \$diff_tool,
141 'timeout=s' => \$opt{'timeout'},
142 'setenv=s' => \&setenv_cmd,
145 'enable-coverage+' => \$enable_coverage,
148 if($enable_coverage){
149 print "Enable coverage\n";
153 use File::Temp qw/ tempfile /;
154 ($diff_tool_tmp_fh, $diff_tool_tmp_filename) = tempfile();
155 print "New tesh: $diff_tool_tmp_filename\n";
158 unless($tesh_file=~/(.*)\.tesh/){
159 $tesh_file="(stdin)";
160 $tesh_name="(stdin)";
161 print "Test suite from stdin\n";
164 print "Test suite `$tesh_name'\n";
167 $opt{'verbose'} = scalar @verbose;
169 $opt{'cfg'} .= " --cfg=$_";
174 my %opts = get_options(@ARGV);
198 if (POSIX::WIFEXITED($status)) {
199 $exitcode=POSIX::WEXITSTATUS($status)+40;
200 return "returned code ".POSIX::WEXITSTATUS($status);
201 } elsif (POSIX::WIFSIGNALED($status)) {
203 if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
204 elsif (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
205 elsif (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
206 elsif (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
207 elsif (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
208 $exitcode=POSIX::WTERMSIG($status)+4;
209 return "got signal $code";
211 return "Unparsable status. Is the process stopped?";
214 #if ($@) { # no POSIX available?
215 # warn "POSIX not usable to parse the return value of forked child: $@\n";
217 # return "returned code -1 $@ ";
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
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 (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
283 kill(SIGKILL, $cmd{'pid'});
289 # Cleanup the executing child, and kill the timeouter brother on need
290 $cmd{'return'} = 0 unless defined($cmd{'return'});
291 if($cmd{'background'} != 1){
292 waitpid ($cmd{'pid'}, 0);
293 $cmd{'gotret'} = exit_status($?);
296 # & commands, which will be handled at the end
297 push @bg_cmds, \%cmd;
298 # no timeout for background commands
300 kill(SIGKILL, $forked);
310 my $gotret=$cmd{'gotret'};
314 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
315 $wantret = "got signal $cmd{'expect'}";
317 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
320 local *got = $cmd{'got'};
322 # pop all output from executing child
324 while(defined(my $got=<got>)) {
327 print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
329 if (!($enable_coverage and $got=~ /^profiling:/)){
335 # Save the unsorted observed output to report it on error.
336 map { push @{$cmd{'unsorted got'}}, $_ } @got;
339 substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
342 @got = sort mysort @got;
343 while (@got and $got[0] eq "") {
347 # Sort the expected output to make it easier to write for humans
348 if(defined($cmd{'out'})){
349 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
350 while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
351 shift @{$cmd{'out'}};
356 #Did we timeout ? If yes, handle it. If not, kill the forked process.
358 if($timeout==-1 and $gotret eq "got signal SIGKILL"){
359 $gotret="return code 0";
361 $gotret= "timeout after $time_to_wait sec";
364 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
368 if($gotret ne $wantret) {
370 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
372 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
374 map {$msg .= "|| $_\n"} @got;
377 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
388 # Check the result of execution
391 if (defined($cmd{'output display'})){
392 print "[Tesh/INFO] Here is the (ignored) command output:\n";
393 map { print "||$_\n" } @got;
395 elsif (!defined($cmd{'output ignore'})){
396 $diff = build_diff(\@{$cmd{'out'}}, \@got);
398 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
401 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"")."\n";
402 map { print "$_\n" } split(/\n/,$diff);
404 print "WARNING: both the observed output and expected output were sorted as requested.\n";
405 print "----8<--------------- Begin of unprocessed observed output (as it should appear in file):\n";
406 map {print "> $_\n"} @{$cmd{'unsorted got'}};
407 print "--------------->8---- End of the unprocessed observed output.\n";
410 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 $teshfile=$tesh_file;
430 #$teshfile=~ s{\.[^.]+$}{};
432 unless($tesh_file eq "(stdin)"){
433 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
436 my %cmd; # everything about the next command to run
439 LINE: while (not $finished and not $error) {
443 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
446 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
454 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
456 # deal with line continuations
457 while ($line =~ /^(.*?)\\$/) {
459 die "[TESH/CRITICAL] Continued line at end of file\n"
460 unless defined($next);
463 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
467 # Push delayed commands on empty lines
468 unless ($line =~ m/^(.)(.*)$/) {
469 if (defined($cmd{'cmd'})) {
473 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
477 my ($cmd,$arg) = ($1,$2);
478 print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
482 # handle the commands
483 if ($cmd =~ /^#/) { #comment
484 } elsif ($cmd eq '>'){ #expected result line
485 print "[TESH/debug] push expected result\n" if $opts{'debug'};
486 push @{$cmd{'out'}}, $arg;
488 } elsif ($cmd eq '<') { # provided input
489 print "[TESH/debug] push provided input\n" if $opts{'debug'};
490 push @{$cmd{'in'}}, $arg;
492 } elsif ($cmd eq 'p') { # comment
493 print "[$tesh_name:$line_num] $arg\n";
495 } elsif ($cmd eq '$') { # Command
496 # if we have something buffered, run it now
497 if (defined($cmd{'cmd'})) {
501 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
502 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
505 $cmd{'arg'} =~ s/\s*mkfile //;
509 } elsif ($arg =~ /^\s*cd /) {
510 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
511 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
517 } else { # regular command
519 $cmd{'file'} = $tesh_file;
520 $cmd{'line'} = $line_num;
523 elsif($cmd eq '&'){ # parallel command line
525 if (defined($cmd{'cmd'})) {
529 $cmd{'background'} = 1;
531 $cmd{'file'} = $tesh_file;
532 $cmd{'line'} = $line_num;
534 elsif($line =~ /^!\s*output sort/){ #output sort
535 if (defined($cmd{'cmd'})) {
540 if ($line =~ /^!\s*output sort\s+(\d+)/) {
544 elsif($line =~ /^!\s*output ignore/){ #output ignore
545 if (defined($cmd{'cmd'})) {
549 $cmd{'output ignore'} = 1;
551 elsif($line =~ /^!\s*output display/){ #output display
552 if (defined($cmd{'cmd'})) {
556 $cmd{'output display'} = 1;
558 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
559 if (defined($cmd{'cmd'})) {
564 $cmd{'expect'} = "$1";
566 elsif($line =~ /^!\s*expect return/){ #expect return
567 if (defined($cmd{'cmd'})) {
571 $line =~ s/^! expect return //g;
573 $cmd{'return'} = $line;
575 elsif($line =~ /^!\s*setenv/){ #setenv
576 if (defined($cmd{'cmd'})) {
580 $line =~ s/^! setenv //g;
584 elsif($line =~ /^!\s*include/){ #include
585 if (defined($cmd{'cmd'})) {
589 print color("red"), "[Tesh/CRITICAL] need include";
590 print color("reset"), "\n";
593 elsif($line =~ /^!\s*timeout/){ #timeout
594 if (defined($cmd{'cmd'})) {
598 $line =~ s/^! timeout //;
600 $cmd{'timeout'} = $line;
602 die "[TESH/CRITICAL] parse error: $line\n";
605 kill(SIGKILL, $forked);
613 # Deal with last command
614 if (defined($cmd{'cmd'})) {
621 kill(SIGKILL, $forked);
627 waitpid ($test{'pid'}, 0);
628 $test{'gotret'} = exit_status($?);
635 close $diff_tool_tmp_fh;
636 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
637 unlink $diff_tool_tmp_filename;
642 }elsif($tesh_file eq "(stdin)"){
643 print "Test suite from stdin OK\n";
645 print "Test suite `$tesh_name' OK\n";
649 #push @a,"bl1"; push @b,"bl1";
650 #push @a,"bl2"; push @b,"bl2";
651 #push @a,"bl3"; push @b,"bl3";
652 #push @a,"bl4"; push @b,"bl4";
653 #push @a,"bl5"; push @b,"bl5";
654 #push @a,"bl6"; push @b,"bl6";
655 #push @a,"bl7"; push @b,"bl7";
656 ##push @a,"Perl"; push @b,"ruby";
657 #push @a,"END1"; push @b,"END1";
658 #push @a,"END2"; push @b,"END2";
659 #push @a,"END3"; push @b,"END3";
660 #push @a,"END4"; push @b,"END4";
661 #push @a,"END5"; push @b,"END5";
662 #push @a,"END6"; push @b,"END6";
663 #push @a,"END7"; push @b,"END7";
664 #print "Identical:\n". build_diff(\@a,\@b);
667 #push @a,"AZE"; push @b,"EZA";
668 #print "Different:\n".build_diff(\@a,\@b);
670 use lib "@CMAKE_BINARY_DIR@/bin" ;
672 use Diff qw(diff); # postpone a bit to have time to change INC
676 my $diff = Diff->new(@_);
678 $diff->Base( 1 ); # Return line numbers, not indices
679 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
680 return "" if ($chunk_count == 1 && $diff->Same());
682 while( $diff->Next() ) {
683 my @same = $diff->Same();
684 if ($diff->Same() ) {
685 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
686 $res .= ' '.$same[0]."\n" ;
687 $res .= ' '.$same[1]."\n" if (scalar @same>1);
689 $res .= "...\n" if (scalar @same>2);
690 # $res .= $diff->Next(0)."/$chunk_count\n";
691 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
692 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
693 $res .= ' '.$same[scalar @same -1]."\n";
696 next if $diff->Same();
697 map { $res .= "- $_\n" } $diff->Items(1);
698 map { $res .= "+ $_\n" } $diff->Items(2);