#cargo #coverage #subcommand #testing

bin+lib cargo-llvm-cov

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage)

31 releases

0.4.0 May 12, 2022
0.3.0 Apr 8, 2022
0.2.4 Mar 18, 2022
0.1.13 Dec 14, 2021
0.1.0-alpha.2 Feb 12, 2021

#31 in Cargo plugins

Download history 752/week @ 2022-01-27 804/week @ 2022-02-03 750/week @ 2022-02-10 1066/week @ 2022-02-17 649/week @ 2022-02-24 916/week @ 2022-03-03 1116/week @ 2022-03-10 1688/week @ 2022-03-17 2624/week @ 2022-03-24 2093/week @ 2022-03-31 2561/week @ 2022-04-07 3817/week @ 2022-04-14 3549/week @ 2022-04-21 2028/week @ 2022-04-28 2745/week @ 2022-05-05 3508/week @ 2022-05-12

12,710 downloads per month
Used in cargo-run-bin

Apache-2.0 OR MIT

135KB
2.5K SLoC

cargo-llvm-cov

crates.io license rustc build status

Cargo subcommand to easily use LLVM source-based code coverage.

This is a wrapper around rustc -C instrument-coverage and provides:

  • Generate very precise coverage data. (line coverage and region coverage)
  • Support cargo test, cargo run, and cargo nextest with command-line interface compatible with cargo.
  • Support for proc-macro, including coverage of UI tests.
  • Support for doc tests. (this is currently optional and requires nightly, see #2 for more)
  • Fast because it does not introduce extra layers between rustc, cargo, and llvm-tools.

Table of Contents:

Usage

Basic usage

Click to show a complete list of options
$ cargo llvm-cov --help
cargo-llvm-cov
Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).

Use -h for short descriptions and --help for more details.

USAGE:
    cargo llvm-cov [OPTIONS] [-- <ARGS>...] [SUBCOMMAND]

ARGS:
    <ARGS>...
            Arguments for the test binary

OPTIONS:
        --json
            Export coverage data in "json" format

            If --output-path is not specified, the report will be printed to stdout.

            This internally calls `llvm-cov export -format=text`. See
            <https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-export> for more.

        --lcov
            Export coverage data in "lcov" format

            If --output-path is not specified, the report will be printed to stdout.

            This internally calls `llvm-cov export -format=lcov`. See
            <https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-export> for more.

        --text
            Generate coverage report in “text” format

            If --output-path or --output-dir is not specified, the report will be printed to stdout.

            This internally calls `llvm-cov show -format=text`. See
            <https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-show> for more.

        --html
            Generate coverage report in "html" format

            If --output-dir is not specified, the report will be generated in `target/llvm-cov/html`
            directory.

            This internally calls `llvm-cov show -format=html`. See
            <https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-show> for more.

        --open
            Generate coverage reports in "html" format and open them in a browser after the
            operation.

            See --html for more.

        --summary-only
            Export only summary information for each file in the coverage data

            This flag can only be used together with either --json or --lcov.

        --output-path <PATH>
            Specify a file to write coverage data into.

            This flag can only be used together with --json, --lcov, or --text. See --output-dir for
            --html and --open.

        --output-dir <DIRECTORY>
            Specify a directory to write coverage report into (default to `target/llvm-cov`).

            This flag can only be used together with --text, --html, or --open. See also
            --output-path.

        --failure-mode <any|all>
            Fail if `any` or `all` profiles cannot be merged (default to `any`)

        --ignore-filename-regex <PATTERN>
            Skip source code files with file paths that match the given regular expression

        --hide-instantiations
            Hide instantiations from report

        --no-cfg-coverage
            Unset cfg(coverage), which is enabled when code is built using cargo-llvm-cov

        --no-cfg-coverage-nightly
            Unset cfg(coverage_nightly), which is enabled when code is built using cargo-llvm-cov
            and nightly compiler

        --no-report
            Run tests, but don't generate coverage report

        --fail-under-lines <MIN>
            Exit with a status of 1 if the total line coverage is less than MIN percent

        --show-missing-lines
            Show lines with no coverage

        --include-build-script
            Include build script in coverage report

        --doctests
            Including doc tests (unstable)

            This flag is unstable. See <https://github.com/taiki-e/cargo-llvm-cov/issues/2> for
            more.

        --no-run
            Generate coverage report without running tests

        --no-fail-fast
            Run all tests regardless of failure

    -q, --quiet
            Display one character per test instead of one line

        --lib
            Test only this package's library unit tests

        --bin <NAME>
            Test only the specified binary

        --bins
            Test all binaries

        --example <NAME>
            Test only the specified example

        --examples
            Test all examples

        --test <NAME>
            Test only the specified test target

        --tests
            Test all tests

        --bench <NAME>
            Test only the specified bench target

        --benches
            Test all benches

        --all-targets
            Test all targets

        --doc
            Test only this library's documentation (unstable)

            This flag is unstable because it automatically enables --doctests flag. See
            <https://github.com/taiki-e/cargo-llvm-cov/issues/2> for more.

    -p, --package <SPEC>
            Package to run tests for

        --workspace
            Test all packages in the workspace

            [aliases: all]

        --exclude <SPEC>
            Exclude packages from both the test and report

        --exclude-from-test <SPEC>
            Exclude packages from the test (but not from the report)

        --exclude-from-report <SPEC>
            Exclude packages from the report (but not from the test)

    -j, --jobs <N>
            Number of parallel jobs, defaults to # of CPUs

    -r, --release
            Build artifacts in release mode, with optimizations

        --profile <PROFILE-NAME>
            Build artifacts with the specified profile

    -F, --features <FEATURES>
            Space or comma separated list of features to activate

        --all-features
            Activate all available features

        --no-default-features
            Do not activate the `default` feature

        --target <TRIPLE>
            Build for the target triple

            When this option is used, coverage for proc-macro and build script will not be displayed
            because cargo does not pass RUSTFLAGS to them.

    -v, --verbose
            Use verbose output

            Use -vv (-vvv) to propagate verbosity to cargo.

        --color <WHEN>
            Coloring

            [possible values: auto, always, never]

        --remap-path-prefix
            Use --remap-path-prefix for workspace root

            Note that this does not fully compatible with doctest.

        --manifest-path <PATH>
            Path to Cargo.toml

        --frozen
            Require Cargo.lock and cache are up to date

        --locked
            Require Cargo.lock is up to date

        --offline
            Run without accessing the network

    -Z <FLAG>
            Unstable (nightly-only) flags to Cargo

    -h, --help
            Print help information

    -V, --version
            Print version information

SUBCOMMANDS:
    run
            Run a binary or example and generate coverage report
    show-env
            Output the environment set by cargo-llvm-cov to build Rust projects
    clean
            Remove artifacts that cargo-llvm-cov has generated in the past
    nextest
            Run tests with cargo nextest
    help
            Print this message or the help of the given subcommand(s)

By default, run tests (via cargo test), and print the coverage summary to stdout.

cargo llvm-cov

To run cargo run instead of cargo test, use run subcommand.

cargo llvm-cov run

With html report (the report will be generated to target/llvm-cov/html directory):

cargo llvm-cov --html
open target/llvm-cov/html/index.html

or

cargo llvm-cov --open

With plain text report (if --output-path is not specified, the report will be printed to stdout):

cargo llvm-cov --text | less -R

With json report (if --output-path is not specified, the report will be printed to stdout):

cargo llvm-cov --json --output-path cov.json

With lcov report (if --output-path is not specified, the report will be printed to stdout):

cargo llvm-cov --lcov --output-path lcov.info

You can get a coverage report in a different format based on the results of a previous run by using --no-run.

cargo llvm-cov --html          # run tests and generate html report
cargo llvm-cov --no-run --lcov # generate lcov report

Merge coverages generated under different test conditions

You can merge the coverages generated under different test conditions by using --no-report and --no-run.

cargo llvm-cov clean --workspace # remove artifacts that may affect the coverage results
cargo llvm-cov --no-report --features a
cargo llvm-cov --no-report --features b
cargo llvm-cov --no-run --lcov # generate report without tests

Get coverage of external tests

cargo test, cargo run, and cargo nextest are available as builtin, but cargo-llvm-cov can also be used for arbitrary binaries built using cargo (including other cargo subcommands or external tests that use make, xtask, etc.)

source <(cargo llvm-cov show-env --export-prefix) # Set the environment variables needed to get coverage.
cargo llvm-cov clean --workspace # remove artifacts that may affect the coverage results
cargo build # build rust binaries
# commands using binaries in target/debug/*, including `cargo test` and other cargo subcommands
# ...
cargo llvm-cov --no-run --lcov # generate report without tests

Exclude file from coverage

To exclude specific file patterns from the report, use the --ignore-filename-regex option.

cargo llvm-cov --open --ignore-filename-regex build

Exclude function from coverage

To exclude the specific function from coverage, use the #[no_coverage] attribute.

Since #[no_coverage] is unstable, it is recommended to use it together with cfg(coverage) or cfg(coverage_nightly) set by cargo-llvm-cov.

// cfg(coverage) is true when code is built using cargo-llvm-cov
// cfg(coverage_nightly) is true when code is built using cargo-llvm-cov and nightly compiler
#![cfg_attr(coverage_nightly, feature(no_coverage))]

#[cfg_attr(coverage_nightly, no_coverage)]
fn exclude_from_coverage() {
    // ...
}

Continuous Integration

Here is an example of GitHub Actions workflow that uploads coverage to Codecov.

name: Coverage

on: [pull_request, push]

jobs:
  coverage:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install Rust
        run: rustup toolchain install stable --component llvm-tools-preview
      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
      - name: Generate code coverage
        run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v1
        with:
          token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
          files: lcov.info
          fail_ci_if_error: true

Note: Currently, only line coverage is available on Codecov. This is because -C instrument-coverage does not support branch coverage and Codecov does not support region coverage. See also #8, #12, and #20.

Installation

Prerequisites

cargo-llvm-cov requires llvm-tools-preview:

rustup component add llvm-tools-preview

Running cargo-llvm-cov requires rustc 1.60+.

From source

cargo install cargo-llvm-cov

Currently, installing cargo-llvm-cov requires rustc 1.54+.

From prebuilt binaries

You can download prebuilt binaries from the Release page. Prebuilt binaries are available for macOS, Linux (gnu and musl), and Windows (static executable).

On GitHub Actions

You can use taiki-e/install-action to install prebuilt binaries on Linux, macOS, and Windows. This makes the installation faster and may avoid the impact of problems caused by upstream changes.

- uses: taiki-e/install-action@cargo-llvm-cov

When used with nextest:

- uses: taiki-e/install-action@cargo-llvm-cov
- uses: taiki-e/install-action@nextest

Via Homebrew

You can install cargo-llvm-cov using Homebrew tap on macOS and Linux:

brew install taiki-e/tap/cargo-llvm-cov

Via AUR (ArchLinux)

You can install cargo-llvm-cov from AUR:

paru -S cargo-llvm-cov

Note: AUR package is maintained by community, not maintainer of cargo-llvm-cov.

Known limitations

See also the code-coverage-related issues reported in rust-lang/rust.

Related Projects

  • cargo-hack: Cargo subcommand to provide various options useful for testing and continuous integration.
  • cargo-minimal-versions: Cargo subcommand for proper use of -Z minimal-versions.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~4–5.5MB
~116K SLoC