demurgos / v8-coverage

Helpers to manipulate V8 coverage files
16 stars 6 forks source link

V8 coverage

This repository contains helper functions to manipulate V8 coverage files.

There are three implementations:

All the implementations expose the same API.

Rust

Support policy: Rust version 1.56.0 or later.

Node

Support policy: Node version 14.13.1 or later.

API

mergeProcessCovs(processCovs: ProcessCov[]): ProcessCov

Merges multiple process coverages into a single process coverage.

mergeScriptCovs(scriptCovs: ScriptCov[]): ScriptCov | undefined

Merges a list of matching script coverages into a single script coverage.

The lib assumes two scripts are matching if they have the same url.

If the list is empty, returns undefined.

mergeFunctionCovs(funcCovs: FunctionCov[]): FunctionCov | undefined

Merges a list of matching function coverages into a single function coverage.

The lib assumes two scripts are matching if their "root range" (first range) has the same offsets.

If the list is empty, returns undefined.

cloneProcessCov(processCov: ProcessCov): ProcessCov

cloneScriptCov(functionCov: FunctionCov): FunctionCov

cloneFunctionCov(scriptCov: ScriptCov): ScriptCov

cloneRangeCov(rangeCov: RangeCov): RangeCov

Types

ProcessCov

Coverage results for a single process. It contains a list of script coverages. There is one entry per script.

interface ProcessCov {
  result: ScriptCov[];
}
pub struct ProcessCov {
  pub result: Vec<ScriptCov>,
}

Properties:

Hypothesis:

ScriptCov

Coverage data for a single script. A script can be a CJS module, an ES module, a Node internal module, a dynamic module (for example cjs-bridge:// modules), etc. It holds a list of function coverages, for each of its functions.

interface ScriptCov {
  scriptId: string;
  url: string;
  functions: FunctionCov[];
}
pub struct ScriptCov {
  pub script_id: String,
  pub url: String,
  pub functions: Vec<FunctionCov>,
}

Properties:

FunctionCov

Coverage data for a single function. This is the main unit of coverage: it has a list of ranges forming a tree covering the AST and holding count data. A function coverage can correspond to a function declaration or expression, an arrow function, or the whole source text (anything outside of a function).

interface FunctionCov {
  functionName: string;
  ranges: RangeCov[];
  isBlockCoverage: boolean;
}
pub struct FunctionCov {
  pub function_name: String,
  pub ranges: Vec<RangeCocv>,
  pub is_block_coverage: bool,
}

Properties:

Hypothesis:

RangeCov

Range coverage is the primary unit of information: it has a count for the number of times a span of code was executed. The offsets and counts are absolute values (relative to the script). The count for an AST node is given by the smallest range containing this node.

interface RangeCov {
  startOffset: number;
  endOffset: number;
  count: number;
}
pub struct FunctionCov {
  pub start_offset: usize,
  pub end_offset: usize,
  pub count: i64,
}

Properties: