mirror of https://github.com/astral-sh/ruff
[`configuration`] Fix unclear error messages for line-length values exceeding `u16::MAX` (#21329)
Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
f63a9f2334
commit
deeda56906
|
|
@ -51,13 +51,17 @@ impl<'de> serde::Deserialize<'de> for LineLength {
|
|||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let value = u16::deserialize(deserializer)?;
|
||||
Self::try_from(value).map_err(|_| {
|
||||
serde::de::Error::custom(format!(
|
||||
"line-length must be between 1 and {} (got {value})",
|
||||
Self::MAX,
|
||||
))
|
||||
})
|
||||
let value = i64::deserialize(deserializer)?;
|
||||
|
||||
u16::try_from(value)
|
||||
.ok()
|
||||
.and_then(|u16_value| Self::try_from(u16_value).ok())
|
||||
.ok_or_else(|| {
|
||||
serde::de::Error::custom(format!(
|
||||
"line-length must be between 1 and {} (got {value})",
|
||||
Self::MAX,
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -468,6 +468,62 @@ line-length = 500
|
|||
"line-length must be between 1 and 320 (got 500)"
|
||||
);
|
||||
|
||||
// Test value at u16::MAX boundary (65535) - should show range error
|
||||
let invalid_line_length_65535 = toml::from_str::<Pyproject>(
|
||||
r"
|
||||
[tool.ruff]
|
||||
line-length = 65535
|
||||
",
|
||||
)
|
||||
.expect_err("Deserialization should have failed for line-length at u16::MAX");
|
||||
|
||||
assert_eq!(
|
||||
invalid_line_length_65535.message(),
|
||||
"line-length must be between 1 and 320 (got 65535)"
|
||||
);
|
||||
|
||||
// Test value exceeding u16::MAX (65536) - should show clear error
|
||||
let invalid_line_length_65536 = toml::from_str::<Pyproject>(
|
||||
r"
|
||||
[tool.ruff]
|
||||
line-length = 65536
|
||||
",
|
||||
)
|
||||
.expect_err("Deserialization should have failed for line-length exceeding u16::MAX");
|
||||
|
||||
assert_eq!(
|
||||
invalid_line_length_65536.message(),
|
||||
"line-length must be between 1 and 320 (got 65536)"
|
||||
);
|
||||
|
||||
// Test value far exceeding u16::MAX (99_999) - should show clear error
|
||||
let invalid_line_length_99999 = toml::from_str::<Pyproject>(
|
||||
r"
|
||||
[tool.ruff]
|
||||
line-length = 99_999
|
||||
",
|
||||
)
|
||||
.expect_err("Deserialization should have failed for line-length far exceeding u16::MAX");
|
||||
|
||||
assert_eq!(
|
||||
invalid_line_length_99999.message(),
|
||||
"line-length must be between 1 and 320 (got 99999)"
|
||||
);
|
||||
|
||||
// Test negative value - should show clear error
|
||||
let invalid_line_length_negative = toml::from_str::<Pyproject>(
|
||||
r"
|
||||
[tool.ruff]
|
||||
line-length = -5
|
||||
",
|
||||
)
|
||||
.expect_err("Deserialization should have failed for negative line-length");
|
||||
|
||||
assert_eq!(
|
||||
invalid_line_length_negative.message(),
|
||||
"line-length must be between 1 and 320 (got -5)"
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue