Skip to content

feat: Improve test coverage and Documentation #12

feat: Improve test coverage and Documentation

feat: Improve test coverage and Documentation #12

Workflow file for this run

name: Task - Test Solidity
on:
workflow_call:
push:
paths:
- 'solidity/**'
pull_request:
paths:
- 'solidity/**'
env:
FOUNDRY_PROFILE: ci
jobs:
check:
env:
working-directory: ./solidity
strategy:
fail-fast: true
name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
fetch-depth: 0
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- name: Run Forge build
working-directory: ${{ env.working-directory }}
run: |
forge --version
forge build --sizes
id: build
- name: Run Forge tests
working-directory: ${{ env.working-directory }}
run: |
forge test -vvv
id: run-tests
- name: Run Forge tests with gas report
working-directory: ${{ env.working-directory }}
run: |
forge test --match-contract PragmaDecoderGasTest -vvv --gas-report > gas_report.txt
id: gas-report
- name: Parse current gas report
working-directory: ${{ env.working-directory }}
run: |
echo "current_gas_results=$(grep -A 6 "Gas used for" gas_report.txt | awk '{print $NF}' | tr '\n' ',' | sed 's/,$//')" >> $GITHUB_ENV
- name: Get previous gas results
working-directory: ${{ env.working-directory }}
run: |
git checkout -b temp-branch
git reset --hard origin/${{ github.base_ref || 'main' }}
if [ -f previous_gas_results.txt ]; then
echo "previous_gas_results=$(cat previous_gas_results.txt)" >> $GITHUB_ENV
else
echo "previous_gas_results=" >> $GITHUB_ENV
fi
- name: Compare gas results
id: compare-gas
working-directory: ${{ env.working-directory }}
run: |
{
echo 'gas_summary<<EOF'
echo "Gas Report Summary:"
echo '```'
current_results=$(grep "Gas used for" gas_report.txt | sed 's/.*Gas used for \(.*\) update: \(.*\)/\1,\2/')
IFS=$'\n' read -d '' -r -a current_array <<< "$current_results"
if [ -f previous_gas_results.txt ]; then
previous_results=$(cat previous_gas_results.txt)
IFS=$'\n' read -d '' -r -a previous_array <<< "$previous_results"
fi
for i in "${!current_array[@]}"; do
IFS=',' read -r test current <<< "${current_array[$i]}"
echo "Gas used for $test update: $current"
if [ ${#previous_array[@]} -gt $i ]; then
IFS=',' read -r prev_test previous <<< "${previous_array[$i]}"
if [ "$test" = "$prev_test" ]; then
diff=$((current - previous))
if [ $diff -gt 0 ]; then
echo "Change: :red_circle: +$diff ($(echo "scale=2; $diff * 100 / $previous" | bc)% increase)"
elif [ $diff -lt 0 ]; then
echo "Change: :green_circle: $diff ($(echo "scale=2; -$diff * 100 / $previous" | bc)% decrease)"
else
echo "Change: :white_circle: No change"
fi
else
echo "Change: N/A (Test name mismatch)"
fi
else
echo "Change: N/A (First run)"
fi
echo ""
done
echo '```'
echo 'EOF'
} >> $GITHUB_OUTPUT
# Update previous_gas_results.txt
grep "Gas used for" gas_report.txt | sed 's/.*Gas used for \(.*\) update: \(.*\)/\1,\2/' > previous_gas_results.txt
- name: Comment PR
uses: actions/github-script@v6
if: github.event_name == 'pull_request'
env:
SUMMARY: ${{ steps.compare-gas.outputs.gas_summary }}
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: process.env.SUMMARY
})
- name: Update gas results
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
working-directory: ${{ env.working-directory }}
run: |
echo "$current_gas_results" > previous_gas_results.txt
git config user.name github-actions
git config user.email github-actions@github.com
git add previous_gas_results.txt
git commit -m "Update gas results" || echo "No changes to commit"
git push origin main
- name: Upload gas report as artifact
uses: actions/upload-artifact@v3
with:
name: gas-report
path: ${{ env.working-directory }}/gas_report.txt