aboutsummaryrefslogtreecommitdiffstats
path: root/tests/winch.rs
blob: 6b16ca94c8f692a20fafa923d4ed7cdd3c539647 (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
mod helpers;

#[test]
fn test_winch_std() {
    use std::io::Write as _;

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

    let mut output = helpers::output(&pty);
    assert_eq!(output.next().unwrap(), "started\r\n");

    pty.resize(pty_process::Size::new(25, 80)).unwrap();
    assert_eq!(output.next().unwrap(), "WINCH\r\n");

    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() {
    use futures::stream::StreamExt as _;
    use tokio::io::AsyncWriteExt as _;

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

            let (pty_r, mut pty_w) = pty.split();
            let mut output = helpers::output_async(pty_r);
            assert_eq!(output.next().await.unwrap(), "started\r\n");

            pty_w.resize(pty_process::Size::new(25, 80)).unwrap();
            assert_eq!(output.next().await.unwrap(), "WINCH\r\n");

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