Skip to content

Commit

Permalink
Trims correctly values during wheel WHEEL file parsing
Browse files Browse the repository at this point in the history
Last changes used by mistake == instead of !=.
A unit test has been added to validate that we correctly trim values.

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.
  • Loading branch information
Coruscant11 committed Sep 28, 2024
1 parent 9312a08 commit a239b78
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.get("Tag").unwrap();
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_eq!(wheel.contains_key("Tag"), false);
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 a239b78

Please sign in to comment.