Skip to content

Commit c25d4f9

Browse files
committed
Add script to toggle E2E test backend between sandboxed and non-sandboxed modes (#2588)
Implements bash script `scripts/switch-e2e-backend.sh` that allows developers to easily switch between sandboxed and non-sandboxed E2E test backends for contracts. Features: - Accepts `--manifest-path` argument pointing to contract's Cargo.toml - Automatically detects current mode and switches to opposite - Converts test attributes between `#[ink_e2e::test]` and `#[ink_sandbox::test(backend(runtime_only(...)))]` - Manages sandbox feature flag in Cargo.toml dependencies - Handles tests with additional parameters (environment, replace_test_attr) - Provides simple output and error handling consistent with other ink scripts
1 parent 2c797fb commit c25d4f9

File tree

1 file changed

+212
-0
lines changed

1 file changed

+212
-0
lines changed

scripts/switch-e2e-backend.sh

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#!/usr/bin/env bash
2+
3+
# switch-e2e-backend - Toggle E2E test backend between sandboxed and non-sandboxed modes
4+
# Usage: switch-e2e-backend --manifest-path <PATH_TO_CONTRACT_CARGO_TOML>
5+
6+
set -euo pipefail
7+
8+
MANIFEST_PATH=""
9+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10+
11+
usage() {
12+
cat << EOF
13+
Usage: $0 --manifest-path <PATH>
14+
15+
Toggle E2E test backend between sandboxed and non-sandboxed modes for a contract.
16+
17+
Arguments:
18+
--manifest-path PATH Path to the contract's Cargo.toml file
19+
20+
Examples:
21+
$0 --manifest-path ./integration-tests/my_contract/Cargo.toml
22+
$0 --manifest-path /path/to/contract/Cargo.toml
23+
24+
EOF
25+
}
26+
27+
error() {
28+
>&2 echo "Error: $1"
29+
exit 1
30+
}
31+
32+
info() {
33+
echo "Info: $1"
34+
}
35+
36+
warn() {
37+
>&2 echo "Warning: $1"
38+
}
39+
40+
while [[ $# -gt 0 ]]; do
41+
case $1 in
42+
--manifest-path)
43+
MANIFEST_PATH="$2"
44+
shift 2
45+
;;
46+
-h|--help)
47+
usage
48+
exit 0
49+
;;
50+
*)
51+
error "Unknown option: $1. Use --help for usage information."
52+
;;
53+
esac
54+
done
55+
56+
if [[ -z "$MANIFEST_PATH" ]]; then
57+
error "--manifest-path is required"
58+
fi
59+
60+
if [[ ! -f "$MANIFEST_PATH" ]]; then
61+
error "Manifest file not found: $MANIFEST_PATH"
62+
fi
63+
64+
MANIFEST_PATH="$(realpath "$MANIFEST_PATH")"
65+
CONTRACT_DIR="$(dirname "$MANIFEST_PATH")"
66+
67+
info "Processing contract at: $CONTRACT_DIR"
68+
69+
is_sandboxed() {
70+
local cargo_toml="$1"
71+
72+
# Primary check: look for ink_sandbox::test attributes or backend(runtime_only) in test files
73+
local test_files
74+
test_files=$(find "$CONTRACT_DIR" -name "*.rs" -type f)
75+
76+
while IFS= read -r file; do
77+
if grep -q '#\[ink_sandbox::test\]' "$file"; then
78+
return 0
79+
fi
80+
if grep -q '#\[.*::test.*backend.*runtime_only\]' "$file"; then
81+
return 0
82+
fi
83+
done <<< "$test_files"
84+
85+
# Secondary check: look for sandbox feature in Cargo.toml
86+
if grep -q 'ink_e2e.*features.*sandbox' "$cargo_toml"; then
87+
return 0
88+
fi
89+
90+
return 1
91+
}
92+
93+
add_sandbox_feature() {
94+
local cargo_toml="$1"
95+
96+
info "Adding sandbox feature to Cargo.toml"
97+
98+
if grep -q 'ink_e2e\s*=' "$cargo_toml"; then
99+
if grep -q 'features.*=' "$cargo_toml"; then
100+
# Add sandbox to existing features
101+
sed -i.bak 's/\(features\s*=\s*\[[^]]*\)/\1, "sandbox"]/' "$cargo_toml"
102+
else
103+
# Add features array with sandbox
104+
sed -i.bak 's/\(ink_e2e\s*=\s*{[^}]*\)/\1, features = ["sandbox"]}/' "$cargo_toml"
105+
fi
106+
else
107+
warn "ink_e2e dependency not found in Cargo.toml"
108+
fi
109+
}
110+
111+
remove_sandbox_feature() {
112+
local cargo_toml="$1"
113+
114+
info "Removing sandbox feature from Cargo.toml"
115+
116+
# Remove sandbox feature from ink_e2e dependency
117+
sed -i.bak -E 's/,\s*"sandbox"//g; s/"sandbox",\s*//g' "$cargo_toml"
118+
119+
# Clean up empty features arrays
120+
sed -i.bak -E 's/features\s*=\s*\[\s*\]/features = []/g; s/,\s*features\s*=\s*\[\]//g' "$cargo_toml"
121+
}
122+
123+
make_tests_sandboxed() {
124+
local test_files
125+
test_files=$(find "$CONTRACT_DIR" -name "*.rs" -type f)
126+
127+
info "Converting tests to sandboxed mode"
128+
129+
while IFS= read -r file; do
130+
if [[ ! -f "$file" ]]; then
131+
continue
132+
fi
133+
134+
# Create backup
135+
cp "$file" "$file.bak"
136+
137+
# Convert #[ink_e2e::test] to #[ink_sandbox::test(backend(runtime_only(...)))]
138+
# First handle simple tests
139+
sed -i.bak -E 's/#\[ink_e2e::test\]/#[ink_sandbox::test(backend(runtime_only(sandbox = ink_sandbox::DefaultSandbox, client = ink_sandbox::SandboxClient)))]/g' "$file"
140+
141+
# Handle tests with other attributes
142+
sed -i.bak -E 's/#\[ink_e2e::test\(([^)]+)\)\]/#[ink_sandbox::test(\1, backend(runtime_only(sandbox = ink_sandbox::DefaultSandbox, client = ink_sandbox::SandboxClient)))]/g' "$file"
143+
144+
# Remove backup if changes were made
145+
if ! diff -q "$file" "$file.bak" > /dev/null; then
146+
rm "$file.bak"
147+
info "Modified: $file"
148+
else
149+
rm "$file.bak"
150+
fi
151+
done <<< "$test_files"
152+
}
153+
154+
make_tests_non_sandboxed() {
155+
local test_files
156+
test_files=$(find "$CONTRACT_DIR" -name "*.rs" -type f)
157+
158+
info "Converting tests to non-sandboxed mode"
159+
160+
while IFS= read -r file; do
161+
if [[ ! -f "$file" ]]; then
162+
continue
163+
fi
164+
165+
# Create backup
166+
cp "$file" "$file.bak"
167+
168+
# Remove backend(runtime_only(...)) from test attributes
169+
sed -i.bak -E 's/backend\s*\(\s*runtime_only\s*\([^)]*\)\)//g' "$file"
170+
171+
# Remove trailing commas and spaces
172+
sed -i.bak -E 's/,\s*\)/)/g' "$file"
173+
174+
# Convert ink_sandbox::test to ink_e2e::test
175+
sed -i.bak -E 's/#\[ink_sandbox::test/#\[ink_e2e::test/g' "$file"
176+
177+
# Remove empty parentheses from test attributes
178+
sed -i.bak -E 's/#\[ink_e2e::test\(\s*\)\]/#[ink_e2e::test]/g' "$file"
179+
180+
# Remove backup if changes were made
181+
if ! diff -q "$file" "$file.bak" > /dev/null; then
182+
rm "$file.bak"
183+
info "Modified: $file"
184+
else
185+
rm "$file.bak"
186+
fi
187+
done <<< "$test_files"
188+
}
189+
190+
# Main logic
191+
if is_sandboxed "$MANIFEST_PATH"; then
192+
info "Current mode: SANDBOXED"
193+
info "Switching to non-sandboxed mode..."
194+
195+
remove_sandbox_feature "$MANIFEST_PATH"
196+
make_tests_non_sandboxed
197+
198+
info "Successfully switched to non-sandboxed mode"
199+
else
200+
info "Current mode: NON-SANDBOXED"
201+
info "Switching to sandboxed mode..."
202+
203+
add_sandbox_feature "$MANIFEST_PATH"
204+
make_tests_sandboxed
205+
206+
info "Successfully switched to sandboxed mode"
207+
fi
208+
209+
# Clean up backup files
210+
find "$CONTRACT_DIR" -name "*.bak" -delete 2>/dev/null || true
211+
212+
info "E2E backend switching completed!"

0 commit comments

Comments
 (0)