From e97aa2af0e7899bd76853b398102281d2b30060a Mon Sep 17 00:00:00 2001 From: YizhePKU Date: Sat, 1 Jun 2024 20:06:11 +0000 Subject: [PATCH] Add test for auto-cd --- tests/terminal/mod.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/terminal/mod.rs b/tests/terminal/mod.rs index deacd1a1e8..457c1ebd1e 100644 --- a/tests/terminal/mod.rs +++ b/tests/terminal/mod.rs @@ -5,6 +5,38 @@ use nu_test_support::terminal::{ }; use std::{io::Write, time::Duration}; +#[test] +fn auto_cd_works() { + // Setup a directory with a sub-directory in it. + let cwd = tempfile::tempdir().unwrap(); + std::fs::create_dir(cwd.path().join("foo")).unwrap(); + + // Create the PTY and the terminal. + let mut pty = pty_with_nushell( + vec!["--no-config-file".to_string()], + Some(cwd.path().to_path_buf()), + ); + let (mut term, mut events) = default_terminal(); + + // Wait for Nushell to initalize. + std::thread::sleep(Duration::from_millis(500)); + + #[cfg(windows)] + pty.writer().write_all(b".\\foo\r").unwrap(); + #[cfg(unix)] + pty.writer().write_all(b"./foo\r").unwrap(); + + pty.writer().write_all(b"pwd\r").unwrap(); + + // Read the response from Nushell. + read_to_end(&mut term, &mut pty, &mut events, pty_write_handler); + + // Examine the terminal state. + let (row, _col) = extract_cursor(&term); + let text = extract_text(&term); + assert!(text[row - 1].contains("foo")); +} + #[test] fn command_hints_are_pwd_aware() { // PWD-aware command hints require setting history file format to "sqlite".