Skip to content

Commit

Permalink
[git-packetline] refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed May 17, 2021
1 parent 119fcc3 commit 2a84b78
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 83 deletions.
2 changes: 2 additions & 0 deletions git-packetline/tests/async-packetline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub fn assert_err_display<T: std::fmt::Debug, E: std::error::Error>(
#[cfg(all(feature = "async-io", not(feature = "blocking-io")))]
mod async_io;
#[cfg(all(feature = "async-io", not(feature = "blocking-io")))]
mod decode;
#[cfg(all(feature = "async-io", not(feature = "blocking-io")))]
mod encode;
#[cfg(all(feature = "async-io", not(feature = "blocking-io")))]
mod write;
2 changes: 2 additions & 0 deletions git-packetline/tests/blocking-packetline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub fn assert_err_display<T: std::fmt::Debug, E: std::error::Error>(
#[cfg(feature = "blocking-io")]
mod blocking;
#[cfg(feature = "blocking-io")]
mod decode;
#[cfg(feature = "blocking-io")]
mod encode;
#[cfg(feature = "blocking-io")]
mod write;
83 changes: 0 additions & 83 deletions git-packetline/tests/blocking/decode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
mod streaming {
use crate::assert_err_display;
use bstr::ByteSlice;
use git_packetline::{
decode::{self, streaming, Stream},
immutable::Error,
Channel, PacketLine,
};

Expand All @@ -22,16 +20,6 @@ mod streaming {
Ok(())
}

#[test]
fn flush() -> crate::Result {
assert_complete(streaming(b"0000someotherstuff"), 4, PacketLine::Flush)
}

#[test]
fn trailing_line_feeds_are_not_removed_automatically() -> crate::Result {
assert_complete(streaming(b"0006a\n"), 6, PacketLine::Data(b"a\n"))
}

#[test]
fn trailing_line_feeds_are_removed_explicitly_roundtrip() -> crate::Result {
let line = decode::all_at_once(b"0006a\n")?;
Expand All @@ -45,47 +33,6 @@ mod streaming {
Ok(())
}

#[test]
fn ignore_extra_bytes() -> crate::Result {
assert_complete(streaming(b"0006a\nhello"), 6, PacketLine::Data(b"a\n"))
}

#[test]
fn error_on_oversized_line() {
assert_err_display(
streaming(b"ffff"),
"The data received claims to be larger than than the maximum allowed size: got 65535, exceeds 65516",
);
}

#[test]
fn error_on_error_line() -> crate::Result {
let line = PacketLine::Data(b"ERR the error");
assert_complete(
streaming(b"0011ERR the error-and just ignored because not part of the size"),
17,
line,
)?;
assert_eq!(
line.check_error().expect("error to be parsed here"),
Error(b"the error")
);
Ok(())
}

#[test]
fn error_on_invalid_hex() {
assert_err_display(
streaming(b"fooo"),
"Failed to decode the first four hex bytes indicating the line length: Invalid character 'o' at position 1",
);
}

#[test]
fn error_on_empty_line() {
assert_err_display(streaming(b"0004"), "Received an invalid empty line");
}

#[test]
fn round_trips() -> crate::Result {
for (line, bytes) in &[
Expand Down Expand Up @@ -126,34 +73,4 @@ mod streaming {
}
Ok(())
}

mod incomplete {
use git_packetline::decode::{self, streaming, Stream};

fn assert_incomplete(res: Result<Stream, decode::Error>, expected_missing: usize) -> crate::Result {
match res? {
Stream::Complete { .. } => {
panic!("expected parsing to be partial, not complete");
}
Stream::Incomplete { bytes_needed } => {
assert_eq!(bytes_needed, expected_missing);
}
}
Ok(())
}

#[test]
fn missing_hex_bytes() -> crate::Result {
assert_incomplete(streaming(b"0"), 3)?;
assert_incomplete(streaming(b"00"), 2)?;
Ok(())
}

#[test]
fn missing_data_bytes() -> crate::Result {
assert_incomplete(streaming(b"0005"), 1)?;
assert_incomplete(streaming(b"0006a"), 1)?;
Ok(())
}
}
}
104 changes: 104 additions & 0 deletions git-packetline/tests/decode/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
mod streaming {
use crate::assert_err_display;
use git_packetline::{
decode::{self, streaming, Stream},
immutable::Error,
PacketLine,
};

fn assert_complete(
res: Result<Stream, decode::Error>,
expected_consumed: usize,
expected_value: PacketLine,
) -> crate::Result {
match res? {
Stream::Complete { line, bytes_consumed } => {
assert_eq!(bytes_consumed, expected_consumed);
assert_eq!(line.as_bstr(), expected_value.as_bstr());
}
Stream::Incomplete { .. } => panic!("expected parsing to be complete, not partial"),
}
Ok(())
}

#[test]
fn flush() -> crate::Result {
assert_complete(streaming(b"0000someotherstuff"), 4, PacketLine::Flush)
}

#[test]
fn trailing_line_feeds_are_not_removed_automatically() -> crate::Result {
assert_complete(streaming(b"0006a\n"), 6, PacketLine::Data(b"a\n"))
}

#[test]
fn ignore_extra_bytes() -> crate::Result {
assert_complete(streaming(b"0006a\nhello"), 6, PacketLine::Data(b"a\n"))
}

#[test]
fn error_on_oversized_line() {
assert_err_display(
streaming(b"ffff"),
"The data received claims to be larger than than the maximum allowed size: got 65535, exceeds 65516",
);
}

#[test]
fn error_on_error_line() -> crate::Result {
let line = PacketLine::Data(b"ERR the error");
assert_complete(
streaming(b"0011ERR the error-and just ignored because not part of the size"),
17,
line,
)?;
assert_eq!(
line.check_error().expect("error to be parsed here"),
Error(b"the error")
);
Ok(())
}

#[test]
fn error_on_invalid_hex() {
assert_err_display(
streaming(b"fooo"),
"Failed to decode the first four hex bytes indicating the line length: Invalid character 'o' at position 1",
);
}

#[test]
fn error_on_empty_line() {
assert_err_display(streaming(b"0004"), "Received an invalid empty line");
}

mod incomplete {
use git_packetline::decode::{self, streaming, Stream};

fn assert_incomplete(res: Result<Stream, decode::Error>, expected_missing: usize) -> crate::Result {
match res? {
Stream::Complete { .. } => {
panic!("expected parsing to be partial, not complete");
}
Stream::Incomplete { bytes_needed } => {
assert_eq!(bytes_needed, expected_missing);
}
}
Ok(())
}

#[test]
fn missing_hex_bytes() -> crate::Result {
assert_incomplete(streaming(b"0"), 3)?;
assert_incomplete(streaming(b"00"), 2)?;
Ok(())
}

#[test]
fn missing_data_bytes() -> crate::Result {
assert_incomplete(streaming(b"0005"), 1)?;
assert_incomplete(streaming(b"0006a"), 1)?;
Ok(())
}
}
}

0 comments on commit 2a84b78

Please sign in to comment.