Skip to content

fix(stellar): assert on raw token name in generated rust tests#824

Open
Assassin859 wants to merge 2 commits into
OpenZeppelin:masterfrom
Assassin859:fix-stellar-test-token-name
Open

fix(stellar): assert on raw token name in generated rust tests#824
Assassin859 wants to merge 2 commits into
OpenZeppelin:masterfrom
Assassin859:fix-stellar-test-token-name

Conversation

@Assassin859

Copy link
Copy Markdown

Description

This PR fixes a bug in the generated Soroban Rust tests where the name assertion fails for token names containing spaces or special characters.

Root Cause

  • The ContractBuilder constructor converts a user-supplied name (e.g. "My Token") to a valid Rust struct identifier (e.g. "MyToken") and stores it as c.name.
  • When asserting on the token's name, the generated test files (printRustNameTest and printVaultRustTest) were using c.name (the Rust struct identifier).
  • However, at runtime, client.name() returns the raw token name supplied to Base::set_metadata(...) (e.g. "My Token").
  • Consequently, the generated tests would fail whenever the token name contained spaces or non-identifier characters.

Changes

  • Updated printRustNameTest and printVaultRustTest inside zip-shared.ts to accept tokenName: string and use it in the assertion rather than c.name.
  • Threaded opts.name (the raw user input) into the test printing functions within zip-rust.ts.
  • Added a unit test validating that raw token names with spaces generate the correct assertion string while keeping the sanitized identifier for the struct.

@Assassin859 Assassin859 requested review from a team as code owners July 3, 2026 13:32
@github-actions

github-actions Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@coderabbitai

coderabbitai Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 482a2451-4043-4fcb-b887-f4d20bcccbe3

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

The printRustNameTest and printVaultRustTest functions now accept an explicit tokenName parameter used in generated assert_eq!(client.name(), ...) assertions instead of deriving it from the contract name. The caller in zip-rust.ts passes opts.name, and tests are updated accordingly with a new case for token names containing spaces.

Changes

Rust name test token name fix

Layer / File(s) Summary
Template signature and assertion update
packages/core/stellar/src/zip-shared.ts
printRustNameTest and printVaultRustTest now accept a tokenName: string parameter and assert against it instead of c.name.
Caller update
packages/core/stellar/src/zip-rust.ts
test.rs generation now passes opts.name as the token name to the updated template functions.
Test updates and new coverage
packages/core/stellar/src/zip-shared.test.ts
Existing tests updated to pass contract.name explicitly; new test verifies token names with spaces are preserved in assertions while identifiers strip spaces.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related issues

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: asserting against the raw token name in generated Rust tests.
Description check ✅ Passed The description matches the code changes and bug fix described in the patch.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Assassin859

Copy link
Copy Markdown
Author

I confirm that I have read and hereby agree to the OpenZeppelin Contributor License Agreement

@Assassin859 Assassin859 marked this pull request as draft July 3, 2026 13:41
@Assassin859 Assassin859 marked this pull request as ready for review July 3, 2026 13:41

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/core/stellar/src/zip-shared.test.ts (1)

203-231: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Good coverage for spaces; consider adding a quote-character case.

This test correctly validates that test('printRustNameTest uses raw token name with spaces in assertion', t => { the raw name flows into the assertion while identifiers stay sanitized. Given the escaping concern raised on printRustNameTest/printVaultRustTest in zip-shared.ts, consider adding a case with a token name containing a " character once that fix lands, to lock in the escaping behavior.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/core/stellar/src/zip-shared.test.ts` around lines 203 - 231, Add
coverage for the escaping path in printRustNameTest by extending the existing
assertion-style test to use a token name containing a double quote character,
not just spaces. Keep the contract identifier sanitized while verifying the raw
token name is safely escaped in the generated Rust assertion string. Use
printRustNameTest and the related printVaultRustTest escaping behavior as the
reference points when updating the test expectations.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/stellar/src/zip-shared.ts`:
- Line 19: The generated Rust test strings in printRustNameTest and
printVaultRustTest are interpolating tokenName directly into String::from_str
literals, which can break the emitted test.rs when tokenName contains quotes or
a trailing backslash. Update the shared Rust string generation in zip-shared.ts
to escape tokenName for Rust string literals before inserting it into both
assertions, using the existing printRustNameTest and printVaultRustTest paths.

---

Nitpick comments:
In `@packages/core/stellar/src/zip-shared.test.ts`:
- Around line 203-231: Add coverage for the escaping path in printRustNameTest
by extending the existing assertion-style test to use a token name containing a
double quote character, not just spaces. Keep the contract identifier sanitized
while verifying the raw token name is safely escaped in the generated Rust
assertion string. Use printRustNameTest and the related printVaultRustTest
escaping behavior as the reference points when updating the test expectations.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: d3a3e801-852e-420f-b9ee-d078add9d8a2

📥 Commits

Reviewing files that changed from the base of the PR and between 90100ee and db6f05f.

📒 Files selected for processing (3)
  • packages/core/stellar/src/zip-rust.ts
  • packages/core/stellar/src/zip-shared.test.ts
  • packages/core/stellar/src/zip-shared.ts

}

export const printRustNameTest = (c: Pick<Contract, 'constructorArgs' | 'name'>) => `#![cfg(test)]
export const printRustNameTest = (c: Pick<Contract, 'constructorArgs' | 'name'>, tokenName: string) => `#![cfg(test)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== file outline =="
ast-grep outline packages/core/stellar/src/zip-shared.ts --view expanded || true

echo
echo "== relevant lines =="
sed -n '1,220p' packages/core/stellar/src/zip-shared.ts | cat -n

echo
echo "== search for tokenName interpolation and any escaping helpers =="
rg -n "tokenName|escapeRust|String::from_str|printRustNameTest|printVaultRustTest" packages/core/stellar/src/zip-shared.ts packages/core/stellar/src -S

Repository: OpenZeppelin/contracts-wizard

Length of output: 29087


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== tokenName origin =="
sed -n '1,140p' packages/core/stellar/src/zip-rust.ts | cat -n

echo
echo "== tests around special characters =="
sed -n '190,245p' packages/core/stellar/src/zip-shared.test.ts | cat -n

echo
echo "== search for any escaping/encoding before printRustNameTest / printVaultRustTest =="
rg -n "escapeRust|replace\\(/\\\\/g|replace\\(/\"/g|tokenName.*replace|opts\\.name|name:.*tokenName|printRustNameTest\\(|printVaultRustTest\\(" packages/core/stellar/src -S

Repository: OpenZeppelin/contracts-wizard

Length of output: 5485


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== name validation / schema for GenericOptions =="
sed -n '1,220p' packages/core/stellar/src/build-generic.ts | cat -n

echo
echo "== ContractBuilder / opts.name sources in Stellar code =="
sed -n '1,180p' packages/core/stellar/src/contract.ts | cat -n
sed -n '1,140p' packages/core/stellar/src/fungible.ts | cat -n
sed -n '1,120p' packages/core/stellar/src/stablecoin.ts | cat -n
sed -n '1,120p' packages/core/stellar/src/non-fungible.ts | cat -n
sed -n '1,120p' packages/core/stellar/src/vault.ts | cat -n

echo
echo "== search for name sanitization / allowed-character checks =="
rg -n "name.*regex|regex.*name|allowed.*name|sanitize.*name|validate.*name|opts\\.name|name\\s*:" packages/core/stellar/src -S

Repository: OpenZeppelin/contracts-wizard

Length of output: 50385


Escape tokenName before embedding it in Rust strings. Both printRustNameTest and printVaultRustTest interpolate tokenName directly into String::from_str(&env, "..."); a " or trailing \ will break the generated test.rs. Escape it for Rust string literals before writing both assertions.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/core/stellar/src/zip-shared.ts` at line 19, The generated Rust test
strings in printRustNameTest and printVaultRustTest are interpolating tokenName
directly into String::from_str literals, which can break the emitted test.rs
when tokenName contains quotes or a trailing backslash. Update the shared Rust
string generation in zip-shared.ts to escape tokenName for Rust string literals
before inserting it into both assertions, using the existing printRustNameTest
and printVaultRustTest paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant