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 # Disabling IPC::Run::Debug saves tons of useless calls.
28 $ENV{'IPCRUNDEBUG'} = 'none'
29 unless exists $ENV{'IPCRUNDEBUG'};
32 my $enable_coverage = 0;
34 my $diff_tool_tmp_fh = 0;
35 my $diff_tool_tmp_filename = 0;
43 $SIG{'PIPE'} = 'IGNORE';
49 use lib "@CMAKE_BINARY_DIR@/bin";
51 use Diff qw(diff); # postpone a bit to have time to change INC
53 use Getopt::Long qw(GetOptions);
56 use IPC::Run qw(start run timeout finish);
61 #### Portability bits for windows
64 use constant RUNNING_ON_WINDOWS => ( $OSNAME =~ /^(?:mswin|dos|os2)/oi );
65 use POSIX qw(:sys_wait_h WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG
66 :signal_h SIGINT SIGTERM SIGKILL SIGABRT SIGSEGV);
69 if (RUNNING_ON_WINDOWS) { # Missing on windows
70 *WIFEXITED = sub { not $_[0] & 127 };
71 *WEXITSTATUS = sub { $_[0] >> 8 };
72 *WIFSIGNALED = sub { ( $_[0] & 127 ) && ( $_[0] & 127 != 127 ) };
73 *WTERMSIG = sub { $_[0] & 127 };
79 #### Command line option handling
82 my %opts = ( "debug" => 0,
83 "timeout" => 120, # No command should run any longer than 2 minutes by default
86 Getopt::Long::config( 'bundling', 'no_getopt_compat', 'no_auto_abbrev' );
88 'debug|d' => \$opts{"debug"},
90 'difftool=s' => \$diff_tool,
92 'cd=s' => sub { cd_cmd( $_[1] ) },
93 'timeout=s' => \$opts{'timeout'},
94 'setenv=s' => sub { setenv_cmd( $_[1] ) },
95 'cfg=s' => sub { $opts{'cfg'} .= " --cfg=$_[1]" },
96 'enable-coverage+' => \$enable_coverage,
99 $tesh_file = pop @ARGV;
101 print "Enable coverage\n" if ($enable_coverage);
104 use File::Temp qw/ tempfile /;
105 ( $diff_tool_tmp_fh, $diff_tool_tmp_filename ) = tempfile();
106 print "New tesh: $diff_tool_tmp_filename\n";
109 if ( $tesh_file =~ m/(.*)\.tesh/ ) {
111 print "Test suite `$tesh_name'\n";
113 $tesh_file = "(stdin)";
114 $tesh_name = "(stdin)";
115 print "Test suite from stdin\n";
118 ###########################################################################
122 if ( WIFEXITED($status) ) {
123 $exitcode = WEXITSTATUS($status) + 40;
124 return "returned code " . WEXITSTATUS($status);
125 } elsif ( WIFSIGNALED($status) ) {
127 if ( WTERMSIG($status) == SIGINT ) { $code = "SIGINT"; }
128 elsif ( WTERMSIG($status) == SIGTERM ) { $code = "SIGTERM"; }
129 elsif ( WTERMSIG($status) == SIGKILL ) { $code = "SIGKILL"; }
130 elsif ( WTERMSIG($status) == SIGABRT ) { $code = "SIGABRT"; }
131 elsif ( WTERMSIG($status) == SIGSEGV ) { $code = "SIGSEGV"; }
132 $exitcode = WTERMSIG($status) + 4;
133 return "got signal $code";
135 return "Unparsable status. Is the process stopped?";
139 my %cmd = %{ $_[0] };
140 if ( $opts{'debug'} ) {
141 map { print "IN: $_\n" } @{ $cmd{'in'} };
142 map { print "OUT: $_\n" } @{ $cmd{'out'} };
143 print "CMD: $cmd{'cmd'}\n";
146 # cleanup the command line
147 if (RUNNING_ON_WINDOWS) {
148 var_subst( $cmd{'cmd'}, "EXEEXT", ".exe" );
150 var_subst( $cmd{'cmd'}, "EXEEXT", "" );
153 # substitute environ variables
154 foreach my $key ( keys %environ ) {
155 $cmd{'cmd'} = var_subst( $cmd{'cmd'}, $key, $environ{$key} );
158 # substitute remaining variables, if any
159 while ( $cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/ ) {
160 $cmd{'cmd'} = var_subst( $cmd{'cmd'}, $1, "" );
162 while ( $cmd{'cmd'} =~ /\$(\w+)/ ) {
163 $cmd{'cmd'} = var_subst( $cmd{'cmd'}, $1, "" );
167 $cmd{'cmd'} .= " $opts{'cfg'}"
168 if ( defined( $opts{'cfg'} ) && length( $opts{'cfg'} ) );
170 # finally trim any remaining space chars
171 $cmd{'cmd'} =~ s/^\s+//;
172 $cmd{'cmd'} =~ s/\s+$//;
174 print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n";
176 $cmd{'return'} ||= 0;
177 $cmd{'timeout'} ||= $opts{'timeout'};
181 # exec the command line
183 my @cmdline = quotewords( '\s+', 0, $cmd{'cmd'} );
184 my $input = defined($cmd{'in'})? join("\n",@{$cmd{'in'}}) : "";
185 my $output = " " x 10240; $output = ""; # Preallocate 10kB, and reset length to 0
186 $cmd{'got'} = \$output;
187 $cmd{'job'} = start \@cmdline, '<', \$input, '>&', \$output, timeout($cmd{'timeout'});
189 if ( $cmd{'background'} ) {
190 # Just enqueue the job. It will be dealed with at the end
191 push @bg_cmds, \%cmd;
193 # Deal with its ending conditions right away
194 analyze_result( \%cmd );
199 my %cmd = %{ $_[0] };
202 finish( $cmd{'job'} );
205 if ($@ =~ /timeout/) {
206 $cmd{'job'}->kill_kill;
207 $cmd{'timeouted'} = 1;
208 } elsif ($@ =~ /^ack / and $@ =~ /pipe/) {
209 print STDERR "Tesh: Broken pipe (ignored).\n";
211 die $@; # Don't know what it is, so let it go.
214 $cmd{'timeouted'} ||= 0;
216 my $gotret = $cmd{'gotret'} = exit_status($?);
220 if ( defined( $cmd{'expect'} ) and ( $cmd{'expect'} ne "" ) ) {
221 $wantret = "got signal $cmd{'expect'}";
223 $wantret = "returned code " . ( defined( $cmd{'return'} ) ? $cmd{'return'} : 0 );
226 # pop all output from executing child
228 map { print "GOT: $_\n" } ${$cmd{'got'}} if $opts{'debug'};
229 foreach my $got ( split("\n", ${$cmd{'got'}}) ) {
232 print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
234 unless ( $enable_coverage and $got =~ /^profiling:/ ) {
239 if ( $cmd{'sort'} ) {
241 # Save the unsorted observed output to report it on error.
242 map { push @{ $cmd{'unsorted got'} }, $_ } @got;
245 substr( $a, 0, $sort_prefix ) cmp substr( $b, 0, $sort_prefix );
248 if ( $sort_prefix > 0 ) {
249 @got = sort mysort @got;
253 while ( @got and $got[0] eq "" ) {
257 # Sort the expected output to make it easier to write for humans
258 if ( defined( $cmd{'out'} ) ) {
259 if ( $sort_prefix > 0 ) {
260 @{ $cmd{'out'} } = sort mysort @{ $cmd{'out'} };
262 @{ $cmd{'out'} } = sort @{ $cmd{'out'} };
264 while ( @{ $cmd{'out'} } and ${ $cmd{'out'} }[0] eq "" ) {
265 shift @{ $cmd{'out'} };
272 if ( $cmd{'timeouted'} ) {
273 $gotret = "timeout after $cmd{'timeout'} sec";
276 print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
278 if ( $gotret ne $wantret ) {
280 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
282 $msg = $msg . "Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
283 map { $msg .= "|| $_\n" } @got;
285 $msg .= "<$cmd{'file'}:$cmd{'line'}> No output so far.\n";
290 # Does the output match?
292 if ( defined( $cmd{'output display'} ) ) {
293 print "[Tesh/INFO] Here is the (ignored) command output:\n";
294 map { print "||$_\n" } @got;
295 } elsif ( defined( $cmd{'output ignore'} ) ) {
296 print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n";
298 $diff = build_diff( \@{ $cmd{'out'} }, \@got );
300 if ( length $diff ) {
301 print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch" . ( $cmd{'sort'} ? " (even after sorting)" : "" ) . ":\n";
302 map { print "$_\n" } split( /\n/, $diff );
303 if ( $cmd{'sort'} ) {
304 print "WARNING: Both the observed output and expected output were sorted as requested.\n";
305 print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
306 if ( $sort_prefix > 0 );
307 print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
309 # print "----8<--------------- Begin of unprocessed observed output (as it should appear in file):\n";
310 # map {print "> $_\n"} @{$cmd{'unsorted got'}};
311 # print "--------------->8---- End of the unprocessed observed output.\n";
314 print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
320 my $infh; # The file descriptor from which we should read the teshfile
321 if ( $tesh_file eq "(stdin)" ) {
324 open $infh, $tesh_file
325 or die "[Tesh/CRITICAL] Unable to open $tesh_file: $!\n";
328 my %cmd; # everything about the next command to run
330 LINE: while ( defined( my $line = <$infh> ) and not $error ) {
335 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
337 # deal with line continuations
338 while ( $line =~ /^(.*?)\\$/ ) {
340 die "[TESH/CRITICAL] Continued line at end of file\n"
341 unless defined($next);
344 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
348 # Push delayed commands on empty lines
349 unless ( $line =~ m/^(.)(.*)$/ ) {
350 if ( defined( $cmd{'cmd'} ) ) {
354 print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
358 my ( $cmd, $arg ) = ( $1, $2 );
359 print $diff_tool_tmp_fh "$line\n" if ( $diff_tool and $cmd ne '>' );
364 # handle the commands
365 if ( $cmd =~ /^#/ ) { # comment
366 } elsif ( $cmd eq '>' ) { # expected result line
367 print "[TESH/debug] push expected result\n" if $opts{'debug'};
368 push @{ $cmd{'out'} }, $arg;
370 } elsif ( $cmd eq '<' ) { # provided input
371 print "[TESH/debug] push provided input\n" if $opts{'debug'};
372 push @{ $cmd{'in'} }, $arg;
374 } elsif ( $cmd eq 'p' ) { # comment
375 print "[$tesh_name:$line_num] $arg\n";
377 } elsif ( $cmd eq '$' ) { # Command
378 # if we have something buffered, run it now
379 if ( defined( $cmd{'cmd'} ) ) {
383 if ( $arg =~ /^\s*mkfile / ) { # "mkfile" command line
384 die "[TESH/CRITICAL] Output expected from mkfile command!\n"
385 if scalar @{ cmd { 'out' } };
388 $cmd{'arg'} =~ s/\s*mkfile //;
392 } elsif ( $arg =~ /^\s*cd / ) {
393 die "[TESH/CRITICAL] Input provided to cd command!\n"
394 if scalar @{ cmd { 'in' } };
395 die "[TESH/CRITICAL] Output expected from cd command!\n"
396 if scalar @{ cmd { 'out' } };
402 } else { # regular command
404 $cmd{'file'} = $tesh_file;
405 $cmd{'line'} = $line_num;
407 } elsif ( $cmd eq '&' ) { # background command line
409 if ( defined( $cmd{'cmd'} ) ) {
413 $cmd{'background'} = 1;
415 $cmd{'file'} = $tesh_file;
416 $cmd{'line'} = $line_num;
418 } elsif ( $line =~ /^!\s*output sort/ ) { #output sort
419 if ( defined( $cmd{'cmd'} ) ) {
424 if ( $line =~ /^!\s*output sort\s+(\d+)/ ) {
427 } elsif ( $line =~ /^!\s*output ignore/ ) { #output ignore
428 if ( defined( $cmd{'cmd'} ) ) {
432 $cmd{'output ignore'} = 1;
433 } elsif ( $line =~ /^!\s*output display/ ) { #output display
434 if ( defined( $cmd{'cmd'} ) ) {
438 $cmd{'output display'} = 1;
439 } elsif ( $line =~ /^!\s*expect signal (\w*)/ ) { #expect signal SIGABRT
440 if ( defined( $cmd{'cmd'} ) ) {
444 $cmd{'expect'} = "$1";
445 } elsif ( $line =~ /^!\s*expect return/ ) { #expect return
446 if ( defined( $cmd{'cmd'} ) ) {
450 $line =~ s/^! expect return //g;
452 $cmd{'return'} = $line;
453 } elsif ( $line =~ /^!\s*setenv/ ) { #setenv
454 if ( defined( $cmd{'cmd'} ) ) {
458 $line =~ s/^! setenv //g;
461 } elsif ( $line =~ /^!\s*timeout/ ) { #timeout
462 if ( defined( $cmd{'cmd'} ) ) {
466 $line =~ s/^! timeout //;
468 $cmd{'timeout'} = $line;
470 die "[TESH/CRITICAL] parse error: $line\n";
474 # We're done reading the input file
475 close $infh unless ( $tesh_file eq "(stdin)" );
477 # Deal with last command
478 if ( defined( $cmd{'cmd'} ) ) {
485 analyze_result( \%test );
489 close $diff_tool_tmp_fh;
490 system("$diff_tool $diff_tool_tmp_filename $tesh_file");
491 unlink $diff_tool_tmp_filename;
496 } elsif ( $tesh_file eq "(stdin)" ) {
497 print "Test suite from stdin OK\n";
499 print "Test suite `$tesh_name' OK\n";
505 #### Helper functions
510 my $diff = Diff->new(@_);
512 $diff->Base(1); # Return line numbers, not indices
513 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
514 return "" if ( $chunk_count == 1 && $diff->Same() );
516 while ( $diff->Next() ) {
517 my @same = $diff->Same();
518 if ( $diff->Same() ) {
519 if ( $diff->Next(0) > 1 ) { # not first chunk: print 2 first lines
520 $res .= ' ' . $same[0] . "\n";
521 $res .= ' ' . $same[1] . "\n" if ( scalar @same > 1 );
523 $res .= "...\n" if ( scalar @same > 2 );
525 # $res .= $diff->Next(0)."/$chunk_count\n";
526 if ( $diff->Next(0) < $chunk_count ) { # not last chunk: print 2 last lines
527 $res .= ' ' . $same[ scalar @same - 2 ] . "\n"
528 if ( scalar @same > 1 );
529 $res .= ' ' . $same[ scalar @same - 1 ] . "\n";
532 next if $diff->Same();
533 map { $res .= "- $_\n" } $diff->Items(1);
534 map { $res .= "+ $_\n" } $diff->Items(2);
539 # Helper function replacing any occurence of variable '$name' by its '$value'
540 # As in Bash, ${$value:=BLABLA} is rewritten to $value if set or to BLABLA if $value is not set
542 my ( $text, $name, $value ) = @_;
544 $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
545 $text =~ s/\$$name(\W|$)/$value$1/g;
547 $text =~ s/\${$name:=([^}]*)}/$1/g;
548 $text =~ s/\${$name}//g;
549 $text =~ s/\$$name(\W|$)/$1/g;
554 ################################ The possible commands ################################
557 my %cmd = %{ $_[0] };
558 my $file = $cmd{'arg'};
559 print STDERR "[Tesh/INFO] mkfile $file. Ctn: >>".join( '\n', @{ $cmd{'in'} })."<<\n"
563 open( FILE, ">$file" )
564 or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
565 print FILE join( "\n", @{ $cmd{'in'} } );
566 print FILE "\n" if ( scalar @{ $cmd{'in'} } > 0 );
570 # Command CD. Just change to the provided directory
572 my $directory = shift;
574 if ( -e $directory && -d $directory ) {
576 print "[Tesh/INFO] change directory to $directory\n";
578 } elsif ( -e $directory ) {
579 print "Cannot change directory to '$directory': it is not a directory\n";
581 print "Chdir to $directory failed: No such file or directory\n";
583 if ( $failure == 1 ) {
584 print "Test suite `$tesh_file': NOK (system error)\n";
589 # Command setenv. Gets "variable=content", and update the environment accordingly
592 if ( $arg =~ /^(.*)=(.*)$/ ) {
593 my ( $var, $ctn ) = ( $1, $2 );
594 print "[Tesh/INFO] setenv $var=$ctn\n";
595 $environ{$var} = $ctn;
598 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$arg'\n";