Skip to content

Commit

Permalink
add e2e tests for all non-windows shells
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-babichenko committed Nov 15, 2024
1 parent 185b35d commit 2a65052
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/rules/command_not_found.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn command_not_found_impl(

fn detect_command(cmd: &[String], error: &str) -> Option<usize> {
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();

Expand Down
31 changes: 23 additions & 8 deletions tests/bash.rs
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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();
}
42 changes: 42 additions & 0 deletions tests/fish.rs
Original file line number Diff line number Diff line change
@@ -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();
}
46 changes: 46 additions & 0 deletions tests/zsh.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 2a65052

Please sign in to comment.