Skip to content

Commit 7261de6

Browse files
committed
add an integration test for zsh cd hook
1 parent eececd3 commit 7261de6

File tree

5 files changed

+139
-1
lines changed

5 files changed

+139
-1
lines changed

Cargo.lock

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rv/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ indoc = { workspace = true }
5858
fs-err = { workspace = true }
5959
camino-tempfile-ext = { workspace = true }
6060
mockito = "1.4.0"
61+
rexpect = "0.6.2"

crates/rv/tests/integration_tests/common.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ impl RvTest {
5353
}
5454

5555
pub fn rv_command(&self) -> Command {
56-
let mut cmd = Command::new(env!("CARGO_BIN_EXE_rv"));
56+
self.command(env!("CARGO_BIN_EXE_rv"))
57+
}
58+
59+
pub fn command<S: AsRef<std::ffi::OsStr>>(&self, program: S) -> Command {
60+
let mut cmd = Command::new(program);
5761
cmd.current_dir(&self.cwd);
5862
cmd.env_clear().envs(&self.env);
5963
cmd
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
mod env_test;
22
mod init_test;
3+
mod zsh_test;
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use rexpect::{reader::Options, session::PtyReplSession};
2+
3+
use crate::common::RvTest;
4+
5+
fn make_session(test: &RvTest) -> Result<PtyReplSession, Box<dyn std::error::Error>> {
6+
let mut cmd = test.command("zsh");
7+
cmd.arg("--no-rcs").arg("--login").arg("--interactive");
8+
cmd.env("TERM", "xterm-256color")
9+
.env("COLUMNS", "1000")
10+
.env_remove("RV_TEST_EXE")
11+
.env("HOST", ">>>")
12+
.env("PATH", "/bin");
13+
let pty_session = rexpect::spawn_with_options(
14+
cmd,
15+
Options {
16+
timeout_ms: Some(1000),
17+
strip_ansi_escape_codes: true,
18+
},
19+
)?;
20+
let mut session = PtyReplSession {
21+
prompt: "% \r \r\r[PEXPECT]$ ".to_owned(),
22+
pty_session,
23+
quit_command: Some("builtin exit".to_owned()),
24+
echo_on: true,
25+
};
26+
27+
session.send_line("prompt restore")?;
28+
session.send_line(r"PS1='[PEXPECT]%(!.#.$) '")?;
29+
session.send_line(r"unset RPROMPT")?;
30+
session.send_line(r"unset PROMPT_COMMAND")?;
31+
32+
session.wait_for_prompt()?;
33+
session.send_line(&format!(
34+
"eval \"$({} shell init zsh)\"",
35+
test.rv_command().get_program().display()
36+
))?;
37+
assert_eq!(session.wait_for_prompt()?.trim(), "");
38+
39+
Ok(session)
40+
}
41+
42+
#[test]
43+
fn test_no_rubies() -> Result<(), Box<dyn std::error::Error>> {
44+
let test = RvTest::new();
45+
let mut session = make_session(&test)?;
46+
session.send_line("mkdir foobartest")?;
47+
session.wait_for_prompt()?;
48+
session.send_line("cd foobartest")?;
49+
assert_eq!(session.wait_for_prompt()?.trim(), "");
50+
session.send_line("cd ..")?;
51+
session.send_line(r"echo '3.4' > foobartest/.ruby-version")?;
52+
session.send_line("cd foobartest")?;
53+
assert_eq!(session.wait_for_prompt()?.trim(), "");
54+
session.send_line(&format!(
55+
"{} ruby pin",
56+
test.rv_command().get_program().display()
57+
))?;
58+
session.exp_string("/foobartest is pinned to Ruby 3.4")?;
59+
session.wait_for_prompt()?;
60+
session.send_line("cd ..")?;
61+
assert_eq!(session.wait_for_prompt()?.trim(), "");
62+
63+
Ok(())
64+
}
65+
66+
#[test]
67+
fn test_rubies() -> Result<(), Box<dyn std::error::Error>> {
68+
let test = RvTest::new();
69+
test.create_ruby_dir("3.3.4");
70+
test.create_ruby_dir("3.4.1");
71+
let mut session = make_session(&test)?;
72+
session.send_line("mkdir foobartest")?;
73+
session.wait_for_prompt()?;
74+
session.send_line("cd foobartest")?;
75+
assert_eq!(session.wait_for_prompt()?.trim(), "");
76+
session.send_line("cd ..")?;
77+
session.send_line(r"echo '3.3' > foobartest/.ruby-version")?;
78+
session.send_line("cd foobartest")?;
79+
assert_eq!(session.wait_for_prompt()?.trim(), "");
80+
session.send_line(&format!(
81+
"{} ruby pin",
82+
test.rv_command().get_program().display()
83+
))?;
84+
session.exp_string("/foobartest is pinned to Ruby 3.3")?;
85+
session.wait_for_prompt()?;
86+
session.send_line("ruby")?;
87+
assert_eq!(
88+
session.wait_for_prompt()?.trim(),
89+
"ruby\r\n3.3.4\r\naarch64-darwin23\r\naarch64\r\ndarwin23"
90+
);
91+
session.send_line("cd ..")?;
92+
assert_eq!(session.wait_for_prompt()?.trim(), "");
93+
session.send_line("ruby")?;
94+
assert_eq!(
95+
session.wait_for_prompt()?.trim(),
96+
"ruby\r\n3.4.1\r\naarch64-darwin23\r\naarch64\r\ndarwin23"
97+
);
98+
99+
Ok(())
100+
}

0 commit comments

Comments
 (0)