Skip to content

Commit b9810b2

Browse files
committed
test: combined test coverage reports from Hardhat and Foundry
1 parent 889cf63 commit b9810b2

File tree

4 files changed

+62
-18
lines changed

4 files changed

+62
-18
lines changed

.github/workflows/contracts-testing.yml

+8-16
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,22 @@ jobs:
5959
key: ${{ runner.os }}-build-${{ secrets.CACHE_VERSION }}-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}
6060
restore-keys: |
6161
${{ runner.os }}-build-${{ secrets.CACHE_VERSION }}-${{ env.cache-name }}-
62-
62+
6363
- name: Install contracts dependencies
64-
run: |
65-
yarn workspace @kleros/kleros-v2-contracts install
64+
run: yarn workspace @kleros/kleros-v2-contracts install
6665

6766
- name: Install Foundry
6867
uses: foundry-rs/foundry-toolchain@8f1998e9878d786675189ef566a2e4bf24869773 # v1.2.0
6968
with:
7069
version: nightly
7170

72-
- name: Run Foundry tests
73-
run: forge test -vvv
74-
working-directory: contracts
75-
76-
- name: Compile
77-
run: |
78-
yarn hardhat compile
79-
working-directory: contracts
80-
81-
- name: Run Hardhat tests with coverage
82-
run: |
83-
yarn hardhat coverage --solcoverjs ./.solcover.js --temp artifacts --testfiles './test/**/*.ts' --show-stack-traces
84-
working-directory: contracts
71+
- name: Install lcov
72+
run: sudo apt-get install -y lcov
8573

74+
- name: Run Hardhat and Foundry tests with coverage
75+
run: yarn coverage
76+
working-directory: contracts
77+
8678
- name: Upload a build artifact
8779
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
8880
with:

contracts/.solcover.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const shell = require("shelljs");
55
// The environment variables are loaded in hardhat.config.ts
66

77
module.exports = {
8-
istanbulReporter: ["html"],
8+
istanbulReporter: ["lcov"],
99
onCompileComplete: async function (_config) {
1010
await run("typechain");
1111
},
@@ -14,7 +14,7 @@ module.exports = {
1414
shell.rm("-rf", "./artifacts");
1515
shell.rm("-rf", "./typechain");
1616
},
17-
skipFiles: ["mocks", "test"],
17+
skipFiles: ["test", "token", "kleros-v1", "proxy/mock", "gateway/mock", "rng/mock"],
1818
mocha: {
1919
timeout: 20000,
2020
grep: "@skip-on-coverage", // Find everything with this tag

contracts/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"clean": "hardhat clean",
2323
"check": "hardhat check",
2424
"test": "TS_NODE_TRANSPILE_ONLY=1 hardhat test",
25+
"coverage": "scripts/coverage.sh",
2526
"start": "hardhat node --tags nop",
2627
"start-local": "hardhat node --tags Arbitration,HomeArbitrable --hostname 0.0.0.0",
2728
"deploy": "hardhat deploy",

contracts/scripts/coverage.sh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
3+
set -e # exit on error
4+
5+
rm -rf coverage
6+
mkdir -p coverage
7+
8+
# Generate the Forge coverage report
9+
forge coverage --report summary --report lcov --report-file coverage/lcov-forge.info
10+
11+
# Generate the Hardhat coverage report
12+
yarn clean
13+
yarn hardhat coverage --solcoverjs ./.solcover.js --temp artifacts --show-stack-traces --testfiles "test/**/*.ts"
14+
mv coverage/lcov.info coverage/lcov-hardhat.info
15+
16+
# Make the Hardhat report paths relative for consistency with Forge coverage report
17+
sed -i -e 's/\/.*\/kleros-v2\/contracts\///g' coverage/lcov-hardhat.info
18+
19+
# Merge the two reports
20+
lcov \
21+
--ignore-errors format \
22+
--ignore-errors inconsistent \
23+
--rc max_message_count=3 \
24+
--rc derive_function_end_line=0 \
25+
--rc branch_coverage=1 \
26+
--add-tracefile coverage/lcov-hardhat.info \
27+
--add-tracefile coverage/lcov-forge.info \
28+
--output-file coverage/merged-lcov.info
29+
30+
# Filter out unnecessary contracts from the report
31+
lcov \
32+
--ignore-errors inconsistent \
33+
--rc max_message_count=3 \
34+
--rc branch_coverage=1 \
35+
--rc derive_function_end_line=0 \
36+
--remove coverage/merged-lcov.info \
37+
--output-file coverage/filtered-lcov.info \
38+
"../node_modules" "src/test" "src/token" "src/kleros-v1" "src/proxy/mock" "src/gateway/mock" "src/rng/mock"
39+
40+
# Open more granular breakdown in browser
41+
if [ "$CI" != "true" ]; then
42+
# Generate the HTML report
43+
genhtml coverage/filtered-lcov.info \
44+
--ignore-errors inconsistent \
45+
--rc branch_coverage=1 \
46+
--rc max_message_count=3 \
47+
-o coverage \
48+
--ignore-errors category \
49+
--ignore-errors format
50+
open coverage/index.html
51+
fi

0 commit comments

Comments
 (0)