Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
not implicit on all systems
[simgrid.git] / buildtools / Cmake / Scripts / tesh.pl
1 #! /usr/bin/perl
2 eval 'exec perl -S $0 ${1+"$@"}'
3   if $running_under_some_shell;
4
5 =encoding UTF-8
6
7 =head1 NAME
8
9 tesh -- testing shell
10
11 =head1 SYNOPSIS
12
13 B<tesh> [I<options>] I<tesh_file>
14
15 =cut
16 my($bindir)=".";
17 my($srcdir)=".";
18 my($timeout)=0;
19 my($time_to_wait)=0;
20 my $path = $0;
21 my $OS;
22 my $enable_coverage=0;
23 my $tesh_file;
24 my $tesh_name;
25 my $error=0;
26 my $exitcode=0;
27 my @bg_cmds;
28
29 $path =~ s|[^/]*$||;
30 push @INC,$path;
31
32 use Getopt::Long qw(GetOptions);
33 use strict;
34 use Term::ANSIColor;
35 use IPC::Open3;
36 use IO::File;
37
38 if($^O eq "linux"){
39     $OS = "UNIX";
40 }
41 else{
42     $OS = "WIN";
43     $ENV{"PRINTF_EXPONENT_DIGITS"} = "2"; 
44 }
45
46
47 sub trim($)
48 {
49     my $string = shift;
50     $string =~ s/^\s+//;
51     $string =~ s/\s+$//;
52     return $string;
53 }
54
55 # make sure we received a tesh file
56 scalar @ARGV > 0 || die "Usage:\n  tesh [*options*] *tesh_file*\n";
57
58 #Add current directory to path
59 $ENV{PATH} = "$ENV{PATH}:.";
60
61 ##
62 ## Command line option handling
63 ##
64
65 # option handling helper subs
66 sub cd_cmd {
67   my $directory=$_[1];
68   my $failure=1;
69   if (-e $directory && -d $directory) {
70     chdir("$directory");
71     print "[Tesh/INFO] change directory to $directory\n";
72   $failure=0;
73   } elsif (-e $directory) {
74     print "Cannot change directory to '$directory': it is not a directory\n";
75   } else {
76     print "Chdir to $directory failed: No such file or directory\n";
77   }
78   if($failure==1){
79   $error=1;
80   $exitcode=4;
81   print "Test suite `$tesh_file': NOK (system error)\n";
82   exit 4;
83   }
84 }
85
86 sub setenv_cmd {
87   my($var,$ctn);
88   if ($_[0] =~ /^(.*)=(.*)$/) {
89     ($var,$ctn)=($1,$2);
90   }elsif ($_[1] =~ /^(.*)=(.*)$/) {
91     ($var,$ctn)=($1,$2);
92   } else { 
93       die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
94   }
95     
96     if($var =~ /bindir/){
97         print "[Tesh/INFO] setenv $var=$ctn\n";
98         $bindir = $ctn;
99     }
100     else
101     {
102         if($var =~ /srcdir/){
103             $srcdir = $ctn;
104         }
105         else{
106             $ENV{$var} = $ctn;
107             print "[Tesh/INFO] setenv $var=$ctn\n";
108         }
109     }    
110 }
111
112 # Main option parsing sub
113
114 sub get_options {
115   # remove the tesh file from the ARGV used
116   my @ARGV = @_;
117   $tesh_file = pop @ARGV;
118
119   # temporary arrays for GetOption
120   my @verbose = ();
121   my @cfg;
122   my $log; # ignored
123
124
125   my %opt = (
126     "help"  => 0,
127     "debug"   => 0,
128     "verbose" => 0
129     );
130
131   Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
132   
133   GetOptions(
134     'help|h'   => \$opt{'help'},
135
136     'verbose|v'  => \@verbose,
137     'debug|d'  => \$opt{"debug"},
138
139     'cd=s'     => \&cd_cmd,
140     'timeout=s'  => \$opt{'timeout'},    
141     'setenv=s'   => \&setenv_cmd,
142     'cfg=s'    => \@cfg,
143     'log=s'    => \$log,
144     'enable-coverage+'  => \$enable_coverage,    
145     );
146
147   if($enable_coverage){
148     print "Enable coverage\n";
149   }
150
151   unless($tesh_file=~/(.*)\.tesh/){
152     $tesh_file="(stdin)";
153     $tesh_name="(stdin)";
154     print "Test suite from stdin\n";
155   }else{
156     $tesh_name=$1;
157     print "Test suite `$tesh_name'\n";
158   }
159
160   $opt{'verbose'} = scalar @verbose;
161   foreach (@cfg) {
162     $opt{'cfg'} .= " --cfg=$_";
163   }
164   return %opt;
165 }
166
167 my %opts = get_options(@ARGV);
168
169 ##
170 ## File parsing
171 ##
172 my($nb_arg)=0;
173 my($old_buffer);
174 my($linebis);
175 my($SIGABRT)=0;
176 my($verbose)=0;
177 my($return)=-1;
178 my($pid);
179 my($result);
180 my($result_err);
181 my($forked);
182 my($config)="";
183 my($tesh_command)=0;
184 my(@buffer_tesh)=();
185
186 #eval {
187   use POSIX;
188
189   sub exit_status {
190     my $status = shift;
191     if (POSIX::WIFEXITED($status)) {
192       $exitcode=POSIX::WEXITSTATUS($status)+40;
193       return "returned code ".POSIX::WEXITSTATUS($status);
194     } elsif (POSIX::WIFSIGNALED($status)) {
195       my $code;
196       if (POSIX::WTERMSIG($status) == SIGINT){$code="SIGINT"; }
197       elsif  (POSIX::WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
198       elsif  (POSIX::WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
199       elsif  (POSIX::WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
200       elsif  (POSIX::WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
201       $exitcode=POSIX::WTERMSIG($status)+4;
202       return "got signal $code";
203     }
204     return "Unparsable status. Is the process stopped?";
205   }
206 #};
207 #if ($@) { # no POSIX available?
208 #  warn "POSIX not usable to parse the return value of forked child: $@\n";
209 #  sub exit_status {
210 #    return "returned code -1 $@ ";
211 #  }
212 #}
213
214 sub exec_cmd { 
215   my %cmd = %{$_[0]};
216   if ($opts{'debug'}) {
217     print "IN BEGIN\n";
218     map {print "  $_"} @{$cmd{'in'}};
219     print "IN END\n";
220     print "OUT BEGIN\n";
221     map {print "  $_"} @{$cmd{'out'}};
222     print "OUT END\n";
223     print "CMD: $cmd{'cmd'}\n";
224   }
225
226   # cleanup the command line
227   if($OS eq "WIN"){
228         $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
229         $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
230         $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
231     }
232     else{
233         $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
234     }
235   $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
236   $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
237   $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
238   $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
239   $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
240   $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
241 # $cmd{'cmd'} =~ s|^\./||g;
242 #  $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
243   $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
244   $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
245
246   print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
247
248   ###
249   # exec the command line
250   ###  $line =~ s/\r//g;
251
252   $cmd{'got'} = IO::File->new_tmpfile;
253   $cmd{'got'}->autoflush(1);
254   local *E = $cmd{'got'}; 
255   $cmd{'pid'} = open3(\*CHILD_IN,  ">&E",  ">&E", $cmd{'cmd'} );
256
257   # push all provided input to executing child
258   map { print CHILD_IN "$_\n"; }  @{$cmd{'in'}};
259   close CHILD_IN;
260
261   # if timeout specified, fork and kill executing child at the end of timeout
262   if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
263     $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
264     $forked = fork();
265     $timeout=-1;
266     die "fork() failed: $!" unless defined $forked;
267     if ( $forked == 0 ) { # child
268       sleep $time_to_wait;
269       kill(SIGKILL, $cmd{'pid'});
270       exit $time_to_wait;
271     }
272   }
273
274   
275   # Cleanup the executing child, and kill the timeouter brother on need
276   $cmd{'return'} = 0 unless defined($cmd{'return'});
277   if($cmd{'background'} != 1){
278     waitpid ($cmd{'pid'}, 0);
279     $cmd{'gotret'} = exit_status($?);
280     parse_out(\%cmd);
281   }else{
282     # & commands, which will be handled at the end
283     push @bg_cmds, \%cmd;
284     # no timeout for background commands
285     if($forked){
286        kill(SIGKILL, $forked);
287        $timeout=0;
288        $forked=0;
289     }
290   }
291 }
292
293
294 sub parse_out { 
295   my %cmd = %{$_[0]};
296   my $gotret=$cmd{'gotret'};
297
298   my $wantret;
299
300   if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
301     $wantret = "got signal $cmd{'expect'}";
302   }else{
303     $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
304   }
305
306   local *got = $cmd{'got'};
307   seek(got,0,0);
308   # pop all output from executing child
309   my @got;
310   while(defined(my $got=<got>)) {
311     $got =~ s/\r//g;
312     $got =~ s/^( )*//g;
313     chomp $got;
314     $got=trim($got);
315     if( $got ne ""){
316         if (!($enable_coverage and $got=~ /^profiling:/)){    
317         push @got, "$got";
318      }
319   }
320   }    
321
322   if ($cmd{'sort'}){   
323     sub mysort{
324     $a cmp $b
325     }
326     use sort qw(defaults _quicksort); # force quicksort
327     @got = sort mysort @got;
328     #also resort the other one, as perl sort is not the same as the C one used to generate teshes
329     if(defined($cmd{'out'})){
330       @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
331     }
332   }
333
334   #Did we timeout ? If yes, handle it. If not, kill the forked process.
335
336   if($timeout==-1 and $gotret eq "got signal SIGKILL"){
337     $gotret="return code 0";
338     $timeout=1;
339     $gotret= "timeout after $time_to_wait sec";
340     $error=1;
341     $exitcode=3;
342     print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
343   }else{
344     $timeout=0;  
345   }
346   if($gotret ne $wantret) {
347     $error=1;
348     my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
349     if ($timeout!=1) {
350         $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";    
351     }
352     map {$msg .=  "|| $_\n"} @got;
353     if(!@got) {
354         if($timeout==1){
355         print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
356         }else{
357         $msg .= "||\n";
358         }
359     }
360     $timeout = 0;
361     print STDERR "$msg";
362   }
363
364       
365   ###
366   # Check the result of execution 
367   ###
368   my $diff;
369   if (!defined($cmd{'output ignore'})){
370     $diff = build_diff(\@{$cmd{'out'}}, \@got);
371   }else{
372   print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
373   }
374   if (length $diff) {
375     print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
376     map { print "$_\n" } split(/\n/,$diff);
377
378     print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
379     $error=1;
380     $exitcode=2;
381   }
382 }
383
384 sub mkfile_cmd {
385   my %cmd = %{$_[0]};
386   my $file = $cmd{'arg'};
387   print "[Tesh/INFO] mkfile $file\n";
388
389   unlink($file);
390   open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
391   print FILE join("\n", @{$cmd{'in'}});
392   print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
393   close(FILE);
394 }
395
396 # parse tesh file
397 #my $teshfile=$tesh_file;
398 #$teshfile=~ s{\.[^.]+$}{};
399
400 unless($tesh_file eq "(stdin)"){
401   open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
402 }
403
404 my %cmd; # everything about the next command to run
405 my $line_num=0;
406 my $finished =0;
407 LINE: while (not $finished and not $error) {
408   my $line;
409
410
411   if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
412     $finished=1;
413     next LINE;
414   }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
415     $finished=1;
416     next LINE;
417   }
418
419
420   $line_num++;
421   chomp $line;
422   $line =~ s/\r//g;
423   print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
424   my $next;
425   # deal with line continuations
426   while ($line =~ /^(.*?)\\$/) {
427     $next=<TESH_FILE>;
428     die "[TESH/CRITICAL] Continued line at end of file\n"
429       unless defined($next);
430     $line_num++;
431     chomp $next;
432     print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
433     $line = $1.$next;
434   }
435
436   # Push delayed commands on empty lines
437   unless ($line =~ m/^(.).(.*)$/) {
438     if (defined($cmd{'cmd'}))  {
439       exec_cmd(\%cmd);
440       %cmd = ();
441     }
442     next LINE;
443   }     
444  
445   my ($cmd,$arg) = ($1,$2);
446   $arg =~ s/\r//g;
447   $arg =~ s/\\\\/\\/g;
448   # handle the commands
449   if ($cmd =~ /^#/) {    #comment
450   } elsif ($cmd eq '>'){    #expected result line
451     print "[TESH/debug] push expected result\n" if $opts{'debug'};
452   $arg=trim($arg);
453     if($arg ne ""){
454     push @{$cmd{'out'}}, $arg;
455   }
456
457   } elsif ($cmd eq '<') {    # provided input
458     print "[TESH/debug] push provided input\n" if $opts{'debug'};
459     push @{$cmd{'in'}}, $arg;
460
461   } elsif ($cmd eq 'p') {    # comment
462     print "[$tesh_name:$line_num] $arg\n";
463
464   } elsif ($cmd eq '$') {  # Command
465     # if we have something buffered, run it now
466     if (defined($cmd{'cmd'})) {
467       exec_cmd(\%cmd);
468       %cmd = ();
469     }
470     if ($arg =~ /^\s*mkfile /){      # "mkfile" command line
471       die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
472
473       $cmd{'arg'} = $arg;
474       $cmd{'arg'} =~ s/\s*mkfile //;
475       mkfile_cmd(\%cmd);
476       %cmd = ();
477
478     } elsif ($arg =~ /^\s*cd /) {
479       die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
480       die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
481
482       $arg =~ s/^ *cd //;
483       cd_cmd("",$arg);
484       %cmd = ();
485
486     } else { # regular command
487       $cmd{'cmd'} = $arg;
488       $cmd{'file'} = $tesh_file;
489       $cmd{'line'} = $line_num;
490     }
491   }
492   elsif($cmd eq '&'){      # parallel command line
493
494     if (defined($cmd{'cmd'})) {
495       exec_cmd(\%cmd);
496       %cmd = ();
497     }
498     $cmd{'background'} = 1;
499     $cmd{'cmd'} = $arg;
500     $cmd{'file'} = $tesh_file;
501     $cmd{'line'} = $line_num;
502   }    
503   elsif($line =~ /^!\s*output sort/){    #output sort
504     if (defined($cmd{'cmd'})) {
505       exec_cmd(\%cmd);
506       %cmd = ();
507     }
508     $cmd{'sort'} = 1;
509   }
510   elsif($line =~ /^!\s*output ignore/){    #output ignore
511     if (defined($cmd{'cmd'})) {
512       exec_cmd(\%cmd);
513       %cmd = ();
514     }
515     $cmd{'output ignore'} = 1;
516   }
517   elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
518     if (defined($cmd{'cmd'})) {
519       exec_cmd(\%cmd);
520       %cmd = ();
521     }
522 print "hey\n";
523     $cmd{'expect'} = "$1";
524   }
525   elsif($line =~ /^!\s*expect return/){    #expect return
526     if (defined($cmd{'cmd'})) {
527       exec_cmd(\%cmd);
528       %cmd = ();
529     }
530     $line =~ s/^! expect return //g;
531     $line =~ s/\r//g;
532     $cmd{'return'} = $line;
533   }
534   elsif($line =~ /^!\s*setenv/){    #setenv
535     if (defined($cmd{'cmd'})) {
536       exec_cmd(\%cmd);
537       %cmd = ();
538     }
539     $line =~ s/^! setenv //g;
540     $line =~ s/\r//g;
541     setenv_cmd($line);
542   }
543   elsif($line =~ /^!\s*include/){    #output sort
544     if (defined($cmd{'cmd'})) {
545       exec_cmd(\%cmd);
546       %cmd = ();
547     }
548     print color("red"), "[Tesh/CRITICAL] need include";
549     print color("reset"), "\n";
550     die;
551   }
552   elsif($line =~ /^!\s*timeout/){    #timeout
553     if (defined($cmd{'cmd'})) {
554       exec_cmd(\%cmd);
555       %cmd = ();
556     }
557     $line =~ s/^! timeout //;
558     $line =~ s/\r//g;
559     $cmd{'timeout'} = $line;
560   } else {
561     die "[TESH/CRITICAL] parse error: $line\n";
562   }
563   if($forked){
564    kill(SIGKILL, $forked);
565    $timeout=0;
566   }
567
568 }
569
570
571
572 # Deal with last command
573 if (defined($cmd{'cmd'})) {
574   exec_cmd(\%cmd);
575   %cmd = ();
576 }
577
578
579 if($forked){
580    kill(SIGKILL, $forked);
581    $timeout=0;
582 }
583
584 foreach(@bg_cmds){
585   my %test=%{$_};
586   waitpid ($test{'pid'}, 0);
587   $test{'gotret'} = exit_status($?);
588   parse_out(\%test);
589 }
590
591 @bg_cmds=();
592
593
594 if($error !=0){
595     exit $exitcode;
596 }elsif($tesh_file eq "(stdin)"){
597     print "Test suite from stdin OK\n";
598 }else{
599     print "Test suite `$tesh_name' OK\n";
600 }
601
602 #my (@a,@b);
603 #push @a,"bl1";   push @b,"bl1";
604 #push @a,"bl2";   push @b,"bl2";
605 #push @a,"bl3";   push @b,"bl3";
606 #push @a,"bl4";   push @b,"bl4";
607 #push @a,"bl5";   push @b,"bl5";
608 #push @a,"bl6";   push @b,"bl6";
609 #push @a,"bl7";   push @b,"bl7";
610 ##push @a,"Perl";  push @b,"ruby";
611 #push @a,"END1";   push @b,"END1";
612 #push @a,"END2";   push @b,"END2";
613 #push @a,"END3";   push @b,"END3";
614 #push @a,"END4";   push @b,"END4";
615 #push @a,"END5";   push @b,"END5";
616 #push @a,"END6";   push @b,"END6";
617 #push @a,"END7";   push @b,"END7";
618 #print "Identical:\n". build_diff(\@a,\@b);
619
620 #@a = (); @b =();
621 #push @a,"AZE"; push @b,"EZA";
622 #print "Different:\n".build_diff(\@a,\@b);
623
624 use lib "@CMAKE_BINARY_DIR@/bin" ;
625
626 use Diff qw(diff); # postpone a bit to have time to change INC
627
628 sub build_diff {
629   my $res;
630   my $diff = Diff->new(@_);
631   
632   $diff->Base( 1 );   # Return line numbers, not indices
633   my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
634   return ""   if ($chunk_count == 1 && $diff->Same());
635   $diff->Reset();
636   while(  $diff->Next()  ) {
637     my @same = $diff->Same();
638     if ($diff->Same() ) {
639       if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
640         $res .= '  '.$same[0]."\n" ;
641         $res .= '  '.$same[1]."\n" if (scalar @same>1);
642       }     
643       $res .= "...\n"  if (scalar @same>2);
644 #    $res .= $diff->Next(0)."/$chunk_count\n";
645       if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
646         $res .= '  '.$same[scalar @same -2]."\n" if (scalar @same>1);
647         $res .= '  '.$same[scalar @same -1]."\n";
648       } 
649     } 
650     next if  $diff->Same();
651     map { $res .= "- $_\n" } $diff->Items(1);
652     map { $res .= "+ $_\n" } $diff->Items(2);
653   }
654   return $res;
655 }
656
657