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

#[test]
fn test_fds() {
    check_open_fds();

    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("-Efor my $fd (0..255) { open my $fh, \"<&=$fd\"; print $fd if stat $fh }; say")
        .spawn(&pts)
        .unwrap();

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

    let status = child.wait().unwrap();
    assert_eq!(status.code().unwrap(), 0);
    drop(pty);
    drop(pts);
    check_open_fds();

    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("-Efor my $fd (0..255) { open my $fh, \"<&=$fd\"; print $fd if stat $fh }; say")
        .stderr(std::process::Stdio::null())
        .spawn(&pts)
        .unwrap();

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

    let status = child.wait().unwrap();
    assert_eq!(status.code().unwrap(), 0);
    drop(pty);
    drop(pts);
    check_open_fds();
}

#[track_caller]
fn check_open_fds() {
    let open: Vec<_> = (0..=255)
        .filter(|fd| nix::sys::stat::fstat(*fd).is_ok())
        .collect();
    assert_eq!(open, vec![0, 1, 2]);
}