2 eval 'exec perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
13 B<tesh> [I<options>] I<tesh_file>
24 use Getopt::Long qw(GetOptions);
45 # make sure we received a tesh file
46 scalar @ARGV > 0 || die "Usage:\n tesh [*options*] *tesh_file*\n";
48 #Add current directory to path
49 $ENV{PATH} = "$ENV{PATH}:.";
53 ## Command line option handling
56 # option handling helper subs
60 if (-e $directory && -d $directory) {
62 print "[Tesh/INFO] change directory to $directory\n";
64 } elsif (-e $directory) {
65 print "Cannot change directory to '$directory': it is not a directory\n";
67 print "Chdir to $directory failed: No such file or directory\n";
70 print "Test suite `$tesh_file': NOK (system error)\n";
81 if ($_[0] =~ /^(.*)=(.*)$/) {
83 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
86 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
90 print "[Tesh/INFO] setenv $var=$ctn\n";
100 print "[Tesh/INFO] setenv $var=$ctn\n";
105 # Main option parsing sub
108 # remove the tesh file from the ARGV used
110 $tesh_file = pop @ARGV;
112 # temporary arrays for GetOption
124 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
127 'help|h' => \$opt{'help'},
129 'verbose|v' => \@verbose,
130 'debug|d' => \$opt{"debug"},
133 'timeout=s' => \&timeout_cmd,
134 'setenv=s' => \&setenv_cmd,
137 'enable-coverage+' => \$enable_coverage,
140 if($enable_coverage){
141 print "Enable coverage\n";
144 unless($tesh_file=~/\.tesh/){
145 $tesh_file="(stdin)";
146 print "Test suite from stdin\n";
149 $opt{'verbose'} = scalar @verbose;
151 $opt{'cfg'} .= " --cfg=$_";
156 my %opts = get_options(@ARGV);
166 my($no_output_ignore)=1;
182 if (POSIX::WIFEXITED($status)) {
183 return "returned code ".POSIX::WEXITSTATUS($status);
184 } elsif (POSIX::WIFSIGNALED($status)) {
185 return "got signal ".$SIG{POSIX::WTERMSIG($status)};
187 return "Unparsable status. Is the process stopped?";
190 if ($@) { # no POSIX available?
191 warn "POSIX not usable to parse the return value of forked child: $@\n";
193 return "returned code 0";
199 if ($opts{'debug'}) {
201 map {print " $_"} @{$cmd{'in'}};
204 map {print " $_"} @{$cmd{'out'}};
206 print "CMD: $cmd{'cmd'}\n";
209 # cleanup the command line
211 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
212 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
213 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
216 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
218 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
219 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
220 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
221 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
222 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
223 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
224 $cmd{'cmd'} =~ s|^\./||g;
225 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
226 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
227 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
229 print "[$cmd{'file'}:$cmd{'line'}] $cmd{'cmd'}\n";
232 # exec the command line
234 $pid = open3(\*CHILD_IN, \*OUT, \*OUT, $cmd{'cmd'} );
236 # push all provided input to executing child
237 map { print CHILD_IN "$_\n" } @{$cmd{'in'}};
240 # if timeout specified, fork and kill executing child at the end of timeout
243 die "fork() failed: $!" unless defined $forked;
244 if ( $forked == 0 ) { # child
252 # pop all output from executing child
254 while(defined(my $got=<OUT>)) {
269 use sort qw(defaults _quicksort); # force quicksort
270 @got = sort mysort @got;
271 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
272 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
275 # Cleanup the executing child, and kill the timeouter brother on need
276 $cmd{'return'} = 0 unless defined($cmd{'return'});
277 my $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
279 my $gotret = exit_status($?);
280 if($gotret ne $wantret) {
281 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n".
282 "Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
283 map {$msg .= "|| $_\n"} @got;
287 if($timeout){kill(9, $forked);$timeout=0;}
291 # Check the result of execution
293 my $diff = build_diff(\@{$cmd{'out'}}, \@got);
295 print color("red")."[TESH/CRITICAL$$] Output mismatch\n";
296 map { print "[TESH/CRITICAL] $_\n" } split(/\n/,$diff);
297 print color("reset");
304 my $file = $cmd{'arg'};
305 print "[Tesh/INFO] mkfile $file\n";
307 die "[TESH/CRITICAL] no input provided to mkfile\n" unless defined($cmd{'in'}) && scalar @{$cmd{'in'}};
309 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
310 print FILE join("\n", @{$cmd{'in'}});
311 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
316 #my $teshfile=$tesh_file;
317 #$teshfile=~ s{\.[^.]+$}{};
318 #print "Test suite `$teshfile'\n";
319 unless($tesh_file eq "(stdin)"){
320 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
323 my %cmd; # everything about the next command to run
325 LINE: while (defined(my $line=<TESH_FILE>) or defined(my $line=<>)) {
328 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
330 # deal with line continuations
331 while ($line =~ /^(.*?)\\$/) {
332 my $next=<TESH_FILE>;
333 die "[TESH/CRITICAL] Continued line at end of file\n"
334 unless defined($next);
336 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
340 # Push delayed commands on empty lines
341 unless ($line =~ m/^(..)(.*)$/) {
342 if (defined($cmd{'cmd'})) {
349 my ($cmd,$arg) = ($1,$2);
352 # handle the commands
353 if ($cmd =~ /^#/) { #comment
354 } elsif ($cmd eq '> '){ #expected result line
355 print "[TESH/debug] push expected result\n" if $opts{'debug'};
358 push @{$cmd{'out'}}, $arg;
361 } elsif ($cmd eq '< ') { # provided input
362 print "[TESH/debug] push provided input\n" if $opts{'debug'};
363 push @{$cmd{'in'}}, $arg;
365 } elsif ($cmd eq 'p ') { # comment
366 print "[Tesh/INFO] $arg\n";
368 } elsif ($cmd eq '$ ') { # Command
369 # if we have something buffered, run it now
370 if (defined($cmd{'cmd'})) {
374 if ($arg =~ /^ *mkfile /){ # "mkfile" command line
375 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
378 $cmd{'arg'} =~ s/ *mkfile //;
382 } elsif ($arg =~ /^ *cd /) {
383 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
384 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
390 } else { # regular command
392 $cmd{'file'} = $tesh_file;
393 $cmd{'line'} = $line_num;
396 elsif($cmd eq '& '){ # parallel command line
397 $cmd{'background'} = 1;
400 elsif($line =~ /^! output sort/){ #output sort
404 elsif($line =~ /^! output ignore/){ #output ignore
405 $cmd{'output ignore'} = 1;
407 elsif($line =~ /^! expect signal SIGABRT$/) {#expect signal SIGABRT
408 $cmd{'expect'} = "SIGABRT";
410 elsif($line =~ /^! expect return/){ #expect return
411 $line =~ s/^! expect return //g;
413 $cmd{'return'} = $line;
415 elsif($line =~ /^! setenv/){ #setenv
416 $line =~ s/^! setenv //g;
420 elsif($line =~ /^! include/){ #output sort
421 print color("red"), "[Tesh/CRITICAL] need include";
422 print color("reset"), "\n";
425 elsif($line =~ /^! timeout/){ #timeout
426 $line =~ s/^! timeout //;
428 $cmd{'timeout'} = $line;
430 die "[TESH/CRITICAL] parse error: $line\n";
436 # Deal with last command
437 if (defined($cmd{'cmd'})) {
442 if($tesh_file eq "(stdin)"){
443 print "Test suite from stdin OK\n";
447 #push @a,"bl1"; push @b,"bl1";
448 #push @a,"bl2"; push @b,"bl2";
449 #push @a,"bl3"; push @b,"bl3";
450 #push @a,"bl4"; push @b,"bl4";
451 #push @a,"bl5"; push @b,"bl5";
452 #push @a,"bl6"; push @b,"bl6";
453 #push @a,"bl7"; push @b,"bl7";
454 ##push @a,"Perl"; push @b,"ruby";
455 #push @a,"END1"; push @b,"END1";
456 #push @a,"END2"; push @b,"END2";
457 #push @a,"END3"; push @b,"END3";
458 #push @a,"END4"; push @b,"END4";
459 #push @a,"END5"; push @b,"END5";
460 #push @a,"END6"; push @b,"END6";
461 #push @a,"END7"; push @b,"END7";
462 #print "Identical:\n". build_diff(\@a,\@b);
465 #push @a,"AZE"; push @b,"EZA";
466 #print "Different:\n".build_diff(\@a,\@b);
468 use lib "@CMAKE_BINARY_DIR@/bin" ;
470 use Diff qw(diff); # postpone a bit to have time to change INC
474 my $diff = Diff->new(@_);
476 $diff->Base( 1 ); # Return line numbers, not indices
477 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
478 return "" if ($chunk_count == 1 && $diff->Same());
480 while( $diff->Next() ) {
481 my @same = $diff->Same();
482 if ($diff->Same() ) {
483 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
484 $res .= ' '.$same[0]."\n" ;
485 $res .= ' '.$same[1]."\n" if (scalar @same>1);
487 $res .= "...\n" if (scalar @same>2);
488 # $res .= $diff->Next(0)."/$chunk_count\n";
489 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
490 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
491 $res .= ' '.$same[scalar @same -1]."\n";
494 next if $diff->Same();
495 map { $res .= "- $_\n" } $diff->Items(1);
496 map { $res .= "+ $_\n" } $diff->Items(2);