#[cfg(test)]
mod cli_tests {
use crate::cli::Cli;
use anyhow::Result;
use clap::Parser;
#[test]
fn test_cli_parsing() -> Result<()> {
let cli = Cli::try_parse_from(["agpm", "--help"]);
assert!(cli.is_err());
let _cli = Cli::try_parse_from(["agpm", "list"])?;
Ok(())
}
#[test]
fn test_cli_verbose_flag() -> Result<()> {
let cli = Cli::try_parse_from(["agpm", "--verbose", "list"])?;
assert!(cli.verbose);
Ok(())
}
#[test]
fn test_cli_quiet_flag() -> Result<()> {
let cli = Cli::try_parse_from(["agpm", "--quiet", "list"])?;
assert!(cli.quiet);
Ok(())
}
#[test]
fn test_cli_no_progress_flag() -> Result<()> {
let cli = Cli::try_parse_from(["agpm", "--no-progress", "list"])?;
assert!(cli.no_progress);
Ok(())
}
#[test]
fn test_cli_config_option() -> Result<()> {
let cli = Cli::try_parse_from(["agpm", "--config", "/path/to/config", "list"])?;
assert_eq!(cli.config, Some("/path/to/config".to_string()));
Ok(())
}
#[test]
fn test_cli_all_commands() -> Result<()> {
let commands = vec![
vec!["agpm", "init"],
vec!["agpm", "add", "source", "test", "https://github.com/test/repo.git"],
vec!["agpm", "install"],
vec!["agpm", "update"],
vec!["agpm", "list"],
vec!["agpm", "validate"],
vec!["agpm", "cache", "info"],
vec!["agpm", "config", "show"],
];
for cmd in commands {
Cli::try_parse_from(cmd)?;
}
Ok(())
}
#[tokio::test]
async fn test_cli_execute_with_flags() -> Result<()> {
use crate::cli::CliConfig;
use tempfile::TempDir;
let temp_dir = TempDir::new()?;
let temp_path = temp_dir.path().canonicalize()?;
let manifest_path = temp_path.join("agpm.toml");
std::fs::write(&manifest_path, "[sources]\n")?;
assert!(manifest_path.exists(), "Manifest file was not created");
let cli = Cli::try_parse_from(["agpm", "--verbose", "config", "path"])?;
assert!(cli.verbose);
let config = cli.build_config();
assert_eq!(config.log_level, Some("debug".to_string()));
assert!(!config.no_progress);
let test_config = CliConfig::new(); cli.execute_with_config(test_config).await?;
let cli = Cli::try_parse_from(["agpm", "--quiet", "config", "path"])?;
assert!(cli.quiet);
let config = cli.build_config();
assert_eq!(config.log_level, None);
let test_config = CliConfig::new();
cli.execute_with_config(test_config).await?;
let cli = Cli::try_parse_from(["agpm", "--no-progress", "config", "path"])?;
assert!(cli.no_progress);
let config = cli.build_config();
assert!(config.no_progress);
assert_eq!(config.log_level, Some("info".to_string()));
let test_config = CliConfig::new();
cli.execute_with_config(test_config).await?;
let cli = Cli::try_parse_from(["agpm", "--verbose", "--no-progress", "config", "path"])?;
assert!(cli.verbose);
assert!(cli.no_progress);
let config = cli.build_config();
assert_eq!(config.log_level, Some("debug".to_string()));
assert!(config.no_progress);
let test_config = CliConfig::new();
cli.execute_with_config(test_config).await?;
Ok(())
}
#[test]
fn test_cli_config_builder() -> Result<()> {
let cli = Cli::try_parse_from(["agpm", "--verbose", "list"])?;
let config = cli.build_config();
assert_eq!(config.log_level, Some("debug".to_string()));
assert!(!config.no_progress);
let cli = Cli::try_parse_from(["agpm", "--quiet", "list"])?;
let config = cli.build_config();
assert_eq!(config.log_level, None);
let cli = Cli::try_parse_from(["agpm", "list"])?;
let config = cli.build_config();
assert_eq!(config.log_level, Some("info".to_string()));
let cli = Cli::try_parse_from(["agpm", "--no-progress", "list"])?;
let config = cli.build_config();
assert!(config.no_progress);
let cli = Cli::try_parse_from(["agpm", "--config", "/custom/path", "list"])?;
let config = cli.build_config();
assert_eq!(config.config_path, Some("/custom/path".to_string()));
Ok(())
}
#[tokio::test]
async fn test_cli_execute_all_commands() -> Result<()> {
use crate::cli::CliConfig;
use tempfile::TempDir;
let temp_dir = TempDir::new()?;
let temp_path = temp_dir.path().canonicalize()?;
let manifest_path = temp_path.join("agpm.toml");
std::fs::write(&manifest_path, "[sources]\n")?;
assert!(manifest_path.exists(), "Manifest file was not created");
let test_config = CliConfig::new();
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"list",
])?;
cli.execute_with_config(test_config.clone()).await?;
assert!(manifest_path.exists(), "Manifest disappeared after list");
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"validate",
])?;
cli.execute_with_config(test_config.clone()).await?;
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"cache",
"info",
])?;
cli.execute_with_config(test_config.clone()).await?;
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"install",
])?;
cli.execute_with_config(test_config.clone()).await?;
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"update",
])?;
cli.execute_with_config(test_config.clone()).await?;
let cli = Cli::try_parse_from([
"agpm",
"--manifest-path",
manifest_path.to_str().unwrap(),
"add",
"source",
"test",
"https://github.com/test/repo.git",
])?;
cli.execute_with_config(test_config.clone()).await?;
Ok(())
}
#[tokio::test]
async fn test_cli_execute_method() -> Result<()> {
let cli = Cli::try_parse_from(["agpm", "config", "path"])?;
cli.execute().await?;
Ok(())
}
#[tokio::test]
async fn test_cli_execute_init_command() -> Result<()> {
use tempfile::TempDir;
let temp_dir = TempDir::new()?;
let temp_path = temp_dir.path().to_path_buf();
let cli = Cli::try_parse_from(["agpm", "init", "--path", temp_path.to_str().unwrap()])?;
cli.execute().await?;
let manifest_path = temp_path.join("agpm.toml");
assert!(manifest_path.exists());
Ok(())
}
#[tokio::test]
async fn test_cli_execute_cache_command() -> Result<()> {
use tempfile::TempDir;
let temp_dir = TempDir::new()?;
let temp_path = temp_dir.path().canonicalize()?;
std::fs::write(temp_path.join("agpm.toml"), "[sources]\n")?;
let cli = Cli::try_parse_from(["agpm", "cache", "info"])?;
cli.execute().await?;
Ok(())
}
#[tokio::test]
async fn test_cli_execute_remove_command() -> Result<()> {
use tempfile::TempDir;
let temp_dir = TempDir::new()?;
let temp_path = temp_dir.path().to_path_buf();
std::fs::write(
temp_path.join("agpm.toml"),
r#"[sources]
test-source = "https://github.com/test/repo.git"
[agents]
[snippets]
[commands]
[mcp-servers]
"#,
)?;
let cli = Cli::try_parse_from(["agpm", "remove", "source", "nonexistent"])?;
let result = cli.execute().await;
assert!(result.is_err(), "Remove command should fail for non-existent source");
Ok(())
}
#[tokio::test]
async fn test_cli_execute_config_command() -> Result<()> {
use tempfile::TempDir;
let _temp_dir = TempDir::new()?;
let cli = Cli::try_parse_from(["agpm", "config", "path"])?;
cli.execute().await?;
Ok(())
}
#[test]
fn test_cli_global_flags_work_with_all_commands() -> Result<()> {
let commands = vec!["init", "install", "update", "list", "validate"];
let flags = vec!["--verbose", "--quiet", "--no-progress"];
for cmd in &commands {
for flag in &flags {
Cli::try_parse_from(["agpm", flag, cmd])?;
}
}
Ok(())
}
}
#[cfg(test)]
mod cli_execution_tests {
}