summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/tests/termios/isatty.rs
blob: 5e6fae73d600a105bb2d2430a91fa3a7b23ec589 (plain)
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
use rustix::fd::AsRawFd;
use rustix::termios::{isatty, tcgetwinsize};
use tempfile::{tempdir, TempDir};

#[allow(unused)]
fn tmpdir() -> TempDir {
    tempdir().expect("expected to be able to create a temporary directory")
}

#[test]
fn std_file_is_not_terminal() {
    let tmpdir = tempfile::tempdir().unwrap();
    assert!(!isatty(
        &std::fs::File::create(tmpdir.path().join("file")).unwrap()
    ));
    assert!(!isatty(
        &std::fs::File::open(tmpdir.path().join("file")).unwrap()
    ));
}

#[test]
fn stdout_stderr_terminals() {
    // This test is flaky under qemu.
    if std::env::vars().any(|var| var.0.starts_with("CARGO_TARGET_") && var.0.ends_with("_RUNNER"))
    {
        return;
    }

    // Compare `isatty` against `libc::isatty`.
    assert_eq!(isatty(&std::io::stdout()), unsafe {
        libc::isatty(std::io::stdout().as_raw_fd()) != 0
    });
    assert_eq!(isatty(&std::io::stderr()), unsafe {
        libc::isatty(std::io::stderr().as_raw_fd()) != 0
    });

    // Compare `isatty` against `tcgetwinsize`.
    assert_eq!(
        isatty(&std::io::stdout()),
        tcgetwinsize(&std::io::stdout()).is_ok()
    );
    assert_eq!(
        isatty(&std::io::stderr()),
        tcgetwinsize(&std::io::stderr()).is_ok()
    );
}

#[test]
fn stdio_descriptors() {
    #[cfg(unix)]
    use std::os::unix::io::AsRawFd;
    #[cfg(target_os = "wasi")]
    use std::os::wasi::io::AsRawFd;

    unsafe {
        assert_eq!(
            rustix::io::stdin().as_raw_fd(),
            std::io::stdin().as_raw_fd()
        );
        assert_eq!(
            rustix::io::stdout().as_raw_fd(),
            std::io::stdout().as_raw_fd()
        );
        assert_eq!(
            rustix::io::stderr().as_raw_fd(),
            std::io::stderr().as_raw_fd()
        );
    }
}