Skip to content

Commit cd2e167

Browse files
committed
fix: .success() not reporting panic location
1 parent b5bf1af commit cd2e167

4 files changed

Lines changed: 47 additions & 8 deletions

File tree

src/assert.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ impl OutputAssertExt for process::Output {
5858
}
5959

6060
impl OutputAssertExt for &mut process::Command {
61+
#[track_caller]
6162
fn assert(self) -> Assert {
6263
let output = match self.output() {
6364
Ok(output) => output,
@@ -158,7 +159,11 @@ impl Assert {
158159
/// ```
159160
#[track_caller]
160161
pub fn success(self) -> Self {
161-
self.try_success().unwrap_or_else(AssertError::panic)
162+
match self.try_success() {
163+
Ok(v) => v,
164+
// Called manually so `#[track_caller]` is effective.
165+
Err(e) => e.panic(),
166+
}
162167
}
163168

164169
/// `try_` variant of [`Assert::success`].
@@ -187,7 +192,11 @@ impl Assert {
187192
/// ```
188193
#[track_caller]
189194
pub fn failure(self) -> Self {
190-
self.try_failure().unwrap_or_else(AssertError::panic)
195+
match self.try_failure() {
196+
Ok(v) => v,
197+
// Called manually so `#[track_caller]` is effective.
198+
Err(e) => e.panic(),
199+
}
191200
}
192201

193202
/// Variant of [`Assert::failure`] that returns an [`AssertResult`].
@@ -201,7 +210,11 @@ impl Assert {
201210
/// Ensure the command aborted before returning a code.
202211
#[track_caller]
203212
pub fn interrupted(self) -> Self {
204-
self.try_interrupted().unwrap_or_else(AssertError::panic)
213+
match self.try_interrupted() {
214+
Ok(v) => v,
215+
// Called manually so `#[track_caller]` is effective.
216+
Err(e) => e.panic(),
217+
}
205218
}
206219

207220
/// Variant of [`Assert::interrupted`] that returns an [`AssertResult`].
@@ -266,7 +279,11 @@ impl Assert {
266279
I: IntoCodePredicate<P>,
267280
P: predicates_core::Predicate<i32>,
268281
{
269-
self.try_code(pred).unwrap_or_else(AssertError::panic)
282+
match self.try_code(pred) {
283+
Ok(v) => v,
284+
// Called manually so `#[track_caller]` is effective.
285+
Err(e) => e.panic(),
286+
}
270287
}
271288

272289
/// Variant of [`Assert::code`] that returns an [`AssertResult`].
@@ -364,7 +381,11 @@ impl Assert {
364381
I: IntoOutputPredicate<P>,
365382
P: predicates_core::Predicate<[u8]>,
366383
{
367-
self.try_stdout(pred).unwrap_or_else(AssertError::panic)
384+
match self.try_stdout(pred) {
385+
Ok(v) => v,
386+
// Called manually so `#[track_caller]` is effective.
387+
Err(e) => e.panic(),
388+
}
368389
}
369390

370391
/// Variant of [`Assert::stdout`] that returns an [`AssertResult`].
@@ -460,7 +481,11 @@ impl Assert {
460481
I: IntoOutputPredicate<P>,
461482
P: predicates_core::Predicate<[u8]>,
462483
{
463-
self.try_stderr(pred).unwrap_or_else(AssertError::panic)
484+
match self.try_stderr(pred) {
485+
Ok(v) => v,
486+
// Called manually so `#[track_caller]` is effective.
487+
Err(e) => e.panic(),
488+
}
464489
}
465490

466491
/// Variant of [`Assert::stderr`] that returns an [`AssertResult`].

src/cargo.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,22 @@ pub fn cargo_bin<S: AsRef<str>>(name: S) -> path::PathBuf {
232232
cargo_bin_str(name.as_ref())
233233
}
234234

235+
#[track_caller]
235236
fn cargo_bin_str(name: &str) -> path::PathBuf {
236237
let env_var = format!("{CARGO_BIN_EXE_}{name}");
237-
env::var_os(env_var)
238+
match env::var_os(env_var)
238239
.map(|p| p.into())
239240
.or_else(|| legacy_cargo_bin(name))
240-
.unwrap_or_else(|| missing_cargo_bin(name))
241+
{
242+
Some(path) => path,
243+
// Called manually so `#[track_caller]` is effective.
244+
None => missing_cargo_bin(name),
245+
}
241246
}
242247

243248
const CARGO_BIN_EXE_: &str = "CARGO_BIN_EXE_";
244249

250+
#[track_caller]
245251
fn missing_cargo_bin(name: &str) -> ! {
246252
let possible_names: Vec<_> = env::vars_os()
247253
.filter_map(|(k, _)| k.into_string().ok())

src/cmd.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ impl Command {
154154
/// .unwrap();
155155
/// ```
156156
///
157+
#[track_caller]
157158
pub fn unwrap(&mut self) -> process::Output {
158159
OutputOkExt::unwrap(self)
159160
}
@@ -171,6 +172,7 @@ impl Command {
171172
/// ```
172173
///
173174
/// [Output]: std::process::Output
175+
#[track_caller]
174176
pub fn unwrap_err(&mut self) -> OutputError {
175177
OutputOkExt::unwrap_err(self)
176178
}
@@ -190,6 +192,7 @@ impl Command {
190192
///
191193
/// [`Output`]: std::process::Output
192194
#[must_use]
195+
#[track_caller]
193196
pub fn assert(&mut self) -> Assert {
194197
OutputAssertExt::assert(self)
195198
}
@@ -631,6 +634,7 @@ impl OutputOkExt for &mut Command {
631634
}
632635
}
633636

637+
#[track_caller]
634638
fn unwrap_err(self) -> OutputError {
635639
match self.ok() {
636640
Ok(output) => {
@@ -655,6 +659,7 @@ impl OutputOkExt for &mut Command {
655659
}
656660

657661
impl OutputAssertExt for &mut Command {
662+
#[track_caller]
658663
fn assert(self) -> Assert {
659664
let output = match self.output() {
660665
Ok(output) => output,

src/output.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ where
5959
/// ```
6060
///
6161
/// [`Output`]: std::process::Output
62+
#[track_caller]
6263
fn unwrap(self) -> process::Output {
6364
match self.ok() {
6465
Ok(output) => output,
@@ -81,6 +82,7 @@ where
8182
/// ```
8283
///
8384
/// [`Output`]: std::process::Output
85+
#[track_caller]
8486
fn unwrap_err(self) -> OutputError {
8587
match self.ok() {
8688
Ok(output) => panic!(
@@ -114,6 +116,7 @@ impl OutputOkExt for &mut process::Command {
114116
}
115117
}
116118

119+
#[track_caller]
117120
fn unwrap_err(self) -> OutputError {
118121
match self.ok() {
119122
Ok(output) => panic!(

0 commit comments

Comments
 (0)