diff options
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/integration_tests.rs | 53 | ||||
-rw-r--r-- | cli/tests/raw_mode.ts | 18 |
2 files changed, 71 insertions, 0 deletions
diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 29c4ac1d0..7fd531c29 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -1,8 +1,61 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. #[macro_use] extern crate lazy_static; +#[cfg(unix)] +extern crate nix; +#[cfg(unix)] +extern crate pty; extern crate tempfile; +#[cfg(unix)] +#[test] +pub fn test_raw_tty() { + use pty::fork::*; + use std::io::{Read, Write}; + + let fork = Fork::from_ptmx().unwrap(); + + if let Ok(mut master) = fork.is_parent() { + let mut obytes: [u8; 100] = [0; 100]; + let mut nread = master.read(&mut obytes).unwrap(); + assert_eq!(String::from_utf8_lossy(&obytes[0..nread]), "S"); + master.write_all(b"a").unwrap(); + nread = master.read(&mut obytes).unwrap(); + assert_eq!(String::from_utf8_lossy(&obytes[0..nread]), "A"); + master.write_all(b"b").unwrap(); + nread = master.read(&mut obytes).unwrap(); + assert_eq!(String::from_utf8_lossy(&obytes[0..nread]), "B"); + master.write_all(b"c").unwrap(); + nread = master.read(&mut obytes).unwrap(); + assert_eq!(String::from_utf8_lossy(&obytes[0..nread]), "C"); + } else { + use deno::test_util::*; + use nix::sys::termios; + use std::os::unix::io::AsRawFd; + use std::process::*; + use tempfile::TempDir; + + // Turn off echo such that parent is reading works properly. + let stdin_fd = std::io::stdin().as_raw_fd(); + let mut t = termios::tcgetattr(stdin_fd).unwrap(); + t.local_flags.remove(termios::LocalFlags::ECHO); + termios::tcsetattr(stdin_fd, termios::SetArg::TCSANOW, &t).unwrap(); + + let deno_dir = TempDir::new().expect("tempdir fail"); + let mut child = Command::new(deno_exe_path()) + .env("DENO_DIR", deno_dir.path()) + .current_dir(util::root_path()) + .arg("run") + .arg("cli/tests/raw_mode.ts") + .stdin(Stdio::inherit()) + .stdout(Stdio::inherit()) + .stderr(Stdio::null()) + .spawn() + .expect("Failed to spawn script"); + child.wait().unwrap(); + } +} + #[test] fn test_pattern_match() { assert!(util::pattern_match("foo[BAR]baz", "foobarbaz", "[BAR]")); diff --git a/cli/tests/raw_mode.ts b/cli/tests/raw_mode.ts new file mode 100644 index 000000000..125601b2b --- /dev/null +++ b/cli/tests/raw_mode.ts @@ -0,0 +1,18 @@ +Deno.setRaw(0, true); +Deno.setRaw(0, true); // Can be called multiple times + +Deno.stdout.writeSync(new TextEncoder().encode("S")); + +const buf = new Uint8Array(3); +for (let i = 0; i < 3; i++) { + const nread = await Deno.stdin.read(buf); + if (nread === Deno.EOF) { + break; + } else { + const data = new TextDecoder().decode(buf.subarray(0, nread)); + Deno.stdout.writeSync(new TextEncoder().encode(data.toUpperCase())); + } +} + +Deno.setRaw(0, false); // restores old mode. +Deno.setRaw(0, false); // Can be safely called multiple times |