Skip to content

Commit 96937d3

Browse files
committed
Adding integration tests for the COLUMNS env variable support in stty
1 parent 3a7385c commit 96937d3

File tree

2 files changed

+66
-16
lines changed

2 files changed

+66
-16
lines changed

src/uu/stty/src/stty.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ struct WrappedPrinter {
500500
}
501501

502502
impl WrappedPrinter {
503-
fn new(term_size: &Option<TermSize>) -> Self {
503+
fn new(term_size: Option<&TermSize>) -> Self {
504504
let columns = match term_size {
505505
Some(term_size) => term_size.columns,
506506
None => {
@@ -549,8 +549,8 @@ impl WrappedPrinter {
549549
fn print_terminal_size(
550550
termios: &Termios,
551551
opts: &Options,
552-
window_size: &Option<TermSize>,
553-
term_size: &Option<TermSize>,
552+
window_size: Option<&TermSize>,
553+
term_size: Option<&TermSize>,
554554
) -> nix::Result<()> {
555555
let speed = cfgetospeed(termios);
556556
let mut printer = WrappedPrinter::new(window_size);
@@ -769,7 +769,7 @@ fn control_char_to_string(cc: nix::libc::cc_t) -> nix::Result<String> {
769769
fn print_control_chars(
770770
termios: &Termios,
771771
opts: &Options,
772-
term_size: &Option<TermSize>,
772+
term_size: Option<&TermSize>,
773773
) -> nix::Result<()> {
774774
if !opts.all {
775775
// Print only control chars that differ from sane defaults
@@ -825,8 +825,7 @@ fn print_settings(termios: &Termios, opts: &Options) -> nix::Result<()> {
825825
let device_fd = opts.file.as_raw_fd();
826826
let term_size = {
827827
let mut term_size = TermSize::default();
828-
let term_size =
829-
unsafe { tiocgwinsz(device_fd, &raw mut term_size) }.map(|_| term_size);
828+
let term_size = unsafe { tiocgwinsz(device_fd, &raw mut term_size) }.map(|_| term_size);
830829
if opts.all {
831830
Some(term_size?)
832831
} else {
@@ -835,21 +834,21 @@ fn print_settings(termios: &Termios, opts: &Options) -> nix::Result<()> {
835834
};
836835

837836
let stdout_fd = stdout().as_raw_fd();
838-
let window_size = if device_fd != stdout_fd {
837+
let window_size = if device_fd == stdout_fd {
838+
&term_size
839+
} else {
839840
let mut term_size = TermSize::default();
840841
&unsafe { tiocgwinsz(stdout_fd, &raw mut term_size) }
841842
.map(|_| term_size)
842843
.ok()
843-
} else {
844-
&term_size
845844
};
846845

847-
print_terminal_size(termios, opts, &window_size, &term_size)?;
848-
print_control_chars(termios, opts, &window_size)?;
849-
print_flags(termios, opts, CONTROL_FLAGS, &window_size);
850-
print_flags(termios, opts, INPUT_FLAGS, &window_size);
851-
print_flags(termios, opts, OUTPUT_FLAGS, &window_size);
852-
print_flags(termios, opts, LOCAL_FLAGS, &window_size);
846+
print_terminal_size(termios, opts, window_size.as_ref(), term_size.as_ref())?;
847+
print_control_chars(termios, opts, window_size.as_ref())?;
848+
print_flags(termios, opts, CONTROL_FLAGS, window_size.as_ref());
849+
print_flags(termios, opts, INPUT_FLAGS, window_size.as_ref());
850+
print_flags(termios, opts, OUTPUT_FLAGS, window_size.as_ref());
851+
print_flags(termios, opts, LOCAL_FLAGS, window_size.as_ref());
853852
}
854853
Ok(())
855854
}
@@ -858,7 +857,7 @@ fn print_flags<T: TermiosFlag>(
858857
termios: &Termios,
859858
opts: &Options,
860859
flags: &[Flag<T>],
861-
term_size: &Option<TermSize>,
860+
term_size: Option<&TermSize>,
862861
) {
863862
let mut printer = WrappedPrinter::new(term_size);
864863
for &Flag {

tests/by-util/test_stty.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,54 @@ fn non_negatable_combo() {
349349
.fails()
350350
.stderr_contains("invalid argument '-ek'");
351351
}
352+
353+
#[test]
354+
#[cfg(unix)]
355+
fn test_columns_env_wrapping() {
356+
let (path, _controller, _replica) = pty_path();
357+
358+
for (columns, max_len) in [("20", 20), ("40", 40), ("50", 50)] {
359+
let result = new_ucmd!()
360+
.args(&["--all", "--file", &path])
361+
.env("COLUMNS", columns)
362+
.succeeds();
363+
364+
for line in result.stdout_str().lines() {
365+
assert!(
366+
line.len() <= max_len,
367+
"Line exceeds COLUMNS={columns}: '{line}'"
368+
);
369+
}
370+
}
371+
372+
// Wide columns should allow longer lines
373+
let result = new_ucmd!()
374+
.args(&["--all", "--file", &path])
375+
.env("COLUMNS", "200")
376+
.succeeds();
377+
let has_long_line = result.stdout_str().lines().any(|line| line.len() > 80);
378+
assert!(
379+
has_long_line,
380+
"Expected at least one line longer than 80 chars with COLUMNS=200"
381+
);
382+
383+
// Invalid values should fall back to default
384+
for invalid in ["invalid", "0", "-10"] {
385+
new_ucmd!()
386+
.args(&["--all", "--file", &path])
387+
.env("COLUMNS", invalid)
388+
.succeeds();
389+
}
390+
391+
// Without --all flag
392+
let result = new_ucmd!()
393+
.args(&["--file", &path])
394+
.env("COLUMNS", "30")
395+
.succeeds();
396+
for line in result.stdout_str().lines() {
397+
assert!(
398+
line.len() <= 30,
399+
"Line exceeds COLUMNS=30 without --all: '{line}'"
400+
);
401+
}
402+
}

0 commit comments

Comments
 (0)