diff --git a/tests/by-util/test_env.rs b/tests/by-util/test_env.rs index b19870ee64c..ebc554a6817 100644 --- a/tests/by-util/test_env.rs +++ b/tests/by-util/test_env.rs @@ -246,6 +246,7 @@ fn test_fail_change_directory() { assert!(out.contains("env: cannot change directory to ")); } +#[cfg(unix)] #[test] fn test_simulation_of_terminal_false() { let scene = TestScenario::new(util_name!()); @@ -261,6 +262,7 @@ fn test_simulation_of_terminal_false() { ); } +#[cfg(unix)] #[test] fn test_simulation_of_terminal_true() { let scene = TestScenario::new(util_name!()); @@ -281,6 +283,7 @@ fn test_simulation_of_terminal_true() { ); } +#[cfg(unix)] #[test] fn test_simulation_of_terminal_pty_sends_eot_automatically() { let scene = TestScenario::new(util_name!()); @@ -296,6 +299,7 @@ fn test_simulation_of_terminal_pty_sends_eot_automatically() { assert_eq!(String::from_utf8_lossy(out.stderr()), ""); } +#[cfg(unix)] #[test] fn test_simulation_of_terminal_pty_pipes_into_data_and_sends_eot_automatically() { let scene = TestScenario::new(util_name!()); @@ -316,6 +320,7 @@ fn test_simulation_of_terminal_pty_pipes_into_data_and_sends_eot_automatically() assert_eq!(String::from_utf8_lossy(out.stderr()), ""); } +#[cfg(unix)] #[test] fn test_simulation_of_terminal_pty_write_in_data_and_sends_eot_automatically() { let scene = TestScenario::new(util_name!()); diff --git a/tests/common/util.rs b/tests/common/util.rs index dfaa1b9112c..71f7819db65 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -7,6 +7,7 @@ #![allow(dead_code)] +#[cfg(unix)] use nix::pty::OpenptyResult; use pretty_assertions::assert_eq; #[cfg(any(target_os = "linux", target_os = "android"))] @@ -21,6 +22,7 @@ use std::ffi::CString; use std::ffi::{OsStr, OsString}; use std::fs::{self, hard_link, remove_file, File, OpenOptions}; use std::io::{self, BufWriter, Read, Result, Write}; +#[cfg(unix)] use std::os::fd::OwnedFd; #[cfg(unix)] use std::os::unix::fs::{symlink as symlink_dir, symlink as symlink_file, PermissionsExt}; @@ -29,8 +31,6 @@ use std::os::unix::process::ExitStatusExt; #[cfg(windows)] use std::os::windows::fs::{symlink_dir, symlink_file}; #[cfg(windows)] -use std::os::windows::process::CommandExt; -#[cfg(windows)] use std::path::MAIN_SEPARATOR; use std::path::{Path, PathBuf}; use std::process::{Child, Command, ExitStatus, Output, Stdio}; @@ -1411,11 +1411,13 @@ impl UCommand { /// Set if process should be run in a simulated terminal (unix: pty, windows: ConPTY[not yet supported]) /// This is useful to test behavior that is only active if [`stdout.is_terminal()`] is [`true`]. + #[cfg(unix)] pub fn terminal_simulation(&mut self, enable: bool) -> &mut Self { self.terminal_simulation = enable; self } + #[cfg(unix)] fn read_string_from_pty(pty_fd: std::os::fd::OwnedFd, buffer_out: &mut String) { let result = std::fs::File::from(pty_fd).read_to_string(buffer_out); match result { @@ -1429,6 +1431,7 @@ impl UCommand { } } + #[cfg(unix)] fn spawn_reader_thread( &self, captured_output: Option, @@ -1480,7 +1483,7 @@ impl UCommand { Command, Option, Option, - Option, + Option, ) { if self.bin_path.is_some() { if let Some(util_name) = &self.util_name { @@ -1560,7 +1563,10 @@ impl UCommand { let mut captured_stdout = None; let mut captured_stderr = None; - let mut stdin_pty: Option = None; + #[cfg(unix)] + let mut stdin_pty: Option = None; + #[cfg(not(unix))] + let stdin_pty: Option = None; if self.stderr_to_stdout { let mut output = CapturedOutput::default(); @@ -1594,6 +1600,7 @@ impl UCommand { .stderr(stderr); }; + #[cfg(unix)] if self.terminal_simulation { let terminal_size = libc::winsize { ws_col: 80, @@ -1615,7 +1622,7 @@ impl UCommand { master: pe_master, } = nix::pty::openpty(&terminal_size, None).unwrap(); - stdin_pty = Some(pi_master); + stdin_pty = Some(File::from(pi_master)); captured_stdout = self.spawn_reader_thread(captured_stdout, po_master, "stdout_reader".to_string()); @@ -1935,7 +1942,7 @@ pub struct UChild { util_name: Option, captured_stdout: Option, captured_stderr: Option, - stdin_pty: Option, + stdin_pty: Option, ignore_stdin_write_error: bool, stderr_to_stdout: bool, join_handle: Option>>, @@ -1949,7 +1956,7 @@ impl UChild { child: Child, captured_stdout: Option, captured_stderr: Option, - stdin_pty: Option, + stdin_pty: Option, ) -> Self { Self { raw: child, @@ -2316,7 +2323,7 @@ impl UChild { fn access_stdin_as_writer<'a>(&'a mut self) -> Box { if let Some(stdin_fd) = &self.stdin_pty { - Box::new(BufWriter::new(File::from(stdin_fd.try_clone().unwrap()))) + Box::new(BufWriter::new(stdin_fd.try_clone().unwrap())) } else { let stdin: &mut std::process::ChildStdin = self.raw.stdin.as_mut().unwrap(); Box::new(BufWriter::new(stdin)) @@ -2325,7 +2332,7 @@ impl UChild { fn take_stdin_as_writer(&mut self) -> Box { if let Some(stdin_fd) = mem::take(&mut self.stdin_pty) { - Box::new(BufWriter::new(File::from(stdin_fd))) + Box::new(BufWriter::new(stdin_fd)) } else { let stdin = self .raw