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);
35 # make sure we received a tesh file
36 scalar @ARGV > 0 || die "Usage:\n tesh [*options*] *tesh_file*\n";
38 #Add current directory to path
39 $ENV{PATH} = "$ENV{PATH}:.";
43 ## Command line option handling
46 # option handling helper subs
49 if (-e $directory && -d $directory) {
51 print "[Tesh/INFO] change directory to $directory\n";
52 } elsif (-e $directory) {
53 die "[Tesh/CRITICAL] Cannot change directory to '$directory': it is not a directory\n";
55 die "[Tesh/CRITICAL] Cannot change directory to '$directory': no such directory\n";
60 if ($_[1] =~ /^(.*)=(.*)$/) {
61 my($var,$ctn)=($1,$2);
64 print "[Tesh/INFO] setenv $var=$ctn\n";
74 print "[Tesh/INFO] setenv $var=$ctn\n";
78 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
82 # Main option parsing sub
85 # remove the tesh file from the ARGV used
87 $tesh_file = pop @ARGV;
89 # temporary arrays for GetOption
100 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
103 'help|h' => \$opt{'help'},
105 'verbose|v' => \@verbose,
106 'debug|d' => \$opt{"debug"},
109 'setenv=s' => \&setenv_cmd,
114 $opt{'verbose'} = scalar @verbose;
116 $opt{'cfg'} .= " --cfg=$_";
121 my %opts = get_options(@ARGV);
132 my($no_output_ignore)=1;
148 if (POSIX::WIFEXITED($status)) {
149 return "returned code ".POSIX::WEXITSTATUS($status);
150 } elsif (POSIX::WIFSIGNALED($status)) {
151 return "got signal ".$SIG{POSIX::WTERMSIG($status)};
153 return "Unparsable status. Is the process stopped?";
156 if ($@) { # no POSIX available?
157 warn "POSIX not usable to parse the return value of forked child: $@\n";
159 return "returned code 0";
165 if ($opts{'debug'}) {
167 map {print " $_"} @{$cmd{'in'}};
170 map {print " $_"} @{$cmd{'out'}};
172 print "CMD: $cmd{'cmd'}\n";
175 # cleanup the command line
177 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
180 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
182 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
183 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
184 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
185 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
186 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
187 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
188 $cmd{'cmd'} =~ s|^\./||g;
189 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
190 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
191 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
193 print "[$cmd{'file'}:$cmd{'line'}] $cmd{'cmd'}\n";
196 # exec the command line
198 $pid = open3(\*IN, \*OUT, \*OUT, $cmd{'cmd'} );
200 # if timeout specified, fork and kill executing child at the end of timeout
203 die "fork() failed: $!" unless defined $forked;
204 if ( $forked == 0 ) { # child
211 # push all provided input to executing child
212 map { print IN "$_\n" } $cmd{'in'};
215 # pop all output from executing child
217 while(defined(my $got=<OUT>)) {
225 # Cleanup the executing child, and kill the timeouter brother on need
226 $cmd{'return'} = 0 unless defined($cmd{'return'});
227 my $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
229 my $gotret = exit_status($?);
230 if($gotret ne $wantret) {
231 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n".
232 "Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
233 map {$msg .= "|| $_\n"} @got;
237 if($timeout){kill(9, $forked);$timeout=0;}
241 # Check the result of execution
243 my $diff = build_diff(\@{$cmd{'out'}}, \@got);
245 print color("red")."[TESH/CRITICAL$$] Output mismatch\n";
246 map { print "[TESH/CRITICAL] $_\n" } split(/\n/,$diff);
247 print color("reset");
254 my $file = $cmd{'arg'};
255 print "[Tesh/INFO] mkfile $file\n";
257 die "[TESH/CRITICAL] no input provided to mkfile\n" unless defined($cmd{'in'}) && scalar @{$cmd{'in'}};
259 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
260 print FILE join("\n", @{$cmd{'in'}});
261 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
266 print "Test suite $tesh_file\n";
267 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
270 my %cmd; # everything about the next command to run
272 LINE: while (defined(my $line=<TESH_FILE>)) {
275 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
277 # deal with line continuations
278 while ($line =~ /^(.*?)\\$/) {
279 my $next=<TESH_FILE>;
280 die "[TESH/CRITICAL] Continued line at end of file\n"
281 unless defined($next);
283 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
287 # Push delayed commands on empty lines
288 unless ($line =~ m/^(..)(.*)$/) {
289 if (defined($cmd{'cmd'})) {
296 my ($cmd,$arg) = ($1,$2);
299 # handle the commands
300 if ($cmd =~ /^#/) { #comment
301 } elsif ($cmd eq '> '){ #expected result line
302 print "[TESH/debug] push expected result\n" if $opts{'debug'};
303 push @{$cmd{'out'}}, $arg;
305 } elsif ($cmd eq '< ') { # provided input
306 print "[TESH/debug] push provided input\n" if $opts{'debug'};
307 push @{$cmd{'in'}}, $arg;
309 } elsif ($cmd eq 'p ') { # comment
310 print "[Tesh/INFO] $arg\n";
312 } elsif ($cmd eq '$ ') { # Command
313 # if we have something buffered, run it now
314 if (defined($cmd{'cmd'})) {
318 if ($arg =~ /^ *mkfile /){ # "mkfile" command line
319 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
322 $cmd{'arg'} =~ s/ *mkfile //;
326 } elsif ($arg =~ /^ *cd /) {
327 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
328 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
334 } else { # regular command
336 $cmd{'file'} = $tesh_file;
337 $cmd{'line'} = $line_num;
340 elsif($cmd eq '& '){ # parallel command line
341 $cmd{'background'} = 1;
344 elsif($line =~ /^! output sort/){ #output sort
347 elsif($line =~ /^! output ignore/){ #output ignore
348 $cmd{'output ignore'} = 1;
350 elsif($line =~ /^! expect signal SIGABRT$/) {#expect signal SIGABRT
351 $cmd{'expect'} = "SIGABRT";
353 elsif($line =~ /^! expect return/){ #expect return
354 $line =~ s/^! expect return //g;
356 $cmd{'return'} = $line;
358 elsif($line =~ /^! setenv/){ #setenv
359 $line =~ s/^! setenv //g;
363 elsif($line =~ /^! include/){ #output sort
364 print color("red"), "[Tesh/CRITICAL] need include";
365 print color("reset"), "\n";
368 elsif($line =~ /^! timeout/){ #timeout
369 $line =~ s/^! timeout //;
371 $cmd{'timeout'} = $line;
373 die "[TESH/CRITICAL] parse error: $line\n";
377 # Deal with last command
378 if (defined($cmd{'cmd'})) {
384 #push @a,"bl1"; push @b,"bl1";
385 #push @a,"bl2"; push @b,"bl2";
386 #push @a,"bl3"; push @b,"bl3";
387 #push @a,"bl4"; push @b,"bl4";
388 #push @a,"bl5"; push @b,"bl5";
389 #push @a,"bl6"; push @b,"bl6";
390 #push @a,"bl7"; push @b,"bl7";
391 ##push @a,"Perl"; push @b,"ruby";
392 #push @a,"END1"; push @b,"END1";
393 #push @a,"END2"; push @b,"END2";
394 #push @a,"END3"; push @b,"END3";
395 #push @a,"END4"; push @b,"END4";
396 #push @a,"END5"; push @b,"END5";
397 #push @a,"END6"; push @b,"END6";
398 #push @a,"END7"; push @b,"END7";
399 #print "Identical:\n". build_diff(\@a,\@b);
402 #push @a,"AZE"; push @b,"EZA";
403 #print "Different:\n".build_diff(\@a,\@b);
405 use Diff qw(diff); # postpone a bit to have time to change INC
409 my $diff = Diff->new(@_);
411 $diff->Base( 1 ); # Return line numbers, not indices
412 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
413 return "" if ($chunk_count == 1 && $diff->Same());
415 while( $diff->Next() ) {
416 my @same = $diff->Same();
417 if ($diff->Same() ) {
418 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
419 $res .= ' '.$same[0]."\n" ;
420 $res .= ' '.$same[1]."\n" if (scalar @same>1);
422 $res .= "...\n" if (scalar @same>2);
423 # $res .= $diff->Next(0)."/$chunk_count\n";
424 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
425 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
426 $res .= ' '.$same[scalar @same -1]."\n";
429 next if $diff->Same();
430 map { $res .= "- $_\n" } $diff->Items(1);
431 map { $res .= "+ $_\n" } $diff->Items(2);