VCPKG - CMakeLists.txt updated to build for vcpkg.
[platform/core/uifw/dali-toolkit.git] / automated-tests / patch-coverage.pl
index 68754eb..5e5e164 100755 (executable)
@@ -19,8 +19,10 @@ use strict;
 use Git;
 use Getopt::Long;
 use Error qw(:try);
+use HTML::Element;
 use Pod::Usage;
 use File::Basename;
+#use Data::Dumper;
 use File::stat;
 use Scalar::Util qw /looks_like_number/;
 use Cwd qw /getcwd/;
@@ -45,16 +47,14 @@ our $repo = Git->repository();
 our $debug=0;
 our $pd_debug=0;
 our $opt_cached;
-our $opt_head;
-#our $opt_workingtree;
-#our $opt_diff=1;
 our $opt_help;
-our $opt_verbose;
+our $opt_output;
 our $opt_quiet;
+our $opt_verbose;
 
 my %options = (
     "cached"       => { "optvar"=>\$opt_cached, "desc"=>"Use index" },
-    "head"         => { "optvar"=>\$opt_head, "desc"=>"Use git show" },
+    "output:s"     => { "optvar"=>\$opt_output, "desc"=>"Generate html output"},
     "help"         => { "optvar"=>\$opt_help, "desc"=>""},
     "quiet"        => { "optvar"=>\$opt_quiet, "desc"=>""},
     "verbose"      => { "optvar"=>\$opt_verbose, "desc"=>"" });
@@ -323,6 +323,7 @@ sub get_coverage
 # output for the patch.
 sub run_diff
 {
+    #print "run_diff(" . join(" ", @_) . ")\n";
     my ($fh, $c) = $repo->command_output_pipe(@_);
     our @patch=();
     while(<$fh>)
@@ -332,6 +333,8 @@ sub run_diff
     }
     $repo->command_close_pipe($fh, $c);
 
+    print "Patch size: " . scalar(@patch) . "\n" if $debug;
+
     # @patch has slurped diff for all files...
     my $filesref = parse_diff ( \@patch );
     show_patch_lines($filesref) if $debug;
@@ -344,6 +347,7 @@ sub run_diff
     for my $file (keys(%$filesref))
     {
         my ($name, $path, $suffix) = fileparse($file, qr{\.[^.]*$});
+        next if($path !~ /^dali/);
         if($suffix eq ".cpp" || $suffix eq ".c" || $suffix eq ".h")
         {
             get_coverage($file, $filesref);
@@ -353,7 +357,6 @@ sub run_diff
     return $filesref;
 }
 
-
 sub calc_patch_coverage_percentage
 {
     my $filesref = shift;
@@ -362,6 +365,9 @@ sub calc_patch_coverage_percentage
 
     foreach my $file (keys(%$filesref))
     {
+        my ($name, $path, $suffix) = fileparse($file, qr{\.[^.]*$});
+        next if($path !~ /^dali/);
+
         my $covered_lines = 0;
         my $uncovered_lines = 0;
 
@@ -401,7 +407,7 @@ sub calc_patch_coverage_percentage
     my $percent = 0;
     if($total_exec > 0) { $percent = 100 * $total_covered_lines / $total_exec; }
 
-    return $percent;
+    return [ $total_exec, $percent ];
 }
 
 sub patch_output
@@ -480,6 +486,145 @@ sub patch_output
 }
 
 
+sub patch_html_output
+{
+    my $filesref = shift;
+
+    my $html = HTML::Element->new('html');
+    my $head = HTML::Element->new('head');
+    my $title = HTML::Element->new('title');
+    $title->push_content("Patch Coverage");
+    $head->push_content($title, "\n");
+    $html->push_content($head, "\n");
+
+    my $body = HTML::Element->new('body');
+    $body->attr('bgcolor', "white");
+
+    foreach my $file (sort(keys(%$filesref)))
+    {
+        my ($name, $path, $suffix) = fileparse($file, qr{\.[^.]*$});
+        next if($path !~ /^dali/);
+
+        my $patchref = $filesref->{$file}->{"patch"};
+        my $b_lines_ref = $filesref->{$file}->{"b_lines"};
+        my $coverage_ref = $filesref->{$file}->{"coverage"};
+
+        my $header = HTML::Element->new('h2');
+        $header->push_content($file);
+        $body->push_content($header);
+        $body->push_content("\n");
+        if($coverage_ref)
+        {
+            if( $coverage_ref->{"covered_lines"} > 0
+                ||
+                $coverage_ref->{"uncovered_lines"} > 0 )
+            {
+                my $para = HTML::Element->new('p');
+                my $covered = HTML::Element->new('span');
+                $covered->attr('style', "color:green;");
+                $covered->push_content("Covered: " . $coverage_ref->{"covered_lines"} );
+                $para->push_content($covered);
+
+                my $para2 = HTML::Element->new('p');
+                my $uncovered = HTML::Element->new('span');
+                $uncovered->attr('style', "color:red;");
+                $uncovered->push_content("Uncovered: " . $coverage_ref->{"uncovered_lines"} );
+                $para2->push_content($uncovered);
+                $body->push_content($para, $para2);
+            }
+            else
+            {
+                #print "coverage ref exists for $file:\n" . Data::Dumper::Dumper($coverage_ref) . "\n";
+            }
+        }
+        else
+        {
+            my $para = HTML::Element->new('p');
+            my $span = HTML::Element->new('span');
+            if($suffix eq ".cpp" || $suffix eq ".c" || $suffix eq ".h")
+            {
+                $span->attr('style', "color:red;");
+            }
+            $span->push_content("No coverage found");
+            $para->push_content($span);
+            $body->push_content($para);
+        }
+
+        for my $patch (@$patchref)
+        {
+            my $hunkstr="Hunk: " . $patch->[0];
+            if( $patch->[1] > 1 )
+            {
+                $hunkstr .= " - " . ($patch->[0]+$patch->[1]-1);
+            }
+
+            my $para = HTML::Element->new('p');
+            my $span = HTML::Element->new('span');
+            $span->attr('style', "font-weight:bold;");
+            $span->push_content($hunkstr);
+            $para->push_content($span);
+            $body->push_content($para);
+
+            my $codeHunk = HTML::Element->new('pre');
+            for(my $i = 0; $i < $patch->[1]; $i++ )
+            {
+                my $line = $i + $patch->[0];
+                my $num_line_digits=log($line)/log(10);
+                for $i (0..(6-$num_line_digits-1))
+                {
+                    $codeHunk->push_content(" ");
+                }
+
+                $codeHunk->push_content("$line  ");
+
+                my $srcLine = HTML::Element->new('span');
+                if($coverage_ref)
+                {
+                    my $color;
+
+                    if($coverage_ref->{"covered"}->{$line})
+                    {
+                        $srcLine->attr('style', "color:green;");
+                    }
+                    elsif($coverage_ref->{"uncovered"}->{$line})
+                    {
+                        $srcLine->attr('style', "color:red;font-weight:bold;");
+                    }
+                    else
+                    {
+                        $srcLine->attr('style', "color:black;font-weight:normal;");
+                    }
+                    my $src=$coverage_ref->{"src"}->{$line};
+                    chomp($src);
+                    $srcLine->push_content($src);
+                }
+                else
+                {
+                    # We don't have coverage data, so print it from the patch instead.
+                    my $src = $b_lines_ref->{$line};
+                    $srcLine->attr('style', "color:black;font-weight:normal;");
+                    $srcLine->push_content($src);
+                }
+                $codeHunk->push_content($srcLine, "\n");
+            }
+            $body->push_content($codeHunk, "\n");
+        }
+    }
+    $body->push_content(HTML::Element->new('hr'));
+    $html->push_content($body, "\n");
+
+    open( my $filehandle, ">", $opt_output ) || die "Can't open $opt_output for writing:$!\n";
+
+    print $filehandle <<EOH;
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
+"http://www.w3.org/TR/REC-html40/loose.dtd">
+EOH
+;
+    print $filehandle $html->as_HTML();
+    close $filehandle;
+}
+
+
 ################################################################################
 ##                                    MAIN                                    ##
 ################################################################################
@@ -492,30 +637,82 @@ chdir "build/tizen";
 my @cmd=('--no-pager','diff','--no-ext-diff','-U0','--no-color');
 
 my $status = $repo->command("status", "-s");
-if( $status eq "" )
+if( $status eq "" && !scalar(@ARGV))
 {
-    # There are no changes in the index or working tree. Use the last patch instead
+    # There are no changes in the index or working tree, and
+    # no diff arguments to append. Use the last patch instead.
     push @cmd, ('HEAD~1','HEAD');
 }
-elsif($opt_cached) # TODO: Remove this option. Instead, need full diff
+else
 {
-    push @cmd, "--cached";
+    # detect if there are only cached changes or only working tree changes
+    my $cached = 0;
+    my $working = 0;
+    for my $fstat ( split(/\n/, $status) )
+    {
+        if(substr( $fstat, 0, 1 ) ne " "){ $cached++; }
+        if(substr( $fstat, 1, 1 ) ne " "){ $working++; }
+    }
+    if($cached > 0 )
+    {
+        if($working == 0)
+        {
+            push @cmd, "--cached";
+        }
+        else
+        {
+            die "Both cached & working files - cannot get correct patch from git\n";
+            # Would have to diff from separate clone.
+        }
+    }
 }
 
 push @cmd, @ARGV;
 my $filesref = run_diff(@cmd);
 
-my $percent = calc_patch_coverage_percentage($filesref);
-if( ! $opt_quiet )
+chdir $cwd;
+
+# Check how many actual source files there are in the patch
+my $filecount = 0;
+foreach my $file (keys(%$filesref))
+{
+    my ($name, $path, $suffix) = fileparse($file, qr{\.[^.]*$});
+    next if($path !~ /^dali/);
+    next if($suffix ne ".cpp" && $suffix ne ".c" && $suffix ne ".h");
+    $filecount++;
+}
+if( $filecount == 0 )
+{
+    print "No source files found\n";
+    exit 0;    # Exit with no error.
+}
+
+my $percentref = calc_patch_coverage_percentage($filesref);
+if($percentref->[0] == 0)
+{
+    print "No coverable lines found\n";
+    exit 0;
+}
+my $percent = $percentref->[1];
+
+my $color=BOLD RED;
+if($opt_output)
+{
+    print "Outputing to $opt_output\n" if $debug;
+    patch_html_output($filesref);
+}
+elsif( ! $opt_quiet )
 {
     patch_output($filesref);
-    my $color=BOLD RED;
     if($percent>=90)
     {
         $color=GREEN;
     }
-    printf("Percentage of change covered: $color %5.2f%\n" . RESET, $percent);
+    print RESET;
 }
+
+printf("Percentage of change covered: %5.2f%\n", $percent);
+
 exit($percent<90);