Skip to content

Allow empty extras in pep508-rs and add more corner case to tests #2128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 66 additions & 28 deletions crates/pep508-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,45 @@ fn parse_extras(cursor: &mut Cursor) -> Result<Vec<ExtraName>, Pep508Error> {
return Ok(vec![]);
};
let mut extras = Vec::new();
let mut is_first_iteration = true;

loop {
cursor.eat_whitespace();

// end of the extras section
if let Some(']') = cursor.peek_char() {
cursor.next();
break;
}

// comma separator, except for the first iteration
match (cursor.peek(), is_first_iteration) {
(Some((pos, ',')), true) => {
return Err(Pep508Error {
message: Pep508ErrorSource::String(
"Expected either alphanumerical character (starting the extra name) ']' (ending the extras section), found ','".to_string()
),
start: pos,
len: 1,
input: cursor.to_string(),
});
}
(Some((_, ',')), false) => {
cursor.next();
}
(Some((pos, other)), false) => {
return Err(Pep508Error {
message: Pep508ErrorSource::String(
format!("Expected either ',' (separating extras) or ']' (ending the extras section), found '{other}'",)
),
start: pos,
len: 1,
input: cursor.to_string(),
});
}
_ => {}
}

// wsp* before the identifier
cursor.eat_whitespace();
let mut buffer = String::new();
Expand Down Expand Up @@ -671,35 +708,12 @@ fn parse_extras(cursor: &mut Cursor) -> Result<Vec<ExtraName>, Pep508Error> {
}
_ => {}
};
// wsp* after the identifier
cursor.eat_whitespace();
// end or next identifier?
match cursor.next() {
Some((_, ',')) => {
extras.push(
ExtraName::new(buffer)
.expect("`ExtraName` validation should match PEP 508 parsing"),
);
}
Some((_, ']')) => {
extras.push(
ExtraName::new(buffer)
.expect("`ExtraName` validation should match PEP 508 parsing"),
);
break;
}
Some((pos, other)) => {
return Err(Pep508Error {
message: Pep508ErrorSource::String(format!(
"Expected either ',' (separating extras) or ']' (ending the extras section), found '{other}'"
)),
start: pos,
len: other.len_utf8(),
input: cursor.to_string(),
});
}
None => return Err(early_eof_error),
}
// wsp* after the identifier
extras.push(
ExtraName::new(buffer).expect("`ExtraName` validation should match PEP 508 parsing"),
);
is_first_iteration = false;
}

Ok(extras)
Expand Down Expand Up @@ -1271,6 +1285,30 @@ mod tests {
);
}

#[test]
fn empty_extras() {
let black = Requirement::from_str("black[]").unwrap();
assert_eq!(black.extras, vec![]);
}

#[test]
fn empty_extras_with_spaces() {
let black = Requirement::from_str("black[ ]").unwrap();
assert_eq!(black.extras, vec![]);
}

#[test]
fn error_extra_with_trailing_comma() {
assert_err(
"black[d,]",
indoc! {"
Expected an alphanumeric character starting the extra name, found ']'
black[d,]
^"
},
);
}

#[test]
fn error_parenthesized_pep440() {
assert_err(
Expand Down
2 changes: 1 addition & 1 deletion crates/requirements-txt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1205,7 +1205,7 @@ mod test {
}, {
insta::assert_display_snapshot!(errors, @r###"
Couldn't parse requirement in `<REQUIREMENTS_TXT>` at position 6
Expected an alphanumeric character starting the extra name, found ','
Expected either alphanumerical character (starting the extra name) ']' (ending the extras section), found ','
black[,abcdef]
^
"###);
Expand Down