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

feat: add buffer location to error message #53

Merged
merged 1 commit into from
Nov 27, 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
24 changes: 24 additions & 0 deletions src/junit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,31 @@
pub fn parse_junit_xml(file_bytes: &[u8]) -> PyResult<ParsingInfo> {
let mut reader = Reader::from_reader(file_bytes);
reader.config_mut().trim_text(true);
let thing = use_reader(&mut reader).map_err(|e| {
let pos = reader.buffer_position();
let (line, col) = get_position_info(file_bytes, pos.try_into().unwrap());
ParserError::new_err(format!("Error at {}:{}: {}", line, col, e))

Check warning on line 97 in src/junit.rs

View check run for this annotation

Codecov Notifications / codecov/patch

src/junit.rs#L95-L97

Added lines #L95 - L97 were not covered by tests
})?;
Ok(thing)
}

fn get_position_info(input: &[u8], byte_offset: usize) -> (usize, usize) {
let mut line = 1;
let mut last_newline = 0;

Check warning on line 104 in src/junit.rs

View check run for this annotation

Codecov Notifications / codecov/patch

src/junit.rs#L102-L104

Added lines #L102 - L104 were not covered by tests

for (i, &byte) in input.iter().take(byte_offset).enumerate() {
if byte == b'\n' {
line += 1;
last_newline = i + 1;
}

Check warning on line 110 in src/junit.rs

View check run for this annotation

Codecov Notifications / codecov/patch

src/junit.rs#L106-L110

Added lines #L106 - L110 were not covered by tests
}

let column = byte_offset - last_newline + 1;

(line, column)
}

Check warning on line 116 in src/junit.rs

View check run for this annotation

Codecov Notifications / codecov/patch

src/junit.rs#L113-L116

Added lines #L113 - L116 were not covered by tests

fn use_reader(reader: &mut Reader<&[u8]>) -> PyResult<ParsingInfo> {
let mut testruns: Vec<Testrun> = Vec::new();
let mut saved_testrun: Option<Testrun> = None;

Expand Down
Loading