Skip to content

Commit

Permalink
Make WHEEL parsing error line numbers one indexed.
Browse files Browse the repository at this point in the history
Fixes an off-by-one from #2149. `enumerate` is zero-based, human line numbers are one based.
  • Loading branch information
konstin committed Mar 4, 2024
1 parent 836b90c commit bf041be
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions crates/install-wheel-rs/src/wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ pub(crate) fn read_record_file(record: &mut impl Read) -> Result<Vec<RecordEntry

/// Parse a file with `Key: value` entries such as WHEEL and METADATA
fn parse_key_value_file(
file: &mut impl Read,
file: impl Read,
debug_filename: &str,
) -> Result<FxHashMap<String, Vec<String>>, Error> {
let mut data: FxHashMap<String, Vec<String>> = FxHashMap::default();
Expand All @@ -665,7 +665,8 @@ fn parse_key_value_file(
}
let (key, value) = line.split_once(": ").ok_or_else(|| {
Error::InvalidWheel(format!(
"Line {line_no} of the {debug_filename} file is invalid"
"Line {} of the {debug_filename} file is invalid",
line_no + 1
))
})?;
data.entry(key.to_string())
Expand Down Expand Up @@ -720,6 +721,7 @@ pub(crate) fn parse_metadata(

#[cfg(test)]
mod test {
use std::io::Cursor;
use std::path::Path;

use indoc::{formatdoc, indoc};
Expand Down Expand Up @@ -887,6 +889,25 @@ mod test {
assert_eq!(format_shebang(executable, os_name), "#!/bin/sh\n'''exec' '/usr/bin/path/to/a/very/long/executable/executable/executable/executable/executable/executable/executable/executable/name/python3' \"$0\" \"$@\"\n' '''");
}

#[test]
fn test_invalid_wheel() {
let wheel = indoc! {r"
Wheel-Version: 1.0
Generator: custom
Root-Is-Purelib: false
Tag:
Tag: -manylinux_2_17_x86_64
Tag: -manylinux2014_x86_64
"
};
let reader = Cursor::new(wheel.to_string().into_bytes());
let err = parse_key_value_file(reader, "WHEEL").unwrap_err();
assert_eq!(
err.to_string(),
"The wheel is invalid: Line 4 of the WHEEL file is invalid"
);
}

#[test]
#[cfg(all(windows, target_arch = "x86_64"))]
fn test_launchers_are_small() {
Expand Down

0 comments on commit bf041be

Please sign in to comment.