Skip to content

Commit

Permalink
added configurable terminal size
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Feb 25, 2024
1 parent a4d5def commit 5d57805
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
59 changes: 45 additions & 14 deletions tests/common/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,7 @@ pub struct UCommand {
stderr_to_stdout: bool,
timeout: Option<Duration>,
terminal_simulation: bool,
terminal_size: Option<libc::winsize>,
tmpd: Option<Rc<TempDir>>, // drop last
}

Expand Down Expand Up @@ -1415,6 +1416,12 @@ impl UCommand {
self
}

pub fn terminal_size(&mut self, win_size: libc::winsize) -> &mut Self {
self.terminal_simulation(true);
self.terminal_size = Some(win_size);
self
}

#[cfg(unix)]
fn read_from_pty(pty_fd: std::os::fd::OwnedFd, out: File) {
let read_file = std::fs::File::from(pty_fd);
Expand All @@ -1427,7 +1434,7 @@ impl UCommand {
Err(e) if e.raw_os_error().unwrap_or_default() == 5 => {}
Err(e) => {
eprintln!("Unexpected error: {:?}", e);
assert!(false);
panic!("error forwarding output of pty");
}
}
}
Expand Down Expand Up @@ -1600,12 +1607,12 @@ impl UCommand {

#[cfg(unix)]
if self.terminal_simulation {
let terminal_size = libc::winsize {
let terminal_size = self.terminal_size.unwrap_or(libc::winsize {
ws_col: 80,
ws_row: 30,
ws_xpixel: 800,
ws_ypixel: 300,
};
ws_xpixel: 80 * 8,
ws_ypixel: 30 * 10,
});

let OpenptyResult {
slave: pi_slave,
Expand Down Expand Up @@ -2147,17 +2154,15 @@ impl UChild {
};

if let Some(stdout) = self.captured_stdout.as_mut() {
stdout
.reader_thread_handle
.take()
.map(|handle| handle.join().unwrap());
if let Some(handle) = stdout.reader_thread_handle.take() {
handle.join().unwrap();
}
output.stdout = stdout.output_bytes();
}
if let Some(stderr) = self.captured_stderr.as_mut() {
stderr
.reader_thread_handle
.take()
.map(|handle| handle.join().unwrap());
if let Some(handle) = stderr.reader_thread_handle.take() {
handle.join().unwrap();
}
output.stderr = stderr.output_bytes();
}

Expand Down Expand Up @@ -3597,7 +3602,33 @@ mod tests {
.succeeds();
std::assert_eq!(
String::from_utf8_lossy(out.stdout()),
"stdin is atty\r\nstdout is atty\r\nstderr is atty\r\n"
"stdin is atty\r\nstdout is atty\r\nstderr is atty\r\nterminal size: 30 80\r\n"
);
std::assert_eq!(
String::from_utf8_lossy(out.stderr()),
"This is an error message.\r\n"
);
}

#[cfg(unix)]
#[test]
fn test_simulation_of_terminal_size_information() {
let scene = TestScenario::new("util");

let out = scene
.ccmd("env")
.arg("sh")
.arg("is_atty.sh")
.terminal_size(libc::winsize {
ws_col: 40,
ws_row: 10,
ws_xpixel: 40 * 8,
ws_ypixel: 10 * 10,
})
.succeeds();
std::assert_eq!(
String::from_utf8_lossy(out.stdout()),
"stdin is atty\r\nstdout is atty\r\nstderr is atty\r\nterminal size: 10 40\r\n"
);
std::assert_eq!(
String::from_utf8_lossy(out.stderr()),
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/util/is_atty.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fi

if [ -t 2 ] ; then
echo "stderr is atty"
echo "terminal size: $(stty size)"
else
echo "stderr is not atty"
fi
Expand Down

0 comments on commit 5d57805

Please sign in to comment.