Skip to content

Commit

Permalink
windows compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Jan 23, 2024
1 parent 64f1276 commit 7bfed9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
5 changes: 5 additions & 0 deletions tests/by-util/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!());
Expand All @@ -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!());
Expand All @@ -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!());
Expand All @@ -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!());
Expand All @@ -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!());
Expand Down
25 changes: 16 additions & 9 deletions tests/common/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand All @@ -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};
Expand All @@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -1429,6 +1431,7 @@ impl UCommand {
}
}

#[cfg(unix)]
fn spawn_reader_thread(
&self,
captured_output: Option<CapturedOutput>,
Expand Down Expand Up @@ -1480,7 +1483,7 @@ impl UCommand {
Command,
Option<CapturedOutput>,
Option<CapturedOutput>,
Option<OwnedFd>,
Option<File>,
) {
if self.bin_path.is_some() {
if let Some(util_name) = &self.util_name {
Expand Down Expand Up @@ -1560,7 +1563,10 @@ impl UCommand {

let mut captured_stdout = None;
let mut captured_stderr = None;
let mut stdin_pty: Option<OwnedFd> = None;
#[cfg(unix)]
let mut stdin_pty: Option<File> = None;
#[cfg(not(unix))]
let stdin_pty: Option<File> = None;
if self.stderr_to_stdout {
let mut output = CapturedOutput::default();

Expand Down Expand Up @@ -1594,6 +1600,7 @@ impl UCommand {
.stderr(stderr);
};

#[cfg(unix)]
if self.terminal_simulation {
let terminal_size = libc::winsize {
ws_col: 80,
Expand All @@ -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());
Expand Down Expand Up @@ -1935,7 +1942,7 @@ pub struct UChild {
util_name: Option<String>,
captured_stdout: Option<CapturedOutput>,
captured_stderr: Option<CapturedOutput>,
stdin_pty: Option<OwnedFd>,
stdin_pty: Option<File>,
ignore_stdin_write_error: bool,
stderr_to_stdout: bool,
join_handle: Option<JoinHandle<io::Result<()>>>,
Expand All @@ -1949,7 +1956,7 @@ impl UChild {
child: Child,
captured_stdout: Option<CapturedOutput>,
captured_stderr: Option<CapturedOutput>,
stdin_pty: Option<OwnedFd>,
stdin_pty: Option<File>,
) -> Self {
Self {
raw: child,
Expand Down Expand Up @@ -2316,7 +2323,7 @@ impl UChild {

fn access_stdin_as_writer<'a>(&'a mut self) -> Box<dyn Write + Send + 'a> {
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))
Expand All @@ -2325,7 +2332,7 @@ impl UChild {

fn take_stdin_as_writer(&mut self) -> Box<dyn Write + Send> {
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
Expand Down

0 comments on commit 7bfed9e

Please sign in to comment.