coman/cli/
test_ops.rs

1//! Test runner for API collections
2//!
3//! This module provides the test command functionality for the CLI.
4
5use colored::Colorize;
6
7use crate::cli::{commands::Commands, manager::ManagerCommands, request::RequestCommands};
8use crate::core::collection_manager::CollectionManager;
9
10impl Commands {
11    pub async fn run_tests(&self, collection_name: &str) -> Result<(), Box<dyn std::error::Error>> {
12        let mut manager = CollectionManager::default();
13        let collections = manager
14            .loaded_collections
15            .take()
16            .ok_or("No collections loaded")?;
17        let collection = collections
18            .iter()
19            .find(|col| col.name == collection_name)
20            .ok_or_else(|| format!("Collection '{}' not found", collection_name))?;
21
22        if let Some(requests) = &collection.requests {
23            for request in requests {
24                let command = ManagerCommands::get_endpoint_command(collection_name, &request.name)
25                    .ok_or_else(|| {
26                        format!(
27                            "Endpoint '{}' not found in collection '{}'",
28                            request.name, collection_name
29                        )
30                    })?;
31
32                let stdin_input = Vec::new();
33                // Run the request
34                match command.execute_request(false, stdin_input, false).await {
35                    Ok((response, elapsed)) => {
36                        // Print the test result in the same format as print_request_method
37                        println!(
38                            "[{}] {} - {} ({} ms)\n",
39                            command.to_string().bold().bright_yellow(),
40                            response.url.bold().bright_white(),
41                            RequestCommands::colorize_status(response.status),
42                            elapsed
43                        );
44                    }
45                    Err(e) => {
46                        // Print error message and continue
47                        println!("Failed: {}", e);
48                    }
49                }
50            }
51        } else {
52            println!("No requests found in collection '{}'", collection_name);
53        }
54
55        println!("All tests completed for collection '{}'", collection_name);
56        Ok(())
57    }
58}