Skip to content

Add read_snapshot! and read_binary_snapshot! macros (experimental)#841

Open
max-sixty wants to merge 2 commits intomitsuhiko:masterfrom
max-sixty:read
Open

Add read_snapshot! and read_binary_snapshot! macros (experimental)#841
max-sixty wants to merge 2 commits intomitsuhiko:masterfrom
max-sixty:read

Conversation

@max-sixty
Copy link
Copy Markdown
Collaborator

@max-sixty max-sixty commented Nov 29, 2025

for #353

I think it's somewhat useful. It doesn't allow reading from a different function. As a downside: it adds to the API surface...

From Claude (which also helped with the code):


These new experimental macros allow programmatic reading of snapshot contents, returning String for text snapshots and Vec<u8> for binary snapshots.

The macros resolve snapshot paths using the same logic as assert_snapshot! and assert_binary_snapshot!, respecting settings like snapshot_path, snapshot_suffix, and prepend_module_to_snapshot.

Includes comprehensive functional tests covering various scenarios such as:

  • Reading YAML, JSON, and plain text snapshots
  • Handling nonexistent snapshots
  • Auto-generated snapshot names
  • Custom snapshot_path
  • Binary snapshots with read_binary_snapshot!
  • snapshot_suffix
  • prepend_module_to_snapshot => false
  • Nested modules
  • Combined settings
  • Error cases for reading text as binary and vice-versa.

These new experimental macros allow programmatic reading of snapshot contents, returning `String` for text snapshots and `Vec<u8>` for binary snapshots.

The macros resolve snapshot paths using the same logic as `assert_snapshot!` and `assert_binary_snapshot!`, respecting settings like `snapshot_path`, `snapshot_suffix`, and `prepend_module_to_snapshot`.

Includes comprehensive functional tests covering various scenarios such as:
- Reading YAML, JSON, and plain text snapshots
- Handling nonexistent snapshots
- Auto-generated snapshot names
- Custom `snapshot_path`
- Binary snapshots with `read_binary_snapshot!`
- `snapshot_suffix`
- `prepend_module_to_snapshot => false`
- Nested modules
- Combined settings
- Error cases for reading text as binary and vice-versa.
# Conflicts:
#	cargo-insta/tests/functional/main.rs
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