From a3d05e5945c09faad6107290e1afd8534e7afbc0 Mon Sep 17 00:00:00 2001 From: ahmadabd Date: Sun, 5 May 2024 14:17:52 +0330 Subject: [PATCH] fmt: value of minlength should not be negative closes #6354 --- src/uu/fmt/src/linebreak.rs | 2 +- tests/by-util/test_fmt.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/uu/fmt/src/linebreak.rs b/src/uu/fmt/src/linebreak.rs index b8c592d3f..aa1477eba 100644 --- a/src/uu/fmt/src/linebreak.rs +++ b/src/uu/fmt/src/linebreak.rs @@ -239,7 +239,7 @@ fn find_kp_breakpoints<'a, T: Iterator>>( let mut next_active_breaks = vec![]; let stretch = args.opts.width - args.opts.goal; - let minlength = args.opts.goal - stretch; + let minlength = args.opts.goal.max(stretch + 1) - stretch; let mut new_linebreaks = vec![]; let mut is_sentence_start = false; let mut least_demerits = 0; diff --git a/tests/by-util/test_fmt.rs b/tests/by-util/test_fmt.rs index 3c5c37cc7..d56c4f760 100644 --- a/tests/by-util/test_fmt.rs +++ b/tests/by-util/test_fmt.rs @@ -156,6 +156,16 @@ fn test_fmt_too_big_goal_sometimes_okay() { .stdout_is("this is a\nfile with one\nword per line\n"); } +#[test] +fn test_fmt_goal_too_small_to_check_negative_minlength() { + for param in ["-g", "--goal"] { + new_ucmd!() + .args(&["one-word-per-line.txt", "--width=75", param, "10"]) + .succeeds() + .stdout_is("this is a file with one word per line\n"); + } +} + #[test] fn test_fmt_non_existent_file() { new_ucmd!()