Skip to content

Commit 1770d1f

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 1770d1f

File tree

1 file changed

+241
-0
lines changed

1 file changed

+241
-0
lines changed

scripts/switch-e2e-backend.sh

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

0 commit comments

Comments
 (0)