very_good_coverage icon indicating copy to clipboard operation
very_good_coverage copied to clipboard

Add functionality to a command line tool

Open mtwichel opened this issue 4 years ago • 3 comments

I'm excited about where this project could go, and I think there's an opportunity to support local testing as well.

Problem to solve

I had a colleague that had a very difficult time using lcov on Windows. While he could generate a lcov.info file with flutter test --coverage, the lcov project for analyzing code coverage and generating the HTML report is only for Linux like environments (currently I think the only project that support it is this repo which hasn't been updated since 2014). So, I think this nodejs approach would be great not just on Github actions, but for local development as well.

The solution

This would probably take:

  • move the parsing logic into its own repo
  • refactor this repo to use the new package
  • create a new very-good-coverage command-line tool

Curious on everyone's thoughts :)

mtwichel avatar Oct 28 '20 06:10 mtwichel

I agree that this would be a nice feature. But till then I think the following shell script should also do the job. The script is an improved version of this one and adds the possibility to exclude files by using the following dev dependency: Link

The example bash currently excludes json_serializable and frezzed generated files. Obviously this only works on Linux and Mac and requires to install lcov locally:

brew install lcov
#!/usr/bin/env bash

red=$(tput setaf 1)
none=$(tput sgr0)

show_help() {
    printf "usage: $0 [--help] [--report] [--test] [<path to package>]
Script for running all unit and widget tests with code coverage.
(run from root of repo)
where:
    <path to package>
        runs all tests with coverage and reports
    -t, --test
        runs all tests with coverage, but no report
    -r, --report
        generate a coverage report
        (requires lcov, install with Homebrew)
    -e, --exclude
        excludes json_serializable and freezed files
        (requires flutter dev_dependency remove_from_coverage)
    -h, --help
        print this message
"
}

run_tests() {
    if [[ -f "pubspec.yaml" ]]; then
        rm -f coverage/lcov.info
        rm -f coverage/lcov-final.info
        flutter test --no-pub --coverage --test-randomize-ordering-seed random
    else
        printf "\n${red}Error: this is not a Flutter project${none}"
        exit 1
    fi
}

run_report() {
    if [[ -f "coverage/lcov.info" ]]; then
        lcov -r coverage/lcov.info lib/resources/l10n/\* lib/\*/fake_\*.dart \
             -o coverage/lcov-final.info
        genhtml -o coverage coverage/lcov-final.info
        open coverage/index-sort-l.html
    else
        printf "\n${red}Error: no coverage info was generated${none}"
        exit 1
    fi
}

exclude_files() {
    if [[ -f "coverage/lcov.info" ]]; then
        flutter pub run remove_from_coverage -f coverage/lcov.info -r '.freezed.dart$'
        flutter pub run remove_from_coverage -f coverage/lcov.info -r '.g.dart$'
    else
        printf "\n${red}Error: no coverage info was generated${none}"
        exit 1
    fi
}

case $1 in
    -h|--help)
        show_help
        ;;
    -t|--test)
        run_tests
        ;;    
    -r|--report)
        run_report
        ;;
    -e|--exclude)
        exclude_files
        ;;
    *)
        run_tests
        exclude_files
        run_report
        ;;
esac

pauli2406 avatar Jun 12 '21 22:06 pauli2406

Hello, I am a Windows user too and I am using the coverde CLI, it is excellent, the truth is that it helps me a lot and it is even more intuitive than lcov, it is completely written in Dart.

yeikel16 avatar Feb 28 '22 16:02 yeikel16

@yeikel16 That looks like an awesome tool! Thanks for the tip!

mtwichel avatar Mar 01 '22 07:03 mtwichel

Closing this since this functionality should be covered between very_good test and coverde. Feel free to comment if there is any specific aspect you feel is missing.

felangel avatar Dec 06 '22 20:12 felangel