datahaven/contracts/scripts/check-storage-layout-negative.sh
Ahmad Kaouk b88727d7bd
test(contracts): enforce __GAP slot + size invariant in storage layout check (#448)
## Summary
- Adds an arithmetic invariant check (`slot + size == 151`) to
`check-storage-layout.sh` that catches cases where a new state variable
is added without shrinking `__GAP` accordingly
- The existing snapshot-diff check alone could pass with a wrong gap
size if the snapshot is updated to match — this new check prevents that
- Updates the negative test to also accept the new `__GAP invariant
violated!` error message

## Test plan
- [x] `check-storage-layout.sh` passes on the current correct layout
- [x] `check-storage-layout-negative.sh` passes — the bad layout
contract (slot 107 + size 45 = 152) correctly triggers the invariant
failure
2026-02-20 09:29:34 +01:00

32 lines
945 B
Bash
Executable file

#!/bin/bash
set -euo pipefail
# Negative check: ensure the snapshot-diff storage layout script fails when the layout is broken.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR/.."
set +e
OUTPUT="$(
CONTRACT="DataHavenServiceManagerBadLayout" \
SNAPSHOT="storage-snapshots/DataHavenServiceManager.storage.json" \
./scripts/check-storage-layout.sh 2>&1
)"
EXIT_CODE=$?
set -e
if [ "$EXIT_CODE" -eq 0 ]; then
echo "ERROR: Expected storage layout check to fail for DataHavenServiceManagerBadLayout, but it succeeded."
exit 1
fi
if ! printf '%s\n' "$OUTPUT" | grep -qE "ERROR: (Storage layout has changed!|__GAP invariant violated!)"; then
echo "ERROR: Storage layout check failed, but not for the expected reason."
echo ""
echo "Output:"
echo "$OUTPUT"
exit 1
fi
echo "Negative check OK: storage layout check failed as expected for DataHavenServiceManagerBadLayout."