Skip to content
Merged
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
22 changes: 9 additions & 13 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ where
/// this method with [cross](https://github.com/cross-rs/cross), no extra configuration is
/// needed.
///
/// **NOTE:** Prefer [`cargo_bin!`] as this makes assumptions about cargo
/// Cargo support:
/// - `>1.94`: works
/// - `>=1.91,<=1.93`: works with default `build-dir`
/// - `<=1.92`: works
///
/// # Examples
///
Expand Down Expand Up @@ -130,16 +133,11 @@ where
/// ```
///
/// [`Command`]: std::process::Command
#[deprecated(
since = "2.1.0",
note = "incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin!`"
)]
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError>;
}

impl CommandCargoExt for crate::cmd::Command {
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError> {
#[allow(deprecated)]
crate::cmd::Command::cargo_bin(name)
}
}
Expand All @@ -151,7 +149,6 @@ impl CommandCargoExt for process::Command {
}

pub(crate) fn cargo_bin_cmd<S: AsRef<str>>(name: S) -> Result<process::Command, CargoError> {
#[allow(deprecated)]
let path = cargo_bin(name);
if path.is_file() {
if let Some(runner) = cargo_runner() {
Expand Down Expand Up @@ -217,13 +214,12 @@ impl fmt::Display for NotFoundError {
}
}

/// Look up the path to a cargo-built binary within an integration test.
/// Look up the path to a cargo-built binary within an integration test
///
/// **NOTE:** Prefer [`cargo_bin!`] as this makes assumptions about cargo
#[deprecated(
since = "2.1.0",
note = "incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin!`"
)]
/// Cargo support:
/// - `>1.94`: works
/// - `>=1.91,<=1.93`: works with default `build-dir`
/// - `<=1.92`: works
pub fn cargo_bin<S: AsRef<str>>(name: S) -> path::PathBuf {
cargo_bin_str(name.as_ref())
}
Expand Down
9 changes: 4 additions & 5 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ impl Command {
///
/// See the [`cargo` module documentation][crate::cargo] for caveats and workarounds.
///
/// **NOTE:** Prefer [`cargo_bin!`][crate::cargo::cargo_bin!] as this makes assumptions about cargo
/// Cargo support:
/// - `>1.94`: works
/// - `>=1.91,<=1.93`: works with default `build-dir`
/// - `<=1.92`: works
///
/// # Examples
///
Expand All @@ -60,10 +63,6 @@ impl Command {
/// println!("{:?}", output);
/// ```
///
#[deprecated(
since = "2.1.0",
note = "incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin_cmd!`"
)]
pub fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, crate::cargo::CargoError> {
let cmd = crate::cargo::cargo_bin_cmd(name)?;
Ok(Self::from_std(cmd))
Expand Down
60 changes: 39 additions & 21 deletions tests/assert.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::process::Command;

use assert_cmd::cargo_bin;
use assert_cmd::prelude::*;
use predicates::prelude::*;

#[test]
fn stdout_string() {
let expected = "hello\n".to_owned();
Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
Expand All @@ -16,88 +16,101 @@ fn stdout_string() {

#[test]
fn trait_example() {
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
cmd.assert().success();
}

#[test]
fn trait_assert_example() {
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
cmd.assert().success();
}

#[test]
fn struct_example() {
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
cmd.assert().success();
}

#[test]
fn append_context_example() {
Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.assert()
.append_context("main", "no args")
.success();
}

#[test]
fn success_example() {
Command::new(cargo_bin!("bin_fixture")).assert().success();
Command::cargo_bin("bin_fixture")
.unwrap()
.assert()
.success();
}

#[test]
fn failure_example() {
Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("exit", "1")
.assert()
.failure();
}

#[test]
fn code_example() {
Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("exit", "42")
.assert()
.code(predicate::eq(42));

Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("exit", "42")
.assert()
.code(42);

Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("exit", "42")
.assert()
.code(&[2, 42] as &[i32]);
}

#[test]
fn stdout_example() {
Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(predicate::eq(b"hello\n" as &[u8]));

Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(predicate::str::diff("hello\n"));

Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(b"hello\n" as &[u8]);

Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stdout(vec![b'h', b'e', b'l', b'l', b'o', b'\n']);

Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
Expand All @@ -106,31 +119,36 @@ fn stdout_example() {

#[test]
fn stderr_example() {
Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(predicate::eq(b"world\n" as &[u8]));

Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(predicate::str::diff("world\n"));

Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(b"world\n" as &[u8]);

Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
.stderr(vec![b'w', b'o', b'r', b'l', b'd', b'\n']);

Command::new(cargo_bin!("bin_fixture"))
Command::cargo_bin("bin_fixture")
.unwrap()
.env("stdout", "hello")
.env("stderr", "world")
.assert()
Expand Down
24 changes: 20 additions & 4 deletions tests/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use std::process::Command;

use assert_cmd::cargo_bin;
use assert_cmd::pkg_name;
use assert_cmd::prelude::*;
use escargot::CURRENT_TARGET;

#[test]
fn cargo_binary() {
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
cmd.env("stdout", "42");
cmd.assert().success().stdout("42\n");
}

#[test]
fn cargo_binary_with_empty_env() {
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
cmd.env_clear().env("stdout", "42");
cmd.assert().success().stdout("42\n");
}
Expand All @@ -39,9 +39,25 @@ fn mod_example() {
}
}

#[test]
#[should_panic] // No bin named `assert_cmd`
fn trait_example() {
let mut cmd = Command::cargo_bin(pkg_name!()).unwrap();
let output = cmd.unwrap();
println!("{output:?}");
}

#[test]
#[should_panic] // No bin named `assert_cmd`
fn cargo_bin_example_1() {
let mut cmd = Command::cargo_bin(pkg_name!()).unwrap();
let output = cmd.unwrap();
println!("{output:?}");
}

#[test]
fn cargo_bin_example_2() {
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
let output = cmd.unwrap();
println!("{output:?}");
}
11 changes: 7 additions & 4 deletions tests/examples.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use assert_cmd::cargo::cargo_bin_cmd;
use assert_cmd::Command;

#[test]
fn lib_example() {
let mut cmd = cargo_bin_cmd!("bin_fixture");
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
cmd.assert().success();

let mut cmd = cargo_bin_cmd!("bin_fixture");
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
let assert = cmd
.arg("-A")
.env("stdout", "hello")
Expand All @@ -17,7 +17,10 @@ fn lib_example() {

#[test]
fn timeout_example() {
let assert = cargo_bin_cmd!("bin_fixture")
use assert_cmd::Command;

let assert = Command::cargo_bin("bin_fixture")
.unwrap()
.timeout(std::time::Duration::from_secs(1))
.env("sleep", "100")
.assert();
Expand Down
Loading