Unit Utils.Coverage

Description

Unit which has the functions used by pasc coverage: the Coverage build mode of a test project, the report that kcov writes and the units that the report does not have.

kcov needs no instrumentation: it reads the DWARF line info of the executable and sets a breakpoint on each line. The Coverage build mode has the options of the Default one, with debug info, no optimization, so lines are not merged or removed, and no heaptrc. Its units go to lib/coverage, so they do not get mixed with the Default build, and its executable stays next to the Default one, with -coverage at the end of the name, because tests often find their files from the folder of the executable.

Overview

Classes, Interfaces, Objects and Records

Name Description
Record TCoverageFile Coverage of a source file.
Record TCoverageReport Coverage of a test run, read from the coverage.json of kcov.

Functions and Procedures

function HasBuildMode(const AProjectFile, AName: string): Boolean;
function AddCoverageBuildMode(const AProjectFile: string): Boolean;
function GetCoverageExecutable(const AProjectFile: string): string;
function ReadCoverageReport(const AFileName: string): TCoverageReport;
function CoveragePercent(const ACovered, ATotal: Integer): Double;
function FormatPercent(const APercent: Double): string;
function HasCode(const ASource: string): Boolean;
function GetUnitsNotInReport(const ASourceDirs: TArray<string>; const AReport: TCoverageReport): TArray<string>;
function IgnorePaths(const ARootDir: string; const APaths: TArray<string>): TArray<string>;

Constants

COVERAGE_BUILD_MODE = 'Coverage';
COVERAGE_DIR = 'coverage';
COVERAGE_SUFFIX = '-coverage';

Description

Functions and Procedures

function HasBuildMode(const AProjectFile, AName: string): Boolean;

Returns true if a .lpi has a build mode with the given name, ignoring case.

Parameters
AProjectFile
.lpi file.
AName
Build mode name.
function AddCoverageBuildMode(const AProjectFile: string): Boolean;

Adds the Coverage build mode to a test project, if it does not have it, with the compiler options of the project: the same search paths, so pasc deps keeps them in sync, DWARF 3 debug info, no optimization, no heaptrc, the units in lib/coverage and the executable next to the Default one, with -coverage at the end of its name. Only the new build mode is written, the rest of the file is kept as it is. Returns false if the project already had the build mode.

Parameters
AProjectFile
.lpi file of the test project.
function GetCoverageExecutable(const AProjectFile: string): string;

Returns the complete file name of the executable of the Coverage build mode of a .lpi, or empty if it has no such build mode.

Parameters
AProjectFile
.lpi file of the test project.
function ReadCoverageReport(const AFileName: string): TCoverageReport;

Reads the coverage.json written by kcov.

Parameters
AFileName
coverage.json file.
function CoveragePercent(const ACovered, ATotal: Integer): Double;

Returns the coverage as a percentage, zero when there are no lines.

Parameters
ACovered
Covered lines.
ATotal
Lines with code.
function FormatPercent(const APercent: Double): string;

Formats a percentage with two decimals and a dot, like 94.00%.

Parameters
APercent
Percentage.
function HasCode(const ASource: string): Boolean;

Returns true if the source of a unit has code after its implementation: a begin, an asm, an initialization or an include. A unit with only declarations has no line for kcov, even when a test uses it. Comments and strings are skipped.

Parameters
ASource
Source of the unit.
function GetUnitsNotInReport(const ASourceDirs: TArray<string>; const AReport: TCoverageReport): TArray<string>;

Returns the units (.pas or .pp) of the given folders, not of their subfolders, that have code but are not in the report: no test uses them, so kcov does not see them and they are not counted. Sorted.

Parameters
ASourceDirs
Source folders of the member.
AReport
Coverage report of the member tests.
function IgnorePaths(const ARootDir: string; const APaths: TArray<string>): TArray<string>;

Appends to the .gitignore of a folder the paths that git does not ignore yet, when the folder is in a git repository, like /coverage/. The .gitignore is created if needed, and its lines are kept. Returns the lines added.

Parameters
ARootDir
Folder of the .gitignore.
APaths
Paths relative to the folder, with / as separator and a / at the end of a folder.

Constants

COVERAGE_BUILD_MODE = 'Coverage';

Name of the build mode added to the test projects.

COVERAGE_DIR = 'coverage';

Folder of the reports, at the root of the workspace or project.

COVERAGE_SUFFIX = '-coverage';

Added to the name of the test executable for the Coverage build mode.


Generated by PasDoc 1.0.4.