From e4f8cdaea5554c2de1d80273e57035fea9d1a424 Mon Sep 17 00:00:00 2001 From: bitfalt <75431447+bitfalt@users.noreply.github.com> Date: Tue, 3 Sep 2024 15:08:33 -0600 Subject: [PATCH] [feat] Add testing to CI pipeline --- .github/workflows/contract_checks.yml | 29 ++++++++++++++++++++++++++ contracts/Scarb.toml | 3 +++ scripts/run_tests.sh | 30 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 scripts/run_tests.sh diff --git a/.github/workflows/contract_checks.yml b/.github/workflows/contract_checks.yml index da20175..363999b 100644 --- a/.github/workflows/contract_checks.yml +++ b/.github/workflows/contract_checks.yml @@ -53,3 +53,32 @@ jobs: run: | cd contracts scarb check + + run-tests: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Check for changes + uses: dorny/paths-filter@v3 + id: changes + with: + filters: | + contracts_dir: + - 'contracts/src/**' + + - name: Install scarb + if: steps.changes.outputs.contracts_dir == 'true' + run: | + cd contracts + curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh -s -- -v 2.6.5 + + - name: Install snfoundry + uses: foundry-rs/setup-snfoundry@v3 + with: + starknet-foundry-version: "0.20.1" + + - name: Run Cairo tests + id: cairo_tests + run: bash scripts/run_tests.sh diff --git a/contracts/Scarb.toml b/contracts/Scarb.toml index 4e7f8b2..bfd50e9 100644 --- a/contracts/Scarb.toml +++ b/contracts/Scarb.toml @@ -12,5 +12,8 @@ openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", ta [dev-dependencies] snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.20.1" } +[scripts] +test = "snforge test" + [[target.starknet-contract]] casm = true diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh new file mode 100644 index 0000000..88a611e --- /dev/null +++ b/scripts/run_tests.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +set -e + +tests_output=$(cd contracts && scarb run test || true) + +# Get summary information +echo "::group::Summary" + +tests_passed=$(echo "$tests_output" | awk '/Tests:/ {print $2}') +tests_failed=$(echo "$tests_output" | awk '/Tests:/ {print $4}') +skipped_count=$(echo "$tests_output" | awk '/Tests:/ {print $6}') +ignored_count=$(echo "$tests_output" | awk '/Tests:/ {print $6}') +filtered_out_count=$(echo "$tests_output" | awk '/Tests:/ {print $6}') + +echo "Tests passed: $tests_passed" +echo "Tests failed: $tests_failed" +echo "Skipped: $skipped_count" +echo "Ignored: $ignored_count" +echo "Filtered out: $filtered_out_count" +echo "::endgroup::" + +# Check for failed tests + +if [ "$tests_failed" -gt 0 ]; then + failed_tests=$(echo "$tests_output" | awk '/Failures:/{flag=1;next}/^\s*$/{flag=0}flag') + echo "::error::Tests failed:" + echo "$failed_tests" + exit 1 +fi \ No newline at end of file