README
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/parser
Adding 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 require
d 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
ReadableStream
from theStreams API
- In Node, this is a
Readable
stream from thestreams
module
- 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)
- Parsesinput
and returns aPromise
of aReport
(SeeReports
section).input
- the input source to parse. This determined by the type of parser:ContentParser
- astring
containing the lcov formatted dataFileParser
- astring
containing 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-8
mode
- aReportMode
that specifies the type ofReport
that should be generated. Defaults toReportMode.Simple
rootDirectory
a string containing the "starting" point of files tracked in the lcov data. File paths are relativized based on this string. Ex: passing/root/projectA/src
as the root directory means that a file with path:/root/projectA/src/moduleB/sourceC
becomesmoduleB/sourceC
. Defaults to''
.
parseSync(input, options)
- Parsesinput
and returns aReport
(SeeReports
section).- The same function as
parse
, but runs in synchronously. Seeparse
for details on the parameters.
- The same function as
NOTE:
parseSync
is not available forStreamParser
s.
Reports
Full documentation is available here
Report
s 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.Simple
to 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.Detail
to 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.Tree
to generate this report.
Each report contains the following properties:
total
- the total coverageSummary
that includes hits/totals/misses for lines/functions/branches in all tracked filespaths
- a list of files that are tracked within the reportdetails
- a map ofSummary
s (orSummary
subclasses) that is keyed by the filename associated with theSummary
FlatReport
s contain a map ofDetailedSummary
s instead ofSummary
sTreeReport
s contain a map ofDetailedSummary
s orTreeReportNode
s keyed by the relative filename or relative directory, respectively