vdev/commands/
test.rs

1use anyhow::Result;
2use clap::Args;
3use std::collections::BTreeMap;
4
5use crate::{
6    testing::runner::{LocalTestRunner, TestRunner as _},
7    utils::platform,
8};
9
10/// Execute tests
11#[derive(Args, Debug)]
12#[command()]
13pub struct Cli {
14    /// Extra test command arguments
15    args: Option<Vec<String>>,
16
17    /// Environment variables in the form KEY[=VALUE]
18    #[arg(short, long)]
19    env: Option<Vec<String>>,
20}
21
22fn parse_env(env: Vec<String>) -> BTreeMap<String, Option<String>> {
23    env.into_iter()
24        .map(|entry| {
25            #[allow(clippy::map_unwrap_or)] // Can't use map_or due to borrowing entry
26            entry
27                .split_once('=')
28                .map(|(k, v)| (k.to_owned(), Some(v.to_owned())))
29                .unwrap_or_else(|| (entry, None))
30        })
31        .collect()
32}
33
34impl Cli {
35    pub fn exec(self) -> Result<()> {
36        let mut args = vec!["--workspace".to_string()];
37
38        if let Some(mut extra_args) = self.args {
39            args.append(&mut extra_args);
40        }
41
42        if !args.contains(&"--features".to_string()) {
43            let features = platform::default_features();
44            args.extend(["--features".to_string(), features.to_string()]);
45        }
46
47        LocalTestRunner.test(
48            &parse_env(self.env.unwrap_or_default()),
49            &BTreeMap::default(),
50            None,
51            &args,
52            false, // Don't pre-build Vector for direct test runs
53            false,
54            None,
55        )
56    }
57}