From 946e1bb2d128e57ad23fb78cf180be2659fec3d9 Mon Sep 17 00:00:00 2001 From: Bluelief <33663724+bluelief@users.noreply.github.com> Date: Mon, 14 Aug 2023 22:44:55 +0900 Subject: [PATCH 1/2] fmt: fix panic on width argument --- src/uu/fmt/src/fmt.rs | 2 +- tests/by-util/test_fmt.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index f112d1b07..bf08fbc3e 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -168,7 +168,7 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec, FmtOptions) )); } }; - if !matches.get_flag(OPT_WIDTH) { + if !matches.contains_id(OPT_WIDTH) { fmt_opts.width = cmp::max( fmt_opts.goal * 100 / DEFAULT_GOAL_TO_WIDTH_RATIO, fmt_opts.goal + 3, diff --git a/tests/by-util/test_fmt.rs b/tests/by-util/test_fmt.rs index afce9acd8..c2f67e136 100644 --- a/tests/by-util/test_fmt.rs +++ b/tests/by-util/test_fmt.rs @@ -43,3 +43,34 @@ fn test_fmt_width_too_big() { .stderr_is("fmt: invalid width: '2501': Numerical result out of range\n"); } } + +#[test] +fn test_fmt_goal() { + for param in ["-g", "--goal"] { + new_ucmd!() + .args(&["one-word-per-line.txt", param, "7"]) + .succeeds() + .stdout_is("this is\na file\nwith one\nword per\nline\n"); + } +} + +#[test] +fn test_fmt_goal_too_big() { + for param in ["-g", "--goal"] { + new_ucmd!() + .args(&["one-word-per-line.txt", "--width=75", param, "76"]) + .fails() + .code_is(1) + .stderr_is("fmt: GOAL cannot be greater than WIDTH.\n"); + } +} + +#[test] +fn test_fmt_set_goal_not_contain_width() { + for param in ["-g", "--goal"] { + new_ucmd!() + .args(&["one-word-per-line.txt", param, "74"]) + .succeeds() + .stdout_is("this is a file with one word per line\n"); + } +} From a7ab660a3859b7b24f3b8a46dc2675a0aa717bf4 Mon Sep 17 00:00:00 2001 From: Bluelief <33663724+bluelief@users.noreply.github.com> Date: Tue, 15 Aug 2023 00:43:32 +0900 Subject: [PATCH 2/2] fmt: change test to ignore and fix test output - fix test to get same result as GNU fmt --- tests/by-util/test_fmt.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/by-util/test_fmt.rs b/tests/by-util/test_fmt.rs index c2f67e136..84a19d34d 100644 --- a/tests/by-util/test_fmt.rs +++ b/tests/by-util/test_fmt.rs @@ -44,13 +44,14 @@ fn test_fmt_width_too_big() { } } +#[ignore] #[test] fn test_fmt_goal() { for param in ["-g", "--goal"] { new_ucmd!() .args(&["one-word-per-line.txt", param, "7"]) .succeeds() - .stdout_is("this is\na file\nwith one\nword per\nline\n"); + .stdout_is("this is a\nfile with one\nword per line\n"); } }