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+ # Check if ink_e2e dependency exists and add/modify sandbox feature
99+ if grep -q ' ink_e2e\s*=' " $cargo_toml " ; then
100+ # Use Perl for better TOML handling
101+ perl -i.bak -pe '
102+ if (/ink_e2e\s*=\s*\{[^}]*\}/) {
103+ # If ink_e2e already has features, add sandbox to them
104+ if (/features\s*=\s*\[/) {
105+ s/(features\s*=\s*\[[^]]*)/\1, "sandbox"/;
106+ } else {
107+ # Add features array with sandbox
108+ s/(ink_e2e\s*=\s*\{[^}]*)/\1, features = ["sandbox"]/;
109+ }
110+ }
111+ ' " $cargo_toml "
112+ else
113+ warn " ink_e2e dependency not found in Cargo.toml"
114+ fi
115+ }
116+
117+ remove_sandbox_feature () {
118+ local cargo_toml=" $1 "
119+
120+ info " Removing sandbox feature from Cargo.toml"
121+
122+ # Use Perl for better TOML handling
123+ perl -i.bak -pe '
124+ # Remove sandbox feature from ink_e2e dependency
125+ s/,\s*"sandbox"//g;
126+ s/"sandbox",\s*//g;
127+
128+ # Clean up empty features arrays
129+ s/features\s*=\s*\[\s*\]/features = []/g;
130+
131+ # Remove empty features array entirely
132+ s/,\s*features\s*=\s*\[\]//g;
133+ ' " $cargo_toml "
134+ }
135+
136+ make_tests_sandboxed () {
137+ local test_files
138+ test_files=$( find " $CONTRACT_DIR " -name " *.rs" -type f)
139+
140+ info " Converting tests to sandboxed mode"
141+
142+ while IFS= read -r file; do
143+ if [[ ! -f " $file " ]]; then
144+ continue
145+ fi
146+
147+ # Create backup
148+ cp " $file " " $file .bak"
149+
150+ # Convert #[ink_e2e::test] to #[ink_sandbox::test(backend(runtime_only(...)))]
151+ # First handle simple tests
152+ sed -i.bak -E ' s/#\[ink_e2e::test\]/#[ink_sandbox::test(backend(runtime_only(sandbox = ink_sandbox::DefaultSandbox, client = ink_sandbox::SandboxClient)))]/g' " $file "
153+
154+ # Handle tests with other attributes
155+ 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 "
156+
157+ # Remove backup if changes were made
158+ if ! diff -q " $file " " $file .bak" > /dev/null; then
159+ rm " $file .bak"
160+ info " Modified: $file "
161+ else
162+ rm " $file .bak"
163+ fi
164+ done <<< " $test_files"
165+ }
166+
167+ make_tests_non_sandboxed () {
168+ local test_files
169+ test_files=$( find " $CONTRACT_DIR " -name " *.rs" -type f)
170+
171+ info " Converting tests to non-sandboxed mode"
172+
173+ while IFS= read -r file; do
174+ if [[ ! -f " $file " ]]; then
175+ continue
176+ fi
177+
178+ # Create backup
179+ cp " $file " " $file .bak"
180+
181+ # Use a more robust approach with Perl for complex regex
182+ perl -i.bak -pe '
183+ # Remove backend(runtime_only(...)) from test attributes
184+ s/backend\s*\(\s*runtime_only\s*\([^)]*\)\)//g;
185+
186+ # Remove trailing commas and spaces
187+ s/,\s*\)/)/g;
188+
189+ # Convert ink_sandbox::test to ink_e2e::test
190+ s/#\[ink_sandbox::test/#\[ink_e2e::test/g;
191+
192+ # Remove empty parentheses from test attributes
193+ s/#\[ink_e2e::test\(\s*\)\]/#[ink_e2e::test]/g;
194+ ' " $file "
195+
196+ # Remove backup if changes were made
197+ if ! diff -q " $file " " $file .bak" > /dev/null; then
198+ rm " $file .bak"
199+ info " Modified: $file "
200+ else
201+ rm " $file .bak"
202+ fi
203+ done <<< " $test_files"
204+ }
205+
206+ # Main logic
207+ if is_sandboxed " $MANIFEST_PATH " ; then
208+ info " Current mode: SANDBOXED"
209+ info " Switching to non-sandboxed mode..."
210+
211+ remove_sandbox_feature " $MANIFEST_PATH "
212+ make_tests_non_sandboxed
213+
214+ info " Successfully switched to non-sandboxed mode"
215+ else
216+ info " Current mode: NON-SANDBOXED"
217+ info " Switching to sandboxed mode..."
218+
219+ add_sandbox_feature " $MANIFEST_PATH "
220+ make_tests_sandboxed
221+
222+ info " Successfully switched to sandboxed mode"
223+ fi
224+
225+ # Clean up backup files
226+ find " $CONTRACT_DIR " -name " *.bak" -delete 2> /dev/null || true
227+
228+ info " E2E backend switching completed!"
0 commit comments