Skip to content

Commit b744e27

Browse files
committed
fix: Deprecate cargo_bin (runtime version)
1 parent b8f7ded commit b744e27

File tree

5 files changed

+43
-66
lines changed

5 files changed

+43
-66
lines changed

src/cargo.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,16 @@ where
130130
/// ```
131131
///
132132
/// [`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+
)]
133137
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError>;
134138
}
135139

136140
impl CommandCargoExt for crate::cmd::Command {
137141
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError> {
142+
#[allow(deprecated)]
138143
crate::cmd::Command::cargo_bin(name)
139144
}
140145
}
@@ -146,6 +151,7 @@ impl CommandCargoExt for process::Command {
146151
}
147152

148153
pub(crate) fn cargo_bin_cmd<S: AsRef<str>>(name: S) -> Result<process::Command, CargoError> {
154+
#[allow(deprecated)]
149155
let path = cargo_bin(name);
150156
if path.is_file() {
151157
if let Some(runner) = cargo_runner() {
@@ -229,6 +235,10 @@ fn target_dir() -> path::PathBuf {
229235
/// Look up the path to a cargo-built binary within an integration test.
230236
///
231237
/// **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+
)]
232242
pub fn cargo_bin<S: AsRef<str>>(name: S) -> path::PathBuf {
233243
cargo_bin_str(name.as_ref())
234244
}

src/cmd.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ impl Command {
6060
/// println!("{:?}", output);
6161
/// ```
6262
///
63+
#[deprecated(
64+
since = "2.1.0",
65+
note = "incompatible with a custom cargo build-dir, see instead `cargo::cargo_bin_cmd!`"
66+
)]
6367
pub fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, crate::cargo::CargoError> {
6468
let cmd = crate::cargo::cargo_bin_cmd(name)?;
6569
Ok(Self::from_std(cmd))

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()

tests/cargo.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
use std::process::Command;
22

3-
use assert_cmd::pkg_name;
3+
use assert_cmd::cargo_bin;
44
use assert_cmd::prelude::*;
55
use escargot::CURRENT_TARGET;
66

77
#[test]
88
fn cargo_binary() {
9-
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
9+
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
1010
cmd.env("stdout", "42");
1111
cmd.assert().success().stdout("42\n");
1212
}
1313

1414
#[test]
1515
fn cargo_binary_with_empty_env() {
16-
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
16+
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
1717
cmd.env_clear().env("stdout", "42");
1818
cmd.assert().success().stdout("42\n");
1919
}
@@ -39,25 +39,9 @@ fn mod_example() {
3939
}
4040
}
4141

42-
#[test]
43-
#[should_panic] // No bin named `assert_cmd
44-
fn trait_example() {
45-
let mut cmd = Command::cargo_bin(pkg_name!()).unwrap();
46-
let output = cmd.unwrap();
47-
println!("{output:?}");
48-
}
49-
50-
#[test]
51-
#[should_panic] // No bin named `assert_cmd
52-
fn cargo_bin_example_1() {
53-
let mut cmd = Command::cargo_bin(pkg_name!()).unwrap();
54-
let output = cmd.unwrap();
55-
println!("{output:?}");
56-
}
57-
5842
#[test]
5943
fn cargo_bin_example_2() {
60-
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
44+
let mut cmd = Command::new(cargo_bin!("bin_fixture"));
6145
let output = cmd.unwrap();
6246
println!("{output:?}");
6347
}

tests/examples.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use assert_cmd::Command;
1+
use assert_cmd::cargo::cargo_bin_cmd;
22

33
#[test]
44
fn lib_example() {
5-
let mut cmd = Command::cargo_bin("bin_fixture").unwrap();
5+
let mut cmd = cargo_bin_cmd!("bin_fixture");
66
cmd.assert().success();
77

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

1818
#[test]
1919
fn timeout_example() {
20-
use assert_cmd::Command;
21-
22-
let assert = Command::cargo_bin("bin_fixture")
23-
.unwrap()
20+
let assert = cargo_bin_cmd!("bin_fixture")
2421
.timeout(std::time::Duration::from_secs(1))
2522
.env("sleep", "100")
2623
.assert();

0 commit comments

Comments
 (0)