remove_from_coverage 1.0.7

  • Readme
  • Changelog
  • Example
  • Installing
  • 64

Remove from Coverage #

Author: Jack Warren

Build Status Pub

Manipulate coverage files to ignore files matching given patterns. This can be used to exclude generated files from coverage reports, and is language-agnostic.

Remove files with paths matching given PATTERNs from the FILE
-f, --file=<FILE>         the target file to manipulate
-r, --remove=<PATTERN>    a pattern of paths to exclude from coverage
-h, --help                show this help

The patterns are used to construct RegExp objects, so -r '.g.dart$' becomes RegExp('.g.dart$'). Multiple patterns may be provided by either providing -r multiple times or by using comma-separation, like -r '.g.dart$', 'main.dart'. When evaluating a file's entry in the, if its path matches any pattern it will be excluded. The paths are relative from the project's root.

If a file is not provided via thr -f flag, stdin input is run through the program's filter and is sent to stdout.

Example #

Suppose you want to remove generated files ending in .g.dart from code coverage reports:

  • We'll target the file in the coverage directory
  • We'll use .g.dart$ as the pattern

If pub global scripts are on your path, you can use the following:

remove_from_coverage -f coverage/ -r '.g.dart$'

Otherwise, you can use the following:

pub global run remove_from_coverage:remove_from_coverage -f coverage/ -r '.g.dart$'

An entire example project where remove_from_coverage is useful exists in the example directory of the package.

How it works #

Code coverage reports are generated from an file, which is generated by test coverage packages.

In some cases, it might make sense to have some files excluded from code coverage reports, like generated or experimental files. Some generators of files might support excluding files, but remove_from_coverage provides another route: after a file has been generated, it can be manipulated to remove data relating to ignored files.

This means that remove_from_coverage is agnostic to both how the file is generated or how it is used. It may be used for non-Dart projects, and it has been tested on both Linux and Windows.

1.0.7 #

  • Fix package definition error in example directory

1.0.6 #

  • Include example directory in analysis
  • Add example directory dependencies as root package dev_dependencies

1.0.5 #

  • Remove some notes from 1.0.4 that were based on misunderstanding of other packages' functionality

1.0.4 #

  • Documentation updates to direct users to double-check that they can't directly exclude files during coverage generation

1.0.3 #

  • Attempt to support 2.1.0 to enable automatic deployment

1.0.2 #

  • Update automatic deployment to record errors better

1.0.1 #

  • Update

1.0.0 #

  • Initial version


Example Project #

Given here is an example project that can use remove_from_coverage to fix its coverage data in coverage/

Project Explanation #

In lib, name.dart contains a Name class that stores a first, middle, and last name. Since we want Name to be an immutable value class, it makes use of the excellent built_value package to automatically generate a toString method, == operator, and builder.

built_value does its code generation in another file, name.g.dart, using Dart's part mechanic to link it to name.dart.

We can trigger the code generation with the following command:

pub run build_runner build

In test, name_test.dart covers the functionality we desire from this package.

The Problem #

Our test in name_test.dart is sufficient for our testing of name.dart, but code coverage packages would also evaluate the coverage of name.g.dart. There might be cases where we'd want to test everything in that generated file, but if we don't, that generated file might have very few lines covered.

Suppose we use the test_coverage package to generate coverage statistics:

pub run test_coverage

We're told that the code coverage is only 46% and the output file is saved to the coverage directory.

The Solution #

We can use remove_from_coverage to strip out undesired data from the file, so that any reports generated from it will properly reflect the coverage.

If pub global scripts are on your path, you can use the following:

remove_from_coverage -f coverage/ -r '.g.dart$'

Otherwise, you can use the following:

pub global run remove_from_coverage:remove_from_coverage -f coverage/ -r '.g.dart$'

Now, shows only the files we desire, so any reports/percentages generates from it will now be correct.

In general, note that generated files should not be added to source control as I've done here. I added name.g.dart and .test_coverage.dart so the example can be browsed online.

Similarly, the entire coverage directory is not often necessary to add to source control.

Use this package as an executable

1. Install it

You can install the package from the command line:

$ pub global activate remove_from_coverage

2. Use it

The package has the following executables:

$ remove_from_coverage
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 19, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
args ^1.4.4 1.5.2
Dev dependencies
build_runner ^1.0.0
built_value ^6.4.0
built_value_generator ^6.4.0
pedantic ^1.0.0
test ^1.0.0
test_coverage ^0.2.0