Skip to content

Commit c2e3d8e

Browse files
committed
tests: add tests that exercise commit parsing
We currently do not have any test suites dedicated to parsing commits from their raw representations. Add one based on `git_object__from_raw` to be able to test special cases more easily.
1 parent ee11d47 commit c2e3d8e

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

tests/object/commit/parse.c

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
#include "clar_libgit2.h"
2+
#include "commit.h"
3+
#include "object.h"
4+
#include "signature.h"
5+
6+
static void assert_commit_parses(const char *data, size_t datalen,
7+
const char *expected_treeid,
8+
const char *expected_author,
9+
const char *expected_committer,
10+
const char *expected_encoding,
11+
const char *expected_message,
12+
size_t expected_parents)
13+
{
14+
git_commit *commit;
15+
if (!datalen)
16+
datalen = strlen(data);
17+
cl_git_pass(git_object__from_raw((git_object **) &commit, data, datalen, GIT_OBJ_COMMIT));
18+
19+
if (expected_author) {
20+
git_signature *author;
21+
cl_git_pass(git_signature_from_buffer(&author, expected_author));
22+
cl_assert(git_signature__equal(author, commit->author));
23+
cl_assert_equal_s(author->name, commit->author->name);
24+
cl_assert_equal_s(author->email, commit->author->email);
25+
cl_assert_equal_i(author->when.time, commit->author->when.time);
26+
cl_assert_equal_i(author->when.offset, commit->author->when.offset);
27+
cl_assert_equal_i(author->when.sign, commit->author->when.sign);
28+
git_signature_free(author);
29+
}
30+
31+
if (expected_committer) {
32+
git_signature *committer;
33+
cl_git_pass(git_signature_from_buffer(&committer, expected_committer));
34+
cl_assert_equal_s(committer->name, commit->committer->name);
35+
cl_assert_equal_s(committer->email, commit->committer->email);
36+
cl_assert_equal_i(committer->when.time, commit->committer->when.time);
37+
cl_assert_equal_i(committer->when.offset, commit->committer->when.offset);
38+
cl_assert_equal_i(committer->when.sign, commit->committer->when.sign);
39+
git_signature_free(committer);
40+
}
41+
42+
if (expected_encoding)
43+
cl_assert_equal_s(commit->message_encoding, expected_encoding);
44+
else
45+
cl_assert_equal_p(commit->message_encoding, NULL);
46+
47+
if (expected_message)
48+
cl_assert_equal_s(commit->raw_message, expected_message);
49+
else
50+
cl_assert_equal_p(commit->message_encoding, NULL);
51+
52+
if (expected_treeid) {
53+
git_oid tree_oid;
54+
cl_git_pass(git_oid_fromstr(&tree_oid, expected_treeid));
55+
cl_assert_equal_oid(&tree_oid, &commit->tree_id);
56+
}
57+
58+
cl_assert_equal_i(commit->parent_ids.size, expected_parents);
59+
60+
git_object__free(&commit->object);
61+
}
62+
63+
static void assert_commit_fails(const char *data, size_t datalen)
64+
{
65+
git_object *object;
66+
if (!datalen)
67+
datalen = strlen(data);
68+
cl_git_fail(git_object__from_raw(&object, data, datalen, GIT_OBJ_COMMIT));
69+
}
70+
71+
void test_object_commit_parse__parsing_commit_succeeds(void)
72+
{
73+
const char *commit =
74+
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
75+
"author Author <author@example.com>\n"
76+
"committer Committer <committer@example.com>\n"
77+
"encoding Encoding\n"
78+
"\n"
79+
"Message";
80+
assert_commit_parses(commit, 0,
81+
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
82+
"Author <author@example.com>",
83+
"Committer <committer@example.com>",
84+
"Encoding",
85+
"Message", 0);
86+
}
87+
88+
void test_object_commit_parse__parsing_commit_without_encoding_succeeds(void)
89+
{
90+
const char *commit =
91+
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
92+
"author Author <author@example.com>\n"
93+
"committer Committer <committer@example.com>\n"
94+
"\n"
95+
"Message";
96+
assert_commit_parses(commit, 0,
97+
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
98+
"Author <author@example.com>",
99+
"Committer <committer@example.com>",
100+
NULL,
101+
"Message", 0);
102+
}
103+
104+
void test_object_commit_parse__parsing_commit_with_multiple_authors_succeeds(void)
105+
{
106+
const char *commit =
107+
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
108+
"author Author1 <author@example.com>\n"
109+
"author Author2 <author@example.com>\n"
110+
"author Author3 <author@example.com>\n"
111+
"author Author4 <author@example.com>\n"
112+
"committer Committer <committer@example.com>\n"
113+
"\n"
114+
"Message";
115+
assert_commit_parses(commit, 0,
116+
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
117+
"Author1 <author@example.com>",
118+
"Committer <committer@example.com>",
119+
NULL,
120+
"Message", 0);
121+
}
122+
123+
void test_object_commit_parse__parsing_commit_with_multiple_committers_succeeds(void)
124+
{
125+
const char *commit =
126+
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
127+
"author Author <author@example.com>\n"
128+
"committer Committer1 <committer@example.com>\n"
129+
"committer Committer2 <committer@example.com>\n"
130+
"committer Committer3 <committer@example.com>\n"
131+
"committer Committer4 <committer@example.com>\n"
132+
"\n"
133+
"Message";
134+
assert_commit_parses(commit, 0,
135+
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
136+
"Author <author@example.com>",
137+
"Committer1 <committer@example.com>",
138+
NULL,
139+
"Message", 0);
140+
}
141+
142+
void test_object_commit_parse__parsing_commit_without_message_succeeds(void)
143+
{
144+
const char *commit =
145+
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
146+
"author Author <author@example.com>\n"
147+
"committer Committer <committer@example.com>\n";
148+
assert_commit_parses(commit, 0,
149+
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
150+
"Author <author@example.com>",
151+
"Committer <committer@example.com>",
152+
NULL,
153+
"", 0);
154+
}
155+
156+
void test_object_commit_parse__parsing_commit_with_unknown_fields_succeeds(void)
157+
{
158+
const char *commit =
159+
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
160+
"author Author <author@example.com>\n"
161+
"committer Committer <committer@example.com>\n"
162+
"foo bar\n"
163+
"more garbage\n"
164+
"\n"
165+
"Message";
166+
assert_commit_parses(commit, 0,
167+
"3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8",
168+
"Author <author@example.com>",
169+
"Committer <committer@example.com>",
170+
NULL,
171+
"Message", 0);
172+
}
173+
174+
void test_object_commit_parse__parsing_commit_with_invalid_tree_fails(void)
175+
{
176+
const char *commit =
177+
"tree 3e7ac388cadacccdf1xxx5f3445895b71d9cb0f8\n"
178+
"author Author <author@example.com>\n"
179+
"committer Committer <committer@example.com>\n"
180+
"\n"
181+
"Message";
182+
assert_commit_fails(commit, 0);
183+
}
184+
185+
void test_object_commit_parse__parsing_commit_without_tree_fails(void)
186+
{
187+
const char *commit =
188+
"author Author <author@example.com>\n"
189+
"committer Committer <committer@example.com>\n"
190+
"\n"
191+
"Message";
192+
assert_commit_fails(commit, 0);
193+
}
194+
195+
void test_object_commit_parse__parsing_commit_without_author_fails(void)
196+
{
197+
const char *commit =
198+
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
199+
"committer Committer <committer@example.com>\n"
200+
"\n"
201+
"Message";
202+
assert_commit_fails(commit, 0);
203+
}
204+
205+
void test_object_commit_parse__parsing_commit_without_committer_fails(void)
206+
{
207+
const char *commit =
208+
"tree 3e7ac388cadacccdf1c6c5f3445895b71d9cb0f8\n"
209+
"author Author <author@example.com>\n"
210+
"\n"
211+
"Message";
212+
assert_commit_fails(commit, 0);
213+
}

0 commit comments

Comments
 (0)