Skip to content

Commit

Permalink
Support nextest
Browse files Browse the repository at this point in the history
  • Loading branch information
YizhePKU committed Oct 21, 2024
1 parent 07f11a5 commit 1115b0d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/bin/testbin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! Test binary for UX testing.

use reedline::{DefaultPrompt, Reedline, Signal};
use std::io;

fn main() -> io::Result<()> {
let mut line_editor = Reedline::create();
let prompt = DefaultPrompt::default();

loop {
let sig = line_editor.read_line(&prompt)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
}
Signal::CtrlD | Signal::CtrlC => {
println!("\nAborted!");
break Ok(());
}
}
}
}
37 changes: 28 additions & 9 deletions tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
use alacritty_test::{extract_text, pty_spawn, PtyExt, Terminal};
use std::time::Duration;

/// Return the absolute path to the test binary.
fn testbin() -> String {
if let Ok(nextest) = std::env::var("NEXTEST") {
if nextest == "1" {
return std::env::var("NEXTEST_BIN_EXE_testbin").unwrap();
}
}

#[cfg(not(windows))]
let path = "target/debug/testbin";
#[cfg(windows)]
let path = "target/debug/testbin.exe";

std::fs::canonicalize(path)
.unwrap()
.to_string_lossy()
.to_string()
}

/// Test if Reedline prints the prompt at startup.
#[test]
fn prints_prompt() -> std::io::Result<()> {
let mut pty = pty_spawn("target/debug/examples/basic", vec![], None)?;
let mut pty = pty_spawn(&testbin(), vec![], None)?;
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

Expand All @@ -20,7 +39,7 @@ fn prints_prompt() -> std::io::Result<()> {
/// Test if Reedline echos back input when the user presses Enter.
#[test]
fn echos_input() -> std::io::Result<()> {
let mut pty = pty_spawn("target/debug/examples/basic", vec![], None)?;
let mut pty = pty_spawn(&testbin(), vec![], None)?;
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

Expand All @@ -37,7 +56,7 @@ fn echos_input() -> std::io::Result<()> {
/// Test if Reedline handles backspace correctly.
#[test]
fn backspace() -> std::io::Result<()> {
let mut pty = pty_spawn("target/debug/examples/basic", vec![], None)?;
let mut pty = pty_spawn(&testbin(), vec![], None)?;
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

Expand All @@ -56,7 +75,7 @@ fn backspace() -> std::io::Result<()> {
/// Test if Reedline supports history via up/down arrow.
#[test]
fn history() -> std::io::Result<()> {
let mut pty = pty_spawn("target/debug/examples/basic", vec![], None)?;
let mut pty = pty_spawn(&testbin(), vec![], None)?;
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

Expand Down Expand Up @@ -89,8 +108,8 @@ fn history() -> std::io::Result<()> {
let text = extract_text(terminal.inner());
assert_eq!(&text[6][13..25], " ");

// type "Hel" then arrow up
pty.write_all(b"Hel\x1b[A")?;
// type "Hell" then arrow up
pty.write_all(b"Hell\x1b[A")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());
assert_eq!(&text[6][13..25], "Hello World!");
Expand All @@ -103,7 +122,7 @@ fn history() -> std::io::Result<()> {
/// Test if Reedline supports ctrl-b/ctrl-f/ctrl-left/ctrl-right style movement.
#[test]
fn word_movement() -> std::io::Result<()> {
let mut pty = pty_spawn("target/debug/examples/basic", vec![], None)?;
let mut pty = pty_spawn(&testbin(), vec![], None)?;
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

Expand All @@ -129,7 +148,7 @@ fn word_movement() -> std::io::Result<()> {
/// Test if Ctrl-l clears the screen while keeping current entry.
#[test]
fn clear_screen() -> std::io::Result<()> {
let mut pty = pty_spawn("target/debug/examples/basic", vec![], None)?;
let mut pty = pty_spawn(&testbin(), vec![], None)?;
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

Expand All @@ -148,7 +167,7 @@ fn clear_screen() -> std::io::Result<()> {
/// Test if Reedline supports common Emacs keybindings.
#[test]
fn emacs_keybinds() -> std::io::Result<()> {
let mut pty = pty_spawn("target/debug/examples/basic", vec![], None)?;
let mut pty = pty_spawn(&testbin(), vec![], None)?;
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

Expand Down

0 comments on commit 1115b0d

Please sign in to comment.