Logo AND Algorithmique Numérique Distribuée

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