forked from assert-rs/assert_cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcargo.rs
More file actions
297 lines (275 loc) · 8.54 KB
/
cargo.rs
File metadata and controls
297 lines (275 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! Simplify running `bin`s in a Cargo project.
//!
//! [`CommandCargoExt`] is an extension trait for [`Command`] to easily launch a crate's
//! binaries.
//!
//! # Examples
//!
//! Simple case:
//!
//! ```rust,no_run
//! use assert_cmd::prelude::*;
//! use assert_cmd::pkg_name;
//!
//! use std::process::Command;
//!
//! let mut cmd = Command::cargo_bin(pkg_name!())
//! .unwrap();
//! let output = cmd.unwrap();
//! ```
//!
//! # Limitations
//!
//! - Only works within the context of integration tests. See [`escargot`] for a more
//! flexible API.
//! - Only reuses your existing feature flags, targets, or build mode.
//! - Only works with cargo binaries (`cargo test` ensures they are built).
//!
//! If you run into these limitations, we recommend trying out [`escargot`]:
//!
//! ```rust,no_run
//! use assert_cmd::prelude::*;
//!
//! use std::process::Command;
//!
//! let bin_under_test = escargot::CargoBuild::new()
//! .bin("bin_fixture")
//! .current_release()
//! .current_target()
//! .run()
//! .unwrap();
//! let mut cmd = bin_under_test.command();
//! let output = cmd.unwrap();
//! println!("{:?}", output);
//! ```
//!
//! Notes:
//! - There is a [noticeable per-call overhead][cargo-overhead] for `CargoBuild`. We recommend
//! caching the binary location (`.path()` instead of `.command()`) with [`lazy_static`].
//! - `.current_target()` improves platform coverage at the cost of [slower test runs if you don't
//! explicitly pass `--target <TRIPLET>` on the command line][first-call].
//!
//! [`lazy_static`]: https://crates.io/crates/lazy_static
//! [`Command`]: std::process::Command
//! [`escargot`]: https://crates.io/crates/escargot
//! [cargo-overhead]: https://github.com/assert-rs/assert_cmd/issues/6
//! [first-call]: https://github.com/assert-rs/assert_cmd/issues/57
use std::env;
use std::error::Error;
use std::fmt;
use std::path;
use std::process;
#[doc(inline)]
pub use crate::cargo_bin;
#[doc(inline)]
pub use crate::cargo_bin_cmd;
/// Create a [`Command`] for a `bin` in the Cargo project.
///
/// `CommandCargoExt` is an extension trait for [`Command`][std::process::Command] to easily launch a crate's
/// binaries.
///
/// See the [`cargo` module documentation][super::cargo] for caveats and workarounds.
///
/// # Examples
///
/// ```rust,no_run
/// use assert_cmd::prelude::*;
/// use assert_cmd::pkg_name;
///
/// use std::process::Command;
///
/// let mut cmd = Command::cargo_bin(pkg_name!())
/// .unwrap();
/// let output = cmd.unwrap();
/// println!("{:?}", output);
/// ```
///
/// [`Command`]: std::process::Command
pub trait CommandCargoExt
where
Self: Sized,
{
/// Create a [`Command`] to run a specific binary of the current crate.
///
/// See the [`cargo` module documentation][crate::cargo] for caveats and workarounds.
///
/// The [`Command`] created with this method may run the binary through a runner, as configured
/// in the `CARGO_TARGET_<TRIPLET>_RUNNER` environment variable. This is useful for running
/// binaries that can't be launched directly, such as cross-compiled binaries. When using
/// this method with [cross](https://github.com/cross-rs/cross), no extra configuration is
/// needed.
///
/// Cargo support:
/// - `>1.94`: works
/// - `>=1.91,<=1.93`: works with default `build-dir`
/// - `<=1.92`: works
///
/// # Panic
///
/// Panicks if no binary is found
///
/// # Examples
///
/// ```rust,no_run
/// use assert_cmd::prelude::*;
/// use assert_cmd::pkg_name;
///
/// use std::process::Command;
///
/// let mut cmd = Command::cargo_bin(pkg_name!())
/// .unwrap();
/// let output = cmd.unwrap();
/// println!("{:?}", output);
/// ```
///
/// ```rust,no_run
/// use assert_cmd::prelude::*;
///
/// use std::process::Command;
///
/// let mut cmd = Command::cargo_bin("bin_fixture")
/// .unwrap();
/// let output = cmd.unwrap();
/// println!("{:?}", output);
/// ```
///
/// [`Command`]: std::process::Command
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> {
crate::cmd::Command::cargo_bin(name)
}
}
impl CommandCargoExt for process::Command {
fn cargo_bin<S: AsRef<str>>(name: S) -> Result<Self, CargoError> {
cargo_bin_cmd(name)
}
}
pub(crate) fn cargo_bin_cmd<S: AsRef<str>>(name: S) -> Result<process::Command, CargoError> {
let path = cargo_bin(name);
if path.is_file() {
if let Some(runner) = cargo_runner() {
let mut cmd = process::Command::new(&runner[0]);
cmd.args(&runner[1..]).arg(path);
Ok(cmd)
} else {
Ok(process::Command::new(path))
}
} else {
Err(CargoError::with_cause(NotFoundError { path }))
}
}
pub(crate) fn cargo_runner() -> Option<Vec<String>> {
let runner_env = format!(
"CARGO_TARGET_{}_RUNNER",
CURRENT_TARGET.replace('-', "_").to_uppercase()
);
let runner = env::var(runner_env).ok()?;
Some(runner.split(' ').map(str::to_string).collect())
}
/// Error when finding crate binary.
#[derive(Debug)]
pub struct CargoError {
cause: Option<Box<dyn Error + Send + Sync + 'static>>,
}
impl CargoError {
/// Wrap the underlying error for passing up.
pub fn with_cause<E>(cause: E) -> Self
where
E: Error + Send + Sync + 'static,
{
let cause = Box::new(cause);
Self { cause: Some(cause) }
}
}
impl Error for CargoError {}
impl fmt::Display for CargoError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref cause) = self.cause {
writeln!(f, "Cause: {cause}")?;
}
Ok(())
}
}
/// Error when finding crate binary.
#[derive(Debug)]
struct NotFoundError {
path: path::PathBuf,
}
impl Error for NotFoundError {}
impl fmt::Display for NotFoundError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Cargo command not found: {}", self.path.display())
}
}
/// Look up the path to a cargo-built binary within an integration test
///
/// Cargo support:
/// - `>1.94`: works
/// - `>=1.91,<=1.93`: works with default `build-dir`
/// - `<=1.92`: works
///
/// # Panic
///
/// Panicks if no binary is found
pub fn cargo_bin<S: AsRef<str>>(name: S) -> path::PathBuf {
cargo_bin_str(name.as_ref())
}
fn cargo_bin_str(name: &str) -> path::PathBuf {
let env_var = format!("{CARGO_BIN_EXE_}{name}");
env::var_os(env_var)
.map(|p| p.into())
.or_else(|| legacy_cargo_bin(name))
.unwrap_or_else(|| missing_cargo_bin(name))
}
const CARGO_BIN_EXE_: &str = "CARGO_BIN_EXE_";
fn missing_cargo_bin(name: &str) -> ! {
let possible_names: Vec<_> = env::vars_os()
.filter_map(|(k, _)| k.into_string().ok())
.filter_map(|k| k.strip_prefix(CARGO_BIN_EXE_).map(|s| s.to_owned()))
.collect();
if possible_names.is_empty() {
panic!("`CARGO_BIN_EXE_{name}` is unset
help: if this is running within a unit test, move it to an integration test to gain access to `CARGO_BIN_EXE_{name}`")
} else {
let mut names = String::new();
for (i, name) in possible_names.iter().enumerate() {
use std::fmt::Write as _;
if i != 0 {
let _ = write!(&mut names, ", ");
}
let _ = write!(&mut names, "\"{name}\"");
}
panic!(
"`CARGO_BIN_EXE_{name}` is unset
help: available binary names are {names}"
)
}
}
fn legacy_cargo_bin(name: &str) -> Option<path::PathBuf> {
let target_dir = target_dir()?;
let bin_path = target_dir.join(format!("{}{}", name, env::consts::EXE_SUFFIX));
if !bin_path.exists() {
return None;
}
Some(bin_path)
}
// Adapted from
// https://github.com/rust-lang/cargo/blob/485670b3983b52289a2f353d589c57fae2f60f82/tests/testsuite/support/mod.rs#L507
fn target_dir() -> Option<path::PathBuf> {
let mut path = env::current_exe().ok()?;
let _test_bin_name = path.pop();
if path.ends_with("deps") {
let _deps = path.pop();
}
Some(path)
}
/// The current process' target triplet.
const CURRENT_TARGET: &str = include_str!(concat!(env!("OUT_DIR"), "/current_target.txt"));
#[test]
#[should_panic = "`CARGO_BIN_EXE_non-existent` is unset
help: if this is running within a unit test, move it to an integration test to gain access to `CARGO_BIN_EXE_non-existent`"]
fn cargo_bin_in_unit_test() {
cargo_bin("non-existent");
}