From 3534c0212b5fc8e02ed137d6b695585ce4da10d8 Mon Sep 17 00:00:00 2001 From: OnlyYu1996 <1158673577@qq.com> Date: Sun, 17 May 2026 09:26:28 +0800 Subject: [PATCH] fix(plugin): align empty name errors --- src/cortex-cli/src/agent_cmd/tests.rs | 5 ++- src/cortex-cli/src/plugin_cmd.rs | 47 ++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/cortex-cli/src/agent_cmd/tests.rs b/src/cortex-cli/src/agent_cmd/tests.rs index e2ff07f9f..18f7ba753 100644 --- a/src/cortex-cli/src/agent_cmd/tests.rs +++ b/src/cortex-cli/src/agent_cmd/tests.rs @@ -3,10 +3,9 @@ #[cfg(test)] mod tests { use crate::agent_cmd::cli::{CopyArgs, ExportArgs}; - use crate::agent_cmd::loader::{ - load_builtin_agents, parse_frontmatter, read_file_with_encoding, - }; + use crate::agent_cmd::loader::{load_builtin_agents, parse_frontmatter}; use crate::agent_cmd::types::AgentMode; + use crate::utils::file::read_file_with_encoding; #[test] fn test_read_file_with_utf8() { diff --git a/src/cortex-cli/src/plugin_cmd.rs b/src/cortex-cli/src/plugin_cmd.rs index 3ce99f238..fd0e2ae9c 100644 --- a/src/cortex-cli/src/plugin_cmd.rs +++ b/src/cortex-cli/src/plugin_cmd.rs @@ -21,6 +21,8 @@ use std::process::{Command, Stdio}; use std::sync::mpsc; use std::time::Duration; +const EMPTY_PLUGIN_NAME_ERROR: &str = "Plugin name cannot be empty"; + // ============================================================================= // Plugin SDK Templates (embedded for standalone CLI operation) // ============================================================================= @@ -1063,10 +1065,7 @@ async fn run_list(args: PluginListArgs) -> Result<()> { } async fn run_install(args: PluginInstallArgs) -> Result<()> { - // Validate plugin name is not empty (Issue #3700) - if args.name.trim().is_empty() { - bail!("Plugin name cannot be empty. Please provide a valid plugin name."); - } + validate_plugin_name_not_empty(&args.name)?; let plugins_dir = get_plugins_dir(); @@ -1264,15 +1263,12 @@ async fn run_show(args: PluginShowArgs) -> Result<()> { // ============================================================================= async fn run_new(args: PluginNewArgs) -> Result<()> { + validate_plugin_name_not_empty(&args.name)?; + let output_dir = args .output .unwrap_or_else(|| std::env::current_dir().unwrap_or_default()); - // Validate plugin name - if args.name.is_empty() { - bail!("Plugin name cannot be empty"); - } - if !args .name .chars() @@ -1376,6 +1372,14 @@ async fn run_new(args: PluginNewArgs) -> Result<()> { Ok(()) } +fn validate_plugin_name_not_empty(name: &str) -> Result<()> { + if name.trim().is_empty() { + bail!("{}", EMPTY_PLUGIN_NAME_ERROR); + } + + Ok(()) +} + // ============================================================================= // Dev Command Implementation // ============================================================================= @@ -2432,6 +2436,31 @@ mod tests { assert!(args.force, "force should be true"); } + #[tokio::test] + async fn test_plugin_install_and_new_empty_name_errors_match() { + let install_error = run_install(PluginInstallArgs { + name: String::new(), + version: None, + force: false, + }) + .await + .expect_err("empty install plugin name should fail"); + + let new_error = run_new(PluginNewArgs { + name: " ".to_string(), + description: "A test plugin".to_string(), + author: None, + output: None, + advanced: false, + typescript: false, + }) + .await + .expect_err("empty new plugin name should fail"); + + assert_eq!(install_error.to_string(), EMPTY_PLUGIN_NAME_ERROR); + assert_eq!(new_error.to_string(), EMPTY_PLUGIN_NAME_ERROR); + } + // ========================================================================== // CLI argument parsing tests - PluginRemoveArgs // ==========================================================================