diff --git a/src/rules/command_not_found.rs b/src/rules/command_not_found.rs index e3a7f37..2f7acec 100644 --- a/src/rules/command_not_found.rs +++ b/src/rules/command_not_found.rs @@ -51,7 +51,7 @@ fn command_not_found_impl( fn detect_command(cmd: &[String], error: &str) -> Option { let regex_bash = Regex::new(r"bash:(?: line \d+:)? ([^\s]+): command not found(...)?").unwrap(); - let regex_zsh = Regex::new(r"zsh: command not found: ([^\s]+)").unwrap(); + let regex_zsh = Regex::new(r"zsh:(?:\d+:)? command not found: ([^\s]+)").unwrap(); let regex_fish = Regex::new(r"fish: unknown command: ([^\s]+)").unwrap(); let regex_powershell = Regex::new(r"the term '([^']+)' is not recognized").unwrap(); diff --git a/tests/bash.rs b/tests/bash.rs index 1df5964..74c1ff4 100644 --- a/tests/bash.rs +++ b/tests/bash.rs @@ -1,12 +1,12 @@ -use std::io::Read; use std::time::Duration; use std::{env, process::Command}; use expectrl::Session; +use rstest::{fixture, rstest}; use tempfile::NamedTempFile; -#[test] -fn bash() { +#[fixture] +fn bash() -> (Session, NamedTempFile) { let histfile = NamedTempFile::new().unwrap(); let mut bash = Command::new("bash"); @@ -17,21 +17,36 @@ fn bash() { ) .env("FIXIT_QUICK_ENABLE", "false") .env("HISTFILE", histfile.path()) - .env("SHELL", "/bin/bash"); + .env("SHELL", "bash"); let mut p = Session::spawn(bash).expect("Failed to spawn bash"); + p.send_line("eval \"$(fixit init bash)\"").unwrap(); + + (p, histfile) +} + +#[rstest] +fn fixed(bash: (Session, NamedTempFile)) { + let (mut p, _histfile) = bash; + p.set_expect_timeout(Some(Duration::from_secs(5))); - p.send_line("eval \"$(fixit init bash)\"").unwrap(); p.send_line("eco 'Hello, world!'").unwrap(); p.expect("command not found").unwrap(); p.send_line("fix").unwrap(); p.send_line("").unwrap(); p.expect("Hello, world!").unwrap(); p.send_line("exit").unwrap(); +} + +#[rstest] +fn nothing_to_fix(bash: (Session, NamedTempFile)) { + let (mut p, _histfile) = bash; + + p.set_expect_timeout(Some(Duration::from_secs(5))); - let mut buf = String::new(); - p.read_to_string(&mut buf).unwrap(); - println!("{buf}"); + p.send_line("fix").unwrap(); + p.send_line("").unwrap(); + p.expect("nothing to fix").unwrap(); } diff --git a/tests/fish.rs b/tests/fish.rs new file mode 100644 index 0000000..0580d02 --- /dev/null +++ b/tests/fish.rs @@ -0,0 +1,42 @@ +use std::{env, process::Command, time::Duration}; + +use expectrl::Session; +use rstest::{fixture, rstest}; +use tempfile::NamedTempFile; + +#[fixture] +fn fish() -> (Session, NamedTempFile) { + let histfile = NamedTempFile::new().unwrap(); + + let mut fish = Command::new("fish"); + fish.args(["--no-config", "--interactive", "--private"]) + .env( + "PATH", + &format!("./target/debug/:{}", env::var("PATH").unwrap()), + ) + .env("FIXIT_QUICK_ENABLE", "false") + .env("fish_history", histfile.path()) + .env("SHELL", "fish") + .env("fish_command_not_found_path", ""); + + let mut p = Session::spawn(fish).expect("Failed to spawn fish"); + + // Initialize Fish + p.send_line("fixit init fish | source").unwrap(); + + (p, histfile) +} + +#[rstest] +fn fixed(fish: (Session, NamedTempFile)) { + let (mut p, _histfile) = fish; + + p.set_expect_timeout(Some(Duration::from_secs(5))); + + p.send_line("eco 'Hello, world!'").unwrap(); + p.expect("Unknown command").unwrap(); + p.send_line("fix").unwrap(); + p.send_line("").unwrap(); + p.expect("Hello, world!").unwrap(); + p.send_line("exit").unwrap(); +} diff --git a/tests/zsh.rs b/tests/zsh.rs new file mode 100644 index 0000000..49666ec --- /dev/null +++ b/tests/zsh.rs @@ -0,0 +1,46 @@ +use std::{env, process::Command, time::Duration}; + +use expectrl::Session; +use rstest::{fixture, rstest}; +use tempfile::NamedTempFile; + +#[fixture] +fn zsh() -> (Session, NamedTempFile) { + let histfile = NamedTempFile::new().unwrap(); + + let mut zsh = Command::new("zsh"); + zsh.args(["-f", "-i"]) // -f means no RC files, -i for interactive + .env( + "PATH", + &format!("./target/debug/:{}", env::var("PATH").unwrap()), + ) + .env("FIXIT_QUICK_ENABLE", "false") + .env("HISTFILE", histfile.path()) + .env("SHELL", "zsh"); + // ZSH specific history settings + // .env("SAVEHIST", "1000") + // .env("HISTSIZE", "1000"); + + let mut p = Session::spawn(zsh).expect("Failed to spawn zsh"); + + // Initialize ZSH history + // p.send_line("setopt APPEND_HISTORY").unwrap(); + // p.send_line("setopt INC_APPEND_HISTORY").unwrap(); + p.send_line("eval \"$(fixit init zsh)\"").unwrap(); + + (p, histfile) +} + +#[rstest] +fn fixed(zsh: (Session, NamedTempFile)) { + let (mut p, _histfile) = zsh; + + p.set_expect_timeout(Some(Duration::from_secs(5))); + + p.send_line("eco 'Hello, world!'").unwrap(); + p.expect("command not found").unwrap(); + p.send_line("fix").unwrap(); + p.send_line("").unwrap(); + p.expect("Hello, world!").unwrap(); + p.send_line("exit").unwrap(); +}