Logo AND Algorithmique Numérique Distribuée

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