vdev/commands/
test.rs

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