Skip to content

Commit 867d9b3

Browse files
committed
feat(contracts): add testutils Cargo feature flag (#203)
Introduce a testutils feature in contracts/Cargo.toml that gates soroban-sdk test helpers behind an explicit feature flag, keeping them out of the production WASM binary. Changes: - contracts/Cargo.toml: add [features] section with testutils = ["soroban-sdk/testutils"] - contracts/src/lib.rs: gate mod test with #[cfg(any(test, feature = "testutils"))] so the test module is only compiled when running tests or when the feature is explicitly requested - .github/workflows/contracts-ci.yml: - run cargo test --features testutils so tests can use SDK mocks - build release WASM with --no-default-features (no testutils) - add verification step that asserts testutils symbols are absent from the release binary Closes #203
1 parent a9d3c9e commit 867d9b3

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

.github/workflows/contracts-ci.yml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,28 @@ jobs:
5858

5959
- name: Run contract tests
6060
working-directory: contracts
61-
run: cargo test
61+
run: cargo test --features testutils
6262

63-
- name: Build WASM release binary
63+
- name: Build WASM release binary (no testutils)
6464
working-directory: contracts
65-
run: cargo build --target wasm32-unknown-unknown --release
65+
run: cargo build --target wasm32-unknown-unknown --release --no-default-features
66+
67+
- name: Verify testutils excluded from release binary
68+
working-directory: contracts
69+
run: |
70+
WASM_FILE=$(find target/wasm32-unknown-unknown/release -name "*.wasm" | head -1)
71+
if [ -z "$WASM_FILE" ]; then
72+
echo "ERROR: No WASM file found." >&2
73+
exit 1
74+
fi
75+
# Confirm the strings "testutils" or "register_stellar_asset" do not
76+
# appear in the release binary (they would only be present if test
77+
# utilities were compiled in).
78+
if strings "$WASM_FILE" | grep -qE "testutils|register_stellar_asset"; then
79+
echo "ERROR: Release binary contains test utility symbols. Ensure testutils feature is excluded from release builds." >&2
80+
exit 1
81+
fi
82+
echo "✅ Release binary does not contain test utility symbols."
6683
6784
- name: Report WASM binary size
6885
working-directory: contracts

contracts/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ edition = "2021"
66
[lib]
77
crate-type = ["cdylib"]
88

9+
# Feature flags
10+
# ------------
11+
# `testutils` – enables soroban-sdk's test helpers (mock ledger, StellarAssetClient,
12+
# Address::generate, etc.). This feature MUST NOT be enabled for production/release
13+
# WASM builds; it is only activated during `cargo test` (via dev-dependencies) and
14+
# when explicitly requested with `--features testutils`.
15+
[features]
16+
testutils = ["soroban-sdk/testutils"]
17+
918
[dependencies]
1019
soroban-sdk = "21.0.0"
1120

contracts/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,5 +429,5 @@ fn read_campaign(env: &Env, campaign_id: u64) -> Campaign {
429429
.get(&DataKey::Campaign(campaign_id))
430430
.unwrap_or_else(|| panic!("campaign not found"))
431431
}
432-
#[cfg(test)]
432+
#[cfg(any(test, feature = "testutils"))]
433433
mod test;

0 commit comments

Comments
 (0)