Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b0184cc3b95a42cf1be5a031c851e5d7cd94c376
[simgrid.git] / tools / tesh / tesh.pl
1 #! /usr/bin/env perl
2
3 # Copyright (c) 2012-2014. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8 eval 'exec perl -S $0 ${1+"$@"}'
9   if $running_under_some_shell;
10
11 =encoding UTF-8
12
13 =head1 NAME
14
15 tesh -- testing shell
16
17 =head1 SYNOPSIS
18
19 B<tesh> [I<options>] I<tesh_file>
20
21 =cut
22 my($timeout)=0;
23 my($time_to_wait)=0;
24 my $path = $0;
25 my $enable_coverage=0;
26 my $diff_tool=0;
27 my $diff_tool_tmp_fh=0;
28 my $diff_tool_tmp_filename=0;
29 my $sort_prefix = -1;
30 my $tesh_file;
31 my $tesh_name;
32 my $error=0;
33 my $exitcode=0;
34 my @bg_cmds;
35 my (%environ);
36 $SIG{'PIPE'} = 'IGNORE';
37 $path =~ s|[^/]*$||;
38 push @INC,$path;
39
40 use Getopt::Long qw(GetOptions);
41 use strict;
42 use Term::ANSIColor;
43 use Text::ParseWords;
44 use IPC::Open3;
45 use IO::File;
46 use English;
47
48 ## 
49 ## Portability bits for windows
50 ##
51
52 use constant RUNNING_ON_WINDOWS => ($OSNAME =~ /^(?:mswin|dos|os2)/oi);
53 use POSIX qw(:sys_wait_h WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG
54              :signal_h SIGINT SIGTERM SIGKILL SIGABRT SIGSEGV);
55 # These are not implemented on windows
56 BEGIN {
57     if (RUNNING_ON_WINDOWS) {
58         *WIFEXITED   = sub { not $_[0] & 127 };
59         *WEXITSTATUS = sub { $_[0] >> 8 };
60         *WIFSIGNALED = sub { ($_[0] & 127) && ($_[0] & 127 != 127) };
61         *WTERMSIG    = sub { $_[0] & 127 };
62     }
63 }
64
65
66 ##
67 ## Command line option handling
68 ##
69
70 if ($ARGV[0] eq "--internal-killer-process") {
71     # We fork+exec a waiter process in charge of killing the command after timeout
72     # If the command stops earlier, that's fine: the killer sends a signal to an already stopped process, fails, and quits. 
73     #    Nobody cares about the killer issues. 
74     #    The only problem could arise if another process is given the same PID than cmd. We bet it won't happen :)
75     my $time_to_wait = $ARGV[1];
76     my $pid = $ARGV[2];
77     sleep $time_to_wait;
78     kill('TERM', $pid);
79     sleep 1;
80     kill('KILL', $pid);
81     exit $time_to_wait;
82 }
83
84
85 sub var_subst {
86     my ($text, $name, $value) = @_;
87     if ($value) {
88         $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
89         $text =~ s/\$$name(\W|$)/$value$1/g;
90     }
91     else {
92         $text =~ s/\${$name:=([^}]*)}/$1/g;
93         $text =~ s/\${$name}//g;
94         $text =~ s/\$$name(\W|$)/$1/g;
95     }
96     return $text;
97 }
98
99 # option handling helper subs
100 sub cd_cmd {
101     my $directory=$_[1];
102     my $failure=1;
103     if (-e $directory && -d $directory) {
104         chdir("$directory");
105         print "[Tesh/INFO] change directory to $directory\n";
106         $failure=0;
107     } elsif (-e $directory) {
108         print "Cannot change directory to '$directory': it is not a directory\n";
109     } else {
110         print "Chdir to $directory failed: No such file or directory\n";
111     }
112     if($failure==1){
113         $error=1;
114         $exitcode=4;
115         print "Test suite `$tesh_file': NOK (system error)\n";
116         exit 4;
117     }
118 }
119
120 sub setenv_cmd {
121   my($var,$ctn);
122   if ($_[0] =~ /^(.*)=(.*)$/) {
123     ($var,$ctn)=($1,$2);
124   }elsif ($_[1] =~ /^(.*)=(.*)$/) {
125     ($var,$ctn)=($1,$2);
126   } else {
127       die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
128   }
129
130   print "[Tesh/INFO] setenv $var=$ctn\n";
131   $environ{$var} = $ctn;
132 }
133
134 # Main option parsing sub
135
136 sub get_options {
137   # remove the tesh file from the ARGV used
138   my @ARGV = @_;
139   $tesh_file = pop @ARGV;
140
141   # temporary arrays for GetOption
142   my @cfg;
143   my $log; # ignored
144
145
146   my %opt = (
147     "help"  => 0,
148     "debug"   => 0,
149     );
150
151   Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
152
153   GetOptions(
154     'help|h'   => \$opt{'help'},
155
156     'debug|d'  => \$opt{"debug"},
157
158     'difftool=s' => \$diff_tool,
159
160     'cd=s'     => \&cd_cmd,
161     'timeout=s'  => \$opt{'timeout'},
162     'setenv=s'   => \&setenv_cmd,
163     'cfg=s'    => \@cfg,
164     'log=s'    => \$log,
165     'enable-coverage+'  => \$enable_coverage,
166     );
167
168   if($enable_coverage){
169     print "Enable coverage\n";
170   }
171
172   if($diff_tool){
173     use File::Temp qw/ tempfile /;
174     ($diff_tool_tmp_fh, $diff_tool_tmp_filename) = tempfile();
175     print "New tesh: $diff_tool_tmp_filename\n";
176   }
177
178   unless($tesh_file=~/(.*)\.tesh/){
179     $tesh_file="(stdin)";
180     $tesh_name="(stdin)";
181     print "Test suite from stdin\n";
182   }else{
183     $tesh_name=$1;
184     print "Test suite `$tesh_name'\n";
185   }
186
187   foreach (@cfg) {
188     $opt{'cfg'} .= " --cfg=$_";
189   }
190   return %opt;
191 }
192
193 my %opts = get_options(@ARGV);
194
195 ##
196 ## File parsing
197 ##
198 my($return)=-1;
199 my($forked);
200 my($config)="";
201 my(@buffer_tesh)=();
202
203 ###########################################################################
204
205 sub exit_status {
206     my $status = shift;
207     if (WIFEXITED($status)) {
208         $exitcode=WEXITSTATUS($status)+40;
209         return "returned code ".WEXITSTATUS($status);
210     } elsif (WIFSIGNALED($status)) {
211         my $code;
212         if (WTERMSIG($status) == SIGINT){$code="SIGINT"; }
213         elsif  (WTERMSIG($status) == SIGTERM) {$code="SIGTERM"; }
214         elsif  (WTERMSIG($status) == SIGKILL) {$code= "SIGKILL"; }
215         elsif  (WTERMSIG($status) == SIGABRT) {$code="SIGABRT"; }
216         elsif  (WTERMSIG($status) == SIGSEGV) {$code="SIGSEGV" ;}
217         $exitcode=WTERMSIG($status)+4;
218         return "got signal $code";
219     }
220     return "Unparsable status. Is the process stopped?";
221 }
222
223 sub exec_cmd {
224   my %cmd = %{$_[0]};
225   if ($opts{'debug'}) {
226     print "IN BEGIN\n";
227     map {print "  $_"} @{$cmd{'in'}};
228     print "IN END\n";
229     print "OUT BEGIN\n";
230     map {print "  $_"} @{$cmd{'out'}};
231     print "OUT END\n";
232     print "CMD: $cmd{'cmd'}\n";
233   }
234
235   # cleanup the command line
236   if(RUNNING_ON_WINDOWS) {
237       var_subst($cmd{'cmd'}, "EXEEXT", ".exe");
238   } else {
239       var_subst($cmd{'cmd'}, "EXEEXT", "");
240   }
241
242   # substitute environ variables
243   foreach my $key (keys %environ) {
244       $cmd{'cmd'} = var_subst($cmd{'cmd'}, $key, $environ{$key});
245   }
246   # substitute remaining variables, if any
247   while ($cmd{'cmd'} =~ /\${(\w+)(?::[=-][^}]*)?}/) {
248       $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
249   }
250   while ($cmd{'cmd'} =~ /\$(\w+)/) {
251       $cmd{'cmd'} = var_subst($cmd{'cmd'}, $1, "");
252   }
253
254   # add cfg options
255   $cmd{'cmd'} .= " $opts{'cfg'}" if (defined($opts{'cfg'}) && length($opts{'cfg'}));
256
257   # final cleanup
258   $cmd{'cmd'} =~ s/^\s+//;
259   $cmd{'cmd'} =~ s/\s+$//;
260
261   print "[$tesh_name:$cmd{'line'}] $cmd{'cmd'}\n" ;
262
263   ###
264   # exec the command line
265   ###  $line =~ s/\r//g;
266
267   $cmd{'got'} = IO::File->new_tmpfile;
268   $cmd{'got'}->autoflush(1);
269   local *E = $cmd{'got'};
270   $cmd{'pid'} = open3(\*CHILD_IN,  ">&E",  ">&E",
271                       quotewords('\s+', 0, $cmd{'cmd'}));
272
273   # push all provided input to executing child
274   map { print CHILD_IN "$_\n"; }  @{$cmd{'in'}};
275   close CHILD_IN;
276
277   # if timeout specified, fork and kill executing child at the end of timeout
278   if (not $cmd{'background'} and (defined($cmd{'timeout'}) or defined($opts{'timeout'}))){
279     $time_to_wait= defined($cmd{'timeout'}) ? $cmd{'timeout'} : $opts{'timeout'};
280     $forked = fork();
281     $timeout=-1;
282     die "fork() failed: $!" unless defined $forked;
283     if ( $forked == 0 ) { # child
284         exec("$PROGRAM_NAME --internal-killer-process $time_to_wait $cmd{'pid'}");
285     }
286   }
287
288   # Cleanup the executing child, and kill the timeouter brother on need
289   $cmd{'return'} = 0 unless defined($cmd{'return'});
290   if ($cmd{'background'} != 1) {
291     waitpid ($cmd{'pid'}, 0);
292     $cmd{'gotret'} = exit_status($?);
293     parse_out(\%cmd);
294   } else {
295     # & commands, which will be handled at the end
296     push @bg_cmds, \%cmd;
297   }
298 }
299
300
301 sub parse_out {
302   my %cmd = %{$_[0]};
303   my $gotret=$cmd{'gotret'};
304
305   my $wantret;
306
307   if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
308     $wantret = "got signal $cmd{'expect'}";
309   }else{
310     $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
311   }
312
313   local *got = $cmd{'got'};
314   seek(got,0,0);
315   # pop all output from executing child
316   my @got;
317   while(defined(my $got=<got>)) {
318     $got =~ s/\r//g;
319     chomp $got;
320     print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
321
322     if (!($enable_coverage and $got=~ /^profiling:/)){
323       push @got, $got;
324     }
325   }
326
327   if ($cmd{'sort'}){
328     # Save the unsorted observed output to report it on error.
329     map { push @{$cmd{'unsorted got'}}, $_ } @got;
330
331     sub mysort{
332         substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
333     }
334     use sort 'stable';
335     if ($sort_prefix>0) {
336         @got = sort mysort @got;
337     } else {
338         @got = sort @got;
339     }       
340     while (@got and $got[0] eq "") {
341       shift @got;
342     }
343
344     # Sort the expected output to make it easier to write for humans
345     if(defined($cmd{'out'})){
346       if ($sort_prefix>0) {
347           @{$cmd{'out'}} = sort mysort @{$cmd{'out'}};
348       } else {
349           @{$cmd{'out'}} = sort @{$cmd{'out'}};
350       }
351       while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
352         shift @{$cmd{'out'}};
353       }
354     }
355   }
356
357   # Did we timeout ? If yes, handle it. If not, kill the forked process.
358
359   if($timeout==-1 and ($gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL")){
360     $gotret="return code 0";
361     $timeout=1;
362     $gotret= "timeout after $time_to_wait sec";
363     $error=1;
364     $exitcode=3;
365     print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
366   }else{
367     $timeout=0;
368   }
369   if($gotret ne $wantret) {
370     $error=1;
371     my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
372     if ($timeout!=1) {
373         $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
374     }
375     map {$msg .=  "|| $_\n"} @got;
376     if(!@got) {
377         if($timeout==1){
378         print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
379         }else{
380         $msg .= "||\n";
381         }
382     }
383     $timeout = 0;
384     print STDERR "$msg";
385   }
386
387
388   ###
389   # Check the result of execution
390   ###
391   my $diff;
392   if (defined($cmd{'output display'})){
393     print "[Tesh/INFO] Here is the (ignored) command output:\n";
394     map { print "||$_\n" } @got;
395   }
396   elsif (!defined($cmd{'output ignore'})){
397     $diff = build_diff(\@{$cmd{'out'}}, \@got);
398   }else{
399     print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
400   }
401   if (length $diff) {
402     print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
403     map { print "$_\n" } split(/\n/,$diff);
404     if ($cmd{'sort'}) {
405         print "WARNING: Both the observed output and expected output were sorted as requested.\n";
406         print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
407           if ($sort_prefix>0);
408         print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
409         # print "----8<---------------  Begin of unprocessed observed output (as it should appear in file):\n";
410         # map {print "> $_\n"} @{$cmd{'unsorted got'}};
411         # print "--------------->8----  End of the unprocessed observed output.\n";
412     }
413
414     print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
415     $error=1;
416     $exitcode=2;
417   }
418 }
419
420 sub mkfile_cmd {
421   my %cmd = %{$_[0]};
422   my $file = $cmd{'arg'};
423   print "[Tesh/INFO] mkfile $file\n";
424
425   unlink($file);
426   open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
427   print FILE join("\n", @{$cmd{'in'}});
428   print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
429   close(FILE);
430 }
431
432 # parse tesh file
433 #my $teshfile=$tesh_file;
434 #$teshfile=~ s{\.[^.]+$}{};
435
436 unless($tesh_file eq "(stdin)"){
437   open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
438 }
439
440 my %cmd; # everything about the next command to run
441 my $line_num=0;
442 my $finished =0;
443 LINE: while (not $finished and not $error) {
444   my $line;
445
446
447   if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
448     $finished=1;
449     next LINE;
450   }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
451     $finished=1;
452     next LINE;
453   }
454
455   $line_num++;
456   chomp $line;
457   $line =~ s/\r//g;
458   print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
459   my $next;
460   # deal with line continuations
461   while ($line =~ /^(.*?)\\$/) {
462     $next=<TESH_FILE>;
463     die "[TESH/CRITICAL] Continued line at end of file\n"
464       unless defined($next);
465     $line_num++;
466     chomp $next;
467     print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
468     $line = $1.$next;
469   }
470
471   # Push delayed commands on empty lines
472   unless ($line =~ m/^(.)(.*)$/) {
473     if (defined($cmd{'cmd'}))  {
474       exec_cmd(\%cmd);
475       %cmd = ();
476     }
477     print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
478     next LINE;
479   }
480
481   my ($cmd,$arg) = ($1,$2);
482   print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
483   $arg =~ s/^ //g;
484   $arg =~ s/\r//g;
485   $arg =~ s/\\\\/\\/g;
486   # handle the commands
487   if ($cmd =~ /^#/) {    #comment
488   } elsif ($cmd eq '>'){    #expected result line
489     print "[TESH/debug] push expected result\n" if $opts{'debug'};
490     push @{$cmd{'out'}}, $arg;
491
492   } elsif ($cmd eq '<') {    # provided input
493     print "[TESH/debug] push provided input\n" if $opts{'debug'};
494     push @{$cmd{'in'}}, $arg;
495
496   } elsif ($cmd eq 'p') {    # comment
497     print "[$tesh_name:$line_num] $arg\n";
498
499   } elsif ($cmd eq '$') {  # Command
500     # if we have something buffered, run it now
501     if (defined($cmd{'cmd'})) {
502       exec_cmd(\%cmd);
503       %cmd = ();
504     }
505     if ($arg =~ /^\s*mkfile /){      # "mkfile" command line
506       die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
507
508       $cmd{'arg'} = $arg;
509       $cmd{'arg'} =~ s/\s*mkfile //;
510       mkfile_cmd(\%cmd);
511       %cmd = ();
512
513     } elsif ($arg =~ /^\s*cd /) {
514       die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
515       die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
516
517       $arg =~ s/^ *cd //;
518       cd_cmd("",$arg);
519       %cmd = ();
520
521     } else { # regular command
522       $cmd{'cmd'} = $arg;
523       $cmd{'file'} = $tesh_file;
524       $cmd{'line'} = $line_num;
525     }
526   }
527   elsif($cmd eq '&'){      # parallel command line
528
529     if (defined($cmd{'cmd'})) {
530       exec_cmd(\%cmd);
531       %cmd = ();
532     }
533     $cmd{'background'} = 1;
534     $cmd{'cmd'} = $arg;
535     $cmd{'file'} = $tesh_file;
536     $cmd{'line'} = $line_num;
537   }
538   elsif($line =~ /^!\s*output sort/){    #output sort
539     if (defined($cmd{'cmd'})) {
540       exec_cmd(\%cmd);
541       %cmd = ();
542     }
543     $cmd{'sort'} = 1;
544     if ($line =~ /^!\s*output sort\s+(\d+)/) {
545         $sort_prefix = $1;
546     }
547   }
548   elsif($line =~ /^!\s*output ignore/){    #output ignore
549     if (defined($cmd{'cmd'})) {
550       exec_cmd(\%cmd);
551       %cmd = ();
552     }
553     $cmd{'output ignore'} = 1;
554   }
555   elsif($line =~ /^!\s*output display/){    #output display
556     if (defined($cmd{'cmd'})) {
557       exec_cmd(\%cmd);
558       %cmd = ();
559     }
560     $cmd{'output display'} = 1;
561   }
562   elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
563     if (defined($cmd{'cmd'})) {
564       exec_cmd(\%cmd);
565       %cmd = ();
566     }
567 print "hey\n";
568     $cmd{'expect'} = "$1";
569   }
570   elsif($line =~ /^!\s*expect return/){    #expect return
571     if (defined($cmd{'cmd'})) {
572       exec_cmd(\%cmd);
573       %cmd = ();
574     }
575     $line =~ s/^! expect return //g;
576     $line =~ s/\r//g;
577     $cmd{'return'} = $line;
578   }
579   elsif($line =~ /^!\s*setenv/){    #setenv
580     if (defined($cmd{'cmd'})) {
581       exec_cmd(\%cmd);
582       %cmd = ();
583     }
584     $line =~ s/^! setenv //g;
585     $line =~ s/\r//g;
586     setenv_cmd($line);
587   }
588   elsif($line =~ /^!\s*include/){    #include
589     if (defined($cmd{'cmd'})) {
590       exec_cmd(\%cmd);
591       %cmd = ();
592     }
593     print color("red"), "[Tesh/CRITICAL] need include";
594     print color("reset"), "\n";
595     die;
596   }
597   elsif($line =~ /^!\s*timeout/){    #timeout
598     if (defined($cmd{'cmd'})) {
599       exec_cmd(\%cmd);
600       %cmd = ();
601     }
602     $line =~ s/^! timeout //;
603     $line =~ s/\r//g;
604     $cmd{'timeout'} = $line;
605   } else {
606     die "[TESH/CRITICAL] parse error: $line\n";
607   }
608   if($forked){
609    kill('KILL', $forked);
610    $timeout=0;
611   }
612
613 }
614
615
616
617 # Deal with last command
618 if (defined($cmd{'cmd'})) {
619   exec_cmd(\%cmd);
620   %cmd = ();
621 }
622
623
624 if($forked){
625    kill('KILL', $forked);
626    $timeout=0;
627 }
628
629 foreach(@bg_cmds){
630   my %test=%{$_};
631   waitpid ($test{'pid'}, 0);
632   $test{'gotret'} = exit_status($?);
633   parse_out(\%test);
634 }
635
636 @bg_cmds=();
637
638 if ($diff_tool) {
639   close $diff_tool_tmp_fh;
640   system("$diff_tool $diff_tool_tmp_filename $tesh_file");
641   unlink $diff_tool_tmp_filename;
642 }
643
644 if($error !=0){
645     exit $exitcode;
646 }elsif($tesh_file eq "(stdin)"){
647     print "Test suite from stdin OK\n";
648 }else{
649     print "Test suite `$tesh_name' OK\n";
650 }
651
652 #my (@a,@b);
653 #push @a,"bl1";   push @b,"bl1";
654 #push @a,"bl2";   push @b,"bl2";
655 #push @a,"bl3";   push @b,"bl3";
656 #push @a,"bl4";   push @b,"bl4";
657 #push @a,"bl5";   push @b,"bl5";
658 #push @a,"bl6";   push @b,"bl6";
659 #push @a,"bl7";   push @b,"bl7";
660 ##push @a,"Perl";  push @b,"ruby";
661 #push @a,"END1";   push @b,"END1";
662 #push @a,"END2";   push @b,"END2";
663 #push @a,"END3";   push @b,"END3";
664 #push @a,"END4";   push @b,"END4";
665 #push @a,"END5";   push @b,"END5";
666 #push @a,"END6";   push @b,"END6";
667 #push @a,"END7";   push @b,"END7";
668 #print "Identical:\n". build_diff(\@a,\@b);
669
670 #@a = (); @b =();
671 #push @a,"AZE"; push @b,"EZA";
672 #print "Different:\n".build_diff(\@a,\@b);
673
674 use lib "@CMAKE_BINARY_DIR@/bin" ;
675
676 use Diff qw(diff); # postpone a bit to have time to change INC
677
678 sub build_diff {
679   my $res;
680   my $diff = Diff->new(@_);
681
682   $diff->Base( 1 );   # Return line numbers, not indices
683   my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
684   return ""   if ($chunk_count == 1 && $diff->Same());
685   $diff->Reset();
686   while(  $diff->Next()  ) {
687     my @same = $diff->Same();
688     if ($diff->Same() ) {
689       if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
690         $res .= '  '.$same[0]."\n" ;
691         $res .= '  '.$same[1]."\n" if (scalar @same>1);
692       }
693       $res .= "...\n"  if (scalar @same>2);
694 #    $res .= $diff->Next(0)."/$chunk_count\n";
695       if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
696         $res .= '  '.$same[scalar @same -2]."\n" if (scalar @same>1);
697         $res .= '  '.$same[scalar @same -1]."\n";
698       }
699     }
700     next if  $diff->Same();
701     map { $res .= "- $_\n" } $diff->Items(1);
702     map { $res .= "+ $_\n" } $diff->Items(2);
703   }
704   return $res;
705 }
706
707