Skip to content

Commit

Permalink
ci: add code coverage report (#42)
Browse files Browse the repository at this point in the history
* ci: add coverage

* chore: use make to generate html coverage report
  • Loading branch information
spalen0 authored Oct 18, 2024
1 parent 7abcf9a commit 9ca7672
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
58 changes: 58 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: coverage

on:
push:
branches:
- master
- develop
pull_request:

env:
FOUNDRY_PROFILE: ci
ETH_RPC_URL: ${{ secrets.ETH_RPC_URL }}

jobs:
coverage:
strategy:
fail-fast: true

name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Install lcov
run: sudo apt-get -y install lcov

- name: Run generate html report
run: make coverage-html

- name: Generate summary report
run: |
lcov --summary lcov.info > coverage-report/summary.md
sed -i '1d' coverage-report/summary.md
sed -i '/Message summary:/,$d' coverage-report/summary.md
- name: Upload coverage report artifact
uses: actions/upload-artifact@v4
with:
name: CoverageReport # Artifact name
path: coverage-report # Directory containing files to upload

- name: Add comment to PR
if: github.event_name == 'pull_request' && env.GH_TOKEN != ''
run: gh pr comment $PR_NUMBER --body-file coverage-report/summary.md
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
PR_NUMBER: ${{ github.event.number }}

- name: Publish coverage in build summary
run: cat coverage-report/summary.md >> $GITHUB_STEP_SUMMARY
shell: bash
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ env:
FOUNDRY_PROFILE: ci

jobs:
check:
test:
strategy:
fail-fast: true

Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,16 @@ coverage :; forge coverage --fork-url ${FORK_URL}
coverage-report :; forge coverage --report lcov --fork-url ${FORK_URL}
coverage-debug :; forge coverage --report debug --fork-url ${FORK_URL}

coverage-html:
@echo "Running coverage..."
forge coverage --report lcov --fork-url ${FORK_URL}
@if [ "`uname`" = "Darwin" ]; then \
lcov --ignore-errors inconsistent --remove lcov.info 'src/test/**' --output-file lcov.info; \
genhtml --ignore-errors inconsistent -o coverage-report lcov.info; \
else \
lcov --remove lcov.info 'src/test/**' --output-file lcov.info; \
genhtml -o coverage-report lcov.info; \
fi
@echo "Coverage report generated at coverage-report/index.html"

clean :; forge clean
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ NOTE: Compiler defaults to 8.23 but it can be adjusted in the foundry toml.

## Testing

Due to the nature of the BaseStrategy utilizing an external contract for the majority of its logic, the default interface for any tokenized strategy will not allow proper testing of all functions. Testing of your Strategy should utilize the pre-built [IStrategyInterface](https://github.com/yearn/tokenized-strategy-foundry-mix/blob/master/src/interfaces/IStrategyInterface.sol) to cast any deployed strategy through for testing, as seen in the Setup example. You can add any external functions that you add for your specific strategy to this interface to be able to test all functions with one variable.
Due to the nature of the BaseStrategy utilizing an external contract for the majority of its logic, the default interface for any tokenized strategy will not allow proper testing of all functions. Testing of your Strategy should utilize the pre-built [IStrategyInterface](https://github.com/yearn/tokenized-strategy-foundry-mix/blob/master/src/interfaces/IStrategyInterface.sol) to cast any deployed strategy through for testing, as seen in the Setup example. You can add any external functions that you add for your specific strategy to this interface to be able to test all functions with one variable.

Example:

Expand Down Expand Up @@ -95,6 +95,22 @@ When testing on chains other than mainnet you will need to make sure a valid `CH

To update to a new API version of the TokenizeStrategy you will need to simply remove and reinstall the dependency.

### Test Coverage

Run the following command to generate a test coverage:

```sh
make coverage
```

To generate test coverage report in HTML, you need to have installed [`lcov`](https://github.com/linux-test-project/lcov) and run:

```sh
make coverage-html
```

The generated report will be in `coverage-report/index.html`.

### Deployment

#### Contract Verification
Expand All @@ -115,3 +131,9 @@ This repo uses [GitHub Actions](.github/workflows) for CI. There are three workf
To enable test workflow you need to add the `ETH_RPC_URL` secret to your repo. For more info see [GitHub Actions docs](https://docs.github.com/en/codespaces/managing-codespaces-for-your-organization/managing-encrypted-secrets-for-your-repository-and-organization-for-github-codespaces#adding-secrets-for-a-repository).

If the slither finds some issues that you want to suppress, before the issue add comment: `//slither-disable-next-line DETECTOR_NAME`. For more info about detectors see [Slither docs](https://github.com/crytic/slither/wiki/Detector-Documentation).

### Coverage

If you want to use [`coverage.yml`](.github/workflows/coverage.yml) workflow on other chains than mainnet, you need to add the additional `CHAIN_RPC_URL` secret.

Coverage workflow will generate coverage summary and attach it to PR as a comment. To enable this feature you need to add the [`GH_TOKEN`](.github/workflows/coverage.yml#L53) secret to your Github repo. Token must have permission to "Read and Write access to pull requests". To generate token go to [Github settings page](https://github.com/settings/tokens?type=beta). For more info see [GitHub Access Tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens).

0 comments on commit 9ca7672

Please sign in to comment.