vdev/commands/compose_tests/
show.rs

1use anyhow::Result;
2
3use crate::{
4    environment::Environment,
5    testing::{config::ComposeTestConfig, state},
6};
7
8pub fn exec(integration: Option<&String>, path: &str) -> Result<()> {
9    match integration {
10        None => show_all(path),
11        Some(integration) => show_one(integration, path),
12    }
13}
14
15/// Print only the environment names for a single integration, one per line.
16pub fn exec_environments_only(integration: &str, path: &str) -> Result<()> {
17    let (_test_dir, config) = ComposeTestConfig::load(path, integration)?;
18    for environment in config.environments().keys() {
19        println!("{environment}");
20    }
21    Ok(())
22}
23
24fn show_all(path: &str) -> Result<()> {
25    let entries = ComposeTestConfig::collect_all(path)?;
26    let width = entries
27        .keys()
28        .fold(16, |width, entry| width.max(entry.len()));
29    println!("{:width$}  Environment Name(s)", "Integration Name");
30    println!("{:width$}  -------------------", "----------------");
31    for (integration, config) in entries {
32        let envs_dir = state::EnvsDir::new(&integration);
33        let active_env = envs_dir.active()?;
34        let environments = config
35            .environments()
36            .keys()
37            .map(|environment| format(active_env.as_ref(), environment))
38            .collect::<Vec<_>>()
39            .join("  ");
40        println!("{integration:width$}  {environments}");
41    }
42    Ok(())
43}
44
45fn show_one(integration: &str, path: &str) -> Result<()> {
46    let (_test_dir, config) = ComposeTestConfig::load(path, integration)?;
47    let envs_dir = state::EnvsDir::new(integration);
48    let active_env = envs_dir.active()?;
49
50    if let Some(args) = &config.args {
51        println!("Test args: {}", args.join(" "));
52    } else {
53        println!("Test args: N/A");
54    }
55
56    if config.features.is_empty() {
57        println!("Features: N/A");
58    } else {
59        println!("Features: {}", config.features.join(","));
60    }
61
62    println!(
63        "Test filter: {}",
64        config.test_filter.as_deref().unwrap_or("N/A")
65    );
66
67    println!("Environment:");
68    print_env("  ", &config.env);
69    println!("Runner:");
70    println!("  Environment:");
71    print_env("    ", &config.runner.env);
72    println!("  Volumes:");
73    if config.runner.volumes.is_empty() {
74        println!("    N/A");
75    } else {
76        for (target, mount) in &config.runner.volumes {
77            println!("    {target} => {mount}");
78        }
79    }
80    println!(
81        "  Needs docker socket: {}",
82        config.runner.needs_docker_socket
83    );
84
85    println!("Environments:");
86    for environment in config.environments().keys() {
87        println!("  {}", format(active_env.as_ref(), environment));
88    }
89
90    Ok(())
91}
92
93fn print_env(prefix: &str, environment: &Environment) {
94    if environment.is_empty() {
95        println!("{prefix}N/A");
96    } else {
97        for (key, value) in environment {
98            match value {
99                Some(value) => println!("{prefix}{key}={value:?}"),
100                None => println!("{prefix}{key} (passthrough)"),
101            }
102        }
103    }
104}
105
106fn format(active_env: Option<&String>, environment: &str) -> String {
107    match active_env {
108        Some(active) if active == environment => format!("{environment} (active)"),
109        _ => environment.into(),
110    }
111}