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 # Existing OSes: https://metacpan.org/source/SMUELLER/PathTools-3.47/lib/File/Spec.pm
51 if($^O eq "linux" || $^O eq "MacOS"){
53 } elsif ($^O eq "MSWin32") {
55 $ENV{"PRINTF_EXPONENT_DIGITS"} = "2";
57 die "Tesh: Unknown operating system: $^O\n";
59 use if $^O eq 'MSWin32', 'Win32::Job';
62 ## Command line option handling
66 my ($text, $name, $value) = @_;
68 $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
69 $text =~ s/\$$name(\W|$)/$value$1/g;
72 $text =~ s/\${$name:=([^}]*)}/$1/g;
73 $text =~ s/\${$name}//g;
74 $text =~ s/\$$name(\W|$)/$1/g;
79 # option handling helper subs
83 if (-e $directory && -d $directory) {
85 print "[Tesh/INFO] change directory to $directory\n";
87 } elsif (-e $directory) {
88 print "Cannot change directory to '$directory': it is not a directory\n";
90 print "Chdir to $directory failed: No such file or directory\n";
95 print "Test suite `$tesh_file': NOK (system error)\n";
102 if ($_[0] =~ /^(.*)=(.*)$/) {
104 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
107 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
110 print "[Tesh/INFO] setenv $var=$ctn\n";
111 $environ{$var} = $ctn;
114 # Main option parsing sub
117 # remove the tesh file from the ARGV used
119 $tesh_file = pop @ARGV;
121 # temporary arrays for GetOption
133 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
136 'help|h' => \$opt{'help'},
138 'verbose|v' => \@verbose,
139 'debug|d' => \$opt{"debug"},
141 'difftool=s' => \$diff_tool,
144 'timeout=s' => \$opt{'timeout'},
145 'setenv=s' => \&setenv_cmd,
148 'enable-coverage+' => \$enable_coverage,
151 if($enable_coverage){
152 print "Enable coverage\n";
156 use File::Temp qw/ tempfile /;
157 ($diff_tool_tmp_fh, $diff_tool_tmp_filename) = tempfile();
158 print "New tesh: $diff_tool_tmp_filename\n";
161 unless($tesh_file=~/(.*)\.tesh/){
162 $tesh_file="(stdin)";
163 $tesh_name="(stdin)";
164 print "Test suite from stdin\n";
167 print "Test suite `$tesh_name'\n";
170 $opt{'verbose'} = scalar @verbose;
172 $opt{'cfg'} .= " --cfg=$_";
177 my %opts = get_options(@ARGV);
201 if (POSIX::WIFEXITED($status)) {
202 $exitcode=POSIX::WEXITSTATUS($status)+40;
203 return "returned code ".POSIX::WEXITSTATUS($status);
204 } elsif (POSIX::WIFSIGNALED($status)) {
206 if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
207 elsif (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
208 elsif (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
209 elsif (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
210 elsif (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
211 $exitcode=POSIX::WTERMSIG($status)+4;
212 return "got signal $code";
214 return "Unparsable status. Is the process stopped?";
217 #if ($@) { # no POSIX available?
218 # warn "POSIX not usable to parse the return value of forked child: $@\n";
220 # return "returned code -1 $@ ";
226 if ($opts{'debug'}) {
228 map {print " $_"} @{$cmd{'in'}};
231 map {print " $_"} @{$cmd{'out'}};
233 print "CMD: $cmd{'cmd'}\n";
236 # cleanup the command line
238 var_subst($cmd{'cmd'}, "EXEEXT", ".exe");
240 var_subst($cmd{'cmd'}, "EXEEXT", "");
243 # substitute environ variables
244 foreach my $key (keys %environ) {
245 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $key, $environ{$key});
247 # substitute remaining variables, if any
248 while ($cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/) {
249 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
251 while ($cmd{'cmd'} =~ /\$(\w+)/) {
252 $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
256 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
259 $cmd{'cmd'} =~ s/^\s+//;
260 $cmd{'cmd'} =~ s/\s+$//;
262 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
265 # exec the command line
266 ### $line =~ s/\r//g;
268 $cmd{'got'} = IO::File->new_tmpfile;
269 $cmd{'got'}->autoflush(1);
270 local *E = $cmd{'got'};
271 $cmd{'pid'} = open3(\*CHILD_IN, ">&E", ">&E",
272 quotewords('\s+', 0, $cmd{'cmd'}));
274 # push all provided input to executing child
275 map { print CHILD_IN "$_\n"; } @{$cmd{'in'}};
278 # if timeout specified, fork and kill executing child at the end of timeout
279 if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
280 $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
283 die "fork() failed: $!" unless defined $forked;
284 if ( $forked == 0 ) { # child
287 kill(SIGTERM, $cmd{'pid'});
289 kill(SIGKILL, $cmd{'pid'});
290 } elsif ($OS eq "WIN") {
291 system("TASKKILL /F /T /PID $cmd{'pid'}");
301 # Cleanup the executing child, and kill the timeouter brother on need
302 $cmd{'return'} = 0 unless defined($cmd{'return'});
303 if($cmd{'background'} != 1){
304 waitpid ($cmd{'pid'}, 0);
305 $cmd{'gotret'} = exit_status($?);
308 # & commands, which will be handled at the end
309 push @bg_cmds, \%cmd;
310 # no timeout for background commands
312 kill(SIGKILL, $forked);
322 my $gotret=$cmd{'gotret'};
326 if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
327 $wantret = "got signal $cmd{'expect'}";
329 $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
332 local *got = $cmd{'got'};
334 # pop all output from executing child
336 while(defined(my $got=<got>)) {
339 print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
341 if (!($enable_coverage and $got=~ /^profiling:/)){
347 # Save the unsorted observed output to report it on error.
348 map { push @{$cmd{'unsorted got'}}, $_ } @got;
351 substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
354 if ($sort_prefix>0) {
355 @got = sort mysort @got;
359 while (@got and $got[0] eq "") {
363 # Sort the expected output to make it easier to write for humans
364 if(defined($cmd{'out'})){
365 if ($sort_prefix>0) {
366 @{$cmd{'out'}} = sort mysort @{$cmd{'out'}};
368 @{$cmd{'out'}} = sort @{$cmd{'out'}};
370 while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
371 shift @{$cmd{'out'}};
376 # Did we timeout ? If yes, handle it. If not, kill the forked process.
378 if($timeout==-1 and ($gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL")){
379 $gotret="return code 0";
381 $gotret= "timeout after $time_to_wait sec";
384 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
388 if($gotret ne $wantret) {
390 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
392 $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
394 map {$msg .= "|| $_\n"} @got;
397 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
408 # Check the result of execution
411 if (defined($cmd{'output display'})){
412 print "[Tesh/INFO] Here is the (ignored) command output:\n";
413 map { print "||$_\n" } @got;
415 elsif (!defined($cmd{'output ignore'})){
416 $diff = build_diff(\@{$cmd{'out'}}, \@got);
418 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
421 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
422 map { print "$_\n" } split(/\n/,$diff);
424 print "WARNING: Both the observed output and expected output were sorted as requested.\n";
425 print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
427 print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
428 # print "----8<--------------- Begin of unprocessed observed output (as it should appear in file):\n";
429 # map {print "> $_\n"} @{$cmd{'unsorted got'}};
430 # print "--------------->8---- End of the unprocessed observed output.\n";
433 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
441 my $file = $cmd{'arg'};
442 print "[Tesh/INFO] mkfile $file\n";
445 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
446 print FILE join("\n", @{$cmd{'in'}});
447 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
452 #my $teshfile=$tesh_file;
453 #$teshfile=~ s{\.[^.]+$}{};
455 unless($tesh_file eq "(stdin)"){
456 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
459 my %cmd; # everything about the next command to run
462 LINE: while (not $finished and not $error) {
466 if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
469 }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
477 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
479 # deal with line continuations
480 while ($line =~ /^(.*?)\\$/) {
482 die "[TESH/CRITICAL] Continued line at end of file\n"
483 unless defined($next);
486 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
490 # Push delayed commands on empty lines
491 unless ($line =~ m/^(.)(.*)$/) {
492 if (defined($cmd{'cmd'})) {
496 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
500 my ($cmd,$arg) = ($1,$2);
501 print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
505 # handle the commands
506 if ($cmd =~ /^#/) { #comment
507 } elsif ($cmd eq '>'){ #expected result line
508 print "[TESH/debug] push expected result\n" if $opts{'debug'};
509 push @{$cmd{'out'}}, $arg;
511 } elsif ($cmd eq '<') { # provided input
512 print "[TESH/debug] push provided input\n" if $opts{'debug'};
513 push @{$cmd{'in'}}, $arg;
515 } elsif ($cmd eq 'p') { # comment
516 print "[$tesh_name:$line_num] $arg\n";
518 } elsif ($cmd eq '$') { # Command
519 # if we have something buffered, run it now
520 if (defined($cmd{'cmd'})) {
524 if ($arg =~ /^\s*mkfile /){ # "mkfile" command line
525 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
528 $cmd{'arg'} =~ s/\s*mkfile //;
532 } elsif ($arg =~ /^\s*cd /) {
533 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
534 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
540 } else { # regular command
542 $cmd{'file'} = $tesh_file;
543 $cmd{'line'} = $line_num;
546 elsif($cmd eq '&'){ # parallel command line
548 if (defined($cmd{'cmd'})) {
552 $cmd{'background'} = 1;
554 $cmd{'file'} = $tesh_file;
555 $cmd{'line'} = $line_num;
557 elsif($line =~ /^!\s*output sort/){ #output sort
558 if (defined($cmd{'cmd'})) {
563 if ($line =~ /^!\s*output sort\s+(\d+)/) {
567 elsif($line =~ /^!\s*output ignore/){ #output ignore
568 if (defined($cmd{'cmd'})) {
572 $cmd{'output ignore'} = 1;
574 elsif($line =~ /^!\s*output display/){ #output display
575 if (defined($cmd{'cmd'})) {
579 $cmd{'output display'} = 1;
581 elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
582 if (defined($cmd{'cmd'})) {
587 $cmd{'expect'} = "$1";
589 elsif($line =~ /^!\s*expect return/){ #expect return
590 if (defined($cmd{'cmd'})) {
594 $line =~ s/^! expect return //g;
596 $cmd{'return'} = $line;
598 elsif($line =~ /^!\s*setenv/){ #setenv
599 if (defined($cmd{'cmd'})) {
603 $line =~ s/^! setenv //g;
607 elsif($line =~ /^!\s*include/){ #include
608 if (defined($cmd{'cmd'})) {
612 print color("red"), "[Tesh/CRITICAL] need include";
613 print color("reset"), "\n";
616 elsif($line =~ /^!\s*timeout/){ #timeout
617 if (defined($cmd{'cmd'})) {
621 $line =~ s/^! timeout //;
623 $cmd{'timeout'} = $line;
625 die "[TESH/CRITICAL] parse error: $line\n";
628 kill(SIGKILL, $forked);
636 # Deal with last command
637 if (defined($cmd{'cmd'})) {
644 kill(SIGKILL, $forked);
650 waitpid ($test{'pid'}, 0);
651 $test{'gotret'} = exit_status($?);
658 close $diff_tool_tmp_fh;
659 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
660 unlink $diff_tool_tmp_filename;
665 }elsif($tesh_file eq "(stdin)"){
666 print "Test suite from stdin OK\n";
668 print "Test suite `$tesh_name' OK\n";
672 #push @a,"bl1"; push @b,"bl1";
673 #push @a,"bl2"; push @b,"bl2";
674 #push @a,"bl3"; push @b,"bl3";
675 #push @a,"bl4"; push @b,"bl4";
676 #push @a,"bl5"; push @b,"bl5";
677 #push @a,"bl6"; push @b,"bl6";
678 #push @a,"bl7"; push @b,"bl7";
679 ##push @a,"Perl"; push @b,"ruby";
680 #push @a,"END1"; push @b,"END1";
681 #push @a,"END2"; push @b,"END2";
682 #push @a,"END3"; push @b,"END3";
683 #push @a,"END4"; push @b,"END4";
684 #push @a,"END5"; push @b,"END5";
685 #push @a,"END6"; push @b,"END6";
686 #push @a,"END7"; push @b,"END7";
687 #print "Identical:\n". build_diff(\@a,\@b);
690 #push @a,"AZE"; push @b,"EZA";
691 #print "Different:\n".build_diff(\@a,\@b);
693 use lib "@CMAKE_BINARY_DIR@/bin" ;
695 use Diff qw(diff); # postpone a bit to have time to change INC
699 my $diff = Diff->new(@_);
701 $diff->Base( 1 ); # Return line numbers, not indices
702 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
703 return "" if ($chunk_count == 1 && $diff->Same());
705 while( $diff->Next() ) {
706 my @same = $diff->Same();
707 if ($diff->Same() ) {
708 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
709 $res .= ' '.$same[0]."\n" ;
710 $res .= ' '.$same[1]."\n" if (scalar @same>1);
712 $res .= "...\n" if (scalar @same>2);
713 # $res .= $diff->Next(0)."/$chunk_count\n";
714 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
715 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
716 $res .= ' '.$same[scalar @same -1]."\n";
719 next if $diff->Same();
720 map { $res .= "- $_\n" } $diff->Items(1);
721 map { $res .= "+ $_\n" } $diff->Items(2);