cli_xtask/subcommand/
test.rs

1use std::process::Command;
2
3use crate::{
4    args::{EnvArgs, FeatureArgs},
5    config::Config,
6    process::CommandExt,
7    Result, Run,
8};
9
10/// Arguments definition of the `test` subcommand.
11#[cfg_attr(doc, doc = include_str!("../../doc/cargo-xtask-test.md"))]
12#[derive(Debug, Clone, Default, clap::Args)]
13#[non_exhaustive]
14pub struct Test {
15    /// Environment variables to set for `cargo test`.
16    #[clap(flatten)]
17    pub env_args: EnvArgs,
18    /// Features to run the `cargo test` with
19    #[clap(flatten)]
20    pub feature_args: FeatureArgs,
21    /// Options to pass to the `cargo test`
22    pub extra_options: Vec<String>,
23}
24
25impl Run for Test {
26    fn run(&self, config: &Config) -> Result<()> {
27        self.run(config)
28    }
29}
30
31impl Test {
32    /// Runs the `test` subcommand.
33    #[tracing::instrument(name = "test", skip_all, err)]
34    pub fn run(&self, _config: &Config) -> Result<()> {
35        let Self {
36            env_args,
37            feature_args,
38            extra_options,
39        } = self;
40
41        for res in feature_args.features() {
42            let (workspace, package, features) = res?;
43            // cargo test --package <pkg> <features> <extra_options>
44            // DO NOT USE `--all-targets` here, doctests are not built with `--all-targets`
45            Command::new("cargo")
46                .args(["test", "--package", &package.name])
47                .args(features.map(|f| f.to_args()).unwrap_or_default())
48                .args(extra_options)
49                .envs(env_args.env.clone())
50                .workspace_spawn(workspace)?;
51        }
52
53        Ok(())
54    }
55}