-
Notifications
You must be signed in to change notification settings - Fork 3
/
coverage.sh
executable file
·124 lines (102 loc) · 3.04 KB
/
coverage.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
set -e # exit on error
# Functions
function forge_coverage() {
echo 'Running Forge coverage'
# reduces the amount of tests in fuzzing, so coverage runs faster
export FOUNDRY_PROFILE=coverage
export CURRENT_PACKAGE=$(basename "$PWD")
# generates lcov.info
forge coverage --report lcov
# Initialize variables
current_file=""
lines_found=0
lines_hit=0
sed "s/\/.*$CURRENT_PACKAGE.//g" lcov.info > lcov-forge.info
sed -i -e "s/\.\.contracts\//contracts\//g" lcov-forge.info
}
function hardhat_coverage() {
echo 'Running Hardhat coverage'
# generates coverage/lcov.info
COVERAGE=true yarn hardhat coverage
# Foundry uses relative paths but Hardhat uses absolute paths.
# Convert absolute paths to relative paths for consistency.
sed -i -e "s/\/.*$CURRENT_PACKAGE.//g" coverage/lcov.info
mv coverage/lcov.info lcov-hardhat.info
}
function merge() {
echo 'Merging coverage files...'
if [[ "$1" == 'forge' ]]; then
lcov \
--rc lcov_branch_coverage=1 \
--rc derive_function_end_line=0 \
--add-tracefile lcov-forge.info \
--output-file lcov-merged.info
elif [[ "$1" == 'hardhat' ]]; then
lcov \
--rc lcov_branch_coverage=1 \
--rc derive_function_end_line=0 \
--add-tracefile lcov-forge.info \
--output-file lcov-merged.info
elif [[ "$1" == 'all' ]]; then
lcov \
--rc lcov_branch_coverage=1 \
--rc derive_function_end_line=0 \
--add-tracefile lcov-forge.info \
--add-tracefile lcov-hardhat.info \
--output-file lcov-merged.info
fi
}
function filter_and_display() {
echo 'Filtering report...'
if [[ $CURRENT_PACKAGE == "vault" ]]; then
# Filter out node_modules, test, and mock files
lcov \
--rc lcov_branch_coverage=1 \
--ignore-errors unused \
--rc derive_function_end_line=0 \
--remove lcov-merged.info \
"*node_modules*" "*test*" "*Mock*" \
--output-file lcov-filtered.info
else
# Filter out node_modules, test, mock files and vault contracts
lcov \
--rc lcov_branch_coverage=1 \
--ignore-errors unused \
--rc derive_function_end_line=0 \
--remove lcov-merged.info \
"*node_modules*" "*test*" "*Mock*" "*/vault/*" \
--output-file lcov-filtered.info
fi
echo 'Generating summary...'
# Generate summary
lcov \
--rc lcov_branch_coverage=1 \
--rc derive_function_end_line=0 \
--list lcov-filtered.info
echo 'Display!'
# Open more granular breakdown in browser
rm -rf coverage-genhtml/
genhtml \
--rc lcov_branch_coverage=1 \
--rc derive_function_end_line=0 \
--output-directory coverage-genhtml \
lcov-filtered.info
open coverage-genhtml/index.html
}
# Script
if [[ "$1" == 'forge' ]]; then
forge_coverage
elif [[ "$1" == 'hardhat' ]]; then
hardhat_coverage
elif [[ "$1" == 'all' ]]; then
forge_coverage
hardhat_coverage
else
echo 'Usage: ./coverage.sh [forge | hardhat | all]'
exit 1
fi
merge "$1"
filter_and_display
# Delete temp files
rm -rf lcov-*.info lcov-*.info-e coverage/ lcov.info coverage.json