Improve `requirements-txt` error formatting (#1026)

- Wrap filename in quotes
- Only show the start position (I think the end is a bit noisy)
This commit is contained in:
Charlie Marsh 2024-01-22 08:42:17 -05:00 committed by GitHub
parent 765e3175e1
commit db0c76c4ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 25 deletions

View File

@ -99,7 +99,7 @@ fn invalid_requirements_txt_requirement() -> Result<()> {
----- stdout ----- ----- stdout -----
----- stderr ----- ----- stderr -----
error: Couldn't parse requirement in requirements.txt position 0 to 12 error: Couldn't parse requirement in `requirements.txt` at position 0
Caused by: after parsing 1.0, found ".x" after it, which is not part of a valid version Caused by: after parsing 1.0, found ".x" after it, which is not part of a valid version
flask==1.0.x flask==1.0.x
^^^^^^^ ^^^^^^^

View File

@ -593,14 +593,11 @@ impl Display for RequirementsTxtParserError {
RequirementsTxtParserError::UnsupportedRequirement { start, end, .. } => { RequirementsTxtParserError::UnsupportedRequirement { start, end, .. } => {
write!(f, "Unsupported requirement in position {start} to {end}") write!(f, "Unsupported requirement in position {start} to {end}")
} }
RequirementsTxtParserError::Pep508 { start, end, .. } => { RequirementsTxtParserError::Pep508 { start, .. } => {
write!(f, "Couldn't parse requirement in position {start} to {end}") write!(f, "Couldn't parse requirement at position {start}")
} }
RequirementsTxtParserError::Subfile { start, end, .. } => { RequirementsTxtParserError::Subfile { start, .. } => {
write!( write!(f, "Error parsing included file at position {start}")
f,
"Error parsing file included at position {start} to {end}"
)
} }
} }
} }
@ -627,25 +624,22 @@ impl Display for RequirementsTxtFileError {
RequirementsTxtParserError::InvalidEditablePath(given) => { RequirementsTxtParserError::InvalidEditablePath(given) => {
write!( write!(
f, f,
"Invalid editable path in {}: {given}", "Invalid editable path in `{}`: {given}",
self.file.display() self.file.display()
) )
} }
RequirementsTxtParserError::UnsupportedUrl(url) => { RequirementsTxtParserError::UnsupportedUrl(url) => {
write!( write!(
f, f,
"Unsupported URL (expected a `file://` scheme) in {}: {}", "Unsupported URL (expected a `file://` scheme) in `{}`: {url}",
self.file.display(), self.file.display(),
url
) )
} }
RequirementsTxtParserError::Parser { message, location } => { RequirementsTxtParserError::Parser { message, location } => {
write!( write!(
f, f,
"{} in {} position {}", "{message} in `{}` at position {location}",
message,
self.file.display(), self.file.display(),
location
) )
} }
RequirementsTxtParserError::UnsupportedRequirement { start, end, .. } => { RequirementsTxtParserError::UnsupportedRequirement { start, end, .. } => {
@ -657,22 +651,18 @@ impl Display for RequirementsTxtFileError {
end, end,
) )
} }
RequirementsTxtParserError::Pep508 { start, end, .. } => { RequirementsTxtParserError::Pep508 { start, .. } => {
write!( write!(
f, f,
"Couldn't parse requirement in {} position {} to {}", "Couldn't parse requirement in `{}` at position {start}",
self.file.display(), self.file.display(),
start,
end,
) )
} }
RequirementsTxtParserError::Subfile { start, end, .. } => { RequirementsTxtParserError::Subfile { start, .. } => {
write!( write!(
f, f,
"Error parsing file included into {} at position {} to {}", "Error parsing included file in `{}` at position {start}",
self.file.display(), self.file.display(),
start,
end
) )
} }
} }
@ -771,7 +761,7 @@ mod test {
assert_eq!( assert_eq!(
errors[0], errors[0],
format!( format!(
"Error parsing file included into {} at position 0 to 14", "Error parsing included file in `{}` at position 0",
basic.display() basic.display()
) )
); );
@ -793,7 +783,7 @@ mod test {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let expected = &[ let expected = &[
format!( format!(
"Couldn't parse requirement in {} position 0 to 15", "Couldn't parse requirement in `{}` at position 0",
basic.display() basic.display()
), ),
indoc! {" indoc! {"
@ -820,7 +810,7 @@ mod test {
.map(ToString::to_string) .map(ToString::to_string)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let expected = &[ let expected = &[
"Unsupported URL (expected a `file://` scheme) in ./test-data/requirements-txt/unsupported-editable.txt: http://localhost:8080/".to_string() "Unsupported URL (expected a `file://` scheme) in `./test-data/requirements-txt/unsupported-editable.txt`: http://localhost:8080/".to_string()
]; ];
assert_eq!(errors, expected); assert_eq!(errors, expected);
} }