Logo AND Algorithmique Numérique Distribuée

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