Skip to content

Commit b84678b

Browse files
authored
Merge pull request #248 from epage/dep
fix: Deprecate cargo_bin (runtime version)
2 parents b883443 + b744e27 commit b84678b

File tree

6 files changed

+105
-77
lines changed

6 files changed

+105
-77
lines changed

src/cargo.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
//!
1010
//! ```rust,no_run
1111
//! use assert_cmd::prelude::*;
12+
//! use assert_cmd::pkg_name;
1213
//!
1314
//! use std::process::Command;
1415
//!
15-
//! let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
16+
//! let mut cmd = Command::cargo_bin(pkg_name!())
1617
//! .unwrap();
1718
//! let output = cmd.unwrap();
1819
//! ```
@@ -62,6 +63,8 @@ use std::process;
6263

6364
#[doc(inline)]
6465
pub use crate::cargo_bin;
66+
#[doc(inline)]
67+
pub use crate::cargo_bin_cmd;
6568

6669
/// Create a [`Command`] for a `bin` in the Cargo project.
6770
///
@@ -74,10 +77,11 @@ pub use crate::cargo_bin;
7477
///
7578
/// ```rust,no_run
7679
/// use assert_cmd::prelude::*;
80+
/// use assert_cmd::pkg_name;
7781
///
7882
/// use std::process::Command;
7983
///
80-
/// let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
84+
/// let mut cmd = Command::cargo_bin(pkg_name!())
8185
/// .unwrap();
8286
/// let output = cmd.unwrap();
8387
/// println!("{:?}", output);
@@ -104,10 +108,11 @@ where
104108
///
105109
/// ```rust,no_run
106110
/// use assert_cmd::prelude::*;
111+
/// use assert_cmd::pkg_name;
107112
///
108113
/// use std::process::Command;
109114
///
110-
/// let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
115+
/// let mut cmd = Command::cargo_bin(pkg_name!())
111116
/// .unwrap();
112117
/// let output = cmd.unwrap();
113118
/// println!("{:?}", output);
@@ -125,11 +130,16 @@ where
125130
/// ```
126131
///
127132
/// [`Command`]: std::process::Command
133+
#[deprecated(
134+
since = "2.1.0",
135+
note = "incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin!`"
136+
)]
128137
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError>;
129138
}
130139

131140
impl CommandCargoExt for crate::cmd::Command {
132141
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError> {
142+
#[allow(deprecated)]
133143
crate::cmd::Command::cargo_bin(name)
134144
}
135145
}
@@ -141,6 +151,7 @@ impl CommandCargoExt for process::Command {
141151
}
142152

143153
pub(crate) fn cargo_bin_cmd<S: AsRef<str>>(name: S) -> Result<process::Command, CargoError> {
154+
#[allow(deprecated)]
144155
let path = cargo_bin(name);
145156
if path.is_file() {
146157
if let Some(runner) = cargo_runner() {
@@ -224,6 +235,10 @@ fn target_dir() -> path::PathBuf {
224235
/// Look up the path to a cargo-built binary within an integration test.
225236
///
226237
/// **NOTE:** Prefer [`cargo_bin!`] as this makes assumptions about cargo
238+
#[deprecated(
239+
since = "2.1.0",
240+
note = "incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin!`"
241+
)]
227242
pub fn cargo_bin<S: AsRef<str>>(name: S) -> path::PathBuf {
228243
cargo_bin_str(name.as_ref())
229244
}

src/cmd.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ impl Command {
4343
///
4444
/// ```rust,no_run
4545
/// use assert_cmd::Command;
46+
/// use assert_cmd::pkg_name;
4647
///
47-
/// let mut cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
48+
/// let mut cmd = Command::cargo_bin(pkg_name!())
4849
/// .unwrap();
4950
/// let output = cmd.unwrap();
5051
/// println!("{:?}", output);
@@ -59,6 +60,10 @@ impl Command {
5960
/// println!("{:?}", output);
6061
/// ```
6162
///
63+
#[deprecated(
64+
since = "2.1.0",
65+
note = "incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin_cmd!`"
66+
)]
6267
pub fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, crate::cargo::CargoError> {
6368
let cmd = crate::cargo::cargo_bin_cmd(name)?;
6469
Ok(Self::from_std(cmd))

src/macros.rs

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/// ```should_panic
66
/// use assert_cmd::Command;
77
///
8-
/// let mut cmd = Command::cargo_bin(assert_cmd::crate_name!()).unwrap();
8+
/// let mut cmd = Command::cargo_bin(assert_cmd::pkg_name!()).unwrap();
99
/// let assert = cmd
1010
/// .arg("-A")
1111
/// .env("stdout", "hello")
@@ -18,6 +18,15 @@
1818
/// .stdout("hello\n");
1919
/// ```
2020
#[macro_export]
21+
macro_rules! pkg_name {
22+
() => {
23+
env!("CARGO_PKG_NAME")
24+
};
25+
}
26+
27+
/// Deprecated, replaced with [`pkg_name`]
28+
#[deprecated(since = "2.1.0", note = "replaced with `pkg_name!`")]
29+
#[macro_export]
2130
macro_rules! crate_name {
2231
() => {
2332
env!("CARGO_PKG_NAME")
@@ -33,18 +42,53 @@ macro_rules! crate_name {
3342
///
3443
/// ## Example
3544
///
36-
/// ```rust,no_run
37-
/// #[test]
38-
/// fn cli_tests() {
39-
/// trycmd::TestCases::new()
40-
/// .default_bin_path(trycmd::cargo_bin!("bin-fixture"))
41-
/// .case("tests/cmd/*.trycmd");
42-
/// }
45+
/// ```rust,ignore
46+
/// use assert_cmd::prelude::*;
47+
/// use assert_cmd::cargo::cargo_bin;
48+
///
49+
/// use std::process::Command;
50+
///
51+
/// let mut cmd = Command::new(cargo_bin!())
52+
/// .unwrap();
53+
/// let output = cmd.unwrap();
4354
/// ```
4455
#[macro_export]
4556
#[doc(hidden)]
4657
macro_rules! cargo_bin {
58+
() => {
59+
$crate::cargo::cargo_bin!($crate::pkg_name!())
60+
};
4761
($bin_target_name:expr) => {
4862
::std::path::Path::new(env!(concat!("CARGO_BIN_EXE_", $bin_target_name)))
4963
};
5064
}
65+
66+
/// The absolute path to a binary target's executable.
67+
///
68+
/// The `bin_target_name` is the name of the binary
69+
/// target, exactly as-is.
70+
///
71+
/// **NOTE:** This is only set when building an integration test or benchmark.
72+
///
73+
/// ## Example
74+
///
75+
/// ```rust,ignore
76+
/// use assert_cmd::prelude::*;
77+
/// use assert_cmd::cargo::cargo_bin_cmd;
78+
///
79+
/// use std::process::Command;
80+
///
81+
/// let mut cmd = cargo_bin_cmd!()
82+
/// .unwrap();
83+
/// let output = cmd.unwrap();
84+
/// ```
85+
#[macro_export]
86+
#[doc(hidden)]
87+
macro_rules! cargo_bin_cmd {
88+
() => {
89+
$crate::cargo::cargo_bin_cmd!($crate::pkg_name!())
90+
};
91+
($bin_target_name:expr) => {
92+
$crate::Command::new($crate::cargo::cargo_bin!($bin_target_name))
93+
};
94+
}

tests/assert.rs

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::process::Command;
22

3+
use assert_cmd::cargo_bin;
34
use assert_cmd::prelude::*;
45
use predicates::prelude::*;
56

67
#[test]
78
fn stdout_string() {
89
let expected = "hello\n".to_owned();
9-
Command::cargo_bin("bin_fixture")
10-
.unwrap()
10+
Command::new(cargo_bin!("bin_fixture"))
1111
.env("stdout", "hello")
1212
.env("stderr", "world")
1313
.assert()
@@ -16,101 +16,88 @@ fn stdout_string() {
1616

1717
#[test]
1818
fn trait_example() {
19-
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
19+
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
2020
cmd.assert().success();
2121
}
2222

2323
#[test]
2424
fn trait_assert_example() {
25-
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
25+
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
2626
cmd.assert().success();
2727
}
2828

2929
#[test]
3030
fn struct_example() {
31-
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
31+
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
3232
cmd.assert().success();
3333
}
3434

3535
#[test]
3636
fn append_context_example() {
37-
Command::cargo_bin("bin_fixture")
38-
.unwrap()
37+
Command::new(cargo_bin!("bin_fixture"))
3938
.assert()
4039
.append_context("main", "no args")
4140
.success();
4241
}
4342

4443
#[test]
4544
fn success_example() {
46-
Command::cargo_bin("bin_fixture")
47-
.unwrap()
48-
.assert()
49-
.success();
45+
Command::new(cargo_bin!("bin_fixture")).assert().success();
5046
}
5147

5248
#[test]
5349
fn failure_example() {
54-
Command::cargo_bin("bin_fixture")
55-
.unwrap()
50+
Command::new(cargo_bin!("bin_fixture"))
5651
.env("exit", "1")
5752
.assert()
5853
.failure();
5954
}
6055

6156
#[test]
6257
fn code_example() {
63-
Command::cargo_bin("bin_fixture")
64-
.unwrap()
58+
Command::new(cargo_bin!("bin_fixture"))
6559
.env("exit", "42")
6660
.assert()
6761
.code(predicate::eq(42));
6862

69-
Command::cargo_bin("bin_fixture")
70-
.unwrap()
63+
Command::new(cargo_bin!("bin_fixture"))
7164
.env("exit", "42")
7265
.assert()
7366
.code(42);
7467

75-
Command::cargo_bin("bin_fixture")
76-
.unwrap()
68+
Command::new(cargo_bin!("bin_fixture"))
7769
.env("exit", "42")
7870
.assert()
7971
.code(&[2, 42] as &[i32]);
8072
}
8173

8274
#[test]
8375
fn stdout_example() {
84-
Command::cargo_bin("bin_fixture")
85-
.unwrap()
76+
Command::new(cargo_bin!("bin_fixture"))
8677
.env("stdout", "hello")
8778
.env("stderr", "world")
8879
.assert()
8980
.stdout(predicate::eq(b"hello\n" as &[u8]));
9081

91-
Command::cargo_bin("bin_fixture")
92-
.unwrap()
82+
Command::new(cargo_bin!("bin_fixture"))
9383
.env("stdout", "hello")
9484
.env("stderr", "world")
9585
.assert()
9686
.stdout(predicate::str::diff("hello\n"));
9787

98-
Command::cargo_bin("bin_fixture")
99-
.unwrap()
88+
Command::new(cargo_bin!("bin_fixture"))
10089
.env("stdout", "hello")
10190
.env("stderr", "world")
10291
.assert()
10392
.stdout(b"hello\n" as &[u8]);
10493

105-
Command::cargo_bin("bin_fixture")
106-
.unwrap()
94+
Command::new(cargo_bin!("bin_fixture"))
10795
.env("stdout", "hello")
10896
.env("stderr", "world")
10997
.assert()
11098
.stdout(vec![b'h', b'e', b'l', b'l', b'o', b'\n']);
11199

112-
Command::cargo_bin("bin_fixture")
113-
.unwrap()
100+
Command::new(cargo_bin!("bin_fixture"))
114101
.env("stdout", "hello")
115102
.env("stderr", "world")
116103
.assert()
@@ -119,36 +106,31 @@ fn stdout_example() {
119106

120107
#[test]
121108
fn stderr_example() {
122-
Command::cargo_bin("bin_fixture")
123-
.unwrap()
109+
Command::new(cargo_bin!("bin_fixture"))
124110
.env("stdout", "hello")
125111
.env("stderr", "world")
126112
.assert()
127113
.stderr(predicate::eq(b"world\n" as &[u8]));
128114

129-
Command::cargo_bin("bin_fixture")
130-
.unwrap()
115+
Command::new(cargo_bin!("bin_fixture"))
131116
.env("stdout", "hello")
132117
.env("stderr", "world")
133118
.assert()
134119
.stderr(predicate::str::diff("world\n"));
135120

136-
Command::cargo_bin("bin_fixture")
137-
.unwrap()
121+
Command::new(cargo_bin!("bin_fixture"))
138122
.env("stdout", "hello")
139123
.env("stderr", "world")
140124
.assert()
141125
.stderr(b"world\n" as &[u8]);
142126

143-
Command::cargo_bin("bin_fixture")
144-
.unwrap()
127+
Command::new(cargo_bin!("bin_fixture"))
145128
.env("stdout", "hello")
146129
.env("stderr", "world")
147130
.assert()
148131
.stderr(vec![b'w', b'o', b'r', b'l', b'd', b'\n']);
149132

150-
Command::cargo_bin("bin_fixture")
151-
.unwrap()
133+
Command::new(cargo_bin!("bin_fixture"))
152134
.env("stdout", "hello")
153135
.env("stderr", "world")
154136
.assert()

0 commit comments

Comments
 (0)