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 print "OS: ".$OS."\n";
37 # make sure we received a tesh file
38 scalar @ARGV > 0 || die "Usage:\n tesh [*options*] *tesh_file*\n";
40 #Add current directory to path
41 $ENV{PATH} = "$ENV{PATH}:.";
45 ## Command line option handling
48 # option handling helper subs
51 if (-e $directory && -d $directory) {
53 print "[Tesh/INFO] change directory to $directory\n";
54 } elsif (-e $directory) {
55 die "[Tesh/CRITICAL] Cannot change directory to '$directory': it is not a directory\n";
57 die "[Tesh/CRITICAL] Cannot change directory to '$directory': no such directory\n";
62 if ($_[1] =~ /^(.*)=(.*)$/) {
63 my($var,$ctn)=($1,$2);
66 print "[Tesh/INFO] setenv $var=$ctn\n";
76 print "[Tesh/INFO] setenv $var=$ctn\n";
80 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
84 # Main option parsing sub
87 # remove the tesh file from the ARGV used
89 $tesh_file = pop @ARGV;
91 # temporary arrays for GetOption
102 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
105 'help|h' => \$opt{'help'},
107 'verbose|v' => \@verbose,
108 'debug|d' => \$opt{"debug"},
111 'setenv=s' => \&setenv_cmd,
116 $opt{'verbose'} = scalar @verbose;
118 $opt{'cfg'} .= " --cfg=$_";
123 my %opts = get_options(@ARGV);
134 my($no_output_ignore)=1;
150 if (POSIX::WIFEXITED($status)) {
151 return "returned code ".POSIX::WEXITSTATUS($status);
152 } elsif (POSIX::WIFSIGNALED($status)) {
153 return "got signal ".$SIG{POSIX::WTERMSIG($status)};
155 return "Unparsable status. Is the process stopped?";
158 if ($@) { # no POSIX available?
159 warn "POSIX not usable to parse the return value of forked child: $@\n";
161 return "returned code 0";
167 if ($opts{'debug'}) {
169 map {print " $_"} @{$cmd{'in'}};
172 map {print " $_"} @{$cmd{'out'}};
174 print "CMD: $cmd{'cmd'}\n";
177 # cleanup the command line
179 $cmd{'cmd'} =~ s/\${EXEEXT:=}/.exe/g;
180 $cmd{'cmd'} =~ s/\${EXEEXT}/.exe/g;
181 $cmd{'cmd'} =~ s/\$EXEEXT/.exe/g;
184 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
186 $cmd{'cmd'} =~ s/\${bindir:=}/$bindir/g;
187 $cmd{'cmd'} =~ s/\${srcdir:=}/$srcdir/g;
188 $cmd{'cmd'} =~ s/\${bindir:=.}/$bindir/g;
189 $cmd{'cmd'} =~ s/\${srcdir:=.}/$srcdir/g;
190 $cmd{'cmd'} =~ s/\${bindir}/$bindir/g;
191 $cmd{'cmd'} =~ s/\${srcdir}/$srcdir/g;
192 $cmd{'cmd'} =~ s|^\./||g;
193 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
194 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
195 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
197 print "[$cmd{'file'}:$cmd{'line'}] $cmd{'cmd'}\n";
200 # exec the command line
202 $pid = open3(\*IN, \*OUT, \*OUT, $cmd{'cmd'} );
204 # if timeout specified, fork and kill executing child at the end of timeout
207 die "fork() failed: $!" unless defined $forked;
208 if ( $forked == 0 ) { # child
215 # push all provided input to executing child
216 map { print IN "$_\n" } $cmd{'in'};
219 # pop all output from executing child
221 while(defined(my $got=<OUT>)) {
229 # Cleanup the executing child, and kill the timeouter brother on need
230 $cmd{'return'} = 0 unless defined($cmd{'return'});
231 my $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
233 my $gotret = exit_status($?);
234 if($gotret ne $wantret) {
235 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n".
236 "Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
237 map {$msg .= "|| $_\n"} @got;
241 if($timeout){kill(9, $forked);$timeout=0;}
245 # Check the result of execution
247 my $diff = build_diff(\@{$cmd{'out'}}, \@got);
249 print color("red")."[TESH/CRITICAL$$] Output mismatch\n";
250 map { print "[TESH/CRITICAL] $_\n" } split(/\n/,$diff);
251 print color("reset");
258 my $file = $cmd{'arg'};
259 print "[Tesh/INFO] mkfile $file\n";
261 die "[TESH/CRITICAL] no input provided to mkfile\n" unless defined($cmd{'in'}) && scalar @{$cmd{'in'}};
263 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
264 print FILE join("\n", @{$cmd{'in'}});
265 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
270 print "Test suite $tesh_file\n";
271 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
274 my %cmd; # everything about the next command to run
276 LINE: while (defined(my $line=<TESH_FILE>)) {
279 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
281 # deal with line continuations
282 while ($line =~ /^(.*?)\\$/) {
283 my $next=<TESH_FILE>;
284 die "[TESH/CRITICAL] Continued line at end of file\n"
285 unless defined($next);
287 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
291 # Push delayed commands on empty lines
292 unless ($line =~ m/^(..)(.*)$/) {
293 if (defined($cmd{'cmd'})) {
300 my ($cmd,$arg) = ($1,$2);
303 # handle the commands
304 if ($cmd =~ /^#/) { #comment
305 } elsif ($cmd eq '> '){ #expected result line
306 print "[TESH/debug] push expected result\n" if $opts{'debug'};
307 push @{$cmd{'out'}}, $arg;
309 } elsif ($cmd eq '< ') { # provided input
310 print "[TESH/debug] push provided input\n" if $opts{'debug'};
311 push @{$cmd{'in'}}, $arg;
313 } elsif ($cmd eq 'p ') { # comment
314 print "[Tesh/INFO] $arg\n";
316 } elsif ($cmd eq '$ ') { # Command
317 # if we have something buffered, run it now
318 if (defined($cmd{'cmd'})) {
322 if ($arg =~ /^ *mkfile /){ # "mkfile" command line
323 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
326 $cmd{'arg'} =~ s/ *mkfile //;
330 } elsif ($arg =~ /^ *cd /) {
331 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
332 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
338 } else { # regular command
340 $cmd{'file'} = $tesh_file;
341 $cmd{'line'} = $line_num;
344 elsif($cmd eq '& '){ # parallel command line
345 $cmd{'background'} = 1;
348 elsif($line =~ /^! output sort/){ #output sort
351 elsif($line =~ /^! output ignore/){ #output ignore
352 $cmd{'output ignore'} = 1;
354 elsif($line =~ /^! expect signal SIGABRT$/) {#expect signal SIGABRT
355 $cmd{'expect'} = "SIGABRT";
357 elsif($line =~ /^! expect return/){ #expect return
358 $line =~ s/^! expect return //g;
360 $cmd{'return'} = $line;
362 elsif($line =~ /^! setenv/){ #setenv
363 $line =~ s/^! setenv //g;
367 elsif($line =~ /^! include/){ #output sort
368 print color("red"), "[Tesh/CRITICAL] need include";
369 print color("reset"), "\n";
372 elsif($line =~ /^! timeout/){ #timeout
373 $line =~ s/^! timeout //;
375 $cmd{'timeout'} = $line;
377 die "[TESH/CRITICAL] parse error: $line\n";
381 # Deal with last command
382 if (defined($cmd{'cmd'})) {
388 #push @a,"bl1"; push @b,"bl1";
389 #push @a,"bl2"; push @b,"bl2";
390 #push @a,"bl3"; push @b,"bl3";
391 #push @a,"bl4"; push @b,"bl4";
392 #push @a,"bl5"; push @b,"bl5";
393 #push @a,"bl6"; push @b,"bl6";
394 #push @a,"bl7"; push @b,"bl7";
395 ##push @a,"Perl"; push @b,"ruby";
396 #push @a,"END1"; push @b,"END1";
397 #push @a,"END2"; push @b,"END2";
398 #push @a,"END3"; push @b,"END3";
399 #push @a,"END4"; push @b,"END4";
400 #push @a,"END5"; push @b,"END5";
401 #push @a,"END6"; push @b,"END6";
402 #push @a,"END7"; push @b,"END7";
403 #print "Identical:\n". build_diff(\@a,\@b);
406 #push @a,"AZE"; push @b,"EZA";
407 #print "Different:\n".build_diff(\@a,\@b);
409 use Diff qw(diff); # postpone a bit to have time to change INC
413 my $diff = Diff->new(@_);
415 $diff->Base( 1 ); # Return line numbers, not indices
416 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
417 return "" if ($chunk_count == 1 && $diff->Same());
419 while( $diff->Next() ) {
420 my @same = $diff->Same();
421 if ($diff->Same() ) {
422 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
423 $res .= ' '.$same[0]."\n" ;
424 $res .= ' '.$same[1]."\n" if (scalar @same>1);
426 $res .= "...\n" if (scalar @same>2);
427 # $res .= $diff->Next(0)."/$chunk_count\n";
428 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
429 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
430 $res .= ' '.$same[scalar @same -1]."\n";
433 next if $diff->Same();
434 map { $res .= "- $_\n" } $diff->Items(1);
435 map { $res .= "+ $_\n" } $diff->Items(2);