3 # Copyright (c) 2012-2015. The SimGrid Team.
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8 eval 'exec perl -S $0 ${1+"$@"}'
9 if $running_under_some_shell;
11 # If you change this file, please stick to the formatting you got with:
12 # perltidy --backup-and-modify-in-place --maximum-line-length=180 --output-line-ending=unix --cuddled-else
22 B<tesh> [I<options>] I<tesh_file>
27 my ($time_to_wait) = 0;
29 my $enable_coverage = 0;
31 my $diff_tool_tmp_fh = 0;
32 my $diff_tool_tmp_filename = 0;
40 $SIG{'PIPE'} = 'IGNORE';
44 use lib "@CMAKE_BINARY_DIR@/bin";
46 use Diff qw(diff); # postpone a bit to have time to change INC
48 use Getopt::Long qw(GetOptions);
56 #### Portability bits for windows
59 use constant RUNNING_ON_WINDOWS => ( $OSNAME =~ /^(?:mswin|dos|os2)/oi );
60 use POSIX qw(:sys_wait_h WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG
61 :signal_h SIGINT SIGTERM SIGKILL SIGABRT SIGSEGV);
64 if (RUNNING_ON_WINDOWS) { # Missing on windows
65 *WIFEXITED = sub { not $_[0] & 127 };
66 *WEXITSTATUS = sub { $_[0] >> 8 };
67 *WIFSIGNALED = sub { ( $_[0] & 127 ) && ( $_[0] & 127 != 127 ) };
68 *WTERMSIG = sub { $_[0] & 127 };
74 #### Command line option handling
77 if ( $ARGV[0] eq "--internal-killer-process" ) {
79 # We fork+exec a waiter process in charge of killing the command after timeout
80 # If the command stops earlier, that's fine: the killer sends a signal to an already stopped process, fails, and quits.
81 # Nobody cares about the killer issues.
82 # The only problem could arise if another process is given the same PID than cmd. We bet it won't happen :)
83 my $time_to_wait = $ARGV[1];
92 my %opts = ( "debug" => 0 );
94 Getopt::Long::config( 'bundling', 'no_getopt_compat', 'no_auto_abbrev' );
96 'debug|d' => \$opts{"debug"},
98 'difftool=s' => \$diff_tool,
100 'cd=s' => sub { cd_cmd( $_[1] ) },
101 'timeout=s' => \$opts{'timeout'},
102 'setenv=s' => sub { setenv_cmd( $_[1] ) },
103 'cfg=s' => sub { $opts{'cfg'} .= " --cfg=$_[1]" },
104 'enable-coverage+' => \$enable_coverage,
107 $tesh_file = pop @ARGV;
109 if ($enable_coverage) {
110 print "Enable coverage\n";
114 use File::Temp qw/ tempfile /;
115 ( $diff_tool_tmp_fh, $diff_tool_tmp_filename ) = tempfile();
116 print "New tesh: $diff_tool_tmp_filename\n";
119 if ( $tesh_file =~ m/(.*)\.tesh/ ) {
121 print "Test suite `$tesh_name'\n";
123 $tesh_file = "(stdin)";
124 $tesh_name = "(stdin)";
125 print "Test suite from stdin\n";
134 my (@buffer_tesh) = ();
136 ###########################################################################
140 if ( WIFEXITED($status) ) {
141 $exitcode = WEXITSTATUS($status) + 40;
142 return "returned code " . WEXITSTATUS($status);
143 } elsif ( WIFSIGNALED($status) ) {
145 if ( WTERMSIG($status) == SIGINT ) { $code = "SIGINT"; }
146 elsif ( WTERMSIG($status) == SIGTERM ) { $code = "SIGTERM"; }
147 elsif ( WTERMSIG($status) == SIGKILL ) { $code = "SIGKILL"; }
148 elsif ( WTERMSIG($status) == SIGABRT ) { $code = "SIGABRT"; }
149 elsif ( WTERMSIG($status) == SIGSEGV ) { $code = "SIGSEGV"; }
150 $exitcode = WTERMSIG($status) + 4;
151 return "got signal $code";
153 return "Unparsable status. Is the process stopped?";
157 my %cmd = %{ $_[0] };
158 if ( $opts{'debug'} ) {
160 map { print " $_" } @{ $cmd{'in'} };
163 map { print " $_" } @{ $cmd{'out'} };
165 print "CMD: $cmd{'cmd'}\n";
168 # cleanup the command line
169 if (RUNNING_ON_WINDOWS) {
170 var_subst( $cmd{'cmd'}, "EXEEXT", ".exe" );
172 var_subst( $cmd{'cmd'}, "EXEEXT", "" );
175 # substitute environ variables
176 foreach my $key ( keys %environ ) {
177 $cmd{'cmd'} = var_subst( $cmd{'cmd'}, $key, $environ{$key} );
180 # substitute remaining variables, if any
181 while ( $cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/ ) {
182 $cmd{'cmd'} = var_subst( $cmd{'cmd'}, $1, "" );
184 while ( $cmd{'cmd'} =~ /\$(\w+)/ ) {
185 $cmd{'cmd'} = var_subst( $cmd{'cmd'}, $1, "" );
189 $cmd{'cmd'} .= " $opts{'cfg'}"
190 if ( defined( $opts{'cfg'} ) && length( $opts{'cfg'} ) );
193 $cmd{'cmd'} =~ s/^\s+//;
194 $cmd{'cmd'} =~ s/\s+$//;
196 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n";
199 # exec the command line
201 $cmd{'got'} = IO::File->new_tmpfile;
202 $cmd{'got'}->autoflush(1);
203 local *E = $cmd{'got'};
205 open3( \*CHILD_IN, ">&E", ">&E", quotewords( '\s+', 0, $cmd{'cmd'} ) );
207 # push all provided input to executing child
208 map { print CHILD_IN "$_\n"; } @{ $cmd{'in'} };
211 # if timeout specified, fork and kill executing child at the end of timeout
212 if ( not $cmd{'background'}
213 and ( defined( $cmd{'timeout'} ) or defined( $opts{'timeout'} ) ) )
216 defined( $cmd{'timeout'} ) ? $cmd{'timeout'} : $opts{'timeout'};
219 die "fork() failed: $!" unless defined $forked;
220 if ( $forked == 0 ) { # child
221 exec("$PROGRAM_NAME --internal-killer-process $time_to_wait $cmd{'pid'}");
225 # Cleanup the executing child, and kill the timeouter brother on need
226 $cmd{'return'} = 0 unless defined( $cmd{'return'} );
227 if ( $cmd{'background'} != 1 ) {
228 waitpid( $cmd{'pid'}, 0 );
229 $cmd{'gotret'} = exit_status($?);
230 parse_result( \%cmd );
233 # & commands, which will be handled at the end
234 push @bg_cmds, \%cmd;
239 my %cmd = %{ $_[0] };
240 my $gotret = $cmd{'gotret'};
244 if ( defined( $cmd{'expect'} ) and ( $cmd{'expect'} ne "" ) ) {
245 $wantret = "got signal $cmd{'expect'}";
248 "returned code " . ( defined( $cmd{'return'} ) ? $cmd{'return'} : 0 );
251 local *got = $cmd{'got'};
254 # pop all output from executing child
256 while ( defined( my $got = <got> ) ) {
259 print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
261 if ( !( $enable_coverage and $got =~ /^profiling:/ ) ) {
266 if ( $cmd{'sort'} ) {
268 # Save the unsorted observed output to report it on error.
269 map { push @{ $cmd{'unsorted got'} }, $_ } @got;
272 substr( $a, 0, $sort_prefix ) cmp substr( $b, 0, $sort_prefix );
275 if ( $sort_prefix > 0 ) {
276 @got = sort mysort @got;
280 while ( @got and $got[0] eq "" ) {
284 # Sort the expected output to make it easier to write for humans
285 if ( defined( $cmd{'out'} ) ) {
286 if ( $sort_prefix > 0 ) {
287 @{ $cmd{'out'} } = sort mysort @{ $cmd{'out'} };
289 @{ $cmd{'out'} } = sort @{ $cmd{'out'} };
291 while ( @{ $cmd{'out'} } and ${ $cmd{'out'} }[0] eq "" ) {
292 shift @{ $cmd{'out'} };
297 # Did we timeout ? If yes, handle it. If not, kill the forked process.
300 and ( $gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL" ) )
302 $gotret = "return code 0";
304 $gotret = "timeout after $time_to_wait sec";
307 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
311 if ( $gotret ne $wantret ) {
313 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
314 if ( $timeout != 1 ) {
315 $msg = $msg . "Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
317 map { $msg .= "|| $_\n" } @got;
319 if ( $timeout == 1 ) {
320 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
330 # Check the result of execution
333 if ( defined( $cmd{'output display'} ) ) {
334 print "[Tesh/INFO] Here is the (ignored) command output:\n";
335 map { print "||$_\n" } @got;
336 } elsif ( defined( $cmd{'output ignore'} ) ) {
337 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n";
339 $diff = build_diff( \@{ $cmd{'out'} }, \@got );
341 if ( length $diff ) {
342 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch" . ( $cmd{'sort'} ? " (even after sorting)" : "" ) . ":\n";
343 map { print "$_\n" } split( /\n/, $diff );
344 if ( $cmd{'sort'} ) {
345 print "WARNING: Both the observed output and expected output were sorted as requested.\n";
346 print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
347 if ( $sort_prefix > 0 );
348 print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
350 # print "----8<--------------- Begin of unprocessed observed output (as it should appear in file):\n";
351 # map {print "> $_\n"} @{$cmd{'unsorted got'}};
352 # print "--------------->8---- End of the unprocessed observed output.\n";
355 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
361 my $infh; # The file descriptor from which we should read the teshfile
362 if ( $tesh_file eq "(stdin)" ) {
365 open $infh, $tesh_file
366 or die "[Tesh/CRITICAL] Unable to open $tesh_file: $!\n";
369 my %cmd; # everything about the next command to run
371 LINE: while ( defined( my $line = <$infh> ) and not $error ) {
376 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
379 # deal with line continuations
380 while ( $line =~ /^(.*?)\\$/ ) {
382 die "[TESH/CRITICAL] Continued line at end of file\n"
383 unless defined($next);
386 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
390 # Push delayed commands on empty lines
391 unless ( $line =~ m/^(.)(.*)$/ ) {
392 if ( defined( $cmd{'cmd'} ) ) {
396 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
400 my ( $cmd, $arg ) = ( $1, $2 );
401 print $diff_tool_tmp_fh "$line\n" if ( $diff_tool and $cmd ne '>' );
406 # handle the commands
407 if ( $cmd =~ /^#/ ) { # comment
408 } elsif ( $cmd eq '>' ) { # expected result line
409 print "[TESH/debug] push expected result\n" if $opts{'debug'};
410 push @{ $cmd{'out'} }, $arg;
412 } elsif ( $cmd eq '<' ) { # provided input
413 print "[TESH/debug] push provided input\n" if $opts{'debug'};
414 push @{ $cmd{'in'} }, $arg;
416 } elsif ( $cmd eq 'p' ) { # comment
417 print "[$tesh_name:$line_num] $arg\n";
419 } elsif ( $cmd eq '$' ) { # Command
420 # if we have something buffered, run it now
421 if ( defined( $cmd{'cmd'} ) ) {
425 if ( $arg =~ /^\s*mkfile / ) { # "mkfile" command line
426 die "[TESH/CRITICAL] Output expected from mkfile command!\n"
427 if scalar @{ cmd { 'out' } };
430 $cmd{'arg'} =~ s/\s*mkfile //;
434 } elsif ( $arg =~ /^\s*cd / ) {
435 die "[TESH/CRITICAL] Input provided to cd command!\n"
436 if scalar @{ cmd { 'in' } };
437 die "[TESH/CRITICAL] Output expected from cd command!\n"
438 if scalar @{ cmd { 'out' } };
444 } else { # regular command
446 $cmd{'file'} = $tesh_file;
447 $cmd{'line'} = $line_num;
449 } elsif ( $cmd eq '&' ) { # background command line
451 if ( defined( $cmd{'cmd'} ) ) {
455 $cmd{'background'} = 1;
457 $cmd{'file'} = $tesh_file;
458 $cmd{'line'} = $line_num;
460 } elsif ( $line =~ /^!\s*output sort/ ) { #output sort
461 if ( defined( $cmd{'cmd'} ) ) {
466 if ( $line =~ /^!\s*output sort\s+(\d+)/ ) {
469 } elsif ( $line =~ /^!\s*output ignore/ ) { #output ignore
470 if ( defined( $cmd{'cmd'} ) ) {
474 $cmd{'output ignore'} = 1;
475 } elsif ( $line =~ /^!\s*output display/ ) { #output display
476 if ( defined( $cmd{'cmd'} ) ) {
480 $cmd{'output display'} = 1;
481 } elsif ( $line =~ /^!\s*expect signal (\w*)/ ) { #expect signal SIGABRT
482 if ( defined( $cmd{'cmd'} ) ) {
487 $cmd{'expect'} = "$1";
488 } elsif ( $line =~ /^!\s*expect return/ ) { #expect return
489 if ( defined( $cmd{'cmd'} ) ) {
493 $line =~ s/^! expect return //g;
495 $cmd{'return'} = $line;
496 } elsif ( $line =~ /^!\s*setenv/ ) { #setenv
497 if ( defined( $cmd{'cmd'} ) ) {
501 $line =~ s/^! setenv //g;
504 } elsif ( $line =~ /^!\s*timeout/ ) { #timeout
505 if ( defined( $cmd{'cmd'} ) ) {
509 $line =~ s/^! timeout //;
511 $cmd{'timeout'} = $line;
513 die "[TESH/CRITICAL] parse error: $line\n";
516 kill( 'KILL', $forked );
521 # We're done reading the input file
522 close $infh unless ( $tesh_file eq "(stdin)" );
524 # Deal with last command
525 if ( defined( $cmd{'cmd'} ) ) {
531 kill( 'KILL', $forked );
537 waitpid( $test{'pid'}, 0 );
538 $test{'gotret'} = exit_status($?);
539 parse_result( \%test );
543 close $diff_tool_tmp_fh;
544 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
545 unlink $diff_tool_tmp_filename;
550 } elsif ( $tesh_file eq "(stdin)" ) {
551 print "Test suite from stdin OK\n";
553 print "Test suite `$tesh_name' OK\n";
557 #### Helper functions
562 my $diff = Diff->new(@_);
564 $diff->Base(1); # Return line numbers, not indices
565 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
566 return "" if ( $chunk_count == 1 && $diff->Same() );
568 while ( $diff->Next() ) {
569 my @same = $diff->Same();
570 if ( $diff->Same() ) {
571 if ( $diff->Next(0) > 1 ) { # not first chunk: print 2 first lines
572 $res .= ' ' . $same[0] . "\n";
573 $res .= ' ' . $same[1] . "\n" if ( scalar @same > 1 );
575 $res .= "...\n" if ( scalar @same > 2 );
577 # $res .= $diff->Next(0)."/$chunk_count\n";
578 if ( $diff->Next(0) < $chunk_count ) { # not last chunk: print 2 last lines
579 $res .= ' ' . $same[ scalar @same - 2 ] . "\n"
580 if ( scalar @same > 1 );
581 $res .= ' ' . $same[ scalar @same - 1 ] . "\n";
584 next if $diff->Same();
585 map { $res .= "- $_\n" } $diff->Items(1);
586 map { $res .= "+ $_\n" } $diff->Items(2);
591 # Helper function replacing any occurence of variable '$name' by its '$value'
592 # As in Bash, ${$value:=BLABLA} is rewritten to $value if set or to BLABLA if $value is not set
594 my ( $text, $name, $value ) = @_;
596 $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
597 $text =~ s/\$$name(\W|$)/$value$1/g;
599 $text =~ s/\${$name:=([^}]*)}/$1/g;
600 $text =~ s/\${$name}//g;
601 $text =~ s/\$$name(\W|$)/$1/g;
606 ################################ The possible commands ################################
609 my %cmd = %{ $_[0] };
610 my $file = $cmd{'arg'};
611 print "[Tesh/INFO] mkfile $file\n";
614 open( FILE, ">$file" )
615 or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
616 print FILE join( "\n", @{ $cmd{'in'} } );
617 print FILE "\n" if ( scalar @{ $cmd{'in'} } > 0 );
621 # Command CD. Just change to the provided directory
623 my $directory = shift;
625 if ( -e $directory && -d $directory ) {
627 print "[Tesh/INFO] change directory to $directory\n";
629 } elsif ( -e $directory ) {
630 print "Cannot change directory to '$directory': it is not a directory\n";
632 print "Chdir to $directory failed: No such file or directory\n";
634 if ( $failure == 1 ) {
635 print "Test suite `$tesh_file': NOK (system error)\n";
640 # Command setenv. Gets "variable=content", and update the environment accordingly
643 if ( $arg =~ /^(.*)=(.*)$/ ) {
644 my ( $var, $ctn ) = ( $1, $2 );
645 print "[Tesh/INFO] setenv $var=$ctn\n";
646 $environ{$var} = $ctn;
648 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$arg'\n";