diff options
Diffstat (limited to 'test_util/src/lib.rs')
-rw-r--r-- | test_util/src/lib.rs | 101 |
1 files changed, 10 insertions, 91 deletions
diff --git a/test_util/src/lib.rs b/test_util/src/lib.rs index d4effd88b..b38d72cd9 100644 --- a/test_util/src/lib.rs +++ b/test_util/src/lib.rs @@ -16,6 +16,7 @@ use hyper::StatusCode; use lazy_static::lazy_static; use npm::CUSTOM_NPM_PACKAGE_CACHE; use pretty_assertions::assert_eq; +use pty::Pty; use regex::Regex; use rustls::Certificate; use rustls::PrivateKey; @@ -24,7 +25,6 @@ use std::collections::HashMap; use std::convert::Infallible; use std::env; use std::io; -use std::io::Read; use std::io::Write; use std::mem::replace; use std::net::SocketAddr; @@ -92,13 +92,8 @@ pub const PERMISSION_VARIANTS: [&str; 5] = pub const PERMISSION_DENIED_PATTERN: &str = "PermissionDenied"; lazy_static! { - // STRIP_ANSI_RE and strip_ansi_codes are lifted from the "console" crate. - // Copyright 2017 Armin Ronacher <armin.ronacher@active-4.com>. MIT License. - static ref STRIP_ANSI_RE: Regex = Regex::new( - r"[\x1b\x9b][\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><]" - ).unwrap(); - - static ref GUARD: Mutex<HttpServerCount> = Mutex::new(HttpServerCount::default()); + static ref GUARD: Mutex<HttpServerCount> = + Mutex::new(HttpServerCount::default()); } pub fn env_vars_for_npm_tests_no_sync_download() -> Vec<(String, String)> { @@ -1758,7 +1753,7 @@ pub fn http_server() -> HttpServerGuard { /// Helper function to strip ansi codes. pub fn strip_ansi_codes(s: &str) -> std::borrow::Cow<str> { - STRIP_ANSI_RE.replace_all(s, "") + console_static_text::ansi::strip_ansi_codes(s) } pub fn run( @@ -2171,82 +2166,8 @@ pub fn pattern_match(pattern: &str, s: &str, wildcard: &str) -> bool { t.1.is_empty() } -pub enum PtyData { - Input(&'static str), - Output(&'static str), -} - -pub fn test_pty2(args: &str, data: Vec<PtyData>) { - use std::io::BufRead; - - with_pty(&args.split_whitespace().collect::<Vec<_>>(), |console| { - let mut buf_reader = std::io::BufReader::new(console); - for d in data.iter() { - match d { - PtyData::Input(s) => { - println!("INPUT {}", s.escape_debug()); - buf_reader.get_mut().write_text(s); - - // Because of tty echo, we should be able to read the same string back. - assert!(s.ends_with('\n')); - let mut echo = String::new(); - buf_reader.read_line(&mut echo).unwrap(); - println!("ECHO: {}", echo.escape_debug()); - - // Windows may also echo the previous line, so only check the end - assert_ends_with!(normalize_text(&echo), normalize_text(s)); - } - PtyData::Output(s) => { - let mut line = String::new(); - if s.ends_with('\n') { - buf_reader.read_line(&mut line).unwrap(); - } else { - // assumes the buffer won't have overlapping virtual terminal sequences - while normalize_text(&line).len() < normalize_text(s).len() { - let mut buf = [0; 64 * 1024]; - let bytes_read = buf_reader.read(&mut buf).unwrap(); - assert!(bytes_read > 0); - let buf_str = std::str::from_utf8(&buf) - .unwrap() - .trim_end_matches(char::from(0)); - line += buf_str; - } - } - println!("OUTPUT {}", line.escape_debug()); - assert_eq!(normalize_text(&line), normalize_text(s)); - } - } - } - }); - - // This normalization function is not comprehensive - // and may need to updated as new scenarios emerge. - fn normalize_text(text: &str) -> String { - lazy_static! { - static ref MOVE_CURSOR_RIGHT_ONE_RE: Regex = - Regex::new(r"\x1b\[1C").unwrap(); - static ref FOUND_SEQUENCES_RE: Regex = - Regex::new(r"(\x1b\]0;[^\x07]*\x07)*(\x08)*(\x1b\[\d+X)*").unwrap(); - static ref CARRIAGE_RETURN_RE: Regex = - Regex::new(r"[^\n]*\r([^\n])").unwrap(); - } - - // any "move cursor right" sequences should just be a space - let text = MOVE_CURSOR_RIGHT_ONE_RE.replace_all(text, " "); - // replace additional virtual terminal sequences that strip ansi codes doesn't catch - let text = FOUND_SEQUENCES_RE.replace_all(&text, ""); - // strip any ansi codes, which also strips more terminal sequences - let text = strip_ansi_codes(&text); - // get rid of any text that is overwritten with only a carriage return - let text = CARRIAGE_RETURN_RE.replace_all(&text, "$1"); - // finally, trim surrounding whitespace - text.trim().to_string() - } -} - -pub fn with_pty(deno_args: &[&str], mut action: impl FnMut(Box<dyn pty::Pty>)) { - if !atty::is(atty::Stream::Stdin) || !atty::is(atty::Stream::Stderr) { - eprintln!("Ignoring non-tty environment."); +pub fn with_pty(deno_args: &[&str], mut action: impl FnMut(Pty)) { + if !Pty::is_supported() { return; } @@ -2257,14 +2178,12 @@ pub fn with_pty(deno_args: &[&str], mut action: impl FnMut(Box<dyn pty::Pty>)) { "DENO_DIR".to_string(), deno_dir.path().to_string_lossy().to_string(), ); - let pty = pty::create_pty( - &deno_exe_path().to_string_lossy().to_string(), + action(Pty::new( + &deno_exe_path(), deno_args, - testdata_path(), + &testdata_path(), Some(env_vars), - ); - - action(pty); + )) } pub struct WrkOutput { |