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 print "OS: ".$OS."\n";
47 # make sure we received a tesh file
48 scalar @ARGV > 0 || die "Usage:\n tesh [*options*] *tesh_file*\n";
50 #Add current directory to path
51 $ENV{PATH} = "$ENV{PATH}:.";
55 ## Command line option handling
58 # option handling helper subs
61 if (-e $directory && -d $directory) {
63 print "[Tesh/INFO] change directory to $directory\n";
64 } elsif (-e $directory) {
65 die "[Tesh/CRITICAL] Cannot change directory to '$directory': it is not a directory\n";
67 die "[Tesh/CRITICAL] Cannot change directory to '$directory': no such directory\n";
77 if ($_[0] =~ /^(.*)=(.*)$/) {
79 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
82 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
86 print "[Tesh/INFO] setenv $var=$ctn\n";
96 print "[Tesh/INFO] setenv $var=$ctn\n";
101 # Main option parsing sub
104 # remove the tesh file from the ARGV used
106 $tesh_file = pop @ARGV;
108 # temporary arrays for GetOption
119 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
122 'help|h' => \$opt{'help'},
124 'verbose|v' => \@verbose,
125 'debug|d' => \$opt{"debug"},
128 'timeout=s' => \&timeout_cmd,
129 'setenv=s' => \&setenv_cmd,
134 $opt{'verbose'} = scalar @verbose;
136 $opt{'cfg'} .= " --cfg=$_";
141 my %opts = get_options(@ARGV);
151 my($no_output_ignore)=1;
167 if (POSIX::WIFEXITED($status)) {
168 return "returned code ".POSIX::WEXITSTATUS($status);
169 } elsif (POSIX::WIFSIGNALED($status)) {
170 return "got signal ".$SIG{POSIX::WTERMSIG($status)};
172 return "Unparsable status. Is the process stopped?";
175 if ($@) { # no POSIX available?
176 warn "POSIX not usable to parse the return value of forked child: $@\n";
178 return "returned code 0";
184 if ($opts{'debug'}) {
186 map {print " $_"} @{$cmd{'in'}};
189 map {print " $_"} @{$cmd{'out'}};
191 print "CMD: $cmd{'cmd'}\n";
194 # cleanup the command line
196 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
197 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
198 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
201 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
203 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
204 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
205 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
206 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
207 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
208 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
209 $cmd{'cmd'} =~ s|^\./||g;
210 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
211 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
212 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
214 print "[$cmd{'file'}:$cmd{'line'}] $cmd{'cmd'}\n";
217 # exec the command line
219 $pid = open3(\*IN, \*OUT, \*OUT, $cmd{'cmd'} );
221 # if timeout specified, fork and kill executing child at the end of timeout
224 die "fork() failed: $!" unless defined $forked;
225 if ( $forked == 0 ) { # child
232 # push all provided input to executing child
233 map { print IN "$_\n" } $cmd{'in'};
236 # pop all output from executing child
238 while(defined(my $got=<OUT>)) {
253 use sort qw(defaults _quicksort); # force quicksort
254 @got = sort mysort @got;
255 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
256 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
259 # Cleanup the executing child, and kill the timeouter brother on need
260 $cmd{'return'} = 0 unless defined($cmd{'return'});
261 my $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
263 my $gotret = exit_status($?);
264 if($gotret ne $wantret) {
265 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n".
266 "Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
267 map {$msg .= "|| $_\n"} @got;
271 if($timeout){kill(9, $forked);$timeout=0;}
275 # Check the result of execution
277 my $diff = build_diff(\@{$cmd{'out'}}, \@got);
279 print color("red")."[TESH/CRITICAL$$] Output mismatch\n";
280 map { print "[TESH/CRITICAL] $_\n" } split(/\n/,$diff);
281 print color("reset");
288 my $file = $cmd{'arg'};
289 print "[Tesh/INFO] mkfile $file\n";
291 die "[TESH/CRITICAL] no input provided to mkfile\n" unless defined($cmd{'in'}) && scalar @{$cmd{'in'}};
293 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
294 print FILE join("\n", @{$cmd{'in'}});
295 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
300 print "Test suite $tesh_file\n";
301 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
304 my %cmd; # everything about the next command to run
306 LINE: while (defined(my $line=<TESH_FILE>)) {
309 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
311 # deal with line continuations
312 while ($line =~ /^(.*?)\\$/) {
313 my $next=<TESH_FILE>;
314 die "[TESH/CRITICAL] Continued line at end of file\n"
315 unless defined($next);
317 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
321 # Push delayed commands on empty lines
322 unless ($line =~ m/^(..)(.*)$/) {
323 if (defined($cmd{'cmd'})) {
330 my ($cmd,$arg) = ($1,$2);
333 # handle the commands
334 if ($cmd =~ /^#/) { #comment
335 } elsif ($cmd eq '> '){ #expected result line
336 print "[TESH/debug] push expected result\n" if $opts{'debug'};
339 push @{$cmd{'out'}}, $arg;
342 } elsif ($cmd eq '< ') { # provided input
343 print "[TESH/debug] push provided input\n" if $opts{'debug'};
344 push @{$cmd{'in'}}, $arg;
346 } elsif ($cmd eq 'p ') { # comment
347 print "[Tesh/INFO] $arg\n";
349 } elsif ($cmd eq '$ ') { # Command
350 # if we have something buffered, run it now
351 if (defined($cmd{'cmd'})) {
355 if ($arg =~ /^ *mkfile /){ # "mkfile" command line
356 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
359 $cmd{'arg'} =~ s/ *mkfile //;
363 } elsif ($arg =~ /^ *cd /) {
364 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
365 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
371 } else { # regular command
373 $cmd{'file'} = $tesh_file;
374 $cmd{'line'} = $line_num;
377 elsif($cmd eq '& '){ # parallel command line
378 $cmd{'background'} = 1;
381 elsif($line =~ /^! output sort/){ #output sort
385 elsif($line =~ /^! output ignore/){ #output ignore
386 $cmd{'output ignore'} = 1;
388 elsif($line =~ /^! expect signal SIGABRT$/) {#expect signal SIGABRT
389 $cmd{'expect'} = "SIGABRT";
391 elsif($line =~ /^! expect return/){ #expect return
392 $line =~ s/^! expect return //g;
394 $cmd{'return'} = $line;
396 elsif($line =~ /^! setenv/){ #setenv
397 $line =~ s/^! setenv //g;
401 elsif($line =~ /^! include/){ #output sort
402 print color("red"), "[Tesh/CRITICAL] need include";
403 print color("reset"), "\n";
406 elsif($line =~ /^! timeout/){ #timeout
407 $line =~ s/^! timeout //;
409 $cmd{'timeout'} = $line;
411 die "[TESH/CRITICAL] parse error: $line\n";
415 # Deal with last command
416 if (defined($cmd{'cmd'})) {
422 #push @a,"bl1"; push @b,"bl1";
423 #push @a,"bl2"; push @b,"bl2";
424 #push @a,"bl3"; push @b,"bl3";
425 #push @a,"bl4"; push @b,"bl4";
426 #push @a,"bl5"; push @b,"bl5";
427 #push @a,"bl6"; push @b,"bl6";
428 #push @a,"bl7"; push @b,"bl7";
429 ##push @a,"Perl"; push @b,"ruby";
430 #push @a,"END1"; push @b,"END1";
431 #push @a,"END2"; push @b,"END2";
432 #push @a,"END3"; push @b,"END3";
433 #push @a,"END4"; push @b,"END4";
434 #push @a,"END5"; push @b,"END5";
435 #push @a,"END6"; push @b,"END6";
436 #push @a,"END7"; push @b,"END7";
437 #print "Identical:\n". build_diff(\@a,\@b);
440 #push @a,"AZE"; push @b,"EZA";
441 #print "Different:\n".build_diff(\@a,\@b);
443 use lib "@CMAKE_BINARY_DIR@/bin" ;
445 use Diff qw(diff); # postpone a bit to have time to change INC
449 my $diff = Diff->new(@_);
451 $diff->Base( 1 ); # Return line numbers, not indices
452 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
453 return "" if ($chunk_count == 1 && $diff->Same());
455 while( $diff->Next() ) {
456 my @same = $diff->Same();
457 if ($diff->Same() ) {
458 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
459 $res .= ' '.$same[0]."\n" ;
460 $res .= ' '.$same[1]."\n" if (scalar @same>1);
462 $res .= "...\n" if (scalar @same>2);
463 # $res .= $diff->Next(0)."/$chunk_count\n";
464 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
465 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
466 $res .= ' '.$same[scalar @same -1]."\n";
469 next if $diff->Same();
470 map { $res .= "- $_\n" } $diff->Items(1);
471 map { $res .= "+ $_\n" } $diff->Items(2);