Skip to content

Commit e774d5a

Browse files
author
Edward Thomson
committed
diff::parse tests: test parsing a diff
Test that we can create a diff file, then parse the results and that the two are identical in-memory.
1 parent 853e585 commit e774d5a

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

tests/diff/diff_helpers.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,39 @@ void diff_print_raw(FILE *fp, git_diff *diff)
241241
git_diff_print(diff, GIT_DIFF_FORMAT_RAW,
242242
git_diff_print_callback__to_file_handle, fp ? fp : stderr));
243243
}
244+
245+
void diff_assert_equal(git_diff *a, git_diff *b)
246+
{
247+
const git_diff_delta *ad, *bd;
248+
size_t i;
249+
250+
assert(a && b);
251+
252+
cl_assert_equal_i(git_diff_num_deltas(a), git_diff_num_deltas(b));
253+
254+
for (i = 0; i < git_diff_num_deltas(a); i++) {
255+
ad = git_diff_get_delta(a, i);
256+
bd = git_diff_get_delta(b, i);
257+
258+
cl_assert_equal_i(ad->status, bd->status);
259+
cl_assert_equal_i(ad->flags, bd->flags);
260+
cl_assert_equal_i(ad->similarity, bd->similarity);
261+
cl_assert_equal_i(ad->nfiles, bd->nfiles);
262+
263+
/* Don't examine the size or the flags of the deltas;
264+
* computed deltas have sizes (parsed deltas do not) and
265+
* computed deltas will have flags of `VALID_ID` and
266+
* `EXISTS` (parsed deltas will not query the ODB.)
267+
*/
268+
cl_assert_equal_oid(&ad->old_file.id, &bd->old_file.id);
269+
cl_assert_equal_i(ad->old_file.id_abbrev, bd->old_file.id_abbrev);
270+
cl_assert_equal_s(ad->old_file.path, bd->old_file.path);
271+
cl_assert_equal_i(ad->old_file.mode, bd->old_file.mode);
272+
273+
cl_assert_equal_oid(&ad->new_file.id, &bd->new_file.id);
274+
cl_assert_equal_i(ad->new_file.id_abbrev, bd->new_file.id_abbrev);
275+
cl_assert_equal_s(ad->new_file.path, bd->new_file.path);
276+
cl_assert_equal_i(ad->new_file.mode, bd->new_file.mode);
277+
}
278+
}
279+

tests/diff/diff_helpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ extern int diff_foreach_via_iterator(
6868

6969
extern void diff_print(FILE *fp, git_diff *diff);
7070
extern void diff_print_raw(FILE *fp, git_diff *diff);
71+
72+
extern void diff_assert_equal(git_diff *a, git_diff *b);
73+

tests/diff/parse.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#include "clar_libgit2.h"
22
#include "patch.h"
33
#include "patch_parse.h"
4+
#include "diff_helpers.h"
45

56
#include "../patch/patch_common.h"
67

8+
void test_diff_parse__cleanup(void)
9+
{
10+
cl_git_sandbox_cleanup();
11+
}
12+
713
void test_diff_parse__nonpatches_fail_with_notfound(void)
814
{
915
git_diff *diff;
@@ -58,3 +64,65 @@ void test_diff_parse__invalid_patches_fails(void)
5864
test_parse_invalid_diff(PATCH_CORRUPT_MISSING_HUNK_HEADER);
5965
}
6066

67+
static void test_tree_to_tree_computed_to_parsed(
68+
const char *sandbox, const char *a_id, const char *b_id)
69+
{
70+
git_repository *repo;
71+
git_diff *computed, *parsed;
72+
git_tree *a, *b;
73+
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
74+
git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
75+
git_buf computed_buf = GIT_BUF_INIT;
76+
77+
repo = cl_git_sandbox_init(sandbox);
78+
79+
opts.id_abbrev = GIT_OID_HEXSZ;
80+
opts.flags = GIT_DIFF_SHOW_BINARY;
81+
82+
cl_assert((a = resolve_commit_oid_to_tree(repo, a_id)) != NULL);
83+
cl_assert((b = resolve_commit_oid_to_tree(repo, b_id)) != NULL);
84+
85+
cl_git_pass(git_diff_tree_to_tree(&computed, repo, a, b, &opts));
86+
cl_git_pass(git_diff_to_buf(&computed_buf,
87+
computed, GIT_DIFF_FORMAT_PATCH));
88+
89+
cl_git_pass(git_diff_from_buffer(&parsed,
90+
computed_buf.ptr, computed_buf.size));
91+
92+
diff_assert_equal(computed, parsed);
93+
94+
git_tree_free(a);
95+
git_tree_free(b);
96+
97+
git_diff_free(computed);
98+
git_diff_free(parsed);
99+
100+
git_buf_free(&computed_buf);
101+
102+
cl_git_sandbox_cleanup();
103+
}
104+
105+
void test_diff_parse__can_parse_generated_diff(void)
106+
{
107+
test_tree_to_tree_computed_to_parsed("diff", "d70d245e", "7a9e0b02");
108+
test_tree_to_tree_computed_to_parsed(
109+
"unsymlinked.git", "806999", "a8595c");
110+
test_tree_to_tree_computed_to_parsed("diff",
111+
"d70d245ed97ed2aa596dd1af6536e4bfdb047b69",
112+
"7a9e0b02e63179929fed24f0a3e0f19168114d10");
113+
test_tree_to_tree_computed_to_parsed(
114+
"unsymlinked.git", "7fccd7", "806999");
115+
test_tree_to_tree_computed_to_parsed(
116+
"unsymlinked.git", "7fccd7", "a8595c");
117+
test_tree_to_tree_computed_to_parsed("attr", "605812a", "370fe9ec22");
118+
test_tree_to_tree_computed_to_parsed(
119+
"attr", "f5b0af1fb4f5c", "370fe9ec22");
120+
test_tree_to_tree_computed_to_parsed("diff", "d70d245e", "d70d245e");
121+
test_tree_to_tree_computed_to_parsed("diff_format_email",
122+
"873806f6f27e631eb0b23e4b56bea2bfac14a373",
123+
"897d3af16ca9e420cd071b1c4541bd2b91d04c8c");
124+
test_tree_to_tree_computed_to_parsed("diff_format_email",
125+
"897d3af16ca9e420cd071b1c4541bd2b91d04c8c",
126+
"873806f6f27e631eb0b23e4b56bea2bfac14a373");
127+
}
128+

0 commit comments

Comments
 (0)