diff options
Diffstat (limited to 'tests/behavior.rs')
-rw-r--r-- | tests/behavior.rs | 315 |
1 files changed, 179 insertions, 136 deletions
diff --git a/tests/behavior.rs b/tests/behavior.rs index d45dd14..7969f72 100644 --- a/tests/behavior.rs +++ b/tests/behavior.rs @@ -3,11 +3,12 @@ mod helpers; #[test] fn test_multiple() { let pty = pty_process::blocking::Pty::new().unwrap(); + let pts = pty.pts().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); let mut child = pty_process::blocking::Command::new("echo") .arg("foo") - .spawn(&pty) + .spawn(&pts) .unwrap(); let mut output = helpers::output(&pty); @@ -18,10 +19,9 @@ fn test_multiple() { let mut child = pty_process::blocking::Command::new("echo") .arg("bar") - .spawn(&pty) + .spawn(&pts) .unwrap(); - let mut output = helpers::output(&pty); assert_eq!(output.next().unwrap(), "bar\r\n"); let status = child.wait().unwrap(); @@ -33,32 +33,37 @@ fn test_multiple() { fn test_multiple_async() { use futures::stream::StreamExt as _; - async_std::task::block_on(async { - let pty = pty_process::Pty::new().unwrap(); - pty.resize(pty_process::Size::new(24, 80)).unwrap(); + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap() + .block_on(async { + let mut pty = pty_process::Pty::new().unwrap(); + let pts = pty.pts().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut child = pty_process::Command::new("echo") - .arg("foo") - .spawn(&pty) - .unwrap(); + let mut child = pty_process::Command::new("echo") + .arg("foo") + .spawn(&pts) + .unwrap(); + let (pty_r, _) = pty.split(); - let mut output = helpers::output_async(&pty); - assert_eq!(output.next().await.unwrap(), "foo\r\n"); + let mut output = helpers::output_async(pty_r); + assert_eq!(output.next().await.unwrap(), "foo\r\n"); - let status = child.status().await.unwrap(); - assert_eq!(status.code().unwrap(), 0); + let status = child.wait().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); - let mut child = pty_process::Command::new("echo") - .arg("bar") - .spawn(&pty) - .unwrap(); + let mut child = pty_process::Command::new("echo") + .arg("bar") + .spawn(&pts) + .unwrap(); - let mut output = helpers::output_async(&pty); - assert_eq!(output.next().await.unwrap(), "bar\r\n"); + assert_eq!(output.next().await.unwrap(), "bar\r\n"); - let status = child.status().await.unwrap(); - assert_eq!(status.code().unwrap(), 0); - }); + let status = child.wait().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); + }); } #[test] @@ -67,6 +72,7 @@ fn test_multiple_configured() { use std::os::unix::io::FromRawFd as _; let pty = pty_process::blocking::Pty::new().unwrap(); + let pts = pty.pts().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); let (stderr_pipe_r, stderr_pipe_w) = nix::unistd::pipe().unwrap(); @@ -90,7 +96,7 @@ fn test_multiple_configured() { Ok(()) }); } - let mut child = cmd.spawn(&pty).unwrap(); + let mut child = cmd.spawn(&pts).unwrap(); let mut output = helpers::output(&pty); assert_eq!(output.next().unwrap(), "foo\r\n"); @@ -110,7 +116,8 @@ fn test_multiple_configured() { let status = child.wait().unwrap(); assert_eq!(status.code().unwrap(), 0); - let mut child = cmd.spawn(&pty).unwrap(); + let mut child = cmd.spawn(&pts).unwrap(); + let mut output = helpers::output(&pty); assert_eq!(output.next().unwrap(), "foo\r\n"); @@ -133,105 +140,126 @@ fn test_multiple_configured() { #[cfg(feature = "async")] #[test] fn test_multiple_configured_async() { - use async_std::io::prelude::BufReadExt as _; use futures::stream::StreamExt as _; use std::os::unix::io::FromRawFd as _; + use tokio::io::AsyncBufReadExt as _; - async_std::task::block_on(async { - let pty = pty_process::Pty::new().unwrap(); - pty.resize(pty_process::Size::new(24, 80)).unwrap(); - - let (stderr_pipe_r, stderr_pipe_w) = nix::unistd::pipe().unwrap(); - let mut stderr_pipe_r = async_std::io::BufReader::new(unsafe { - async_std::fs::File::from_raw_fd(stderr_pipe_r) - }); - let (pre_exec_pipe_r, pre_exec_pipe_w) = nix::unistd::pipe().unwrap(); - let mut pre_exec_pipe_r = async_std::io::BufReader::new(unsafe { - async_std::fs::File::from_raw_fd(pre_exec_pipe_r) - }); - let mut cmd = pty_process::Command::new("perl"); - cmd.arg("-Esay 'foo'; say STDERR 'foo-stderr'; open my $fh, '>&=3'; say $fh 'foo-3';") - .stderr(unsafe { std::process::Stdio::from_raw_fd(stderr_pipe_w) }); - unsafe { - cmd.pre_exec(move || { - nix::unistd::dup2(pre_exec_pipe_w, 3)?; - nix::fcntl::fcntl( - 3, - nix::fcntl::F_SETFD(nix::fcntl::FdFlag::empty()), - )?; - Ok(()) - }); - } - let mut child = cmd.spawn(&pty).unwrap(); - - let mut output = helpers::output_async(&pty); - assert_eq!(output.next().await.unwrap(), "foo\r\n"); - - let mut buf = vec![]; - async_std::future::timeout( - std::time::Duration::from_secs(5), - stderr_pipe_r.read_until(b'\n', &mut buf), - ) - .await - .unwrap() - .unwrap(); - assert_eq!( - std::string::String::from_utf8(buf).unwrap(), - "foo-stderr\n" - ); - - let mut buf = vec![]; - async_std::future::timeout( - std::time::Duration::from_secs(5), - pre_exec_pipe_r.read_until(b'\n', &mut buf), - ) - .await + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() .unwrap() - .unwrap(); - assert_eq!(std::string::String::from_utf8(buf).unwrap(), "foo-3\n"); + .block_on(async { + let mut pty = pty_process::Pty::new().unwrap(); + let pts = pty.pts().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let (pty_r, _) = pty.split(); + + let (stderr_pipe_r, stderr_pipe_w) = nix::unistd::pipe().unwrap(); + let mut stderr_pipe_r = tokio::io::BufReader::new(unsafe { + tokio::fs::File::from_raw_fd(stderr_pipe_r) + }); + let (pre_exec_pipe_r, pre_exec_pipe_w) = + nix::unistd::pipe().unwrap(); + let mut pre_exec_pipe_r = tokio::io::BufReader::new(unsafe { + tokio::fs::File::from_raw_fd(pre_exec_pipe_r) + }); + let mut cmd = pty_process::Command::new("perl"); + cmd.arg( + "-Esay 'foo'; \ + say STDERR 'foo-stderr'; \ + open my $fh, '>&=3'; \ + say $fh 'foo-3';", + ) + .stderr(unsafe { + std::process::Stdio::from_raw_fd(stderr_pipe_w) + }); + unsafe { + cmd.pre_exec(move || { + nix::unistd::dup2(pre_exec_pipe_w, 3)?; + nix::fcntl::fcntl( + 3, + nix::fcntl::F_SETFD(nix::fcntl::FdFlag::empty()), + )?; + Ok(()) + }); + } + let mut child = cmd.spawn(&pts).unwrap(); + + let mut output = helpers::output_async(pty_r); + assert_eq!(output.next().await.unwrap(), "foo\r\n"); + + let mut buf = vec![]; + tokio::time::timeout( + std::time::Duration::from_secs(5), + stderr_pipe_r.read_until(b'\n', &mut buf), + ) + .await + .unwrap() + .unwrap(); + assert_eq!( + std::string::String::from_utf8(buf).unwrap(), + "foo-stderr\n" + ); + + let mut buf = vec![]; + tokio::time::timeout( + std::time::Duration::from_secs(5), + pre_exec_pipe_r.read_until(b'\n', &mut buf), + ) + .await + .unwrap() + .unwrap(); + assert_eq!( + std::string::String::from_utf8(buf).unwrap(), + "foo-3\n" + ); - let status = child.status().await.unwrap(); - assert_eq!(status.code().unwrap(), 0); + let status = child.wait().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); - let mut child = cmd.spawn(&pty).unwrap(); + let mut child = cmd.spawn(&pts).unwrap(); - assert_eq!(output.next().await.unwrap(), "foo\r\n"); + assert_eq!(output.next().await.unwrap(), "foo\r\n"); - let mut buf = vec![]; - async_std::future::timeout( - std::time::Duration::from_secs(5), - stderr_pipe_r.read_until(b'\n', &mut buf), - ) - .await - .unwrap() - .unwrap(); - assert_eq!( - std::string::String::from_utf8(buf).unwrap(), - "foo-stderr\n" - ); - - let mut buf = vec![]; - async_std::future::timeout( - std::time::Duration::from_secs(5), - pre_exec_pipe_r.read_until(b'\n', &mut buf), - ) - .await - .unwrap() - .unwrap(); - assert_eq!(std::string::String::from_utf8(buf).unwrap(), "foo-3\n"); + let mut buf = vec![]; + tokio::time::timeout( + std::time::Duration::from_secs(5), + stderr_pipe_r.read_until(b'\n', &mut buf), + ) + .await + .unwrap() + .unwrap(); + assert_eq!( + std::string::String::from_utf8(buf).unwrap(), + "foo-stderr\n" + ); + + let mut buf = vec![]; + tokio::time::timeout( + std::time::Duration::from_secs(5), + pre_exec_pipe_r.read_until(b'\n', &mut buf), + ) + .await + .unwrap() + .unwrap(); + assert_eq!( + std::string::String::from_utf8(buf).unwrap(), + "foo-3\n" + ); - let status = child.status().await.unwrap(); - assert_eq!(status.code().unwrap(), 0); - }); + let status = child.wait().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); + }); } #[test] fn test_controlling_terminal() { let pty = pty_process::blocking::Pty::new().unwrap(); + let pts = pty.pts().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); let mut child = pty_process::blocking::Command::new("perl") .arg("-Eopen my $fh, '<', '/dev/tty' or die; if (-t $fh) { say 'true' } else { say 'false' }") - .spawn(&pty) + .spawn(&pts) .unwrap(); let mut output = helpers::output(&pty); @@ -246,29 +274,39 @@ fn test_controlling_terminal() { fn test_controlling_terminal_async() { use futures::stream::StreamExt as _; - async_std::task::block_on(async { - let pty = pty_process::Pty::new().unwrap(); - pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut child = pty_process::Command::new("perl") - .arg("-Eopen my $fh, '<', '/dev/tty' or die; if (-t $fh) { say 'true' } else { say 'false' }") - .spawn(&pty) - .unwrap(); - - let mut output = helpers::output_async(&pty); - assert_eq!(output.next().await.unwrap(), "true\r\n"); - - let status = child.status().await.unwrap(); - assert_eq!(status.code().unwrap(), 0); - }); + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap() + .block_on(async { + let mut pty = pty_process::Pty::new().unwrap(); + let pts = pty.pts().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let (pty_r, _) = pty.split(); + let mut child = pty_process::Command::new("perl") + .arg( + "-Eopen my $fh, '<', '/dev/tty' or die; \ + if (-t $fh) { say 'true' } else { say 'false' }", + ) + .spawn(&pts) + .unwrap(); + + let mut output = helpers::output_async(pty_r); + assert_eq!(output.next().await.unwrap(), "true\r\n"); + + let status = child.wait().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); + }); } #[test] fn test_session_leader() { let pty = pty_process::blocking::Pty::new().unwrap(); + let pts = pty.pts().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); let mut child = pty_process::blocking::Command::new("python") .arg("-cimport os; print(os.getpid() == os.getsid(0))") - .spawn(&pty) + .spawn(&pts) .unwrap(); let mut output = helpers::output(&pty); @@ -283,20 +321,25 @@ fn test_session_leader() { fn test_session_leader_async() { use futures::stream::StreamExt as _; - async_std::task::block_on(async { - let pty = pty_process::Pty::new().unwrap(); - pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut child = pty_process::Command::new("python") - .arg("-cimport os; print(os.getpid() == os.getsid(0))") - .spawn(&pty) - .unwrap(); - - { - let mut output = helpers::output_async(&pty); + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap() + .block_on(async { + let mut pty = pty_process::Pty::new().unwrap(); + let pts = pty.pts().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = pty_process::Command::new("python") + .arg("-cimport os; print(os.getpid() == os.getsid(0))") + .spawn(&pts) + .unwrap(); + + let (pty_r, _) = pty.split(); + let mut output = helpers::output_async(pty_r); assert_eq!(output.next().await.unwrap(), "True\r\n"); - } - let status = child.status().await.unwrap(); - assert_eq!(status.code().unwrap(), 0); - }); + let status = child.wait().await.unwrap(); + eprintln!("{:?}", status); + assert_eq!(status.code().unwrap(), 0); + }); } |