Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(coverage): add --json compatibility to forge coverage --report summary #4071

Closed
Tracked by #8794
sambacha opened this issue Jan 11, 2023 · 2 comments
Closed
Tracked by #8794
Labels
A-extensions Area: extensions Cmd-forge-coverage Command: forge coverage T-feature Type: feature
Milestone

Comments

@sambacha
Copy link
Contributor

Component

Forge

Describe the feature you would like

Coverage Snapshot

i.e. gas-snapshot but for coverage.

Snapshot

This script records line and function coverage in the format:

lineCoverage result 66.7 %
functionCoverage result 50.0 %

Coverage Script

#!/usr/bin/env bash

set -euo pipefail

mkdir -p "$PWD"/.coverage/

coverageSnapshotPhase() 
{
    mkdir -p "$PWD"/.coverage/ 
    genhtml lcov.info -o  --branch-coverage "$PWD"/.coverage > log
    # genhtml lcov.info $lcovExtraTraceFiles -o $out/.coverage > log
    lineCoverage="$(sed 's/.*lines\.*: \([0-9\.]\+\)%.*/\1/; t ; d' log)"
    functionCoverage="$(sed 's/.*functions\.*: \([0-9\.]\+\)%.*/\1/; t ; d' log)"
        if [ -z "$lineCoverage" -o -z "$functionCoverage" ]; then
            echo "⛔︎ Failed to get coverage statistics"
            exit 1
        fi
    echo "lineCoverage result $lineCoverage %" >> "$PWD"/.coverage/snapshot
    echo "functionCoverage result $functionCoverage %" >> "$PWD"/.coverage/snapshot
    touch "$PWD"/.coverage/CI_TIMESTAMP
    date +"%Y%m%d%H%M%S" >> "$PWD"/.coverage/CI_TIMESTAMP
}

coverageSnapshotPhase

echo "Snapshot recorded"

FOUND_WARNINGS=$(grep "\d+ result" -Po .coverage/snapshot | sed 's/ result//g')
if (( $(echo "$FOUND_WARNINGS <= 50" | bc -l) )) ; then
  COLOR=red
elif (( $(echo "$FOUND_WARNINGS > 60" | bc -l) )); then
  COLOR=green
else
  COLOR=orange
fi

echo "$FOUND_WARNINGS"

rm log
sleep 1
exit 0

This does not follow the gas snapshot format which tbh I dislike as it is difficult to regex against.

Additional context

Note: I flatten the contract to run coverage reporting against, ergo this script works for that context.

@sambacha sambacha added the T-feature Type: feature label Jan 11, 2023
@zerosnacks zerosnacks added A-gas-snapshots Area: gas snapshotting/reporting Cmd-forge-coverage Command: forge coverage A-extensions Area: extensions and removed A-gas-snapshots Area: gas snapshotting/reporting labels Jun 28, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@zerosnacks zerosnacks changed the title Feat: Coverage Snapshot feat(coverage): add --json compatibility to forge coverage --report summary Oct 10, 2024
@zerosnacks
Copy link
Member

I think this can best be captured by adding compatibility with the --json flag so it serializes the summary table

| File                 | % Lines       | % Statements  | % Branches    | % Funcs       |
|----------------------|---------------|---------------|---------------|---------------|
| script/Counter.s.sol | 0.00% (0/3)   | 0.00% (0/3)   | 100.00% (0/0) | 0.00% (0/2)   |
| src/Counter.sol      | 100.00% (2/2) | 100.00% (2/2) | 100.00% (0/0) | 100.00% (2/2) |
| Total                | 40.00% (2/5)  | 40.00% (2/5)  | 100.00% (0/0) | 50.00% (2/4)  |

into a JSON structure

Right now passing the --json flag is incompatible

@zerosnacks
Copy link
Member

Completed in #9111

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-extensions Area: extensions Cmd-forge-coverage Command: forge coverage T-feature Type: feature
Projects
Status: Done
Development

No branches or pull requests

2 participants