Logo AND Algorithmique Numérique Distribuée

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