diff options
Diffstat (limited to 'tests/behavior.rs')
-rw-r--r-- | tests/behavior.rs | 309 |
1 files changed, 137 insertions, 172 deletions
diff --git a/tests/behavior.rs b/tests/behavior.rs index 7969f72..bb40e35 100644 --- a/tests/behavior.rs +++ b/tests/behavior.rs @@ -29,41 +29,36 @@ fn test_multiple() { } #[cfg(feature = "async")] +#[tokio::main] #[test] -fn test_multiple_async() { +async fn test_multiple_async() { use futures::stream::StreamExt as _; - 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 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(&pts) - .unwrap(); - let (pty_r, _) = pty.split(); + 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_r); - 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.wait().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(&pts) - .unwrap(); + let mut child = pty_process::Command::new("echo") + .arg("bar") + .spawn(&pts) + .unwrap(); - assert_eq!(output.next().await.unwrap(), "bar\r\n"); + assert_eq!(output.next().await.unwrap(), "bar\r\n"); - let status = child.wait().await.unwrap(); - assert_eq!(status.code().unwrap(), 0); - }); + let status = child.wait().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); } #[test] @@ -138,118 +133,98 @@ fn test_multiple_configured() { } #[cfg(feature = "async")] +#[tokio::main] #[test] -fn test_multiple_configured_async() { +async fn test_multiple_configured_async() { use futures::stream::StreamExt as _; use std::os::unix::io::FromRawFd as _; use tokio::io::AsyncBufReadExt as _; - 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 (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'; \ + 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.wait().await.unwrap(); - assert_eq!(status.code().unwrap(), 0); - - let mut child = cmd.spawn(&pts).unwrap(); - - 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.wait().await.unwrap(); - assert_eq!(status.code().unwrap(), 0); + ) + .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.wait().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); + + let mut child = cmd.spawn(&pts).unwrap(); + + 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.wait().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); } #[test] @@ -270,33 +245,28 @@ fn test_controlling_terminal() { } #[cfg(feature = "async")] +#[tokio::main] #[test] -fn test_controlling_terminal_async() { +async fn test_controlling_terminal_async() { use futures::stream::StreamExt as _; - 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; \ + 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(); + ) + .spawn(&pts) + .unwrap(); - let mut output = helpers::output_async(pty_r); - assert_eq!(output.next().await.unwrap(), "true\r\n"); + 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); - }); + let status = child.wait().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); } #[test] @@ -317,29 +287,24 @@ fn test_session_leader() { } #[cfg(feature = "async")] +#[tokio::main] #[test] -fn test_session_leader_async() { +async fn test_session_leader_async() { use futures::stream::StreamExt as _; - 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.wait().await.unwrap(); - eprintln!("{:?}", status); - assert_eq!(status.code().unwrap(), 0); - }); + 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.wait().await.unwrap(); + eprintln!("{:?}", status); + assert_eq!(status.code().unwrap(), 0); } |