Skip to content

Commit

Permalink
Assure peek behaves exactly as we want it to with ERR lines
Browse files Browse the repository at this point in the history
Notes that we only need that complication to support V1.
Can't wait for the day when we can drop it.

#20
  • Loading branch information
Byron committed Sep 14, 2020
1 parent 8bcf535 commit bbdaee5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
20 changes: 13 additions & 7 deletions git-packetline/src/provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,20 @@ where
self.stopped_at = self.delimiters.iter().find(|l| **l == line).cloned();
self.peek_buf.clear();
return None;
} else {
let len = line
.as_slice()
.map(|s| s.len() + U16_HEX_BYTES)
.unwrap_or(U16_HEX_BYTES);
self.peek_buf.resize(len, 0);
Ok(Ok(crate::decode(&self.peek_buf).expect("only valid data here")))
} else if self.fail_on_err_lines {
if let Some(err) = line.check_error() {
self.is_done = true;
let err = err.0.as_bstr().to_string();
self.peek_buf.clear();
return Some(Err(io::Error::new(io::ErrorKind::Other, err)));
}
}
let len = line
.as_slice()
.map(|s| s.len() + U16_HEX_BYTES)
.unwrap_or(U16_HEX_BYTES);
self.peek_buf.resize(len, 0);
Ok(Ok(crate::decode(&self.peek_buf).expect("only valid data here")))
}
Ok(Err(err)) => {
self.peek_buf.clear();
Expand Down
23 changes: 23 additions & 0 deletions git-packetline/tests/packet_line/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ fn peek_follows_read_line_delimiter_logic() -> crate::Result {
Ok(())
}

#[test]
fn peek_follows_read_line_err_logic() -> crate::Result {
let mut rd = git_packetline::Provider::new(&b"0005a0009ERR e0000"[..], &[PacketLine::Flush]);
rd.fail_on_err_lines(true);
assert_eq!(rd.peek_line().expect("line")??, PacketLine::Data(b"a"));
rd.read_line();
assert_eq!(
rd.peek_line().expect("line").unwrap_err().to_string(),
"e",
"io errors are used to communicate remote errors when peeking"
);
assert!(rd.peek_line().is_none(), "we are still done, no way around it");
assert_eq!(rd.stopped_at(), None, "we stopped not because of a delimiter");
rd.reset();
assert!(rd.peek_line().is_none(), "it should stop due to the delimiter");
assert_eq!(
rd.stopped_at(),
Some(PacketLine::Flush),
"Stopped tracking is done even when peeking"
);
Ok(())
}

#[test]
fn peek_non_data() -> crate::Result {
let mut rd = git_packetline::Provider::new(&b"000000010002"[..], &[PacketLine::ResponseEnd]);
Expand Down

0 comments on commit bbdaee5

Please sign in to comment.