aboutsummaryrefslogtreecommitdiffstats
path: root/tests/winch.rs
blob: 9d26751cd6fbdf3814b5174558a88df4adb83351 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#[test]
fn test_winch_std() {
    use std::io::{Read as _, Write as _};

    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")
        .args(&[
            "-E",
            "$|++; $SIG{WINCH} = sub { say 'WINCH' }; say 'started'; <>",
        ])
        .spawn(pty)
        .unwrap();

    let mut buf = [0u8; 1024];
    let bytes = child.pty().read(&mut buf).unwrap();
    assert_eq!(&buf[..bytes], b"started\r\n");

    child.pty().resize(pty_process::Size::new(25, 80)).unwrap();

    let bytes = child.pty().read(&mut buf).unwrap();
    assert_eq!(&buf[..bytes], b"WINCH\r\n");

    child.pty().write_all(b"\n").unwrap();
    let status = child.wait().unwrap();
    assert_eq!(status.code().unwrap(), 0);
}

#[cfg(feature = "async")]
#[test]
fn test_winch_async_std() {
    use async_std::io::prelude::WriteExt as _;
    use async_std::io::ReadExt as _;

    let status = 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")
            .args(&[
                "-E",
                "$|++; $SIG{WINCH} = sub { say 'WINCH' }; say 'started'; <>",
            ])
            .spawn(pty)
            .unwrap();

        let mut buf = [0u8; 1024];
        let bytes = child.pty().read(&mut buf).await.unwrap();
        assert_eq!(&buf[..bytes], b"started\r\n");

        child.pty().resize(pty_process::Size::new(25, 80)).unwrap();

        let bytes = child.pty().read(&mut buf).await.unwrap();
        assert_eq!(&buf[..bytes], b"WINCH\r\n");

        child.pty().write_all(b"\n").await.unwrap();
        child.status().await.unwrap()
    });
    assert_eq!(status.code().unwrap(), 0);
}

#[cfg(feature = "async")]
#[test]
fn test_winch_smol() {
    use smol::io::{AsyncReadExt as _, AsyncWriteExt as _};

    let status = smol::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")
            .args(&[
                "-E",
                "$|++; $SIG{WINCH} = sub { say 'WINCH' }; say 'started'; <>",
            ])
            .spawn(pty)
            .unwrap();

        let mut buf = [0u8; 1024];
        let bytes = child.pty().read(&mut buf).await.unwrap();
        assert_eq!(&buf[..bytes], b"started\r\n");

        child.pty().resize(pty_process::Size::new(25, 80)).unwrap();

        let bytes = child.pty().read(&mut buf).await.unwrap();
        assert_eq!(&buf[..bytes], b"WINCH\r\n");

        child.pty().write_all(b"\n").await.unwrap();
        child.status().await.unwrap()
    });
    assert_eq!(status.code().unwrap(), 0);
}

#[cfg(feature = "async")]
#[test]
fn test_winch_tokio() {
    use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
    use tokio_util::compat::FuturesAsyncReadCompatExt as _;

    async fn async_test_cat_tokio() -> std::process::ExitStatus {
        let pty = pty_process::Pty::new().unwrap();
        pty.resize(pty_process::Size::new(24, 80)).unwrap();
        let mut child = pty_process::Command::new("perl")
            .args(&[
                "-E",
                "$|++; $SIG{WINCH} = sub { say 'WINCH' }; say 'started'; <>",
            ])
            .spawn(pty)
            .unwrap();

        let mut buf = [0u8; 1024];
        let bytes = child.pty().compat().read(&mut buf).await.unwrap();
        assert_eq!(&buf[..bytes], b"started\r\n");

        child.pty().resize(pty_process::Size::new(25, 80)).unwrap();

        let bytes = child.pty().compat().read(&mut buf).await.unwrap();
        assert_eq!(&buf[..bytes], b"WINCH\r\n");

        child.pty().compat().write_all(b"\n").await.unwrap();
        child.status().await.unwrap()
    }
    tokio::runtime::Runtime::new().unwrap().block_on(async {
        let status = async_test_cat_tokio().await;
        assert_eq!(status.code().unwrap(), 0);
    });
}