1use rustix::fd::AsRawFd;
2use rustix::termios::{isatty, tcgetwinsize};
3use tempfile::{tempdir, TempDir};
4
5#[allow(unused)]
6fn tmpdir() -> TempDir {
7    tempdir().expect("expected to be able to create a temporary directory")
8}
9
10#[test]
11fn std_file_is_not_terminal() {
12    let tmpdir = tempfile::tempdir().unwrap();
13    assert!(!isatty(
14        &std::fs::File::create(tmpdir.path().join("file")).unwrap()
15    ));
16    assert!(!isatty(
17        &std::fs::File::open(tmpdir.path().join("file")).unwrap()
18    ));
19}
20
21#[test]
22fn stdout_stderr_terminals() {
23    // This test is flaky under qemu.
24    if std::env::vars().any(|var| var.0.starts_with("CARGO_TARGET_") && var.0.ends_with("_RUNNER"))
25    {
26        return;
27    }
28
29    // Compare `isatty` against `libc::isatty`.
30    assert_eq!(isatty(&std::io::stdout()), unsafe {
31        libc::isatty(std::io::stdout().as_raw_fd()) != 0
32    });
33    assert_eq!(isatty(&std::io::stderr()), unsafe {
34        libc::isatty(std::io::stderr().as_raw_fd()) != 0
35    });
36
37    // Compare `isatty` against `tcgetwinsize`.
38    assert_eq!(
39        isatty(&std::io::stdout()),
40        tcgetwinsize(&std::io::stdout()).is_ok()
41    );
42    assert_eq!(
43        isatty(&std::io::stderr()),
44        tcgetwinsize(&std::io::stderr()).is_ok()
45    );
46}
47
48#[test]
49fn stdio_descriptors() {
50    #[cfg(unix)]
51    use std::os::unix::io::AsRawFd;
52    #[cfg(target_os = "wasi")]
53    use std::os::wasi::io::AsRawFd;
54
55    unsafe {
56        assert_eq!(
57            rustix::io::stdin().as_raw_fd(),
58            std::io::stdin().as_raw_fd()
59        );
60        assert_eq!(
61            rustix::io::stdout().as_raw_fd(),
62            std::io::stdout().as_raw_fd()
63        );
64        assert_eq!(
65            rustix::io::stderr().as_raw_fd(),
66            std::io::stderr().as_raw_fd()
67        );
68    }
69}
70