From a475475171ba9cea5bd48366cfb8558475f47621 Mon Sep 17 00:00:00 2001 From: Coruscant11 Date: Sun, 29 Sep 2024 01:50:49 +0200 Subject: [PATCH] Correctly trims values during wheel WHEEL file parsing 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. --- crates/install-wheel-rs/src/wheel.rs | 41 +++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/crates/install-wheel-rs/src/wheel.rs b/crates/install-wheel-rs/src/wheel.rs index 13e73a126a98..783afcf492ec 100644 --- a/crates/install-wheel-rs/src/wheel.rs +++ b/crates/install-wheel-rs/src/wheel.rs @@ -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(); } @@ -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