Merge pull request #6096 from cakebaker/fmt_fail_if_goal_bigger_than_default_width

fmt: fail if goal is bigger than default width
This commit is contained in:
Sylvestre Ledru 2024-03-20 11:10:48 +01:00 committed by GitHub
commit a3ff7b7719
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 4 deletions

View file

@ -21,6 +21,10 @@ mod parasplit;
const ABOUT: &str = help_about!("fmt.md");
const USAGE: &str = help_usage!("fmt.md");
const MAX_WIDTH: usize = 2500;
const DEFAULT_GOAL: usize = 70;
const DEFAULT_WIDTH: usize = 75;
// by default, goal is 93% of width
const DEFAULT_GOAL_TO_WIDTH_RATIO: usize = 93;
mod options {
pub const CROWN_MARGIN: &str = "crown-margin";
@ -39,9 +43,6 @@ mod options {
pub const FILES: &str = "files";
}
// by default, goal is 93% of width
const DEFAULT_GOAL_TO_WIDTH_RATIO: usize = 93;
pub type FileOrStdReader = BufReader<Box<dyn Read + 'static>>;
pub struct FmtOptions {
crown: bool,
@ -100,10 +101,13 @@ impl FmtOptions {
(w, g)
}
(None, Some(&g)) => {
if g > DEFAULT_WIDTH {
return Err(USimpleError::new(1, "GOAL cannot be greater than WIDTH."));
}
let w = (g * 100 / DEFAULT_GOAL_TO_WIDTH_RATIO).max(g + 3);
(w, g)
}
(None, None) => (75, 70),
(None, None) => (DEFAULT_WIDTH, DEFAULT_GOAL),
};
debug_assert!(width >= goal, "GOAL {goal} should not be greater than WIDTH {width} when given {width_opt:?} and {goal_opt:?}.");

View file

@ -93,6 +93,17 @@ fn test_fmt_goal_too_big() {
}
}
#[test]
fn test_fmt_goal_bigger_than_default_width_of_75() {
for param in ["-g", "--goal"] {
new_ucmd!()
.args(&["one-word-per-line.txt", param, "76"])
.fails()
.code_is(1)
.stderr_is("fmt: GOAL cannot be greater than WIDTH.\n");
}
}
#[test]
fn test_fmt_invalid_goal() {
for param in ["-g", "--goal"] {