trycmd/cargo.rs
1//! Interact with `cargo`
2
3#[doc(inline)]
4pub use snapbox::cmd::cargo_bin;
5
6/// Prepare an example for testing
7///
8/// Unlike `cargo_bin!`, this does not inherit all of the current compiler settings. It
9/// will match the current target and profile but will not get feature flags. Pass those arguments
10/// to the compiler via `args`.
11///
12/// ## Example
13///
14/// ```rust,no_run
15/// #[test]
16/// fn cli_tests() {
17/// trycmd::TestCases::new()
18/// .register_bin("example-fixture", trycmd::cargo::compile_example("example-fixture", []))
19/// .case("examples/cmd/*.trycmd");
20/// }
21/// ```
22#[cfg(feature = "examples")]
23pub fn compile_example<'a>(
24 target_name: &str,
25 args: impl IntoIterator<Item = &'a str>,
26) -> crate::schema::Bin {
27 snapbox::cmd::compile_example(target_name, args).into()
28}
29
30/// Prepare all examples for testing
31///
32/// Unlike `cargo_bin!`, this does not inherit all of the current compiler settings. It
33/// will match the current target and profile but will not get feature flags. Pass those arguments
34/// to the compiler via `args`.
35///
36/// ## Example
37///
38/// ```rust,no_run
39/// #[test]
40/// fn cli_tests() {
41/// trycmd::TestCases::new()
42/// .register_bins(trycmd::cargo::compile_examples([]).unwrap())
43/// .case("examples/cmd/*.trycmd");
44/// }
45/// ```
46#[cfg(feature = "examples")]
47pub fn compile_examples<'a>(
48 args: impl IntoIterator<Item = &'a str>,
49) -> Result<impl Iterator<Item = (String, crate::schema::Bin)>, crate::Error> {
50 snapbox::cmd::compile_examples(args).map(|i| i.map(|(name, path)| (name, path.into())))
51}