Skip to content

Commit ef02e43

Browse files
committed
added configurable terminal size
1 parent 726aefa commit ef02e43

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

tests/common/util.rs

+45-14
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,7 @@ pub struct UCommand {
12291229
stderr_to_stdout: bool,
12301230
timeout: Option<Duration>,
12311231
terminal_simulation: bool,
1232+
terminal_size: Option<libc::winsize>,
12321233
tmpd: Option<Rc<TempDir>>, // drop last
12331234
}
12341235

@@ -1415,6 +1416,12 @@ impl UCommand {
14151416
self
14161417
}
14171418

1419+
pub fn terminal_size(&mut self, win_size: libc::winsize) -> &mut Self {
1420+
self.terminal_simulation(true);
1421+
self.terminal_size = Some(win_size);
1422+
self
1423+
}
1424+
14181425
#[cfg(unix)]
14191426
fn read_from_pty(pty_fd: std::os::fd::OwnedFd, out: File) {
14201427
let read_file = std::fs::File::from(pty_fd);
@@ -1427,7 +1434,7 @@ impl UCommand {
14271434
Err(e) if e.raw_os_error().unwrap_or_default() == 5 => {}
14281435
Err(e) => {
14291436
eprintln!("Unexpected error: {:?}", e);
1430-
assert!(false);
1437+
panic!("error forwarding output of pty");
14311438
}
14321439
}
14331440
}
@@ -1600,12 +1607,12 @@ impl UCommand {
16001607

16011608
#[cfg(unix)]
16021609
if self.terminal_simulation {
1603-
let terminal_size = libc::winsize {
1610+
let terminal_size = self.terminal_size.unwrap_or(libc::winsize {
16041611
ws_col: 80,
16051612
ws_row: 30,
1606-
ws_xpixel: 800,
1607-
ws_ypixel: 300,
1608-
};
1613+
ws_xpixel: 80 * 8,
1614+
ws_ypixel: 30 * 10,
1615+
});
16091616

16101617
let OpenptyResult {
16111618
slave: pi_slave,
@@ -2147,17 +2154,15 @@ impl UChild {
21472154
};
21482155

21492156
if let Some(stdout) = self.captured_stdout.as_mut() {
2150-
stdout
2151-
.reader_thread_handle
2152-
.take()
2153-
.map(|handle| handle.join().unwrap());
2157+
if let Some(handle) = stdout.reader_thread_handle.take() {
2158+
handle.join().unwrap();
2159+
}
21542160
output.stdout = stdout.output_bytes();
21552161
}
21562162
if let Some(stderr) = self.captured_stderr.as_mut() {
2157-
stderr
2158-
.reader_thread_handle
2159-
.take()
2160-
.map(|handle| handle.join().unwrap());
2163+
if let Some(handle) = stderr.reader_thread_handle.take() {
2164+
handle.join().unwrap();
2165+
}
21612166
output.stderr = stderr.output_bytes();
21622167
}
21632168

@@ -3597,7 +3602,33 @@ mod tests {
35973602
.succeeds();
35983603
std::assert_eq!(
35993604
String::from_utf8_lossy(out.stdout()),
3600-
"stdin is atty\r\nstdout is atty\r\nstderr is atty\r\n"
3605+
"stdin is atty\r\nstdout is atty\r\nstderr is atty\r\nterminal size: 30 80\r\n"
3606+
);
3607+
std::assert_eq!(
3608+
String::from_utf8_lossy(out.stderr()),
3609+
"This is an error message.\r\n"
3610+
);
3611+
}
3612+
3613+
#[cfg(unix)]
3614+
#[test]
3615+
fn test_simulation_of_terminal_size_information() {
3616+
let scene = TestScenario::new("util");
3617+
3618+
let out = scene
3619+
.ccmd("env")
3620+
.arg("sh")
3621+
.arg("is_atty.sh")
3622+
.terminal_size(libc::winsize {
3623+
ws_col: 40,
3624+
ws_row: 10,
3625+
ws_xpixel: 40 * 8,
3626+
ws_ypixel: 10 * 10,
3627+
})
3628+
.succeeds();
3629+
std::assert_eq!(
3630+
String::from_utf8_lossy(out.stdout()),
3631+
"stdin is atty\r\nstdout is atty\r\nstderr is atty\r\nterminal size: 10 40\r\n"
36013632
);
36023633
std::assert_eq!(
36033634
String::from_utf8_lossy(out.stderr()),

tests/fixtures/util/is_atty.sh

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fi
1414

1515
if [ -t 2 ] ; then
1616
echo "stderr is atty"
17+
echo "terminal size: $(stty size)"
1718
else
1819
echo "stderr is not atty"
1920
fi

0 commit comments

Comments
 (0)