pgbackrest.git / summary / log / commit / refs

commit    22ecbc153a447b4f635fad2ab71fdd92ed8294bc
Author:   David Steele <david@pgmasters.net>
Date:     Sun Nov 11 22:32:42 2018 +0000

    New, concise coverage report for C.
    
    The report HTML generated by lcov is overly verbose and cumbersome to navigate. Since we maintain 100% coverage it's far more interesting to look at what is not covered than what is.
    
    The new report presents all missing coverage on a single page and excludes code that is covered for brevity.


doc/xml/release.xml | 4 + test/lib/pgBackRestTest/Common/CoverageTest.pm | 493 +++++++++++++++++++++++++ test/test.pl | 10 + 3 files changed, 507 insertions(+) diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 438adab4e..5122370e2 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -146,6 +146,10 @@ <p>Test speed improvements. Mount <id>tmpfs</id> in <file>Vagrantfile</file> instead <file>test.pl</file>. Preserve contents of C unit test build directory between <file>test.pl</file> executions. Improve efficiency of code generation.</p> </release-item> + <release-item> + <p>New, concise coverage report for C.</p> + </release-item> + <release-item> <p>Improve alignment of expected vs. actual error test results.</p> </release-item> diff --git a/test/lib/pgBackRestTest/Common/CoverageTest.pm b/test/lib/pgBackRestTest/Common/CoverageTest.pm new file mode 100644 index 000000000..d2e3c13d4 --- /dev/null +++ b/test/lib/pgBackRestTest/Common/CoverageTest.pm @@ -0,0 +1,493 @@ +#################################################################################################################################### +# Generate C Coverage Report +#################################################################################################################################### +package pgBackRestTest::Common::CoverageTest; + +#################################################################################################################################### +# Perl includes +#################################################################################################################################### +use strict; +use warnings FATAL => qw(all); +use Carp qw(confess); +use English '-no_match_vars'; + +use Exporter qw(import); + our @EXPORT = qw(); + +use pgBackRest::Common::Log; +use pgBackRest::Common::String; +use pgBackRest::Version; + +use BackRestDoc::Html::DocHtmlBuilder; +use BackRestDoc::Html::DocHtmlElement; + +#################################################################################################################################### +# Generate a C coverage report +#################################################################################################################################### +sub coverageGenerate +{ + my $oStorage = shift; + my $strCoveragePath = shift; + my $strOutFile = shift; + + # Track missing coverage + my $rhCoverage = {}; + + # Find all lcov files in the coverage path + my $rhManifest = $oStorage->manifest($strCoveragePath); + + foreach my $strFileCov (sort(keys(%{$rhManifest}))) + { + if ($strFileCov =~ /\.lcov$/) + { + my $strCoverage = ${$oStorage->get("${strCoveragePath}/${strFileCov}")}; + + # Show that the file is part of the coverage report even if there is no missing coverage + my $strFile = substr($strFileCov, 0, length($strFileCov) - 5) . '.c'; + $rhCoverage->{$strFile} = undef; + + my $iBranchLine = -1; + my $iBranch = undef; + my $iBranchIdx = -1; + my $iBranchPart = undef; + + foreach my $strLine (split("\n", $strCoverage)) + { + # Check branch coverage + if ($strLine =~ /^BRDA\:/) + { + my @stryData = split("\,", substr($strLine, 5)); + + if (@stryData < 4) + { + confess &log(ERROR, "'${strLine}' should have four fields"); + } + + my $strBranchLine = sprintf("%09d", $stryData[0]); + + if ($iBranchLine != $stryData[0]) + { + $iBranchLine = $stryData[0]; + $iBranch = $stryData[1]; + $iBranchIdx = 0; + $iBranchPart = 0; + } + elsif ($iBranch != $stryData[1]) + { + if ($iBranchPart != 1) + { + confess &log(ERROR, "line ${iBranchLine}, branch ${iBranch} does not have two parts"); + } + + $iBranch = $stryData[1]; + $iBranchIdx++; + $iBranchPart = 0; + } + else + { + $iBranchPart++; + } + + $rhCoverage->{$strFile}{line}{$strBranchLine}{branch}{$iBranchIdx}{$iBranchPart} = + $stryData[3] eq '-' || $stryData[3] eq '0' ? false : true; + } + + # Check line coverage + if ($strLine =~ /^DA\:/) + { + my @stryData = split("\,", substr($strLine, 3)); + + if (@stryData < 2) + { + confess &log(ERROR, "'${strLine}' should have two fields"); + } + + my $strBranchLine = sprintf("%09d", $stryData[0]); + + if ($stryData[1] eq '0') + { + $rhCoverage->{$strFile}{line}{$strBranchLine}{statement} = 0; + } + } + } + } + } + + # Remove branch coverage on lines that are completely covered + foreach my $strFile (sort(keys(%{$rhCoverage}))) + { + foreach my $iLine (sort(keys(%{$rhCoverage->{$strFile}{line}}))) + { + if (defined($rhCoverage->{$strFile}{line}{$iLine}{branch})) + { + # We'll assume the line is completely covered + my $bCovered = true; + + foreach my $iBranch (sort(keys(%{$rhCoverage->{$strFile}{line}{$iLine}{branch}}))) + { + foreach my $iBranchPart (sort(keys(%{$rhCoverage->{$strFile}{line}{$iLine}{branch}{$iBranch}}))) + { + if (!$rhCoverage->{$strFile}{line}{$iLine}{branch}{$iBranch}{$iBranchPart}) + { + $bCovered = false; + } + } + } + + if ($bCovered) + { + # &log(WARN, "removed branch coverage for ${strFile} line ${iLine}"); + if (defined($rhCoverage->{$strFile}{line}{$iLine}{statement})) + { + delete($rhCoverage->{$strFile}{line}{$iLine}{branch}); + } + else + { + delete($rhCoverage->{$strFile}{line}{$iLine}); + } + } + } + } + } + + # Remove line when no lines are uncovered + foreach my $strFile (sort(keys(%{$rhCoverage}))) + { + my $bCovered = true; + + foreach my $iLine (sort(keys(%{$rhCoverage->{$strFile}{line}}))) + { + $bCovered = false; + last; + } + + if ($bCovered) + { + delete($rhCoverage->{$strFile}{line}); + } + } + + # Determine all lines to report for coverage. We'll just go backward and forward to the next blank line. + foreach my $strFile (sort(keys(%{$rhCoverage}))) + { + if (defined($rhCoverage->{$strFile}{line})) + { + my $strC = ${$oStorage->get("${strCoveragePath}/${strFile}")}; + my @stryC = split("\n", $strC); + + foreach my $iLine (sort(keys(%{$rhCoverage->{$strFile}{line}}))) + { + # Run back to the last blank or brace-only line + for (my $iLineIdx = $iLine; $iLineIdx >= 0; $iLineIdx--) + { + if (trim($stryC[$iLineIdx - 1]) eq '' || trim($stryC[$iLineIdx - 1]) eq '{') + { + last; + } + else + { + if (!defined($rhCoverage->{$strFile}{line}{sprintf("%09d", $iLineIdx)})) + { + $rhCoverage->{$strFile}{line}{sprintf("%09d", $iLineIdx)} = undef; + } + } + } + + # Run forward to the last blank or brace-only line + for (my $iLineIdx = $iLine; $iLineIdx <= @stryC; $iLineIdx++) + { + if (trim($stryC[$iLineIdx - 1]) eq '' || trim($stryC[$iLineIdx - 1]) eq '}') + { + last; + } + else + { + if (!defined($rhCoverage->{$strFile}{line}{sprintf("%09d", $iLineIdx)})) + { + $rhCoverage->{$strFile}{line}{sprintf("%09d", $iLineIdx)} = undef; + } + } + } + } + } + } + + # Build html + my $strTitle = BACKREST_NAME . ' Coverage Report'; + my $strDarkRed = '#580000'; + + my $oHtml = new BackRestDoc::Html::DocHtmlBuilder( + BACKREST_NAME, $strTitle, + undef, undef, undef, + true, true, + + "html\n" . + "{\n" . + " background-color: #f8f8f8;\n" . + " font-family: Avenir, Corbel, sans-serif;\n" . + " font-size: 12pt;\n" . + " margin-top: 8px;\n" . + " margin-left: 1\%;\n" . + " margin-right: 1\%;\n" . + " width: 98\%;\n" . + "}\n" . + "\n" . + "body\n" . + "{\n" . + " margin: 0px auto;\n" . + " padding: 0px;\n" . + " width: 100\%;\n" . + " text-align: justify;\n" . + "}\n" . + + ".title\n" . + "{\n" . + " width: 100\%;\n" . + " text-align: center;\n" . + " font-size: 200\%;\n" . + "}\n" . + + "\n" . + ".list-table\n" . + "{\n" . + " width: 100\%;\n" . + "}\n" . + + "\n" . + ".list-table-caption\n" . + "{\n" . + " margin-top: 1em;\n" . + " font-size: 130\%;\n" . + " margin-bottom: .25em;\n" . + "}\n" . + + "\n" . + ".list-table-caption::after\n" . + "{\n" . + " content: \"Modules Tested for Coverage:\";\n" . + "}\n" . + + "\n" . + ".list-table-header-file\n" . + "{\n" . + " padding-left: .5em;\n" . + " padding-right: .5em;\n" . + " background-color: gray;\n" . + " width: 100\%;\n" . + "}\n" . + + "\n" . + ".list-table-row-uncovered\n" . + "{\n" . + " background-color: ${strDarkRed};\n" . + " color: white;\n" . + " width: 100\%;\n" . + "}\n" . + + "\n" . + ".list-table-row-file\n" . + "{\n" . + "}\n" . + + "\n" . + ".report-table\n" . + "{\n" . + " width: 100\%;\n" . + "}\n" . + + "\n" . + ".report-table-caption\n" . + "{\n" . + " margin-top: 1em;\n" . + " font-size: 130\%;\n" . + " margin-bottom: .25em;\n" . + "}\n" . + + "\n" . + ".report-table-caption::after\n" . + "{\n" . + " content: \" report:\";\n" . + "}\n" . + + "\n" . + ".report-table-header\n" . + "{\n" . + "}\n" . + + "\n" . + ".report-table-header-line, .report-table-header-branch, .report-table-header-code\n" . + "{\n" . + " padding-left: .5em;\n" . + " padding-right: .5em;\n" . + " background-color: gray;\n" . + "}\n" . + + "\n" . + ".report-table-header-code\n" . + "{\n" . + " width: 100\%;\n" . + "}\n" . + + "\n" . + ".report-table-row-dot-tr, .report-table-row\n" . + "{\n" . + " font-family: \"Courier New\", Courier, monospace;\n" . + "}\n" . + + "\n" . + ".report-table-row-dot-skip\n" . + "{\n" . + " padding-top: .25em;\n" . + " padding-bottom: .25em;\n" . + " text-align: center;\n" . + "}\n" . + + "\n" . + ".report-table-row-dot-skip::after\n" . + "{\n" . + " content: \"...\";\n" . + "}\n" . + + "\n" . + ".report-table-row-line, .report-table-row-branch, .report-table-row-branch-uncovered," . + " .report-table-row-code, .report-table-row-code-uncovered\n" . + "{\n" . + " padding-left: .5em;\n" . + " padding-right: .5em;\n" . + "}\n" . + + "\n" . + ".report-table-row-line\n" . + "{\n" . + " text-align: right;\n" . + "}\n" . + + "\n" . + ".report-table-row-branch, .report-table-row-branch-uncovered\n" . + "{\n" . + " text-align: right;\n" . + " white-space: nowrap;\n" . + "}\n" . + + "\n" . + ".report-table-row-branch-uncovered\n" . + "{\n" . + " background-color: ${strDarkRed};\n" . + " color: white;\n" . + "}\n" . + + "\n" . + ".report-table-row-code, .report-table-row-code-uncovered\n" . + "{\n" . + " white-space: pre;\n" . + "}\n" . + + "\n" . + ".report-table-row-code-uncovered\n" . + "{\n" . + " background-color: ${strDarkRed};\n" . + " color: white;\n" . + "}\n"); + + $oHtml->bodyGet()->addNew(HTML_DIV, 'title', {strContent => $strTitle}); + + # Build the file list table + my $oTable = $oHtml->bodyGet()->addNew(HTML_TABLE, 'list-table'); + $oTable->addNew(HTML_DIV, 'list-table-caption'); + + my $oHeader = $oTable->addNew(HTML_TR, 'list-table-header'); + $oHeader->addNew(HTML_TH, 'list-table-header-file', {strContent => 'FILE'}); + + foreach my $strFile (sort(keys(%{$rhCoverage}))) + { + my $oRow = $oTable->addNew(HTML_TR, 'list-table-row-' . (defined($rhCoverage->{$strFile}{line}) ? 'uncovered' : 'covered')); + $oRow->addNew(HTML_TD, 'list-table-row-file', {strContent => $strFile}); + } + + # Report on files that are missing coverage + foreach my $strFile (sort(keys(%{$rhCoverage}))) + { + if (defined($rhCoverage->{$strFile}{line})) + { + # Build the file report table + $oTable = $oHtml->bodyGet()->addNew(HTML_TABLE, 'report-table'); + + $oTable->addNew(HTML_DIV, 'report-table-caption', {strContent => "${strFile}"}); + + $oHeader = $oTable->addNew(HTML_TR, 'report-table-header'); + $oHeader->addNew(HTML_TH, 'report-table-header-line', {strContent => 'LINE'}); + $oHeader->addNew(HTML_TH, 'report-table-header-branch', {strContent => 'BRANCH'}); + $oHeader->addNew(HTML_TH, 'report-table-header-code', {strContent => 'CODE'}); + + my $strC = ${$oStorage->get("${strCoveragePath}/${strFile}")}; + my @stryC = split("\n", $strC); + my $iLastLine = undef; + + foreach my $strLine (sort(keys(%{$rhCoverage->{$strFile}{line}}))) + { + if (defined($iLastLine) && $strLine != $iLastLine + 1) + { + my $oRow = $oTable->addNew(HTML_TR, 'report-table-row-dot'); + $oRow->addNew(HTML_TD, 'report-table-row-dot-skip', {strExtra => 'colspan="3"'}); + } + + $iLastLine = $strLine; + my $iLine = int($strLine); + + my $oRow = $oTable->addNew(HTML_TR, 'report-table-row'); + $oRow->addNew(HTML_TD, 'report-table-row-line', {strContent => $iLine}); + my $strBranch; + + # Show missing branch coverage + if (defined($rhCoverage->{$strFile}{line}{$strLine}{branch}) && + !defined($rhCoverage->{$strFile}{line}{$strLine}{statement})) + { + my $iBranchIdx = 0; + + foreach my $iBranch (sort(keys(%{$rhCoverage->{$strFile}{line}{$strLine}{branch}}))) + { + $strBranch .= + '[' . + ($rhCoverage->{$strFile}{line}{$strLine}{branch}{$iBranch}{0} ? + '+' : '-') . + ' ' . + ($rhCoverage->{$strFile}{line}{$strLine}{branch}{$iBranch}{1} ? + '+' : '-') . + ']'; + + if ($iBranchIdx == 1) + { + $strBranch .= '<br/>'; + $iBranchIdx = 0; + } + else + { + $strBranch .= ' '; + $iBranchIdx++; + } + } + } + + $oRow->addNew( + HTML_TD, 'report-table-row-branch' . (defined($strBranch) ? '-uncovered' : ''), + {strContent => $strBranch}); + + # Color code based on coverage + my $bUncovered = + defined($rhCoverage->{$strFile}{line}{$strLine}{branch}) || + defined($rhCoverage->{$strFile}{line}{$strLine}{statement}); + + $oRow->addNew( + HTML_TD, 'report-table-row-code' . ($bUncovered ? '-uncovered' : ''), + {bPre => true, strContent => $stryC[$strLine - 1]}); + } + } + } + + # Write coverage report + $oStorage->put($strOutFile, $oHtml->htmlGet()); +} + +push @EXPORT, qw(coverageGenerate); + +1; diff --git a/test/test.pl b/test/test.pl index b31d735ce..77e9765e5 100755 --- a/test/test.pl +++ b/test/test.pl @@ -49,6 +49,7 @@ use pgBackRestBuild::Error::Data; use pgBackRestTest::Common::BuildTest; use pgBackRestTest::Common::CodeCountTest; use pgBackRestTest::Common::ContainerTest; +use pgBackRestTest::Common::CoverageTest; use pgBackRestTest::Common::CiTest; use pgBackRestTest::Common::DefineTest; use pgBackRestTest::Common::ExecuteTest; @@ -663,6 +664,11 @@ eval # Remove old coverage dirs -- do it this way so the dirs stay open in finder/explorer, etc. executeTest("rm -rf ${strBackRestBase}/test/coverage/c/* ${strBackRestBase}/test/coverage/perl/*"); + # Overwrite the C coverage report so it will load but not show old coverage + $oStorageTest->pathCreate("${strBackRestBase}/test/coverage", {strMode => '0770', bIgnoreExists => true}); + $oStorageBackRest->put( + "${strBackRestBase}/test/coverage/c-coverage.html", "<center>[ Generating New Report ]</center>"); + # Copy C code for coverage tests if (vmCoverageC($strVm) && !$bDryRun) { @@ -1462,6 +1468,10 @@ eval } } } + + $oStorageBackRest->remove("${strBackRestBase}/test/.vagrant/code/all.lcov", {bIgnoreMissing => true}); + coverageGenerate( + $oStorageBackRest, "${strBackRestBase}/test/.vagrant/code", "${strBackRestBase}/test/coverage/c-coverage.html"); } else { [parent: 3cd43fd60cd4]