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 Getopt::Long qw(GetOptions);
52 ## Portability bits for windows
55 use constant RUNNING_ON_WINDOWS => ( $OSNAME =~ /^(?:mswin|dos|os2)/oi );
56 use POSIX qw(:sys_wait_h WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG
57 :signal_h SIGINT SIGTERM SIGKILL SIGABRT SIGSEGV);
59 # These are not implemented on windows
61 if (RUNNING_ON_WINDOWS) {
62 *WIFEXITED = sub { not $_[0] & 127 };
63 *WEXITSTATUS = sub { $_[0] >> 8 };
64 *WIFSIGNALED = sub { ( $_[0] & 127 ) && ( $_[0] & 127 != 127 ) };
65 *WTERMSIG = sub { $_[0] & 127 };
73 # Helper function replacing any occurence of variable '$name' by its '$value'
74 # As in Bash, ${$value:=BLABLA} is rewritten to $value if set or to BLABLA if $value is not set
76 my ( $text, $name, $value ) = @_;
78 $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
79 $text =~ s/\$$name(\W|$)/$value$1/g;
81 $text =~ s/\${$name:=([^}]*)}/$1/g;
82 $text =~ s/\${$name}//g;
83 $text =~ s/\$$name(\W|$)/$1/g;
88 # Command CD. Just change to the provided directory
90 my $directory = shift;
92 if ( -e $directory && -d $directory ) {
94 print "[Tesh/INFO] change directory to $directory\n";
96 } elsif ( -e $directory ) {
97 print "Cannot change directory to '$directory': it is not a directory\n";
99 print "Chdir to $directory failed: No such file or directory\n";
101 if ( $failure == 1 ) {
102 print "Test suite `$tesh_file': NOK (system error)\n";
107 # Command setenv. Gets "variable=content", and update the environment accordingly
110 if ( $arg =~ /^(.*)=(.*)$/ ) {
111 my ( $var, $ctn ) = ( $1, $2 );
112 print "[Tesh/INFO] setenv $var=$ctn\n";
113 $environ{$var} = $ctn;
115 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$arg'\n";
120 ## Command line option handling
123 if ( $ARGV[0] eq "--internal-killer-process" ) {
125 # We fork+exec a waiter process in charge of killing the command after timeout
126 # If the command stops earlier, that's fine: the killer sends a signal to an already stopped process, fails, and quits.
127 # Nobody cares about the killer issues.
128 # The only problem could arise if another process is given the same PID than cmd. We bet it won't happen :)
129 my $time_to_wait = $ARGV[1];
132 kill( 'TERM', $pid );
134 kill( 'KILL', $pid );
140 # remove the tesh file from the ARGV used
142 $tesh_file = pop @ARGV;
144 # temporary arrays for GetOption
153 Getopt::Long::config( 'bundling', 'no_getopt_compat', 'no_auto_abbrev' );
156 'help|h' => \$opt{'help'},
158 'debug|d' => \$opt{"debug"},
160 'difftool=s' => \$diff_tool,
162 'cd=s' => sub { cd_cmd($_[1]) },
163 'timeout=s' => \$opt{'timeout'},
164 'setenv=s' => sub { setenv_cmd($_[1]) },
167 'enable-coverage+' => \$enable_coverage,
170 if ($enable_coverage) {
171 print "Enable coverage\n";
175 use File::Temp qw/ tempfile /;
176 ( $diff_tool_tmp_fh, $diff_tool_tmp_filename ) = tempfile();
177 print "New tesh: $diff_tool_tmp_filename\n";
180 if ( $tesh_file =~ m/(.*)\.tesh/ ) {
182 print "Test suite `$tesh_name'\n";
184 $tesh_file = "(stdin)";
185 $tesh_name = "(stdin)";
186 print "Test suite from stdin\n";
190 $opt{'cfg'} .= " --cfg=$_";
195 my %opts = get_options(@ARGV);
203 my (@buffer_tesh) = ();
205 ###########################################################################
209 if ( WIFEXITED($status) ) {
210 $exitcode = WEXITSTATUS($status) + 40;
211 return "returned code " . WEXITSTATUS($status);
212 } elsif ( WIFSIGNALED($status) ) {
214 if ( WTERMSIG($status) == SIGINT ) { $code = "SIGINT"; }
215 elsif ( WTERMSIG($status) == SIGTERM ) { $code = "SIGTERM"; }
216 elsif ( WTERMSIG($status) == SIGKILL ) { $code = "SIGKILL"; }
217 elsif ( WTERMSIG($status) == SIGABRT ) { $code = "SIGABRT"; }
218 elsif ( WTERMSIG($status) == SIGSEGV ) { $code = "SIGSEGV"; }
219 $exitcode = WTERMSIG($status) + 4;
220 return "got signal $code";
222 return "Unparsable status. Is the process stopped?";
226 my %cmd = %{ $_[0] };
227 if ( $opts{'debug'} ) {
229 map { print " $_" } @{ $cmd{'in'} };
232 map { print " $_" } @{ $cmd{'out'} };
234 print "CMD: $cmd{'cmd'}\n";
237 # cleanup the command line
238 if (RUNNING_ON_WINDOWS) {
239 var_subst( $cmd{'cmd'}, "EXEEXT", ".exe" );
241 var_subst( $cmd{'cmd'}, "EXEEXT", "" );
244 # substitute environ variables
245 foreach my $key ( keys %environ ) {
246 $cmd{'cmd'} = var_subst( $cmd{'cmd'}, $key, $environ{$key} );
249 # substitute remaining variables, if any
250 while ( $cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/ ) {
251 $cmd{'cmd'} = var_subst( $cmd{'cmd'}, $1, "" );
253 while ( $cmd{'cmd'} =~ /\$(\w+)/ ) {
254 $cmd{'cmd'} = var_subst( $cmd{'cmd'}, $1, "" );
258 $cmd{'cmd'} .= " $opts{'cfg'}"
259 if ( defined( $opts{'cfg'} ) && length( $opts{'cfg'} ) );
262 $cmd{'cmd'} =~ s/^\s+//;
263 $cmd{'cmd'} =~ s/\s+$//;
265 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n";
268 # exec the command line
270 $cmd{'got'} = IO::File->new_tmpfile;
271 $cmd{'got'}->autoflush(1);
272 local *E = $cmd{'got'};
274 open3( \*CHILD_IN, ">&E", ">&E", quotewords( '\s+', 0, $cmd{'cmd'} ) );
276 # push all provided input to executing child
277 map { print CHILD_IN "$_\n"; } @{ $cmd{'in'} };
280 # if timeout specified, fork and kill executing child at the end of timeout
281 if ( not $cmd{'background'}
282 and ( defined( $cmd{'timeout'} ) or defined( $opts{'timeout'} ) ) )
285 defined( $cmd{'timeout'} ) ? $cmd{'timeout'} : $opts{'timeout'};
288 die "fork() failed: $!" unless defined $forked;
289 if ( $forked == 0 ) { # child
290 exec("$PROGRAM_NAME --internal-killer-process $time_to_wait $cmd{'pid'}");
294 # Cleanup the executing child, and kill the timeouter brother on need
295 $cmd{'return'} = 0 unless defined( $cmd{'return'} );
296 if ( $cmd{'background'} != 1 ) {
297 waitpid( $cmd{'pid'}, 0 );
298 $cmd{'gotret'} = exit_status($?);
299 parse_result( \%cmd );
302 # & commands, which will be handled at the end
303 push @bg_cmds, \%cmd;
308 my %cmd = %{ $_[0] };
309 my $gotret = $cmd{'gotret'};
313 if ( defined( $cmd{'expect'} ) and ( $cmd{'expect'} ne "" ) ) {
314 $wantret = "got signal $cmd{'expect'}";
317 "returned code " . ( defined( $cmd{'return'} ) ? $cmd{'return'} : 0 );
320 local *got = $cmd{'got'};
323 # pop all output from executing child
325 while ( defined( my $got = <got> ) ) {
328 print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
330 if ( !( $enable_coverage and $got =~ /^profiling:/ ) ) {
335 if ( $cmd{'sort'} ) {
337 # Save the unsorted observed output to report it on error.
338 map { push @{ $cmd{'unsorted got'} }, $_ } @got;
341 substr( $a, 0, $sort_prefix ) cmp substr( $b, 0, $sort_prefix );
344 if ( $sort_prefix > 0 ) {
345 @got = sort mysort @got;
349 while ( @got and $got[0] eq "" ) {
353 # Sort the expected output to make it easier to write for humans
354 if ( defined( $cmd{'out'} ) ) {
355 if ( $sort_prefix > 0 ) {
356 @{ $cmd{'out'} } = sort mysort @{ $cmd{'out'} };
358 @{ $cmd{'out'} } = sort @{ $cmd{'out'} };
360 while ( @{ $cmd{'out'} } and ${ $cmd{'out'} }[0] eq "" ) {
361 shift @{ $cmd{'out'} };
366 # Did we timeout ? If yes, handle it. If not, kill the forked process.
369 and ( $gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL" ) )
371 $gotret = "return code 0";
373 $gotret = "timeout after $time_to_wait sec";
376 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
380 if ( $gotret ne $wantret ) {
382 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
383 if ( $timeout != 1 ) {
384 $msg = $msg . "Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
386 map { $msg .= "|| $_\n" } @got;
388 if ( $timeout == 1 ) {
389 print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
399 # Check the result of execution
402 if ( defined( $cmd{'output display'} ) ) {
403 print "[Tesh/INFO] Here is the (ignored) command output:\n";
404 map { print "||$_\n" } @got;
405 } elsif ( defined( $cmd{'output ignore'} ) ) {
406 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n";
408 $diff = build_diff( \@{ $cmd{'out'} }, \@got );
410 if ( length $diff ) {
411 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch" . ( $cmd{'sort'} ? " (even after sorting)" : "" ) . ":\n";
412 map { print "$_\n" } split( /\n/, $diff );
413 if ( $cmd{'sort'} ) {
414 print "WARNING: Both the observed output and expected output were sorted as requested.\n";
415 print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
416 if ( $sort_prefix > 0 );
417 print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
419 # print "----8<--------------- Begin of unprocessed observed output (as it should appear in file):\n";
420 # map {print "> $_\n"} @{$cmd{'unsorted got'}};
421 # print "--------------->8---- End of the unprocessed observed output.\n";
424 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
430 my %cmd = %{ $_[0] };
431 my $file = $cmd{'arg'};
432 print "[Tesh/INFO] mkfile $file\n";
435 open( FILE, ">$file" )
436 or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
437 print FILE join( "\n", @{ $cmd{'in'} } );
438 print FILE "\n" if ( scalar @{ $cmd{'in'} } > 0 );
443 my $infh; # The file descriptor from which we should read the teshfile
444 if ( $tesh_file eq "(stdin)" ) {
447 open $infh, $tesh_file
448 or die "[Tesh/CRITICAL] Unable to open $tesh_file: $!\n";
451 my %cmd; # everything about the next command to run
453 LINE: while ( defined( my $line = <$infh> ) and not $error ) {
458 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
461 # deal with line continuations
462 while ( $line =~ /^(.*?)\\$/ ) {
464 die "[TESH/CRITICAL] Continued line at end of file\n"
465 unless defined($next);
468 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
472 # Push delayed commands on empty lines
473 unless ( $line =~ m/^(.)(.*)$/ ) {
474 if ( defined( $cmd{'cmd'} ) ) {
478 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
482 my ( $cmd, $arg ) = ( $1, $2 );
483 print $diff_tool_tmp_fh "$line\n" if ( $diff_tool and $cmd ne '>' );
488 # handle the commands
489 if ( $cmd =~ /^#/ ) { # comment
490 } elsif ( $cmd eq '>' ) { # expected result line
491 print "[TESH/debug] push expected result\n" if $opts{'debug'};
492 push @{ $cmd{'out'} }, $arg;
494 } elsif ( $cmd eq '<' ) { # provided input
495 print "[TESH/debug] push provided input\n" if $opts{'debug'};
496 push @{ $cmd{'in'} }, $arg;
498 } elsif ( $cmd eq 'p' ) { # comment
499 print "[$tesh_name:$line_num] $arg\n";
501 } elsif ( $cmd eq '$' ) { # Command
502 # if we have something buffered, run it now
503 if ( defined( $cmd{'cmd'} ) ) {
507 if ( $arg =~ /^\s*mkfile / ) { # "mkfile" command line
508 die "[TESH/CRITICAL] Output expected from mkfile command!\n"
509 if scalar @{ cmd { 'out' } };
512 $cmd{'arg'} =~ s/\s*mkfile //;
516 } elsif ( $arg =~ /^\s*cd / ) {
517 die "[TESH/CRITICAL] Input provided to cd command!\n"
518 if scalar @{ cmd { 'in' } };
519 die "[TESH/CRITICAL] Output expected from cd command!\n"
520 if scalar @{ cmd { 'out' } };
526 } else { # regular command
528 $cmd{'file'} = $tesh_file;
529 $cmd{'line'} = $line_num;
531 } elsif ( $cmd eq '&' ) { # background command line
533 if ( defined( $cmd{'cmd'} ) ) {
537 $cmd{'background'} = 1;
539 $cmd{'file'} = $tesh_file;
540 $cmd{'line'} = $line_num;
542 } elsif ( $line =~ /^!\s*output sort/ ) { #output sort
543 if ( defined( $cmd{'cmd'} ) ) {
548 if ( $line =~ /^!\s*output sort\s+(\d+)/ ) {
551 } elsif ( $line =~ /^!\s*output ignore/ ) { #output ignore
552 if ( defined( $cmd{'cmd'} ) ) {
556 $cmd{'output ignore'} = 1;
557 } elsif ( $line =~ /^!\s*output display/ ) { #output display
558 if ( defined( $cmd{'cmd'} ) ) {
562 $cmd{'output display'} = 1;
563 } elsif ( $line =~ /^!\s*expect signal (\w*)/ ) { #expect signal SIGABRT
564 if ( defined( $cmd{'cmd'} ) ) {
569 $cmd{'expect'} = "$1";
570 } elsif ( $line =~ /^!\s*expect return/ ) { #expect return
571 if ( defined( $cmd{'cmd'} ) ) {
575 $line =~ s/^! expect return //g;
577 $cmd{'return'} = $line;
578 } elsif ( $line =~ /^!\s*setenv/ ) { #setenv
579 if ( defined( $cmd{'cmd'} ) ) {
583 $line =~ s/^! setenv //g;
586 } elsif ( $line =~ /^!\s*timeout/ ) { #timeout
587 if ( defined( $cmd{'cmd'} ) ) {
591 $line =~ s/^! timeout //;
593 $cmd{'timeout'} = $line;
595 die "[TESH/CRITICAL] parse error: $line\n";
598 kill( 'KILL', $forked );
603 # We're done reading the input file
604 close $infh unless ( $tesh_file eq "(stdin)" );
606 # Deal with last command
607 if ( defined( $cmd{'cmd'} ) ) {
613 kill( 'KILL', $forked );
619 waitpid( $test{'pid'}, 0 );
620 $test{'gotret'} = exit_status($?);
621 parse_result( \%test );
627 close $diff_tool_tmp_fh;
628 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
629 unlink $diff_tool_tmp_filename;
634 } elsif ( $tesh_file eq "(stdin)" ) {
635 print "Test suite from stdin OK\n";
637 print "Test suite `$tesh_name' OK\n";
641 #push @a,"bl1"; push @b,"bl1";
642 #push @a,"bl2"; push @b,"bl2";
643 #push @a,"bl3"; push @b,"bl3";
644 #push @a,"bl4"; push @b,"bl4";
645 #push @a,"bl5"; push @b,"bl5";
646 #push @a,"bl6"; push @b,"bl6";
647 #push @a,"bl7"; push @b,"bl7";
648 ##push @a,"Perl"; push @b,"ruby";
649 #push @a,"END1"; push @b,"END1";
650 #push @a,"END2"; push @b,"END2";
651 #push @a,"END3"; push @b,"END3";
652 #push @a,"END4"; push @b,"END4";
653 #push @a,"END5"; push @b,"END5";
654 #push @a,"END6"; push @b,"END6";
655 #push @a,"END7"; push @b,"END7";
656 #print "Identical:\n". build_diff(\@a,\@b);
659 #push @a,"AZE"; push @b,"EZA";
660 #print "Different:\n".build_diff(\@a,\@b);
662 use lib "@CMAKE_BINARY_DIR@/bin";
664 use Diff qw(diff); # postpone a bit to have time to change INC
668 my $diff = Diff->new(@_);
670 $diff->Base(1); # Return line numbers, not indices
671 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
672 return "" if ( $chunk_count == 1 && $diff->Same() );
674 while ( $diff->Next() ) {
675 my @same = $diff->Same();
676 if ( $diff->Same() ) {
677 if ( $diff->Next(0) > 1 ) { # not first chunk: print 2 first lines
678 $res .= ' ' . $same[0] . "\n";
679 $res .= ' ' . $same[1] . "\n" if ( scalar @same > 1 );
681 $res .= "...\n" if ( scalar @same > 2 );
683 # $res .= $diff->Next(0)."/$chunk_count\n";
684 if ( $diff->Next(0) < $chunk_count ) { # not last chunk: print 2 last lines
685 $res .= ' ' . $same[ scalar @same - 2 ] . "\n"
686 if ( scalar @same > 1 );
687 $res .= ' ' . $same[ scalar @same - 1 ] . "\n";
690 next if $diff->Same();
691 map { $res .= "- $_\n" } $diff->Items(1);
692 map { $res .= "+ $_\n" } $diff->Items(2);