Skip to content

Commit

Permalink
Correctly trims values during wheel WHEEL file parsing (#7770)
Browse files Browse the repository at this point in the history
## Summary

My last changes (#6616) used by mistake == instead of !=.
:disappointed_relieved: Making values currently never trimmed despite
what we wanted.
Values should now be trimmed if needed.

Also removes the trim of the header name, because if a header contains
spaces, the header will be skipped by the mailparse crate in the first
place.

## Test Plan
- A unit test has been added to validate that we correctly trim values.
- A unit test has been added to validate the header names containing
spaces are skipped.
  • Loading branch information
Coruscant11 committed Sep 29, 2024
1 parent 9312a08 commit ada6b36
Showing 1 changed file with 34 additions and 7 deletions.
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

0 comments on commit ada6b36

Please sign in to comment.