Skip to content

Commit f01a858

Browse files
committed
tests: make reflog_check_entry more clar-y
1 parent b9b1f9f commit f01a858

File tree

3 files changed

+75
-37
lines changed

3 files changed

+75
-37
lines changed

tests/refs/reflog/messages.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,31 @@ void test_refs_reflog_messages__setting_head_updates_reflog(void)
3737
cl_git_pass(git_repository_set_head(g_repo, "refs/tags/test")); /* 1 */
3838
cl_git_pass(git_repository_set_head(g_repo, "refs/remotes/test/master")); /* 0 */
3939

40-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 4,
40+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 4,
4141
NULL, "refs/heads/haacked",
4242
"foo@example.com",
4343
"checkout: moving from master to haacked");
44-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 3,
44+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 3,
4545
NULL, "tags/test^{commit}",
4646
"foo@example.com",
4747
"checkout: moving from unborn to e90810b8df3e80c413d903f631643c716887138d");
48-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 2,
48+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 2,
4949
"tags/test^{commit}", "refs/heads/haacked",
5050
"foo@example.com",
5151
"checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked");
52-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 1,
52+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 1,
5353
"refs/heads/haacked", "tags/test^{commit}",
5454
"foo@example.com",
5555
"checkout: moving from haacked to test");
56-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
56+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
5757
"tags/test^{commit}", "refs/remotes/test/master",
5858
"foo@example.com",
5959
"checkout: moving from e90810b8df3e80c413d903f631643c716887138d to test/master");
6060

6161
cl_git_pass(git_annotated_commit_from_revspec(&annotated, g_repo, "haacked~0"));
6262
cl_git_pass(git_repository_set_head_detached_from_annotated(g_repo, annotated));
6363

64-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
64+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
6565
NULL, "refs/heads/haacked",
6666
"foo@example.com",
6767
"checkout: moving from be3563ae3f795b2b4353bcce3a527ad0a4f7f644 to haacked~0");
@@ -96,14 +96,14 @@ void test_refs_reflog_messages__detaching_writes_reflog(void)
9696
msg = "checkout: moving from master to e90810b8df3e80c413d903f631643c716887138d";
9797
git_oid_fromstr(&id, "e90810b8df3e80c413d903f631643c716887138d");
9898
cl_git_pass(git_repository_set_head_detached(g_repo, &id));
99-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
99+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
100100
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
101101
"e90810b8df3e80c413d903f631643c716887138d",
102102
NULL, msg);
103103

104104
msg = "checkout: moving from e90810b8df3e80c413d903f631643c716887138d to haacked";
105105
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/haacked"));
106-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
106+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
107107
"e90810b8df3e80c413d903f631643c716887138d",
108108
"258f0e2a959a364e40ed6603d5d44fbb24765b10",
109109
NULL, msg);
@@ -120,22 +120,22 @@ void test_refs_reflog_messages__orphan_branch_does_not_count(void)
120120
msg = "checkout: moving from master to e90810b8df3e80c413d903f631643c716887138d";
121121
git_oid_fromstr(&id, "e90810b8df3e80c413d903f631643c716887138d");
122122
cl_git_pass(git_repository_set_head_detached(g_repo, &id));
123-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
123+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
124124
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
125125
"e90810b8df3e80c413d903f631643c716887138d",
126126
NULL, msg);
127127

128128
/* Switching to an orphan branch does not write to the reflog */
129129
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/orphan"));
130-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
130+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
131131
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
132132
"e90810b8df3e80c413d903f631643c716887138d",
133133
NULL, msg);
134134

135135
/* And coming back, we set the source to zero */
136136
msg = "checkout: moving from orphan to haacked";
137137
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/haacked"));
138-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
138+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
139139
"0000000000000000000000000000000000000000",
140140
"258f0e2a959a364e40ed6603d5d44fbb24765b10",
141141
NULL, msg);
@@ -245,7 +245,7 @@ void test_refs_reflog_messages__show_merge_for_merge_commits(void)
245245
"Merge commit", tree,
246246
2, (const struct git_commit **) parent_commits));
247247

248-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
248+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
249249
NULL,
250250
git_oid_tostr_s(&merge_commit_oid),
251251
NULL, "commit (merge): Merge commit");
@@ -291,7 +291,7 @@ void test_refs_reflog_messages__renaming_ref(void)
291291
cl_git_pass(git_reference_rename(&new_ref, ref, "refs/heads/renamed", false,
292292
"message"));
293293

294-
reflog_check_entry(g_repo, git_reference_name(new_ref), 0,
294+
cl_reflog_check_entry(g_repo, git_reference_name(new_ref), 0,
295295
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
296296
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
297297
"foo@example.com", "message");
@@ -313,7 +313,7 @@ void test_refs_reflog_messages__updating_a_direct_reference(void)
313313

314314
cl_git_pass(git_reference_set_target(&ref_out, ref, &target_id, message));
315315

316-
reflog_check_entry(g_repo, "refs/heads/master", 0,
316+
cl_reflog_check_entry(g_repo, "refs/heads/master", 0,
317317
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
318318
"258f0e2a959a364e40ed6603d5d44fbb24765b10",
319319
NULL, message);
@@ -340,7 +340,7 @@ void test_refs_reflog_messages__creating_branches_default_messages(void)
340340
cl_git_pass(git_branch_create(&branch, g_repo, NEW_BRANCH_NAME, target, false));
341341

342342
cl_git_pass(git_buf_printf(&buf, "branch: Created from %s", git_oid_tostr_s(git_commit_id(target))));
343-
reflog_check_entry(g_repo, "refs/heads/" NEW_BRANCH_NAME, 0,
343+
cl_reflog_check_entry(g_repo, "refs/heads/" NEW_BRANCH_NAME, 0,
344344
GIT_OID_HEX_ZERO,
345345
git_oid_tostr_s(git_commit_id(target)),
346346
g_email, git_buf_cstr(&buf));
@@ -352,7 +352,7 @@ void test_refs_reflog_messages__creating_branches_default_messages(void)
352352
cl_git_pass(git_annotated_commit_from_revspec(&annotated, g_repo, "e90810b8df3"));
353353
cl_git_pass(git_branch_create_from_annotated(&branch, g_repo, NEW_BRANCH_NAME, annotated, true));
354354

355-
reflog_check_entry(g_repo, "refs/heads/" NEW_BRANCH_NAME, 0,
355+
cl_reflog_check_entry(g_repo, "refs/heads/" NEW_BRANCH_NAME, 0,
356356
GIT_OID_HEX_ZERO,
357357
git_oid_tostr_s(git_commit_id(target)),
358358
g_email, "branch: Created from e90810b8df3");
@@ -371,7 +371,7 @@ void test_refs_reflog_messages__moving_branch_default_message(void)
371371
git_oid_cpy(&id, git_reference_target(branch));
372372
cl_git_pass(git_branch_move(&new_branch, branch, "master2", 0));
373373

374-
reflog_check_entry(g_repo, git_reference_name(new_branch), 0,
374+
cl_reflog_check_entry(g_repo, git_reference_name(new_branch), 0,
375375
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
376376
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
377377
g_email,
@@ -388,7 +388,7 @@ void test_refs_reflog_messages__detaching_head_default_message(void)
388388
cl_assert_equal_i(false, git_repository_head_detached(g_repo));
389389

390390
cl_git_pass(git_repository_detach_head(g_repo));
391-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
391+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
392392
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
393393
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
394394
NULL, "checkout: moving from master to a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
@@ -399,7 +399,7 @@ void test_refs_reflog_messages__detaching_head_default_message(void)
399399
true, "REATTACH"));
400400
git_reference_free(ref);
401401

402-
reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
402+
cl_reflog_check_entry(g_repo, GIT_HEAD_FILE, 0,
403403
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
404404
"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
405405
NULL, "REATTACH");

tests/refs/reflog/reflog_helpers.c

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,89 @@ size_t reflog_entrycount(git_repository *repo, const char *name)
1515
return ret;
1616
}
1717

18-
void reflog_check_entry(git_repository *repo, const char *reflog, size_t idx,
18+
void cl_reflog_check_entry_(git_repository *repo, const char *reflog, size_t idx,
1919
const char *old_spec, const char *new_spec,
20-
const char *email, const char *message)
20+
const char *email, const char *message, const char *file, int line)
2121
{
2222
git_reflog *log;
2323
const git_reflog_entry *entry;
24+
git_buf result = GIT_BUF_INIT;
2425

2526
cl_git_pass(git_reflog_read(&log, repo, reflog));
2627
entry = git_reflog_entry_byindex(log, idx);
28+
if (entry == NULL)
29+
clar__fail(file, line, "Reflog has no such entry", NULL, 1);
2730

2831
if (old_spec) {
2932
git_object *obj = NULL;
3033
if (git_revparse_single(&obj, repo, old_spec) == GIT_OK) {
31-
cl_assert_equal_oid(git_object_id(obj), git_reflog_entry_id_old(entry));
34+
if (git_oid_cmp(git_object_id(obj), git_reflog_entry_id_old(entry)) != 0) {
35+
git_oid__writebuf(&result, "\tOld OID: \"", git_object_id(obj));
36+
git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_old(entry));
37+
git_buf_puts(&result, "\"\n");
38+
}
3239
git_object_free(obj);
3340
} else {
3441
git_oid *oid = git__calloc(1, sizeof(*oid));
3542
git_oid_fromstr(oid, old_spec);
36-
cl_assert_equal_oid(oid, git_reflog_entry_id_old(entry));
43+
if (git_oid_cmp(oid, git_reflog_entry_id_old(entry)) != 0) {
44+
git_oid__writebuf(&result, "\tOld OID: \"", oid);
45+
git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_old(entry));
46+
git_buf_puts(&result, "\"\n");
47+
}
3748
git__free(oid);
3849
}
3950
}
4051
if (new_spec) {
4152
git_object *obj = NULL;
4253
if (git_revparse_single(&obj, repo, new_spec) == GIT_OK) {
43-
cl_assert_equal_oid(git_object_id(obj), git_reflog_entry_id_new(entry));
54+
if (git_oid_cmp(git_object_id(obj), git_reflog_entry_id_new(entry)) != 0) {
55+
git_oid__writebuf(&result, "\tNew OID: \"", git_object_id(obj));
56+
git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_new(entry));
57+
git_buf_puts(&result, "\"\n");
58+
}
4459
git_object_free(obj);
4560
} else {
4661
git_oid *oid = git__calloc(1, sizeof(*oid));
4762
git_oid_fromstr(oid, new_spec);
48-
cl_assert_equal_oid(oid, git_reflog_entry_id_new(entry));
63+
if (git_oid_cmp(oid, git_reflog_entry_id_new(entry)) != 0) {
64+
git_oid__writebuf(&result, "\tNew OID: \"", oid);
65+
git_oid__writebuf(&result, "\" != \"", git_reflog_entry_id_new(entry));
66+
git_buf_puts(&result, "\"\n");
67+
}
4968
git__free(oid);
5069
}
5170
}
5271

53-
if (email) {
54-
cl_assert_equal_s(email, git_reflog_entry_committer(entry)->email);
55-
}
72+
if (email && strcmp(email, git_reflog_entry_committer(entry)->email) != 0)
73+
git_buf_printf(&result, "\tEmail: \"%s\" != \"%s\"\n", email, git_reflog_entry_committer(entry)->email);
74+
5675
if (message) {
57-
cl_assert_equal_s(message, git_reflog_entry_message(entry));
76+
const char *entry_msg = git_reflog_entry_message(entry);
77+
if (entry_msg == NULL) entry_msg = "";
78+
79+
if (entry_msg && strcmp(message, entry_msg) != 0)
80+
git_buf_printf(&result, "\tMessage: \"%s\" != \"%s\"\n", message, entry_msg);
5881
}
82+
if (git_buf_len(&result) != 0)
83+
clar__fail(file, line, "Reflog entry mismatch", git_buf_cstr(&result), 1);
5984

85+
git_buf_free(&result);
6086
git_reflog_free(log);
6187
}
6288

89+
static int reflog_entry_tostr(git_buf *out, const git_reflog_entry *entry)
90+
{
91+
char old_oid[GIT_OID_HEXSZ], new_oid[GIT_OID_HEXSZ];
92+
93+
assert(out && entry);
94+
95+
git_oid_tostr((char *)&old_oid, GIT_OID_HEXSZ, git_reflog_entry_id_old(entry));
96+
git_oid_tostr((char *)&new_oid, GIT_OID_HEXSZ, git_reflog_entry_id_new(entry));
97+
98+
return git_buf_printf(out, "%s %s %s %s", old_oid, new_oid, "somesig", git_reflog_entry_message(entry));
99+
}
100+
63101
void reflog_print(git_repository *repo, const char *reflog_name)
64102
{
65103
git_reflog *reflog;
@@ -70,12 +108,8 @@ void reflog_print(git_repository *repo, const char *reflog_name)
70108

71109
for (idx = 0; idx < git_reflog_entrycount(reflog); idx++) {
72110
const git_reflog_entry *entry = git_reflog_entry_byindex(reflog, idx);
73-
char old_oid[GIT_OID_HEXSZ], new_oid[GIT_OID_HEXSZ];;
74-
75-
git_oid_tostr((char *)&old_oid, GIT_OID_HEXSZ, git_reflog_entry_id_old(entry));
76-
git_oid_tostr((char *)&new_oid, GIT_OID_HEXSZ, git_reflog_entry_id_new(entry));
77-
78-
git_buf_printf(&out, "%ld: %s %s %s %s\n", idx, old_oid, new_oid, "somesig", git_reflog_entry_message(entry));
111+
reflog_entry_tostr(&out, entry);
112+
git_buf_putc(&out, '\n');
79113
}
80114

81115
fprintf(stderr, "%s", git_buf_cstr(&out));

tests/refs/reflog/reflog_helpers.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
size_t reflog_entrycount(git_repository *repo, const char *name);
2-
void reflog_check_entry(git_repository *repo, const char *reflog, size_t idx,
2+
3+
#define cl_reflog_check_entry(repo, reflog, idx, old_spec, new_spec, email, message) \
4+
cl_reflog_check_entry_(repo, reflog, idx, old_spec, new_spec, email, message, __FILE__, __LINE__)
5+
6+
void cl_reflog_check_entry_(git_repository *repo, const char *reflog, size_t idx,
37
const char *old_spec, const char *new_spec,
4-
const char *email, const char *message);
8+
const char *email, const char *message, const char *file, int line);
59

610
void reflog_print(git_repository *repo, const char *reflog_name);

0 commit comments

Comments
 (0)