Skip to content

Commit

Permalink
Merge pull request #5159 from bluelief/fix-fmt-check-opt_width
Browse files Browse the repository at this point in the history
fmt: fix panic on width argument
  • Loading branch information
cakebaker authored Aug 15, 2023
2 parents 6ffea22 + a7ab660 commit 7fcff30
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/uu/fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec<String>, 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,
Expand Down
32 changes: 32 additions & 0 deletions tests/by-util/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,35 @@ fn test_fmt_width_too_big() {
.stderr_is("fmt: invalid width: '2501': Numerical result out of range\n");
}
}

#[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 a\nfile with one\nword per line\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");
}
}

0 comments on commit 7fcff30

Please sign in to comment.