Skip to main content

trycmd/
cargo.rs

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