2 eval 'exec perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
13 B<tesh> [I<options>] I<tesh_file>
23 use Getopt::Long qw(GetOptions);
44 print "OS: ".$OS."\n";
46 # make sure we received a tesh file
47 scalar @ARGV > 0 || die "Usage:\n tesh [*options*] *tesh_file*\n";
49 #Add current directory to path
50 $ENV{PATH} = "$ENV{PATH}:.";
54 ## Command line option handling
57 # option handling helper subs
60 if (-e $directory && -d $directory) {
62 print "[Tesh/INFO] change directory to $directory\n";
63 } elsif (-e $directory) {
64 die "[Tesh/CRITICAL] Cannot change directory to '$directory': it is not a directory\n";
66 die "[Tesh/CRITICAL] Cannot change directory to '$directory': no such directory\n";
72 if ($_[0] =~ /^(.*)=(.*)$/) {
74 }elsif ($_[1] =~ /^(.*)=(.*)$/) {
77 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
81 print "[Tesh/INFO] setenv $var=$ctn\n";
91 print "[Tesh/INFO] setenv $var=$ctn\n";
96 # Main option parsing sub
99 # remove the tesh file from the ARGV used
101 $tesh_file = pop @ARGV;
103 # temporary arrays for GetOption
114 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
117 'help|h' => \$opt{'help'},
119 'verbose|v' => \@verbose,
120 'debug|d' => \$opt{"debug"},
123 'setenv=s' => \&setenv_cmd,
128 $opt{'verbose'} = scalar @verbose;
130 $opt{'cfg'} .= " --cfg=$_";
135 my %opts = get_options(@ARGV);
146 my($no_output_ignore)=1;
162 if (POSIX::WIFEXITED($status)) {
163 return "returned code ".POSIX::WEXITSTATUS($status);
164 } elsif (POSIX::WIFSIGNALED($status)) {
165 return "got signal ".$SIG{POSIX::WTERMSIG($status)};
167 return "Unparsable status. Is the process stopped?";
170 if ($@) { # no POSIX available?
171 warn "POSIX not usable to parse the return value of forked child: $@\n";
173 return "returned code 0";
179 if ($opts{'debug'}) {
181 map {print " $_"} @{$cmd{'in'}};
184 map {print " $_"} @{$cmd{'out'}};
186 print "CMD: $cmd{'cmd'}\n";
189 # cleanup the command line
191 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
192 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
193 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
196 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
198 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
199 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
200 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
201 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
202 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
203 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
204 $cmd{'cmd'} =~ s|^\./||g;
205 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
206 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
207 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
209 print "[$cmd{'file'}:$cmd{'line'}] $cmd{'cmd'}\n";
212 # exec the command line
214 $pid = open3(\*IN, \*OUT, \*OUT, $cmd{'cmd'} );
216 # if timeout specified, fork and kill executing child at the end of timeout
219 die "fork() failed: $!" unless defined $forked;
220 if ( $forked == 0 ) { # child
227 # push all provided input to executing child
228 map { print IN "$_\n" } $cmd{'in'};
231 # pop all output from executing child
233 while(defined(my $got=<OUT>)) {
248 use sort qw(defaults _quicksort); # force quicksort
249 @got = sort mysort @got;
250 #also resort the other one, as perl sort is not the same as the C one used to generate teshes
251 @{$cmd{'out'}}=sort mysort @{$cmd{'out'}};
254 # Cleanup the executing child, and kill the timeouter brother on need
255 $cmd{'return'} = 0 unless defined($cmd{'return'});
256 my $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
258 my $gotret = exit_status($?);
259 if($gotret ne $wantret) {
260 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n".
261 "Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
262 map {$msg .= "|| $_\n"} @got;
266 if($timeout){kill(9, $forked);$timeout=0;}
270 # Check the result of execution
272 my $diff = build_diff(\@{$cmd{'out'}}, \@got);
274 print color("red")."[TESH/CRITICAL$$] Output mismatch\n";
275 map { print "[TESH/CRITICAL] $_\n" } split(/\n/,$diff);
276 print color("reset");
283 my $file = $cmd{'arg'};
284 print "[Tesh/INFO] mkfile $file\n";
286 die "[TESH/CRITICAL] no input provided to mkfile\n" unless defined($cmd{'in'}) && scalar @{$cmd{'in'}};
288 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
289 print FILE join("\n", @{$cmd{'in'}});
290 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
295 print "Test suite $tesh_file\n";
296 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
299 my %cmd; # everything about the next command to run
301 LINE: while (defined(my $line=<TESH_FILE>)) {
304 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
306 # deal with line continuations
307 while ($line =~ /^(.*?)\\$/) {
308 my $next=<TESH_FILE>;
309 die "[TESH/CRITICAL] Continued line at end of file\n"
310 unless defined($next);
312 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
316 # Push delayed commands on empty lines
317 unless ($line =~ m/^(..)(.*)$/) {
318 if (defined($cmd{'cmd'})) {
325 my ($cmd,$arg) = ($1,$2);
328 # handle the commands
329 if ($cmd =~ /^#/) { #comment
330 } elsif ($cmd eq '> '){ #expected result line
331 print "[TESH/debug] push expected result\n" if $opts{'debug'};
334 push @{$cmd{'out'}}, $arg;
337 } elsif ($cmd eq '< ') { # provided input
338 print "[TESH/debug] push provided input\n" if $opts{'debug'};
339 push @{$cmd{'in'}}, $arg;
341 } elsif ($cmd eq 'p ') { # comment
342 print "[Tesh/INFO] $arg\n";
344 } elsif ($cmd eq '$ ') { # Command
345 # if we have something buffered, run it now
346 if (defined($cmd{'cmd'})) {
350 if ($arg =~ /^ *mkfile /){ # "mkfile" command line
351 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
354 $cmd{'arg'} =~ s/ *mkfile //;
358 } elsif ($arg =~ /^ *cd /) {
359 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
360 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
366 } else { # regular command
368 $cmd{'file'} = $tesh_file;
369 $cmd{'line'} = $line_num;
372 elsif($cmd eq '& '){ # parallel command line
373 $cmd{'background'} = 1;
376 elsif($line =~ /^! output sort/){ #output sort
380 elsif($line =~ /^! output ignore/){ #output ignore
381 $cmd{'output ignore'} = 1;
383 elsif($line =~ /^! expect signal SIGABRT$/) {#expect signal SIGABRT
384 $cmd{'expect'} = "SIGABRT";
386 elsif($line =~ /^! expect return/){ #expect return
387 $line =~ s/^! expect return //g;
389 $cmd{'return'} = $line;
391 elsif($line =~ /^! setenv/){ #setenv
392 $line =~ s/^! setenv //g;
396 elsif($line =~ /^! include/){ #output sort
397 print color("red"), "[Tesh/CRITICAL] need include";
398 print color("reset"), "\n";
401 elsif($line =~ /^! timeout/){ #timeout
402 $line =~ s/^! timeout //;
404 $cmd{'timeout'} = $line;
406 die "[TESH/CRITICAL] parse error: $line\n";
410 # Deal with last command
411 if (defined($cmd{'cmd'})) {
417 #push @a,"bl1"; push @b,"bl1";
418 #push @a,"bl2"; push @b,"bl2";
419 #push @a,"bl3"; push @b,"bl3";
420 #push @a,"bl4"; push @b,"bl4";
421 #push @a,"bl5"; push @b,"bl5";
422 #push @a,"bl6"; push @b,"bl6";
423 #push @a,"bl7"; push @b,"bl7";
424 ##push @a,"Perl"; push @b,"ruby";
425 #push @a,"END1"; push @b,"END1";
426 #push @a,"END2"; push @b,"END2";
427 #push @a,"END3"; push @b,"END3";
428 #push @a,"END4"; push @b,"END4";
429 #push @a,"END5"; push @b,"END5";
430 #push @a,"END6"; push @b,"END6";
431 #push @a,"END7"; push @b,"END7";
432 #print "Identical:\n". build_diff(\@a,\@b);
435 #push @a,"AZE"; push @b,"EZA";
436 #print "Different:\n".build_diff(\@a,\@b);
438 use Diff qw(diff); # postpone a bit to have time to change INC
442 my $diff = Diff->new(@_);
444 $diff->Base( 1 ); # Return line numbers, not indices
445 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
446 return "" if ($chunk_count == 1 && $diff->Same());
448 while( $diff->Next() ) {
449 my @same = $diff->Same();
450 if ($diff->Same() ) {
451 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
452 $res .= ' '.$same[0]."\n" ;
453 $res .= ' '.$same[1]."\n" if (scalar @same>1);
455 $res .= "...\n" if (scalar @same>2);
456 # $res .= $diff->Next(0)."/$chunk_count\n";
457 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
458 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
459 $res .= ' '.$same[scalar @same -1]."\n";
462 next if $diff->Same();
463 map { $res .= "- $_\n" } $diff->Items(1);
464 map { $res .= "+ $_\n" } $diff->Items(2);