aboutsummaryrefslogtreecommitdiffstats
path: root/tests/behavior.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-30 15:43:21 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-30 15:43:21 -0500
commitd1a9c4d1d878e7bcb4caac766fabcfb48fe418f2 (patch)
treef54c8030ea9fcdbd06da6c6c119575a0c142ce64 /tests/behavior.rs
parent0eaa0416fe949d8b3f5273f2677725b113ce17d0 (diff)
downloadpty-process-d1a9c4d1d878e7bcb4caac766fabcfb48fe418f2.tar.gz
pty-process-d1a9c4d1d878e7bcb4caac766fabcfb48fe418f2.zip
improve tests
Diffstat (limited to 'tests/behavior.rs')
-rw-r--r--tests/behavior.rs138
1 files changed, 138 insertions, 0 deletions
diff --git a/tests/behavior.rs b/tests/behavior.rs
new file mode 100644
index 0000000..ef7d54f
--- /dev/null
+++ b/tests/behavior.rs
@@ -0,0 +1,138 @@
+mod helpers;
+
+#[test]
+fn test_multiple() {
+ let pty = pty_process::blocking::Pty::new().unwrap();
+ pty.resize(pty_process::Size::new(24, 80)).unwrap();
+
+ let mut child = pty_process::blocking::Command::new("echo")
+ .arg("foo")
+ .spawn(&pty)
+ .unwrap();
+
+ let mut output = helpers::output(&pty);
+ assert_eq!(output.next().unwrap(), "foo\r\n");
+
+ let status = child.wait().unwrap();
+ assert_eq!(status.code().unwrap(), 0);
+
+ let mut child = pty_process::blocking::Command::new("echo")
+ .arg("bar")
+ .spawn(&pty)
+ .unwrap();
+
+ let mut output = helpers::output(&pty);
+ assert_eq!(output.next().unwrap(), "bar\r\n");
+
+ let status = child.wait().unwrap();
+ assert_eq!(status.code().unwrap(), 0);
+}
+
+#[cfg(feature = "async")]
+#[test]
+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();
+
+ let mut child = pty_process::Command::new("echo")
+ .arg("foo")
+ .spawn(&pty)
+ .unwrap();
+
+ let mut output = helpers::output_async(&pty);
+ assert_eq!(output.next().await.unwrap(), "foo\r\n");
+
+ let status = child.status().await.unwrap();
+ assert_eq!(status.code().unwrap(), 0);
+
+ let mut child = pty_process::Command::new("echo")
+ .arg("bar")
+ .spawn(&pty)
+ .unwrap();
+
+ let mut output = helpers::output_async(&pty);
+ assert_eq!(output.next().await.unwrap(), "bar\r\n");
+
+ let status = child.status().await.unwrap();
+ assert_eq!(status.code().unwrap(), 0);
+ });
+}
+
+#[test]
+fn test_controlling_terminal() {
+ let pty = pty_process::blocking::Pty::new().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)
+ .unwrap();
+
+ let mut output = helpers::output(&pty);
+ assert_eq!(output.next().unwrap(), "true\r\n");
+
+ let status = child.wait().unwrap();
+ assert_eq!(status.code().unwrap(), 0);
+}
+
+#[cfg(feature = "async")]
+#[test]
+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);
+ });
+}
+
+#[test]
+fn test_session_leader() {
+ let pty = pty_process::blocking::Pty::new().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)
+ .unwrap();
+
+ let mut output = helpers::output(&pty);
+ assert_eq!(output.next().unwrap(), "True\r\n");
+
+ let status = child.wait().unwrap();
+ assert_eq!(status.code().unwrap(), 0);
+}
+
+#[cfg(feature = "async")]
+#[test]
+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);
+ assert_eq!(output.next().await.unwrap(), "True\r\n");
+ }
+
+ let status = child.status().await.unwrap();
+ assert_eq!(status.code().unwrap(), 0);
+ });
+}