Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl OutputAssertExt for process::Output {
}

impl OutputAssertExt for &mut process::Command {
#[track_caller]
fn assert(self) -> Assert {
let output = match self.output() {
Ok(output) => output,
Expand Down Expand Up @@ -158,7 +159,11 @@ impl Assert {
/// ```
#[track_caller]
pub fn success(self) -> Self {
self.try_success().unwrap_or_else(AssertError::panic)
match self.try_success() {
Ok(v) => v,
// Called manually so `#[track_caller]` is effective.
Err(e) => e.panic(),
}
}

/// `try_` variant of [`Assert::success`].
Expand Down Expand Up @@ -187,7 +192,11 @@ impl Assert {
/// ```
#[track_caller]
pub fn failure(self) -> Self {
self.try_failure().unwrap_or_else(AssertError::panic)
match self.try_failure() {
Ok(v) => v,
// Called manually so `#[track_caller]` is effective.
Err(e) => e.panic(),
}
Comment thread
epage marked this conversation as resolved.
}

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

/// Variant of [`Assert::interrupted`] that returns an [`AssertResult`].
Expand Down Expand Up @@ -266,7 +279,11 @@ impl Assert {
I: IntoCodePredicate<P>,
P: predicates_core::Predicate<i32>,
{
self.try_code(pred).unwrap_or_else(AssertError::panic)
match self.try_code(pred) {
Ok(v) => v,
// Called manually so `#[track_caller]` is effective.
Err(e) => e.panic(),
}
}

/// Variant of [`Assert::code`] that returns an [`AssertResult`].
Expand Down Expand Up @@ -364,7 +381,11 @@ impl Assert {
I: IntoOutputPredicate<P>,
P: predicates_core::Predicate<[u8]>,
{
self.try_stdout(pred).unwrap_or_else(AssertError::panic)
match self.try_stdout(pred) {
Ok(v) => v,
// Called manually so `#[track_caller]` is effective.
Err(e) => e.panic(),
}
}

/// Variant of [`Assert::stdout`] that returns an [`AssertResult`].
Expand Down Expand Up @@ -460,7 +481,11 @@ impl Assert {
I: IntoOutputPredicate<P>,
P: predicates_core::Predicate<[u8]>,
{
self.try_stderr(pred).unwrap_or_else(AssertError::panic)
match self.try_stderr(pred) {
Ok(v) => v,
// Called manually so `#[track_caller]` is effective.
Err(e) => e.panic(),
}
}

/// Variant of [`Assert::stderr`] that returns an [`AssertResult`].
Expand Down
11 changes: 9 additions & 2 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,27 @@ impl fmt::Display for NotFoundError {
/// # Panic
///
/// Panicks if no binary is found
#[track_caller]
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.

This seems uncommon enough and with enough context that it might not be needed

pub fn cargo_bin<S: AsRef<str>>(name: S) -> path::PathBuf {
cargo_bin_str(name.as_ref())
}

#[track_caller]
fn cargo_bin_str(name: &str) -> path::PathBuf {
let env_var = format!("{CARGO_BIN_EXE_}{name}");
env::var_os(env_var)
match env::var_os(env_var)
.map(|p| p.into())
.or_else(|| legacy_cargo_bin(name))
.unwrap_or_else(|| missing_cargo_bin(name))
{
Some(path) => path,
// Called manually so `#[track_caller]` is effective.
None => missing_cargo_bin(name),
}
}

const CARGO_BIN_EXE_: &str = "CARGO_BIN_EXE_";

#[track_caller]
fn missing_cargo_bin(name: &str) -> ! {
let possible_names: Vec<_> = env::vars_os()
.filter_map(|(k, _)| k.into_string().ok())
Expand Down
5 changes: 5 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ impl Command {
/// .unwrap();
/// ```
///
#[track_caller]
pub fn unwrap(&mut self) -> process::Output {
OutputOkExt::unwrap(self)
}
Expand All @@ -171,6 +172,7 @@ impl Command {
/// ```
///
/// [Output]: std::process::Output
#[track_caller]
pub fn unwrap_err(&mut self) -> OutputError {
OutputOkExt::unwrap_err(self)
}
Expand All @@ -190,6 +192,7 @@ impl Command {
///
/// [`Output`]: std::process::Output
#[must_use]
#[track_caller]
pub fn assert(&mut self) -> Assert {
OutputAssertExt::assert(self)
}
Expand Down Expand Up @@ -631,6 +634,7 @@ impl OutputOkExt for &mut Command {
}
}

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

impl OutputAssertExt for &mut Command {
#[track_caller]
fn assert(self) -> Assert {
let output = match self.output() {
Ok(output) => output,
Expand Down
3 changes: 3 additions & 0 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ where
/// ```
///
/// [`Output`]: std::process::Output
#[track_caller]
fn unwrap(self) -> process::Output {
match self.ok() {
Ok(output) => output,
Expand All @@ -81,6 +82,7 @@ where
/// ```
///
/// [`Output`]: std::process::Output
#[track_caller]
fn unwrap_err(self) -> OutputError {
match self.ok() {
Ok(output) => panic!(
Expand Down Expand Up @@ -114,6 +116,7 @@ impl OutputOkExt for &mut process::Command {
}
}

#[track_caller]
fn unwrap_err(self) -> OutputError {
match self.ok() {
Ok(output) => panic!(
Expand Down
Loading