From 10bc9de1f6a1896fb5e6b06229be213ee9afb2fb Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Thu, 18 Jun 2026 03:42:30 +0000 Subject: [PATCH] fix: prevent command detection false positives by enforcing exact word matching Refactored `is_likely_command` in `src/cli/mod.rs` to use `is_command_starter` from `src/providers/mod.rs`, ensuring exact word matching and resolving an issue where standard words (e.g., "catapult") were falsely flagged as commands. Also expanded test coverage. Co-authored-by: insign <1113045+insign@users.noreply.github.com> --- src/cli/mod.rs | 19 +++++++++++++++---- src/providers/mod.rs | 24 ++++++++++++------------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index f7f9e97..1119679 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -675,10 +675,7 @@ fn is_likely_command(text: &str) -> bool { return false; } - let first_word = text.split_whitespace().next().unwrap_or(""); - crate::providers::COMMAND_STARTERS - .iter() - .any(|cmd| first_word.starts_with(cmd)) + crate::providers::is_command_starter(text) } fn list_profiles(config: &Config) -> Result<()> { @@ -767,3 +764,17 @@ fn list_profiles(config: &Config) -> Result<()> { Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_is_likely_command_false_positives() { + assert!(!is_likely_command("catapult is a word")); + assert!(!is_likely_command("finding a solution")); + assert!(!is_likely_command("timeoutish behavior")); + assert!(is_likely_command("cat file.txt")); + assert!(is_likely_command("find . -name \"*.rs\"")); + } +} diff --git a/src/providers/mod.rs b/src/providers/mod.rs index c9a47f1..24df981 100644 --- a/src/providers/mod.rs +++ b/src/providers/mod.rs @@ -128,7 +128,7 @@ pub const COMMAND_STARTERS: &[&str] = &[ ]; /// Checks if a line starts with a known command. -fn line_starts_with_command(line: &str) -> bool { +pub fn is_command_starter(line: &str) -> bool { let first_word = line.split_whitespace().next().unwrap_or(""); COMMAND_STARTERS.iter().any(|&cmd| { if cmd == "./" || cmd == "/" || cmd == "~" { @@ -203,7 +203,7 @@ pub fn flatten_command_if_safe(text: &str) -> Option { return None; } // Must look like a command - if !line_starts_with_command(line) { + if !is_command_starter(line) { return None; } } @@ -294,19 +294,19 @@ mod tests { } #[test] - fn test_line_starts_with_command_exact_matching() { + fn test_is_command_starter_exact_matching() { // False positives should not be detected - assert!(!line_starts_with_command("catapult is a word")); - assert!(!line_starts_with_command(".gitignore is a file")); - assert!(!line_starts_with_command("timeoutish")); + assert!(!is_command_starter("catapult is a word")); + assert!(!is_command_starter(".gitignore is a file")); + assert!(!is_command_starter("timeoutish")); // Valid commands should be detected - assert!(line_starts_with_command("cat file.txt")); - assert!(line_starts_with_command(". ./script.sh")); - assert!(line_starts_with_command("./my_script")); - assert!(line_starts_with_command("/usr/bin/ls")); - assert!(line_starts_with_command("~/bin/run")); - assert!(line_starts_with_command("apt-get install htop")); + assert!(is_command_starter("cat file.txt")); + assert!(is_command_starter(". ./script.sh")); + assert!(is_command_starter("./my_script")); + assert!(is_command_starter("/usr/bin/ls")); + assert!(is_command_starter("~/bin/run")); + assert!(is_command_starter("apt-get install htop")); } #[test]