@vyapti/parser v0.1.2
parser-js
Parse LCOV formatted coverage data from a variety of inputs: Strings, Files, Node Streams and Browser Streams through one consistent API.
Install
Parser-js is available either as a package or directly in browser (using https://unpkg.com/):
Installing as a package
# Using yarn:
yarn add @vyapti/parser
# Using npm:
npm install @vyapti/parserAdding in browser:
<!-->Add this in your head section<-->
<script src="https://unpkg.com/@vyapti/parser@latest/dist/web/index.min.js"></script>
<!-->You can also use the unminified version for debugging<-->
<script src="https://unpkg.com/@vyapti/parser@latest/dist/web/index.js"></script>Usage
For Node Environments, the package can be required and used:
const fs = require('fs');
const { StreamParser, ReportMode } = require('@vyapti/parser');
const stream = fs.createReadStream('/path/to/lcov/file');
const streamParser = new StreamParser();
streamParser.parse(, {
rootDirectory: '/base/path/to/project',
encoding: 'ascii',
mode: ReportMode.Detail,
}).then(report => {
// Use the parsed report...
});For Browser Environments, the package is exposed as VyaptiParser on the
window object.
const { StreamParser, ReportMode } = window.VyaptiParser;
const streamParser = new StreamParser();
fetch(urlToLcovFile)
.then(res => res.body)
.then(stream => streamParser.parse(stream, {
rootDirectory: 'vyapti/parser',
encoding: 'utf-8',
mode: ReportMode.Detail,
}))
.then(report => {
// Use the parsed report...
});Examples
Examples of the parser-js usage are available in the example/ directory for
both node and browser environments. All examples are runnable, so feel free to
clone the project and playaround!
To use the node examples, navigate to example/node and run a yarn install
or npm install. You can then run the available examples using node.
To use the browser examples, simply load the included html files in
example/web.
API
The following parsers are exposed for use:
ContentParser- parses lcov content from a string (new lines are still required)FileParser(Node only) - parses lcov content from a fileStreamParser- parses lcov content from a stream- In Browsers, this is a
ReadableStreamfrom theStreams API - In Node, this is a
Readablestream from thestreamsmodule
- In Browsers, this is a
Additionally, the ReportMode enum is exposed to determine the correct
Report to create (See Reports section).
Each parser can be constructed directly and provides two methods to handle parsing:
parse(input, options)- Parsesinputand returns aPromiseof aReport(SeeReportssection).input- the input source to parse. This determined by the type of parser:ContentParser- astringcontaining the lcov formatted dataFileParser- astringcontaining the path to a file with lcov formatted dataStreamParser- astream(depending on the environment) that contains the lcov formatted data
options- an object that provides several configuration options:encoding- The encoding format used by the input. Defaults toutf-8mode- aReportModethat specifies the type ofReportthat should be generated. Defaults toReportMode.SimplerootDirectorya string containing the "starting" point of files tracked in the lcov data. File paths are relativized based on this string. Ex: passing/root/projectA/srcas the root directory means that a file with path:/root/projectA/src/moduleB/sourceCbecomesmoduleB/sourceC. Defaults to''.
parseSync(input, options)- Parsesinputand returns aReport(SeeReportssection).- The same function as
parse, but runs in synchronously. Seeparsefor details on the parameters.NOTE:
parseSyncis not available forStreamParsers.
- The same function as
Reports
Full documentation is available here
Reports contain parsed data in a structure that allows easy access. There are
several types of reports that can be generated based on the ReportMode passed
to the parse methods:
Report- A basic report that contains a total coverage summary, a list of files tracked in the coverage data, and a map of coverage summaries keyed by filename. UseReportMode.Simpleto generate this report.FlatReport- A detailed report that contains a total coverage summary, a list of files tracked in the coverage data, and a map of detailed coverage summaries keyed by filename. UseReportMode.Detailto generate this report.TreeReport- A detailed report that contains a total coverage summary, a list of files tracked in the coverage data, and a tree structure (matching the file structure heirarchy of tracked files). Nodes in the tree structure (which represent directories) contain a coverage summary, a list of files that are descendants, and a map of nodes or leaves keyed by file name (or directory name). Leaves in the tree structure (which represent single files) are a detailed coverage summary. UseReportMode.Treeto generate this report.
Each report contains the following properties:
total- the total coverageSummarythat includes hits/totals/misses for lines/functions/branches in all tracked filespaths- a list of files that are tracked within the reportdetails- a map ofSummarys (orSummarysubclasses) that is keyed by the filename associated with theSummaryFlatReports contain a map ofDetailedSummarys instead ofSummarysTreeReports contain a map ofDetailedSummarys orTreeReportNodes keyed by the relative filename or relative directory, respectively