-
Notifications
You must be signed in to change notification settings - Fork 4
(WIP) feat: scenarios based testing #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
820b67f
f0fdab5
803aeb9
930d7eb
d077ef5
e6bedf0
f7452f6
ba87369
c28437c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| #!/usr/bin/env bash | ||
| # Installs the repo's git hooks by pointing core.hooksPath at .githooks/ | ||
| set -euo pipefail | ||
| REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" | ||
| git -C "$REPO_ROOT" config core.hooksPath .githooks | ||
| chmod +x "$REPO_ROOT"/.githooks/* | ||
| echo "✓ Git hooks installed (.githooks/)" |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,107 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| #!/usr/bin/env bash | ||||||||||||||||||||||||||||||||||||||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||
| # pre-commit hook for foc-devnet | ||||||||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||||||||
| # Checks staged files before each commit: | ||||||||||||||||||||||||||||||||||||||||||||
| # - Rust: cargo fmt, cargo clippy | ||||||||||||||||||||||||||||||||||||||||||||
| # - Shell: shfmt, shellcheck, executable bit | ||||||||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||||||||
| # Install: bash .githooks/install.sh | ||||||||||||||||||||||||||||||||||||||||||||
| # Skip once: git commit --no-verify | ||||||||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||||||||
| # Auto-fix mode (formats code and re-stages): | ||||||||||||||||||||||||||||||||||||||||||||
| # FIX=1 git commit | ||||||||||||||||||||||||||||||||||||||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||
| set -euo pipefail | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| FIX="${FIX:-0}" | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| RED='\033[0;31m' | ||||||||||||||||||||||||||||||||||||||||||||
| GREEN='\033[0;32m' | ||||||||||||||||||||||||||||||||||||||||||||
| YELLOW='\033[0;33m' | ||||||||||||||||||||||||||||||||||||||||||||
| BLUE='\033[0;34m' | ||||||||||||||||||||||||||||||||||||||||||||
| NC='\033[0m' | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| FAIL=0 | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| pass() { printf "${GREEN}✓${NC} %s\n" "$1"; } | ||||||||||||||||||||||||||||||||||||||||||||
| fail() { printf "${RED}✗${NC} %s\n" "$1"; FAIL=1; } | ||||||||||||||||||||||||||||||||||||||||||||
| skip() { printf "${YELLOW}⊘${NC} %s (skipped — tool not found)\n" "$1"; } | ||||||||||||||||||||||||||||||||||||||||||||
| fixed() { printf "${BLUE}⟳${NC} %s (auto-fixed & re-staged)\n" "$1"; } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| # Collect staged files | ||||||||||||||||||||||||||||||||||||||||||||
| STAGED=$(git diff --cached --name-only --diff-filter=ACM) | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| # ── Rust checks ────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||
| HAS_RS=$(echo "$STAGED" | grep -c '\.rs$' || true) | ||||||||||||||||||||||||||||||||||||||||||||
| if [[ $HAS_RS -gt 0 ]]; then | ||||||||||||||||||||||||||||||||||||||||||||
| if command -v cargo &>/dev/null; then | ||||||||||||||||||||||||||||||||||||||||||||
| if cargo fmt --all -- --check &>/dev/null; then | ||||||||||||||||||||||||||||||||||||||||||||
| pass "cargo fmt" | ||||||||||||||||||||||||||||||||||||||||||||
| elif [[ "$FIX" == "1" ]]; then | ||||||||||||||||||||||||||||||||||||||||||||
| cargo fmt --all | ||||||||||||||||||||||||||||||||||||||||||||
| git diff --name-only -- '*.rs' | xargs -r git add | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
| # ── Rust checks ────────────────────────────────────────────── | |
| HAS_RS=$(echo "$STAGED" | grep -c '\.rs$' || true) | |
| if [[ $HAS_RS -gt 0 ]]; then | |
| if command -v cargo &>/dev/null; then | |
| if cargo fmt --all -- --check &>/dev/null; then | |
| pass "cargo fmt" | |
| elif [[ "$FIX" == "1" ]]; then | |
| cargo fmt --all | |
| git diff --name-only -- '*.rs' | xargs -r git add | |
| STAGED_RS=$(echo "$STAGED" | grep '\.rs$' || true) | |
| # ── Rust checks ────────────────────────────────────────────── | |
| HAS_RS=$(echo "$STAGED_RS" | grep -c '\.rs$' || true) | |
| if [[ $HAS_RS -gt 0 ]]; then | |
| if command -v cargo &>/dev/null; then | |
| if cargo fmt --all -- --check &>/dev/null; then | |
| pass "cargo fmt" | |
| elif [[ "$FIX" == "1" ]]; then | |
| cargo fmt --all | |
| echo "$STAGED_RS" | xargs -r git add |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,112 @@ | ||||||||||||||||||||||||||||||
| #!/usr/bin/env bash | ||||||||||||||||||||||||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||
| # lib.sh — Shared helpers for scenario tests. | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # Sourced (not executed) by each test_*.sh script. | ||||||||||||||||||||||||||||||
| # Provides: assertions, devnet-info access, and result tracking. | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # ── Writing a new scenario ─────────────────────────────────── | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # 1. Create scenarios/test_<name>.sh with this skeleton: | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # #!/usr/bin/env bash | ||||||||||||||||||||||||||||||
| # set -euo pipefail | ||||||||||||||||||||||||||||||
| # SCENARIO_DIR="$(cd "$(dirname "$0")" && pwd)" | ||||||||||||||||||||||||||||||
| # source "${SCENARIO_DIR}/lib.sh" | ||||||||||||||||||||||||||||||
| # scenario_start "<name>" | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # # ... your checks using assert_*, jq_devnet, etc. ... | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # scenario_end | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # 2. Add "test_<name>" to the SCENARIOS array in order.sh. | ||||||||||||||||||||||||||||||
| # 3. chmod +x scenarios/test_<name>.sh | ||||||||||||||||||||||||||||||
| # 4. Run: bash scenarios/test_<name>.sh | ||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||
| # ── Available helpers ──────────────────────────────────────── | ||||||||||||||||||||||||||||||
| # jq_devnet <filter> — query devnet-info.json | ||||||||||||||||||||||||||||||
| # assert_eq <a> <b> <msg> — equality check | ||||||||||||||||||||||||||||||
| # assert_gt <a> <b> <msg> — integer greater-than | ||||||||||||||||||||||||||||||
| # assert_not_empty <v> <msg> — value is non-empty | ||||||||||||||||||||||||||||||
| # assert_ok <cmd...> <msg> — command exits 0 | ||||||||||||||||||||||||||||||
| # info / ok / fail — logging | ||||||||||||||||||||||||||||||
| # ───────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||
| # shellcheck disable=SC2034 # Variables here are used by scripts that source this file | ||||||||||||||||||||||||||||||
| set -euo pipefail | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # ── Paths ──────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||
| DEVNET_INFO="${DEVNET_INFO:-$HOME/.foc-devnet/state/latest/devnet-info.json}" | ||||||||||||||||||||||||||||||
| SCENARIO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||||||||||||||||||||||||||||||
| REPORT_DIR="${REPORT_DIR:-$HOME/.foc-devnet/state/latest}" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Per-scenario counters (reset by scenario_start) | ||||||||||||||||||||||||||||||
| _PASS=0 | ||||||||||||||||||||||||||||||
| _FAIL=0 | ||||||||||||||||||||||||||||||
| _SCENARIO_NAME="" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # ── devnet-info helpers ────────────────────────────────────── | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Shorthand: jq_devnet '.info.users[0].evm_addr' | ||||||||||||||||||||||||||||||
| jq_devnet() { jq -r "$1" "$DEVNET_INFO"; } | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
| jq_devnet() { jq -r "$1" "$DEVNET_INFO"; } | |
| jq_devnet() { | |
| if ! jq -r "$1" "$DEVNET_INFO"; then | |
| fail "jq_devnet: jq failed for filter '$1' on '$DEVNET_INFO'" | |
| return 1 | |
| fi | |
| } |
Copilot
AI
Mar 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logging currently produces doubled brackets (e.g., info passes "[INFO]", but _log wraps the level in brackets again). This results in output like [[INFO]] message. Consider passing plain level names (e.g., INFO, OK, FAIL) or removing the extra brackets in _log so log lines are consistent and readable.
| info() { _log "[INFO]" "$*"; } | |
| ok() { | |
| _log "[ OK ]" "$*" | |
| ((_PASS++)) || true | |
| } | |
| fail() { | |
| _log "[FAIL]" "$*" | |
| info() { _log "INFO" "$*"; } | |
| ok() { | |
| _log " OK " "$*" | |
| ((_PASS++)) || true | |
| } | |
| fail() { | |
| _log "FAIL" "$*" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #!/usr/bin/env bash | ||
| # ───────────────────────────────────────────────────────────── | ||
| # order.sh — Declares the scenario execution order. | ||
| # | ||
| # Each entry is the basename of a script under scenarios/. | ||
| # Scenarios share the same running devnet and execute serially | ||
| # in the order listed here. | ||
| # ───────────────────────────────────────────────────────────── | ||
|
|
||
| # shellcheck disable=SC2034 # SCENARIOS is used by run.sh which sources this file | ||
| SCENARIOS=( | ||
| test_containers | ||
| test_basic_balances | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the value of adding githhooks if we can't rely on them being installed and we're running these checks in CI anyways? Does it speed up developement? As this is basically duplicating what is setu in CI, lets add comments explaining why we're doing this.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On my system, it fixes before commits, things like fmt and others. These do not strictly duplicate CI, but there is an argument here. We can instead deduplicate and run the same script with FIX=0 and FIX=1 I guess.