Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[tesh] reduce the uglyness of that code a bit
[simgrid.git] / tools / tesh / tesh.pl
index 48f5d1c..d55db79 100755 (executable)
@@ -19,8 +19,6 @@ tesh -- testing shell
 B<tesh> [I<options>] I<tesh_file>
 
 =cut
-my($bindir)=".";
-my($srcdir)=".";
 my($timeout)=0;
 my($time_to_wait)=0;
 my $path = $0;
@@ -51,10 +49,10 @@ use English;
 ## Portability bits for windows
 ##
 
-use constant RUNNING_ON_WINDOWS => ($^O =~ /^(?:mswin|dos|os2)/oi);
+use constant RUNNING_ON_WINDOWS => ($OSNAME =~ /^(?:mswin|dos|os2)/oi);
 use POSIX qw(:sys_wait_h WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG
              :signal_h SIGINT SIGTERM SIGKILL SIGABRT SIGSEGV);
-# These are not implemented on windows (see bug 6798 and 6470)
+# These are not implemented on windows
 BEGIN {
     if (RUNNING_ON_WINDOWS) {
        *WIFEXITED   = sub { not $_[0] & 127 };
@@ -112,8 +110,6 @@ sub cd_cmd {
        print "Chdir to $directory failed: No such file or directory\n";
     }
     if($failure==1){
-       $error=1;
-       $exitcode=4;
        print "Test suite `$tesh_file': NOK (system error)\n";
        exit 4;
     }
@@ -141,7 +137,6 @@ sub get_options {
   $tesh_file = pop @ARGV;
 
   # temporary arrays for GetOption
-  my @verbose = ();
   my @cfg;
   my $log; # ignored
 
@@ -149,7 +144,6 @@ sub get_options {
   my %opt = (
     "help"  => 0,
     "debug"   => 0,
-    "verbose" => 0
     );
 
   Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
@@ -157,7 +151,6 @@ sub get_options {
   GetOptions(
     'help|h'   => \$opt{'help'},
 
-    'verbose|v'  => \@verbose,
     'debug|d'  => \$opt{"debug"},
 
     'difftool=s' => \$diff_tool,
@@ -180,16 +173,15 @@ sub get_options {
     print "New tesh: $diff_tool_tmp_filename\n";
   }
 
-  unless($tesh_file=~/(.*)\.tesh/){
+  if ($tesh_file =~ m/(.*)\.tesh/) {
+    $tesh_name=$1;
+    print "Test suite `$tesh_name'\n";
+  } else {
     $tesh_file="(stdin)";
     $tesh_name="(stdin)";
     print "Test suite from stdin\n";
-  }else{
-    $tesh_name=$1;
-    print "Test suite `$tesh_name'\n";
   }
 
-  $opt{'verbose'} = scalar @verbose;
   foreach (@cfg) {
     $opt{'cfg'} .= " --cfg=$_";
   }
@@ -201,18 +193,9 @@ my %opts = get_options(@ARGV);
 ##
 ## File parsing
 ##
-my($nb_arg)=0;
-my($old_buffer);
-my($linebis);
-my($SIGABRT)=0;
-my($verbose)=0;
 my($return)=-1;
-my($pid);
-my($result);
-my($result_err);
 my($forked);
 my($config)="";
-my($tesh_command)=0;
 my(@buffer_tesh)=();
 
 ###########################################################################
@@ -290,7 +273,7 @@ sub exec_cmd {
   close CHILD_IN;
 
   # if timeout specified, fork and kill executing child at the end of timeout
-  if (defined($cmd{'timeout'}) or defined($opts{'timeout'})){
+  if (not $cmd{'background'} and (defined($cmd{'timeout'}) or defined($opts{'timeout'}))){
     $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
     $forked = fork();
     $timeout=-1;
@@ -302,19 +285,13 @@ sub exec_cmd {
 
   # Cleanup the executing child, and kill the timeouter brother on need
   $cmd{'return'} = 0 unless defined($cmd{'return'});
-  if($cmd{'background'} != 1){
+  if ($cmd{'background'} != 1) {
     waitpid ($cmd{'pid'}, 0);
     $cmd{'gotret'} = exit_status($?);
     parse_out(\%cmd);
-  }else{
+  } else {
     # & commands, which will be handled at the end
     push @bg_cmds, \%cmd;
-    # no timeout for background commands
-    if($forked){
-       kill(SIGKILL, $forked);
-       $timeout=0;
-       $forked=0;
-    }
   }
 }
 
@@ -410,14 +387,13 @@ sub parse_out {
   # Check the result of execution
   ###
   my $diff;
-  if (defined($cmd{'output display'})){
+  if (defined($cmd{'output display'})) {
     print "[Tesh/INFO] Here is the (ignored) command output:\n";
-    map { print "||$_\n" } @got;
-  }
-  elsif (!defined($cmd{'output ignore'})){
-    $diff = build_diff(\@{$cmd{'out'}}, \@got);
-  }else{
+    map { print "||$_\n" } @got;      
+  } elsif (defined($cmd{'output ignore'})) {
     print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
+  } else {
+    $diff = build_diff(\@{$cmd{'out'}}, \@got);
   }
   if (length $diff) {
     print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
@@ -433,8 +409,7 @@ sub parse_out {
     }
 
     print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
-    $error=1;
-    $exitcode=2;
+    exit 2;
   }
 }
 
@@ -451,36 +426,26 @@ sub mkfile_cmd {
 }
 
 # parse tesh file
-#my $teshfile=$tesh_file;
-#$teshfile=~ s{\.[^.]+$}{};
-
-unless($tesh_file eq "(stdin)"){
-  open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
+my $infh; # The file descriptor from which we should read the teshfile
+if($tesh_file eq "(stdin)"){
+  $infh = *STDIN;
+} else {
+  open $infh, $tesh_file 
+      or die "[Tesh/CRITICAL] Unable to open $tesh_file: $!\n";
 }
 
 my %cmd; # everything about the next command to run
 my $line_num=0;
-my $finished =0;
-LINE: while (not $finished and not $error) {
-  my $line;
-
-
-  if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
-    $finished=1;
-    next LINE;
-  }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
-    $finished=1;
-    next LINE;
-  }
-
-  $line_num++;
+LINE: while (defined(my $line=<$infh>) and not $error) {
   chomp $line;
   $line =~ s/\r//g;
+
+  $line_num++;
   print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
   my $next;
   # deal with line continuations
   while ($line =~ /^(.*?)\\$/) {
-    $next=<TESH_FILE>;
+    $next=<$infh>;
     die "[TESH/CRITICAL] Continued line at end of file\n"
       unless defined($next);
     $line_num++;
@@ -627,13 +592,13 @@ print "hey\n";
     die "[TESH/CRITICAL] parse error: $line\n";
   }
   if($forked){
-   kill(SIGKILL, $forked);
+   kill('KILL', $forked);
    $timeout=0;
   }
-
 }
 
-
+# We're done reading the input file
+close $infh unless ($tesh_file eq "(stdin)");
 
 # Deal with last command
 if (defined($cmd{'cmd'})) {
@@ -643,7 +608,7 @@ if (defined($cmd{'cmd'})) {
 
 
 if($forked){
-   kill(SIGKILL, $forked);
+   kill('KILL', $forked);
    $timeout=0;
 }
 
@@ -662,11 +627,11 @@ if ($diff_tool) {
   unlink $diff_tool_tmp_filename;
 }
 
-if($error !=0){
+if ($error !=0){
     exit $exitcode;
-}elsif($tesh_file eq "(stdin)"){
+} elsif($tesh_file eq "(stdin)") {
     print "Test suite from stdin OK\n";
-}else{
+} else {
     print "Test suite `$tesh_name' OK\n";
 }