Skip to content
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

Correctly trims values during wheel WHEEL file parsing #7770

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Changes from all commits
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
41 changes: 34 additions & 7 deletions crates/install-wheel-rs/src/wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,16 +823,12 @@ fn parse_email_message_file(
.0;

for header in headers {
let mut name = header.get_key();
let name = header.get_key(); // Will not be trimmed because if it contains space, mailparse will skip the header
let mut value = header.get_value();

// Trim the name and value only if needed, avoiding unnecessary allocations with .trim().to_string().
let trimmed_name = name.trim();
if name == trimmed_name {
name = trimmed_name.to_string();
}
// Trim the value only if needed
let trimmed_value = value.trim();
if value == trimmed_value {
if value != trimmed_value {
value = trimmed_value.to_string();
}

Expand Down Expand Up @@ -871,6 +867,37 @@ mod test {
parse_email_message_file(&mut text.as_bytes(), "WHEEL").unwrap();
}

#[test]
fn test_parse_email_message_file_with_trimmed_value() {
let text = indoc! {"
Wheel-Version: 1.0
Generator: bdist_wheel (0.37.1)
Root-Is-Purelib: false
Tag: cp38-cp38-manylinux_2_17_x86_64
"};

let wheel = parse_email_message_file(&mut text.as_bytes(), "WHEEL").unwrap();
let tags = &wheel["Tag"];
let tag = tags
.first()
.expect("Expected one tag inside the WHEEL file");
assert_eq!(tag, "cp38-cp38-manylinux_2_17_x86_64");
}

#[test]
fn test_parse_email_message_file_is_skipping_keys_with_space() {
let text = indoc! {"
Wheel-Version: 1.0
Generator: bdist_wheel (0.37.1)
Root-Is-Purelib: false
Tag : cp38-cp38-manylinux_2_17_x86_64
"};

let wheel = parse_email_message_file(&mut text.as_bytes(), "WHEEL").unwrap();
assert!(!wheel.contains_key("Tag"));
assert_eq!(3, wheel.keys().len());
}

#[test]
fn test_parse_email_message_file_with_value_starting_with_linesep_and_two_space() {
// Check: https://files.pythonhosted.org/packages/0c/b7/ecfdce6368cc3664d301f7f52db4fe1004aa7da7a12c4a9bf1de534ff6ab/ziglang-0.13.0-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64.whl
Expand Down
Loading