2 eval 'exec perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
13 B<tesh> [I<options>] I<tesh_file>
22 use Getopt::Long qw(GetOptions);
32 print("$OS and %OS%\n");
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";
73 if ($_[0] =~ /^(.*)=(.*)$/) {
75 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
78 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
82 print "[Tesh/INFO] setenv $var=$ctn\n";
92 print "[Tesh/INFO] setenv $var=$ctn\n";
97 # Main option parsing sub
100 # remove the tesh file from the ARGV used
102 $tesh_file = pop @ARGV;
104 # temporary arrays for GetOption
115 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
118 'help|h' => \$opt{'help'},
120 'verbose|v' => \@verbose,
121 'debug|d' => \$opt{"debug"},
124 'setenv=s' => \&setenv_cmd,
129 $opt{'verbose'} = scalar @verbose;
131 $opt{'cfg'} .= " --cfg=$_";
136 my %opts = get_options(@ARGV);
147 my($no_output_ignore)=1;
163 if (POSIX::WIFEXITED($status)) {
164 return "returned code ".POSIX::WEXITSTATUS($status);
165 } elsif (POSIX::WIFSIGNALED($status)) {
166 return "got signal ".$SIG{POSIX::WTERMSIG($status)};
168 return "Unparsable status. Is the process stopped?";
171 if ($@) { # no POSIX available?
172 warn "POSIX not usable to parse the return value of forked child: $@\n";
174 return "returned code 0";
180 if ($opts{'debug'}) {
182 map {print " $_"} @{$cmd{'in'}};
185 map {print " $_"} @{$cmd{'out'}};
187 print "CMD: $cmd{'cmd'}\n";
190 # cleanup the command line
192 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
193 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
194 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
197 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
199 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
200 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
201 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
202 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
203 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
204 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
205 $cmd{'cmd'} =~ s|^\./||g;
206 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
207 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
208 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
210 print "[$cmd{'file'}:$cmd{'line'}] $cmd{'cmd'}\n";
213 # exec the command line
215 $pid = open3(\*IN, \*OUT, \*OUT, $cmd{'cmd'} );
217 # if timeout specified, fork and kill executing child at the end of timeout
220 die "fork() failed: $!" unless defined $forked;
221 if ( $forked == 0 ) { # child
228 # push all provided input to executing child
229 map { print IN "$_\n" } $cmd{'in'};
232 # pop all output from executing child
234 while(defined(my $got=<OUT>)) {
249 use sort qw(defaults _quicksort); # force quicksort
250 @got = sort mysort @got;
251 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
252 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
255 # Cleanup the executing child, and kill the timeouter brother on need
256 $cmd{'return'} = 0 unless defined($cmd{'return'});
257 my $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
259 my $gotret = exit_status($?);
260 if($gotret ne $wantret) {
261 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n".
262 "Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
263 map {$msg .= "|| $_\n"} @got;
267 if($timeout){kill(9, $forked);$timeout=0;}
271 # Check the result of execution
273 my $diff = build_diff(\@{$cmd{'out'}}, \@got);
275 print color("red")."[TESH/CRITICAL$$] Output mismatch\n";
276 map { print "[TESH/CRITICAL] $_\n" } split(/\n/,$diff);
277 print color("reset");
284 my $file = $cmd{'arg'};
285 print "[Tesh/INFO] mkfile $file\n";
287 die "[TESH/CRITICAL] no input provided to mkfile\n" unless defined($cmd{'in'}) && scalar @{$cmd{'in'}};
289 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
290 print FILE join("\n", @{$cmd{'in'}});
291 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
296 print "Test suite $tesh_file\n";
297 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
300 my %cmd; # everything about the next command to run
302 LINE: while (defined(my $line=<TESH_FILE>)) {
305 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
307 # deal with line continuations
308 while ($line =~ /^(.*?)\\$/) {
309 my $next=<TESH_FILE>;
310 die "[TESH/CRITICAL] Continued line at end of file\n"
311 unless defined($next);
313 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
317 # Push delayed commands on empty lines
318 unless ($line =~ m/^(..)(.*)$/) {
319 if (defined($cmd{'cmd'})) {
326 my ($cmd,$arg) = ($1,$2);
329 # handle the commands
330 if ($cmd =~ /^#/) { #comment
331 } elsif ($cmd eq '> '){ #expected result line
332 print "[TESH/debug] push expected result\n" if $opts{'debug'};
335 push @{$cmd{'out'}}, $arg;
338 } elsif ($cmd eq '< ') { # provided input
339 print "[TESH/debug] push provided input\n" if $opts{'debug'};
340 push @{$cmd{'in'}}, $arg;
342 } elsif ($cmd eq 'p ') { # comment
343 print "[Tesh/INFO] $arg\n";
345 } elsif ($cmd eq '$ ') { # Command
346 # if we have something buffered, run it now
347 if (defined($cmd{'cmd'})) {
351 if ($arg =~ /^ *mkfile /){ # "mkfile" command line
352 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
355 $cmd{'arg'} =~ s/ *mkfile //;
359 } elsif ($arg =~ /^ *cd /) {
360 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
361 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
367 } else { # regular command
369 $cmd{'file'} = $tesh_file;
370 $cmd{'line'} = $line_num;
373 elsif($cmd eq '& '){ # parallel command line
374 $cmd{'background'} = 1;
377 elsif($line =~ /^! output sort/){ #output sort
381 elsif($line =~ /^! output ignore/){ #output ignore
382 $cmd{'output ignore'} = 1;
384 elsif($line =~ /^! expect signal SIGABRT$/) {#expect signal SIGABRT
385 $cmd{'expect'} = "SIGABRT";
387 elsif($line =~ /^! expect return/){ #expect return
388 $line =~ s/^! expect return //g;
390 $cmd{'return'} = $line;
392 elsif($line =~ /^! setenv/){ #setenv
393 $line =~ s/^! setenv //g;
397 elsif($line =~ /^! include/){ #output sort
398 print color("red"), "[Tesh/CRITICAL] need include";
399 print color("reset"), "\n";
402 elsif($line =~ /^! timeout/){ #timeout
403 $line =~ s/^! timeout //;
405 $cmd{'timeout'} = $line;
407 die "[TESH/CRITICAL] parse error: $line\n";
411 # Deal with last command
412 if (defined($cmd{'cmd'})) {
418 #push @a,"bl1"; push @b,"bl1";
419 #push @a,"bl2"; push @b,"bl2";
420 #push @a,"bl3"; push @b,"bl3";
421 #push @a,"bl4"; push @b,"bl4";
422 #push @a,"bl5"; push @b,"bl5";
423 #push @a,"bl6"; push @b,"bl6";
424 #push @a,"bl7"; push @b,"bl7";
425 ##push @a,"Perl"; push @b,"ruby";
426 #push @a,"END1"; push @b,"END1";
427 #push @a,"END2"; push @b,"END2";
428 #push @a,"END3"; push @b,"END3";
429 #push @a,"END4"; push @b,"END4";
430 #push @a,"END5"; push @b,"END5";
431 #push @a,"END6"; push @b,"END6";
432 #push @a,"END7"; push @b,"END7";
433 #print "Identical:\n". build_diff(\@a,\@b);
436 #push @a,"AZE"; push @b,"EZA";
437 #print "Different:\n".build_diff(\@a,\@b);
439 use Diff qw(diff); # postpone a bit to have time to change INC
443 my $diff = Diff->new(@_);
445 $diff->Base( 1 ); # Return line numbers, not indices
446 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
447 return "" if ($chunk_count == 1 && $diff->Same());
449 while( $diff->Next() ) {
450 my @same = $diff->Same();
451 if ($diff->Same() ) {
452 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
453 $res .= ' '.$same[0]."\n" ;
454 $res .= ' '.$same[1]."\n" if (scalar @same>1);
456 $res .= "...\n" if (scalar @same>2);
457 # $res .= $diff->Next(0)."/$chunk_count\n";
458 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
459 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
460 $res .= ' '.$same[scalar @same -1]."\n";
463 next if $diff->Same();
464 map { $res .= "- $_\n" } $diff->Items(1);
465 map { $res .= "+ $_\n" } $diff->Items(2);