2 eval 'exec perl -S $0 ${1+"$@"}'
3 if $running_under_some_shell;
13 B<tesh> [I<options>] I<tesh_file>
21 use Getopt::Long qw(GetOptions);
27 # make sure we received a tesh file
28 scalar @ARGV > 0 || die "Usage:\n tesh [*options*] *tesh_file*\n";
30 #Add current directory to path
31 $ENV{PATH} = "$ENV{PATH}:.";
35 ## Command line option handling
38 # option handling helper subs
41 if (-e $directory && -d $directory) {
43 print "[Tesh/INFO] change directory to $directory\n";
44 } elsif (-e $directory) {
45 die "[Tesh/CRITICAL] Cannot change directory to '$directory': it is not a directory\n";
47 die "[Tesh/CRITICAL] Cannot change directory to '$directory': no such directory\n";
52 if ($_[1] =~ /^(.*)=(.*)$/) {
53 my($var,$ctn)=($1,$2);
55 print "[Tesh/INFO] setenv $var=$ctn\n";
57 die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
61 # Main option parsing sub
64 # remove the tesh file from the ARGV used
66 $tesh_file = pop @ARGV;
68 # temporary arrays for GetOption
79 Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
82 'help|h' => \$opt{'help'},
84 'verbose|v' => \@verbose,
85 'debug|d' => \$opt{"debug"},
88 'setenv=s' => \&setenv_cmd,
93 $opt{'verbose'} = scalar @verbose;
95 $opt{'cfg'} .= " --cfg=$_";
100 my %opts = get_options(@ARGV);
111 my($no_output_ignore)=1;
127 if (POSIX::WIFEXITED($status)) {
128 return "returned code ".POSIX::WEXITSTATUS($status);
129 } elsif (POSIX::WIFSIGNALED($status)) {
130 return "got signal ".$SIG{POSIX::WTERMSIG($status)};
132 return "Unparsable status. Is the process stopped?";
135 if ($@) { # no POSIX available?
136 warn "POSIX not usable to parse the return value of forked child: $@\n";
138 return "returned code 0";
144 if ($opts{'debug'}) {
146 map {print " $_"} @{$cmd{'in'}};
149 map {print " $_"} @{$cmd{'out'}};
151 print "CMD: $cmd{'cmd'}\n";
154 # cleanup the command line
155 $cmd{'cmd'} =~ s/\${EXEEXT:=}//g;
156 $cmd{'cmd'} =~ s|^\./||g;
157 # $cmd{'cmd'} =~ s|tesh|tesh.pl|g;
158 $cmd{'cmd'} =~ s/\(%i:%P@%h\)/\\\(%i:%P@%h\\\)/g;
159 $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
161 print "[$cmd{'file'}:$cmd{'line'}] $cmd{'cmd'}\n";
164 # exec the command line
166 $pid = open3(\*IN, \*OUT, \*OUT, $cmd{'cmd'} );
168 # if timeout specified, fork and kill executing child at the end of timeout
171 die "fork() failed: $!" unless defined $forked;
172 if ( $forked == 0 ) { # child
179 # push all provided input to executing child
180 map { print IN "$_\n" } $cmd{'in'};
183 # pop all output from executing child
185 while(defined(my $got=<OUT>)) {
193 # Cleanup the executing child, and kill the timeouter brother on need
194 $cmd{'return'} = 0 unless defined($cmd{'return'});
195 my $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
197 my $gotret = exit_status($?);
198 if($gotret ne $wantret) {
199 my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n".
200 "Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
201 map {$msg .= "|| $_\n"} @got;
205 if($timeout){kill(9, $forked);$timeout=0;}
209 # Check the result of execution
211 my $diff = build_diff(\@{$cmd{'out'}}, \@got);
213 print color("red")."[TESH/CRITICAL$$] Output mismatch\n";
214 map { print "[TESH/CRITICAL] $_\n" } split(/\n/,$diff);
215 print color("reset");
222 my $file = $cmd{'arg'};
223 print "[Tesh/INFO] mkfile $file\n";
225 die "[TESH/CRITICAL] no input provided to mkfile\n" unless defined($cmd{'in'}) && scalar @{$cmd{'in'}};
227 open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
228 print FILE join("\n", @{$cmd{'in'}});
229 print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
234 print "Test suite $tesh_file\n";
235 open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
238 my %cmd; # everything about the next command to run
240 LINE: while (defined(my $line=<TESH_FILE>)) {
243 print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
245 # deal with line continuations
246 while ($line =~ /^(.*?)\\$/) {
247 my $next=<TESH_FILE>;
248 die "[TESH/CRITICAL] Continued line at end of file\n"
249 unless defined($next);
251 print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
255 # Push delayed commands on empty lines
256 unless ($line =~ m/^(..)(.*)$/) {
257 if (defined($cmd{'cmd'})) {
264 my ($cmd,$arg) = ($1,$2);
267 # handle the commands
268 if ($cmd =~ /^#/) { #comment
269 } elsif ($cmd eq '> '){ #expected result line
270 print "[TESH/debug] push expected result\n" if $opts{'debug'};
271 push @{$cmd{'out'}}, $arg;
273 } elsif ($cmd eq '< ') { # provided input
274 print "[TESH/debug] push provided input\n" if $opts{'debug'};
275 push @{$cmd{'in'}}, $arg;
277 } elsif ($cmd eq 'p ') { # comment
278 print "[Tesh/INFO] $arg\n";
280 } elsif ($cmd eq '$ ') { # Command
281 # if we have something buffered, run it now
282 if (defined($cmd{'cmd'})) {
286 if ($arg =~ /^ *mkfile /){ # "mkfile" command line
287 die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
290 $cmd{'arg'} =~ s/ *mkfile //;
294 } elsif ($arg =~ /^ *cd /) {
295 die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
296 die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
302 } else { # regular command
304 $cmd{'file'} = $tesh_file;
305 $cmd{'line'} = $line_num;
308 elsif($cmd eq '& '){ # parallel command line
309 $cmd{'background'} = 1;
312 elsif($line =~ /^! output sort/){ #output sort
315 elsif($line =~ /^! output ignore/){ #output ignore
316 $cmd{'output ignore'} = 1;
318 elsif($line =~ /^! expect signal SIGABRT$/) {#expect signal SIGABRT
319 $cmd{'expect'} = "SIGABRT";
321 elsif($line =~ /^! expect return/){ #expect return
322 $line =~ s/^! expect return //g;
324 $cmd{'return'} = $line;
326 elsif($line =~ /^! setenv/){ #setenv
327 $line =~ s/^! setenv //g;
331 elsif($line =~ /^! include/){ #output sort
332 print color("red"), "[Tesh/CRITICAL] need include";
333 print color("reset"), "\n";
336 elsif($line =~ /^! timeout/){ #timeout
337 $line =~ s/^! timeout //;
339 $cmd{'timeout'} = $line;
341 die "[TESH/CRITICAL] parse error: $line\n";
345 # Deal with last command
346 if (defined($cmd{'cmd'})) {
352 #push @a,"bl1"; push @b,"bl1";
353 #push @a,"bl2"; push @b,"bl2";
354 #push @a,"bl3"; push @b,"bl3";
355 #push @a,"bl4"; push @b,"bl4";
356 #push @a,"bl5"; push @b,"bl5";
357 #push @a,"bl6"; push @b,"bl6";
358 #push @a,"bl7"; push @b,"bl7";
359 ##push @a,"Perl"; push @b,"ruby";
360 #push @a,"END1"; push @b,"END1";
361 #push @a,"END2"; push @b,"END2";
362 #push @a,"END3"; push @b,"END3";
363 #push @a,"END4"; push @b,"END4";
364 #push @a,"END5"; push @b,"END5";
365 #push @a,"END6"; push @b,"END6";
366 #push @a,"END7"; push @b,"END7";
367 #print "Identical:\n". build_diff(\@a,\@b);
370 #push @a,"AZE"; push @b,"EZA";
371 #print "Different:\n".build_diff(\@a,\@b);
373 use Diff qw(diff); # postpone a bit to have time to change INC
377 my $diff = Diff->new(@_);
379 $diff->Base( 1 ); # Return line numbers, not indices
380 my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
381 return "" if ($chunk_count == 1 && $diff->Same());
383 while( $diff->Next() ) {
384 my @same = $diff->Same();
385 if ($diff->Same() ) {
386 if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
387 $res .= ' '.$same[0]."\n" ;
388 $res .= ' '.$same[1]."\n" if (scalar @same>1);
390 $res .= "...\n" if (scalar @same>2);
391 # $res .= $diff->Next(0)."/$chunk_count\n";
392 if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
393 $res .= ' '.$same[scalar @same -2]."\n" if (scalar @same>1);
394 $res .= ' '.$same[scalar @same -1]."\n";
397 next if $diff->Same();
398 map { $res .= "- $_\n" } $diff->Items(1);
399 map { $res .= "+ $_\n" } $diff->Items(2);