Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d67fe47496a5e5ff5e67fe9f878340520cc70e86
[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 (not $cmd{'background'} and (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   }
313 }
314
315
316 sub parse_out {
317   my %cmd = %{$_[0]};
318   my $gotret=$cmd{'gotret'};
319
320   my $wantret;
321
322   if(defined($cmd{'expect'}) and ($cmd{'expect'} ne "")){
323     $wantret = "got signal $cmd{'expect'}";
324   }else{
325     $wantret = "returned code ".(defined($cmd{'return'})? $cmd{'return'} : 0);
326   }
327
328   local *got = $cmd{'got'};
329   seek(got,0,0);
330   # pop all output from executing child
331   my @got;
332   while(defined(my $got=<got>)) {
333     $got =~ s/\r//g;
334     chomp $got;
335     print $diff_tool_tmp_fh "> $got\n" if ($diff_tool);
336
337     if (!($enable_coverage and $got=~ /^profiling:/)){
338       push @got, $got;
339     }
340   }
341
342   if ($cmd{'sort'}){
343     # Save the unsorted observed output to report it on error.
344     map { push @{$cmd{'unsorted got'}}, $_ } @got;
345
346     sub mysort{
347         substr($a, 0, $sort_prefix) cmp substr($b, 0, $sort_prefix)
348     }
349     use sort 'stable';
350     if ($sort_prefix>0) {
351         @got = sort mysort @got;
352     } else {
353         @got = sort @got;
354     }       
355     while (@got and $got[0] eq "") {
356       shift @got;
357     }
358
359     # Sort the expected output to make it easier to write for humans
360     if(defined($cmd{'out'})){
361       if ($sort_prefix>0) {
362           @{$cmd{'out'}} = sort mysort @{$cmd{'out'}};
363       } else {
364           @{$cmd{'out'}} = sort @{$cmd{'out'}};
365       }
366       while (@{$cmd{'out'}} and ${$cmd{'out'}}[0] eq "") {
367         shift @{$cmd{'out'}};
368       }
369     }
370   }
371
372   # Did we timeout ? If yes, handle it. If not, kill the forked process.
373
374   if($timeout==-1 and ($gotret eq "got signal SIGTERM" or $gotret eq "got signal SIGKILL")){
375     $gotret="return code 0";
376     $timeout=1;
377     $gotret= "timeout after $time_to_wait sec";
378     $error=1;
379     $exitcode=3;
380     print STDERR "<$cmd{'file'}:$cmd{'line'}> timeouted. Kill the process.\n";
381   }else{
382     $timeout=0;
383   }
384   if($gotret ne $wantret) {
385     $error=1;
386     my $msg = "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> $gotret)\n";
387     if ($timeout!=1) {
388         $msg=$msg."Output of <$cmd{'file'}:$cmd{'line'}> so far:\n";
389     }
390     map {$msg .=  "|| $_\n"} @got;
391     if(!@got) {
392         if($timeout==1){
393         print STDERR "<$cmd{'file'}:$cmd{'line'}> No output before timeout\n";
394         }else{
395         $msg .= "||\n";
396         }
397     }
398     $timeout = 0;
399     print STDERR "$msg";
400   }
401
402
403   ###
404   # Check the result of execution
405   ###
406   my $diff;
407   if (defined($cmd{'output display'})){
408     print "[Tesh/INFO] Here is the (ignored) command output:\n";
409     map { print "||$_\n" } @got;
410   }
411   elsif (!defined($cmd{'output ignore'})){
412     $diff = build_diff(\@{$cmd{'out'}}, \@got);
413   }else{
414     print "(ignoring the output of <$cmd{'file'}:$cmd{'line'}> as requested)\n"
415   }
416   if (length $diff) {
417     print "Output of <$cmd{'file'}:$cmd{'line'}> mismatch".($cmd{'sort'}?" (even after sorting)":"").":\n";
418     map { print "$_\n" } split(/\n/,$diff);
419     if ($cmd{'sort'}) {
420         print "WARNING: Both the observed output and expected output were sorted as requested.\n";
421         print "WARNING: Output were only sorted using the $sort_prefix first chars.\n"
422           if ($sort_prefix>0);
423         print "WARNING: Use <! output sort 19> to sort by simulated date and process ID only.\n";
424         # print "----8<---------------  Begin of unprocessed observed output (as it should appear in file):\n";
425         # map {print "> $_\n"} @{$cmd{'unsorted got'}};
426         # print "--------------->8----  End of the unprocessed observed output.\n";
427     }
428
429     print "Test suite `$cmd{'file'}': NOK (<$cmd{'file'}:$cmd{'line'}> output mismatch)\n";
430     $error=1;
431     $exitcode=2;
432   }
433 }
434
435 sub mkfile_cmd {
436   my %cmd = %{$_[0]};
437   my $file = $cmd{'arg'};
438   print "[Tesh/INFO] mkfile $file\n";
439
440   unlink($file);
441   open(FILE,">$file") or die "[Tesh/CRITICAL] Unable to create file $file: $!\n";
442   print FILE join("\n", @{$cmd{'in'}});
443   print FILE "\n" if (scalar @{$cmd{'in'}} > 0);
444   close(FILE);
445 }
446
447 # parse tesh file
448 #my $teshfile=$tesh_file;
449 #$teshfile=~ s{\.[^.]+$}{};
450
451 unless($tesh_file eq "(stdin)"){
452   open TESH_FILE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file $!\n";
453 }
454
455 my %cmd; # everything about the next command to run
456 my $line_num=0;
457 my $finished =0;
458 LINE: while (not $finished and not $error) {
459   my $line;
460
461
462   if ($tesh_file ne "(stdin)" and !defined($line=<TESH_FILE>)){
463     $finished=1;
464     next LINE;
465   }elsif ($tesh_file eq "(stdin)" and !defined($line=<>)){
466     $finished=1;
467     next LINE;
468   }
469
470   $line_num++;
471   chomp $line;
472   $line =~ s/\r//g;
473   print "[TESH/debug] $line_num: $line\n" if $opts{'debug'};
474   my $next;
475   # deal with line continuations
476   while ($line =~ /^(.*?)\\$/) {
477     $next=<TESH_FILE>;
478     die "[TESH/CRITICAL] Continued line at end of file\n"
479       unless defined($next);
480     $line_num++;
481     chomp $next;
482     print "[TESH/debug] $line_num: $next\n" if $opts{'debug'};
483     $line = $1.$next;
484   }
485
486   # Push delayed commands on empty lines
487   unless ($line =~ m/^(.)(.*)$/) {
488     if (defined($cmd{'cmd'}))  {
489       exec_cmd(\%cmd);
490       %cmd = ();
491     }
492     print $diff_tool_tmp_fh "$line\n" if ($diff_tool);
493     next LINE;
494   }
495
496   my ($cmd,$arg) = ($1,$2);
497   print $diff_tool_tmp_fh "$line\n" if ($diff_tool and $cmd ne '>');
498   $arg =~ s/^ //g;
499   $arg =~ s/\r//g;
500   $arg =~ s/\\\\/\\/g;
501   # handle the commands
502   if ($cmd =~ /^#/) {    #comment
503   } elsif ($cmd eq '>'){    #expected result line
504     print "[TESH/debug] push expected result\n" if $opts{'debug'};
505     push @{$cmd{'out'}}, $arg;
506
507   } elsif ($cmd eq '<') {    # provided input
508     print "[TESH/debug] push provided input\n" if $opts{'debug'};
509     push @{$cmd{'in'}}, $arg;
510
511   } elsif ($cmd eq 'p') {    # comment
512     print "[$tesh_name:$line_num] $arg\n";
513
514   } elsif ($cmd eq '$') {  # Command
515     # if we have something buffered, run it now
516     if (defined($cmd{'cmd'})) {
517       exec_cmd(\%cmd);
518       %cmd = ();
519     }
520     if ($arg =~ /^\s*mkfile /){      # "mkfile" command line
521       die "[TESH/CRITICAL] Output expected from mkfile command!\n" if scalar @{cmd{'out'}};
522
523       $cmd{'arg'} = $arg;
524       $cmd{'arg'} =~ s/\s*mkfile //;
525       mkfile_cmd(\%cmd);
526       %cmd = ();
527
528     } elsif ($arg =~ /^\s*cd /) {
529       die "[TESH/CRITICAL] Input provided to cd command!\n" if scalar @{cmd{'in'}};
530       die "[TESH/CRITICAL] Output expected from cd command!\n" if scalar @{cmd{'out'}};
531
532       $arg =~ s/^ *cd //;
533       cd_cmd("",$arg);
534       %cmd = ();
535
536     } else { # regular command
537       $cmd{'cmd'} = $arg;
538       $cmd{'file'} = $tesh_file;
539       $cmd{'line'} = $line_num;
540     }
541   }
542   elsif($cmd eq '&'){      # parallel command line
543
544     if (defined($cmd{'cmd'})) {
545       exec_cmd(\%cmd);
546       %cmd = ();
547     }
548     $cmd{'background'} = 1;
549     $cmd{'cmd'} = $arg;
550     $cmd{'file'} = $tesh_file;
551     $cmd{'line'} = $line_num;
552   }
553   elsif($line =~ /^!\s*output sort/){    #output sort
554     if (defined($cmd{'cmd'})) {
555       exec_cmd(\%cmd);
556       %cmd = ();
557     }
558     $cmd{'sort'} = 1;
559     if ($line =~ /^!\s*output sort\s+(\d+)/) {
560         $sort_prefix = $1;
561     }
562   }
563   elsif($line =~ /^!\s*output ignore/){    #output ignore
564     if (defined($cmd{'cmd'})) {
565       exec_cmd(\%cmd);
566       %cmd = ();
567     }
568     $cmd{'output ignore'} = 1;
569   }
570   elsif($line =~ /^!\s*output display/){    #output display
571     if (defined($cmd{'cmd'})) {
572       exec_cmd(\%cmd);
573       %cmd = ();
574     }
575     $cmd{'output display'} = 1;
576   }
577   elsif($line =~ /^!\s*expect signal (\w*)/) {#expect signal SIGABRT
578     if (defined($cmd{'cmd'})) {
579       exec_cmd(\%cmd);
580       %cmd = ();
581     }
582 print "hey\n";
583     $cmd{'expect'} = "$1";
584   }
585   elsif($line =~ /^!\s*expect return/){    #expect return
586     if (defined($cmd{'cmd'})) {
587       exec_cmd(\%cmd);
588       %cmd = ();
589     }
590     $line =~ s/^! expect return //g;
591     $line =~ s/\r//g;
592     $cmd{'return'} = $line;
593   }
594   elsif($line =~ /^!\s*setenv/){    #setenv
595     if (defined($cmd{'cmd'})) {
596       exec_cmd(\%cmd);
597       %cmd = ();
598     }
599     $line =~ s/^! setenv //g;
600     $line =~ s/\r//g;
601     setenv_cmd($line);
602   }
603   elsif($line =~ /^!\s*include/){    #include
604     if (defined($cmd{'cmd'})) {
605       exec_cmd(\%cmd);
606       %cmd = ();
607     }
608     print color("red"), "[Tesh/CRITICAL] need include";
609     print color("reset"), "\n";
610     die;
611   }
612   elsif($line =~ /^!\s*timeout/){    #timeout
613     if (defined($cmd{'cmd'})) {
614       exec_cmd(\%cmd);
615       %cmd = ();
616     }
617     $line =~ s/^! timeout //;
618     $line =~ s/\r//g;
619     $cmd{'timeout'} = $line;
620   } else {
621     die "[TESH/CRITICAL] parse error: $line\n";
622   }
623   if($forked){
624    kill('KILL', $forked);
625    $timeout=0;
626   }
627
628 }
629
630
631
632 # Deal with last command
633 if (defined($cmd{'cmd'})) {
634   exec_cmd(\%cmd);
635   %cmd = ();
636 }
637
638
639 if($forked){
640    kill('KILL', $forked);
641    $timeout=0;
642 }
643
644 foreach(@bg_cmds){
645   my %test=%{$_};
646   waitpid ($test{'pid'}, 0);
647   $test{'gotret'} = exit_status($?);
648   parse_out(\%test);
649 }
650
651 @bg_cmds=();
652
653 if ($diff_tool) {
654   close $diff_tool_tmp_fh;
655   system("$diff_tool $diff_tool_tmp_filename $tesh_file");
656   unlink $diff_tool_tmp_filename;
657 }
658
659 if($error !=0){
660     exit $exitcode;
661 }elsif($tesh_file eq "(stdin)"){
662     print "Test suite from stdin OK\n";
663 }else{
664     print "Test suite `$tesh_name' OK\n";
665 }
666
667 #my (@a,@b);
668 #push @a,"bl1";   push @b,"bl1";
669 #push @a,"bl2";   push @b,"bl2";
670 #push @a,"bl3";   push @b,"bl3";
671 #push @a,"bl4";   push @b,"bl4";
672 #push @a,"bl5";   push @b,"bl5";
673 #push @a,"bl6";   push @b,"bl6";
674 #push @a,"bl7";   push @b,"bl7";
675 ##push @a,"Perl";  push @b,"ruby";
676 #push @a,"END1";   push @b,"END1";
677 #push @a,"END2";   push @b,"END2";
678 #push @a,"END3";   push @b,"END3";
679 #push @a,"END4";   push @b,"END4";
680 #push @a,"END5";   push @b,"END5";
681 #push @a,"END6";   push @b,"END6";
682 #push @a,"END7";   push @b,"END7";
683 #print "Identical:\n". build_diff(\@a,\@b);
684
685 #@a = (); @b =();
686 #push @a,"AZE"; push @b,"EZA";
687 #print "Different:\n".build_diff(\@a,\@b);
688
689 use lib "@CMAKE_BINARY_DIR@/bin" ;
690
691 use Diff qw(diff); # postpone a bit to have time to change INC
692
693 sub build_diff {
694   my $res;
695   my $diff = Diff->new(@_);
696
697   $diff->Base( 1 );   # Return line numbers, not indices
698   my $chunk_count = $diff->Next(-1); # Compute the amount of chuncks
699   return ""   if ($chunk_count == 1 && $diff->Same());
700   $diff->Reset();
701   while(  $diff->Next()  ) {
702     my @same = $diff->Same();
703     if ($diff->Same() ) {
704       if ($diff->Next(0) > 1) { # not first chunk: print 2 first lines
705         $res .= '  '.$same[0]."\n" ;
706         $res .= '  '.$same[1]."\n" if (scalar @same>1);
707       }
708       $res .= "...\n"  if (scalar @same>2);
709 #    $res .= $diff->Next(0)."/$chunk_count\n";
710       if ($diff->Next(0) < $chunk_count) { # not last chunk: print 2 last lines
711         $res .= '  '.$same[scalar @same -2]."\n" if (scalar @same>1);
712         $res .= '  '.$same[scalar @same -1]."\n";
713       }
714     }
715     next if  $diff->Same();
716     map { $res .= "- $_\n" } $diff->Items(1);
717     map { $res .= "+ $_\n" } $diff->Items(2);
718   }
719   return $res;
720 }
721
722