Skip to content

Panic on failure instead of returning Result - #15

Merged
tanglearncode merged 1 commit into
mainfrom
api/panic-by-default
Sep 13, 2026
Merged

tanglearncode merged 1 commit into
mainfrom
api/panic-by-default

Conversation

@tanglearncode

@tanglearncode tanglearncode commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Session::new, mock!, replace!, the expectation methods, verify, checkpoint and restore now panic on failure instead of returning Result. Tests no longer need .unwrap(), ? or Ok(()).

This also stops a pending expectation from hiding the real setup error when ? returned early.

Session::try_new_local and Session::try_new_global still return Result. Nothing was published yet, so no released version breaks.

shimforge is for writing tests, where a failed setup should just fail the test.
Returning Result made every test add .unwrap(), or ? with a final Ok(()). With
?, the drop check of a pending expectation could also replace the real setup
error with "expected at least 1 calls, observed 0".

Session::new, new_local and new_global, mock!, replace!, mock_async,
replace_raw, every expectation response (returns, returning, never and the
rest), and verify, checkpoint and restore now return their values directly and
panic with the error's message on failure. They and the generated mock methods
use #[track_caller], so a failure points at the test line. try_new_local and
try_new_global still return Result<Session, Error>, because reporting a busy
session is their purpose.

Internally the installation, rule and verification code still returns Result,
and a hidden check helper turns an error into a panic at the public boundary.
restore still removes every mock before it panics, so the drop check does not
panic a second time.

Tests drop their unwrap calls and ? operators. Assertions that expected an
error use a panic_message helper and compare the message with the error's
Display text. The README examples and tests/readme.rs no longer return Result.
The compile_fail doctests no longer call unwrap, so they still fail for the
reason they document.

Checked locally with cargo fmt --check, cargo clippy --workspace --all-targets
-- -D warnings on x86_64-pc-windows-msvc, aarch64-pc-windows-msvc,
aarch64-unknown-linux-gnu, aarch64-apple-darwin and x86_64-apple-darwin, and
cargo test --lib (69 passed). Integration tests and doctests run in CI.
@tanglearncode
tanglearncode merged commit 57f36ad into main Sep 13, 2026
7 checks passed
@tanglearncode
tanglearncode deleted the api/panic-by-default branch September 13, 2026 06:06
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