Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add suport for sort prefix length in tesh.pl.
[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     #also resort the other one, as perl sort is not the same as the C one used to generate teshes
314     if(defined($cmd{'out'})){
315       @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
316     }
317   }
318
319   #Did we timeout ? If yes, handle it. If not, kill the forked process.
320
321   if($timeout==-1 and $gotret eq "got signal SIGKILL"){
322     $gotret="return code 0";
323     $timeout=1;
324     $gotret= "timeout after $time_to_wait sec";
325     $error=1;
326     $exitcode=3;
327     print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
328   }else{
329     $timeout=0;  
330   }
331   if($gotret ne $wantret) {
332     $error=1;
333     my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
334     if ($timeout!=1) {
335         $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";    
336     }
337     map {$msg .=  "|| $_\n"} @got;
338     if(!@got) {
339         if($timeout==1){
340         print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
341         }else{
342         $msg .= "||\n";
343         }
344     }
345     $timeout = 0;
346     print STDERR "$msg";
347   }
348
349       
350   ###
351   # Check the result of execution 
352   ###
353   my $diff;
354   if (!defined($cmd{'output ignore'})){
355     $diff = build_diff(\@{$cmd{'out'}}, \@got);
356   }else{
357   print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
358   }
359   if (length $diff) {
360     print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch:\n";
361     map { print "$_\n" } split(/\n/,$diff);
362
363     print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
364     $error=1;
365     $exitcode=2;
366   }
367 }
368
369 sub mkfile_cmd {
370   my %cmd = %{$_[0]};
371   my $file = $cmd{'arg'};
372   print "[Tesh/INFO] mkfile $file\n";
373
374   unlink($file);
375   open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
376   print FILE join("\n", @{$cmd{'in'}});
377   print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
378   close(FILE);
379 }
380
381 # parse tesh file
382 #my $teshfile=$tesh_file;
383 #$teshfile=~ s{\.[^.]+$}{};
384
385 unless($tesh_file eq "(stdin)"){
386   open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
387 }
388
389 my %cmd; # everything about the next command to run
390 my $line_num=0;
391 my $finished =0;
392 LINE: while (not $finished and not $error) {
393   my $line;
394
395
396   if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
397     $finished=1;
398     next LINE;
399   }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
400     $finished=1;
401     next LINE;
402   }
403
404
405   $line_num++;
406   chomp $line;
407   $line =~ s/\r//g;
408   print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
409   my $next;
410   # deal with line continuations
411   while ($line =~ /^(.*?)\\$/) {
412     $next=<TESH_FILE>;
413     die "[TESH/CRITICAL] Continued line at end of file\n"
414       unless defined($next);
415     $line_num++;
416     chomp $next;
417     print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
418     $line = $1.$next;
419   }
420
421   # Push delayed commands on empty lines
422   unless ($line =~ m/^(.)(.*)$/) {
423     if (defined($cmd{'cmd'}))  {
424       exec_cmd(\%cmd);
425       %cmd = ();
426     }
427     next LINE;
428   }     
429  
430   my ($cmd,$arg) = ($1,$2);
431   $arg =~ s/^ //g;
432   $arg =~ s/\r//g;
433   $arg =~ s/\\\\/\\/g;
434   # handle the commands
435   if ($cmd =~ /^#/) {    #comment
436   } elsif ($cmd eq '>'){    #expected result line
437     print "[TESH/debug] push expected result\n" if $opts{'debug'};
438     push @{$cmd{'out'}}, $arg;
439
440   } elsif ($cmd eq '<') {    # provided input
441     print "[TESH/debug] push provided input\n" if $opts{'debug'};
442     push @{$cmd{'in'}}, $arg;
443
444   } elsif ($cmd eq 'p') {    # comment
445     print "[$tesh_name:$line_num] $arg\n";
446
447   } elsif ($cmd eq '$') {  # Command
448     # if we have something buffered, run it now
449     if (defined($cmd{'cmd'})) {
450       exec_cmd(\%cmd);
451       %cmd = ();
452     }
453     if ($arg =~ /^\s*mkfile /){      # "mkfile" command line
454       die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
455
456       $cmd{'arg'} = $arg;
457       $cmd{'arg'} =~ s/\s*mkfile //;
458       mkfile_cmd(\%cmd);
459       %cmd = ();
460
461     } elsif ($arg =~ /^\s*cd /) {
462       die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
463       die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
464
465       $arg =~ s/^ *cd //;
466       cd_cmd("",$arg);
467       %cmd = ();
468
469     } else { # regular command
470       $cmd{'cmd'} = $arg;
471       $cmd{'file'} = $tesh_file;
472       $cmd{'line'} = $line_num;
473     }
474   }
475   elsif($cmd eq '&'){      # parallel command line
476
477     if (defined($cmd{'cmd'})) {
478       exec_cmd(\%cmd);
479       %cmd = ();
480     }
481     $cmd{'background'} = 1;
482     $cmd{'cmd'} = $arg;
483     $cmd{'file'} = $tesh_file;
484     $cmd{'line'} = $line_num;
485   }    
486   elsif($line =~ /^!\s*output sort/){    #output sort
487     if (defined($cmd{'cmd'})) {
488       exec_cmd(\%cmd);
489       %cmd = ();
490     }
491     $cmd{'sort'} = 1;
492     if ($line =~ /^!\s*output sort\s+(\d+)/) {
493         $sort_prefix = $1;
494     }
495   }
496   elsif($line =~ /^!\s*output ignore/){    #output ignore
497     if (defined($cmd{'cmd'})) {
498       exec_cmd(\%cmd);
499       %cmd = ();
500     }
501     $cmd{'output ignore'} = 1;
502   }
503   elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
504     if (defined($cmd{'cmd'})) {
505       exec_cmd(\%cmd);
506       %cmd = ();
507     }
508 print "hey\n";
509     $cmd{'expect'} = "$1";
510   }
511   elsif($line =~ /^!\s*expect return/){    #expect return
512     if (defined($cmd{'cmd'})) {
513       exec_cmd(\%cmd);
514       %cmd = ();
515     }
516     $line =~ s/^! expect return //g;
517     $line =~ s/\r//g;
518     $cmd{'return'} = $line;
519   }
520   elsif($line =~ /^!\s*setenv/){    #setenv
521     if (defined($cmd{'cmd'})) {
522       exec_cmd(\%cmd);
523       %cmd = ();
524     }
525     $line =~ s/^! setenv //g;
526     $line =~ s/\r//g;
527     setenv_cmd($line);
528   }
529   elsif($line =~ /^!\s*include/){    #include
530     if (defined($cmd{'cmd'})) {
531       exec_cmd(\%cmd);
532       %cmd = ();
533     }
534     print color("red"), "[Tesh/CRITICAL] need include";
535     print color("reset"), "\n";
536     die;
537   }
538   elsif($line =~ /^!\s*timeout/){    #timeout
539     if (defined($cmd{'cmd'})) {
540       exec_cmd(\%cmd);
541       %cmd = ();
542     }
543     $line =~ s/^! timeout //;
544     $line =~ s/\r//g;
545     $cmd{'timeout'} = $line;
546   } else {
547     die "[TESH/CRITICAL] parse error: $line\n";
548   }
549   if($forked){
550    kill(SIGKILL, $forked);
551    $timeout=0;
552   }
553
554 }
555
556
557
558 # Deal with last command
559 if (defined($cmd{'cmd'})) {
560   exec_cmd(\%cmd);
561   %cmd = ();
562 }
563
564
565 if($forked){
566    kill(SIGKILL, $forked);
567    $timeout=0;
568 }
569
570 foreach(@bg_cmds){
571   my %test=%{$_};
572   waitpid ($test{'pid'}, 0);
573   $test{'gotret'} = exit_status($?);
574   parse_out(\%test);
575 }
576
577 @bg_cmds=();
578
579
580 if($error !=0){
581     exit $exitcode;
582 }elsif($tesh_file eq "(stdin)"){
583     print "Test suite from stdin OK\n";
584 }else{
585     print "Test suite `$tesh_name' OK\n";
586 }
587
588 #my (@a,@b);
589 #push @a,"bl1";   push @b,"bl1";
590 #push @a,"bl2";   push @b,"bl2";
591 #push @a,"bl3";   push @b,"bl3";
592 #push @a,"bl4";   push @b,"bl4";
593 #push @a,"bl5";   push @b,"bl5";
594 #push @a,"bl6";   push @b,"bl6";
595 #push @a,"bl7";   push @b,"bl7";
596 ##push @a,"Perl";  push @b,"ruby";
597 #push @a,"END1";   push @b,"END1";
598 #push @a,"END2";   push @b,"END2";
599 #push @a,"END3";   push @b,"END3";
600 #push @a,"END4";   push @b,"END4";
601 #push @a,"END5";   push @b,"END5";
602 #push @a,"END6";   push @b,"END6";
603 #push @a,"END7";   push @b,"END7";
604 #print "Identical:\n". build_diff(\@a,\@b);
605
606 #@a = (); @b =();
607 #push @a,"AZE"; push @b,"EZA";
608 #print "Different:\n".build_diff(\@a,\@b);
609
610 use lib "@CMAKE_BINARY_DIR@/bin" ;
611
612 use Diff qw(diff); # postpone a bit to have time to change INC
613
614 sub build_diff {
615   my $res;
616   my $diff = Diff->new(@_);
617   
618   $diff->Base( 1 );   # Return line numbers, not indices
619   my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
620   return ""   if ($chunk_count == 1 && $diff->Same());
621   $diff->Reset();
622   while(  $diff->Next()  ) {
623     my @same = $diff->Same();
624     if ($diff->Same() ) {
625       if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
626         $res .= '  '.$same[0]."\n" ;
627         $res .= '  '.$same[1]."\n" if (scalar @same>1);
628       }     
629       $res .= "...\n"  if (scalar @same>2);
630 #    $res .= $diff->Next(0)."/$chunk_count\n";
631       if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
632         $res .= '  '.$same[scalar @same -2]."\n" if (scalar @same>1);
633         $res .= '  '.$same[scalar @same -1]."\n";
634       } 
635     } 
636     next if  $diff->Same();
637     map { $res .= "- $_\n" } $diff->Items(1);
638     map { $res .= "+ $_\n" } $diff->Items(2);
639   }
640   return $res;
641 }
642
643