Skip to content

Commit 06e22fb

Browse files
committed
Add remaining tag tests, along with some fixes
1 parent 01dd4e2 commit 06e22fb

File tree

6 files changed

+105
-10
lines changed

6 files changed

+105
-10
lines changed

git-object/src/borrowed/tag.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ pub(crate) fn parse_message(i: &[u8]) -> IResult<&[u8], (&BStr, Option<&BStr>),
7373

7474
let (i, _) = tag(NL)(i)?;
7575
if i.is_empty() {
76-
return Err(nom::Err::Error(Error::NomDetail(
77-
i.into(),
78-
"Missing tag message",
79-
)));
76+
return Ok((i, (i.as_bstr(), None)));
8077
}
8178
fn all_but_trailing_newline(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8]), Error> {
8279
if i.len() < 2 {
@@ -85,11 +82,11 @@ pub(crate) fn parse_message(i: &[u8]) -> IResult<&[u8], (&BStr, Option<&BStr>),
8582
"tag message is missing",
8683
)));
8784
}
88-
let (i, _) = tag(NL)(&i[i.len() - 1..])
85+
let (x, _) = tag(NL)(&i[i.len() - 1..])
8986
.map_err(Error::context("tag message must end with newline"))?;
9087
// an empty signature message signals that there is none - the function signature is needed
9188
// to work with 'alt(…)'. PGP signatures are never empty
92-
Ok((i, (&i[..i.len() - 1], &[])))
89+
Ok((x, (&i[..i.len() - 1], &[])))
9390
}
9491
let (i, (message, signature)) = alt((
9592
tuple((

git-object/src/tests/borrowed/tag.rs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,90 @@ mod method {
1919
}
2020

2121
mod parse_tag {
22-
use crate::borrowed::tag::parse_tag;
23-
use crate::tests::borrowed::fixture_bytes;
24-
use crate::tests::borrowed::tag::tag_fixture;
22+
use crate::{
23+
borrowed::{tag::parse_tag, Signature, Tag},
24+
tests::{borrowed::fixture_bytes, borrowed::tag::tag_fixture},
25+
Kind, Sign, Time,
26+
};
27+
use bstr::ByteSlice;
2528

2629
#[test]
2730
fn signed() {
2831
let fixture = fixture_bytes("signed.txt");
2932
assert_eq!(parse_tag(&fixture).unwrap().1, tag_fixture(9000));
3033
}
34+
35+
#[test]
36+
fn empty() {
37+
let fixture = fixture_bytes("empty.txt");
38+
assert_eq!(
39+
parse_tag(&fixture).unwrap().1,
40+
Tag {
41+
target: b"01dd4e2a978a9f5bd773dae6da7aa4a5ac1cdbbc".as_bstr(),
42+
name: b"empty".as_bstr(),
43+
target_kind: Kind::Commit,
44+
message: b"".as_bstr(),
45+
signature: Signature {
46+
name: b"Sebastian Thiel".as_bstr(),
47+
email: b"sebastian.thiel@icloud.com".as_bstr(),
48+
time: Time {
49+
time: 1592381636,
50+
offset: 28800,
51+
sign: Sign::Plus,
52+
},
53+
},
54+
pgp_signature: None
55+
}
56+
);
57+
}
58+
59+
#[test]
60+
fn with_newlines() {
61+
let fixture = fixture_bytes("with-newlines.txt");
62+
assert_eq!(
63+
parse_tag(&fixture).unwrap().1,
64+
Tag {
65+
target: b"ebdf205038b66108c0331aa590388431427493b7".as_bstr(),
66+
name: b"baz".as_bstr(),
67+
target_kind: Kind::Commit,
68+
message: b"hello\n\nworld".as_bstr(),
69+
signature: Signature {
70+
name: b"Sebastian Thiel".as_bstr(),
71+
email: b"sebastian.thiel@icloud.com".as_bstr(),
72+
time: Time {
73+
time: 1592311808,
74+
offset: 28800,
75+
sign: Sign::Plus,
76+
},
77+
},
78+
pgp_signature: None
79+
}
80+
);
81+
}
82+
83+
#[test]
84+
fn whitespace() {
85+
let fixture = fixture_bytes("whitespace.txt");
86+
assert_eq!(
87+
parse_tag(&fixture).unwrap().1,
88+
Tag {
89+
target: b"01dd4e2a978a9f5bd773dae6da7aa4a5ac1cdbbc".as_bstr(),
90+
name: b"whitespace".as_bstr(),
91+
target_kind: Kind::Commit,
92+
message: b" \ttab\nnewline\n\nlast-with-trailer\n".as_bstr(), // odd, was created with \n\n actually
93+
signature: Signature {
94+
name: b"Sebastian Thiel".as_bstr(),
95+
email: b"sebastian.thiel@icloud.com".as_bstr(),
96+
time: Time {
97+
time: 1592382888,
98+
offset: 28800,
99+
sign: Sign::Plus,
100+
},
101+
},
102+
pgp_signature: None
103+
}
104+
);
105+
}
31106
}
32107

33108
fn tag_fixture(offset: i32) -> Tag<'static> {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object 01dd4e2a978a9f5bd773dae6da7aa4a5ac1cdbbc
2+
type commit
3+
tag empty
4+
tagger Sebastian Thiel <sebastian.thiel@icloud.com> 1592381636 +0800
5+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object 01dd4e2a978a9f5bd773dae6da7aa4a5ac1cdbbc
2+
type commit
3+
tag whitespace
4+
tagger Sebastian Thiel <sebastian.thiel@icloud.com> 1592382888 +0800
5+
6+
tab
7+
newline
8+
9+
last-with-trailer
10+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object ebdf205038b66108c0331aa590388431427493b7
2+
type commit
3+
tag baz
4+
tagger Sebastian Thiel <sebastian.thiel@icloud.com> 1592311808 +0800
5+
6+
hello
7+
8+
world

tasks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
* [x] make parser tests unit tests that are close to the code they test
88
* [x] parser tests handle binary strings nicely
99
* [x] switch parsing to nom (binary only)
10-
* [ ] test tags without signature
10+
* [x] test tags without signature
1111
* [ ] Review loose object parsing, but don't use Nom for that

0 commit comments

Comments
 (0)