forked from KCEE0901/trustchain-escrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-contract.sh
More file actions
executable file
·138 lines (122 loc) · 4.73 KB
/
Copy pathtest-contract.sh
File metadata and controls
executable file
·138 lines (122 loc) · 4.73 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
# test-contract.sh
#
# Runs the Soroban contract test suite with coverage, gas profiling, and optional fuzzing.
# Usage: bash scripts/test-contract.sh [--fuzz] [--coverage] [--gas] [--ci] [--build]
#
# Flags:
# --fuzz Run cargo-fuzz targets (requires nightly + cargo-fuzz)
# --coverage Generate llvm-cov HTML + lcov report
# --gas Emit per-test instruction counts via SOROBAN_GAS_PROFILE=1
# --ci Write GitHub Actions step summaries and set output variables
# --build Build optimized WASM artifacts (release profile, opt-level=z)
set -euo pipefail
FUZZ=false
COVERAGE=false
GAS=false
CI_MODE=false
BUILD=false
for arg in "$@"; do
case $arg in
--fuzz) FUZZ=true ;;
--coverage) COVERAGE=true ;;
--gas) GAS=true ;;
--ci) CI_MODE=true ;;
--build) BUILD=true ;;
esac
done
CONTRACTS=(escrow_contract insurance_contract)
ARTIFACTS_DIR="${ARTIFACTS_DIR:-test-artifacts}"
mkdir -p "$ARTIFACTS_DIR"
echo ""
echo "🧪 Running Soroban Contract Tests"
echo "=================================="
echo ""
for CONTRACT in "${CONTRACTS[@]}"; do
CONTRACT_DIR="contracts/$CONTRACT"
echo "── $CONTRACT ──────────────────────────────"
# Format check
echo "📐 Checking format…"
(cd "$CONTRACT_DIR" && cargo fmt -- --check)
echo " ✅ Format OK"
# Clippy
echo "🔍 Running Clippy…"
(cd "$CONTRACT_DIR" && cargo clippy -- -D warnings)
echo " ✅ Clippy OK"
# Tests (with optional gas profiling)
echo "🧪 Running tests…"
TEST_OUT="../../$ARTIFACTS_DIR/${CONTRACT}-test.txt"
if [ "$GAS" = true ]; then
echo " ⛽ Gas profiling enabled"
(cd "$CONTRACT_DIR" && SOROBAN_GAS_PROFILE=1 cargo test -- --nocapture 2>&1 | tee "../../$ARTIFACTS_DIR/${CONTRACT}-gas.txt" | tee "$TEST_OUT" > /dev/null)
else
(cd "$CONTRACT_DIR" && cargo test -- --nocapture 2>&1 | tee "$TEST_OUT")
fi
echo " ✅ Tests OK"
# Coverage
if [ "$COVERAGE" = true ]; then
echo "📊 Generating coverage report…"
if ! command -v cargo-llvm-cov &>/dev/null; then
cargo install cargo-llvm-cov --quiet
fi
(cd "$CONTRACT_DIR" && \
cargo llvm-cov --lcov --output-path "../../$ARTIFACTS_DIR/${CONTRACT}-lcov.info" && \
cargo llvm-cov --html --output-dir "../../$ARTIFACTS_DIR/${CONTRACT}-coverage-html")
echo " ✅ Coverage written to $ARTIFACTS_DIR/${CONTRACT}-lcov.info"
fi
# Optimized WASM build
if [ "$BUILD" = true ]; then
echo "🔨 Building optimized WASM…"
(cd "$CONTRACT_DIR" && cargo build --release --target wasm32-unknown-unknown)
WASM_FILE=$(find "target/wasm32-unknown-unknown/release" -maxdepth 1 -name "*.wasm" 2>/dev/null | head -1)
if [ -z "$WASM_FILE" ]; then
WASM_FILE=$(find "$CONTRACT_DIR/target/wasm32-unknown-unknown/release" -maxdepth 1 -name "*.wasm" 2>/dev/null | head -1)
fi
if [ -n "$WASM_FILE" ]; then
WASM_SIZE=$(du -sh "$WASM_FILE" | cut -f1)
echo " ✅ WASM built: $(basename "$WASM_FILE") ($WASM_SIZE)"
fi
fi
echo ""
done
# Fuzzing (escrow_contract only — requires nightly + cargo-fuzz)
if [ "$FUZZ" = true ]; then
echo "🎲 Running fuzz targets…"
FUZZ_DIR="contracts/escrow_contract/fuzz"
if [ ! -d "$FUZZ_DIR" ]; then
echo " ⚠️ No fuzz directory found at $FUZZ_DIR — skipping"
else
FUZZ_SECS="${FUZZ_SECS:-30}"
for target in "$FUZZ_DIR/fuzz_targets"/*.rs; do
name=$(basename "$target" .rs)
echo " Running fuzz target: $name (${FUZZ_SECS}s)"
(cd contracts/escrow_contract && \
cargo +nightly fuzz run "$name" -- -max_total_time="$FUZZ_SECS" \
-artifact_prefix="../../$ARTIFACTS_DIR/fuzz-$name-" 2>&1 | \
tee "../../$ARTIFACTS_DIR/fuzz-$name.txt") || {
echo " ❌ Fuzz target $name found a failure — artifact saved to $ARTIFACTS_DIR/"
exit 1
}
done
echo " ✅ Fuzzing OK"
fi
echo ""
fi
echo "=================================="
echo "✅ All contract checks passed!"
echo " Artifacts: $ARTIFACTS_DIR/"
# Write GitHub Actions step summary
if [ "$CI_MODE" = true ] && [ -n "${GITHUB_STEP_SUMMARY:-}" ]; then
{
echo "## ✅ Contract Test Results"
echo ""
echo "| Contract | Tests | Gas Profile | Coverage |"
echo "|---|---|---|---|"
for CONTRACT in "${CONTRACTS[@]}"; do
TEST_COUNT=$(grep -c "^test .* ok$" "$ARTIFACTS_DIR/${CONTRACT}-test.txt" 2>/dev/null || echo "—")
GAS_FILE="$ARTIFACTS_DIR/${CONTRACT}-gas.txt"
COV_FILE="$ARTIFACTS_DIR/${CONTRACT}-lcov.info"
echo "| \`$CONTRACT\` | $TEST_COUNT passed | $([ -f "$GAS_FILE" ] && echo "✅" || echo "—") | $([ -f "$COV_FILE" ] && echo "✅" || echo "—") |"
done
} >> "$GITHUB_STEP_SUMMARY"
fi